diff --git a/examples/worldcup/main.go b/examples/worldcup/main.go index 3217a293..e67f024c 100644 --- a/examples/worldcup/main.go +++ b/examples/worldcup/main.go @@ -98,7 +98,7 @@ type WorldCupLive struct { Interval int `json:"interval"` // seconds Duration int `json:"duration"` // seconds Timelines []timeLog `json:"timelines"` - PerfData []string `json:"perfData"` + PerfFile string `json:"perf"` } func NewWorldCupLive(device uixt.Device, matchName, bundleID string, duration, interval int) *WorldCupLive { @@ -268,7 +268,7 @@ func (wc *WorldCupLive) dumpResult() error { encoder.SetEscapeHTML(false) encoder.SetIndent("", " ") - wc.PerfData = wc.driver.GetPerfData() + wc.PerfFile = wc.driver.Device.StopPerf() err := encoder.Encode(wc) if err != nil { log.Error().Err(err).Msg("encode json failed") diff --git a/examples/worldcup/main_test.go b/examples/worldcup/main_test.go index ad194769..5166a79b 100644 --- a/examples/worldcup/main_test.go +++ b/examples/worldcup/main_test.go @@ -60,10 +60,10 @@ func TestIOSDouyinWorldCupLive(t *testing.T) { uixt.WithWDAPort(8700), uixt.WithWDAMjpegPort(8800), uixt.WithXCTest("com.gtf.wda.runner.xctrunner"), - // uixt.WithIOSPerfOptions( - // uixt.WithIOSPerfNetwork(true), - // uixt.WithIOSPerfBundleID("com.ss.iphone.ugc.Aweme"), - // ), + uixt.WithIOSPerfOptions( + uixt.WithIOSPerfNetwork(true), + // uixt.WithIOSPerfBundleID("com.ss.iphone.ugc.Aweme"), + ), uixt.WithIOSPcapOn(true), ), TestSteps: []hrp.IStep{ diff --git a/hrp/pkg/uixt/android_device.go b/hrp/pkg/uixt/android_device.go index d1e6f0c7..6cf2f9e9 100644 --- a/hrp/pkg/uixt/android_device.go +++ b/hrp/pkg/uixt/android_device.go @@ -192,12 +192,21 @@ func (dev *AndroidDevice) NewHTTPDriver(capabilities Capabilities) (driver *uiaD return driver, nil } +func (dev *AndroidDevice) StartPerf() error { + // TODO + return nil +} + +func (dev *AndroidDevice) StopPerf() string { + // TODO + return "" +} + func (dev *AndroidDevice) StartPcap() error { // TODO return nil } -// StopPcap stops pcap monitor and returns the saved pcap file path func (dev *AndroidDevice) StopPcap() string { // TODO return "" diff --git a/hrp/pkg/uixt/ext.go b/hrp/pkg/uixt/ext.go index 78d13f06..fa3b9993 100644 --- a/hrp/pkg/uixt/ext.go +++ b/hrp/pkg/uixt/ext.go @@ -13,6 +13,7 @@ import ( "testing" "time" + "github.com/httprunner/httprunner/v4/hrp/internal/env" "github.com/pkg/errors" "github.com/rs/zerolog/log" ) @@ -205,11 +206,9 @@ type DriverExt struct { frame *bytes.Buffer doneMjpegStream chan bool scale float64 - ocrService OCRService // used to get text from image - StartTime time.Time // used to associate screenshots name - ScreenShots []string // save screenshots path - perfStop chan struct{} // stop performance monitor - perfData []string // save perf data + ocrService OCRService // used to get text from image + StartTime time.Time // used to associate screenshots name + ScreenShots []string // save screenshots path CVArgs } @@ -238,14 +237,6 @@ func NewDriverExt(device Device, driver WebDriver) (dExt *DriverExt, err error) return dExt, nil } -func (dExt *DriverExt) GetPerfData() []string { - if dExt.perfStop == nil { - return nil - } - close(dExt.perfStop) - return dExt.perfData -} - func (dExt *DriverExt) takeScreenShot() (raw *bytes.Buffer, err error) { // wait for action done time.Sleep(500 * time.Millisecond) @@ -300,8 +291,7 @@ func (dExt *DriverExt) ScreenShot(fileName string) (string, error) { return "", errors.Wrap(err, "screenshot failed") } - dir, _ := os.Getwd() - screenshotsDir := filepath.Join(dir, "screenshots") + screenshotsDir := env.ScreenShotsPath if err = os.MkdirAll(screenshotsDir, os.ModePerm); err != nil { return "", errors.Wrap(err, "create screenshots directory failed") } diff --git a/hrp/pkg/uixt/interface.go b/hrp/pkg/uixt/interface.go index 5b81fbe3..3eed2b5b 100644 --- a/hrp/pkg/uixt/interface.go +++ b/hrp/pkg/uixt/interface.go @@ -930,6 +930,9 @@ type Device interface { UUID() string // ios udid or android serial NewDriver(capabilities Capabilities) (driverExt *DriverExt, err error) + StartPerf() error + StopPerf() string + StartPcap() error StopPcap() string } diff --git a/hrp/pkg/uixt/ios_device.go b/hrp/pkg/uixt/ios_device.go index d9e68a0a..4e75fd34 100644 --- a/hrp/pkg/uixt/ios_device.go +++ b/hrp/pkg/uixt/ios_device.go @@ -264,6 +264,10 @@ type IOSDevice struct { AcceptAlertButtonSelector string `json:"accept_alert_button_selector,omitempty" yaml:"accept_alert_button_selector,omitempty"` DismissAlertButtonSelector string `json:"dismiss_alert_button_selector,omitempty" yaml:"dismiss_alert_button_selector,omitempty"` + // performance monitor + perfStop chan struct{} // stop performance monitor + perfFile string // saved perf file path + // pcap monitor pcapStop chan struct{} // stop pcap monitor pcapFile string // saved pcap file path @@ -325,25 +329,9 @@ func (dev *IOSDevice) NewDriver(capabilities Capabilities) (driverExt *DriverExt } if dev.PerfOptions != nil { - data, err := dev.d.PerfStart(dev.perfOpitons()...) - if err != nil { + if err := dev.StartPerf(); err != nil { return nil, err } - - driverExt.perfStop = make(chan struct{}) - // start performance monitor - go func() { - for { - select { - case <-driverExt.perfStop: - dev.d.PerfStop() - return - case d := <-data: - fmt.Println(string(d)) - driverExt.perfData = append(driverExt.perfData, string(d)) - } - } - }() } if dev.PcapOn { @@ -355,14 +343,59 @@ func (dev *IOSDevice) NewDriver(capabilities Capabilities) (driverExt *DriverExt return driverExt, nil } +func (dev *IOSDevice) StartPerf() error { + data, err := dev.d.PerfStart(dev.perfOpitons()...) + if err != nil { + return err + } + + dev.perfFile = filepath.Join(env.ResultsPath, + fmt.Sprintf("perf_%s.log", time.Now().Format("20060102150405"))) + + log.Info().Str("perfFile", dev.perfFile).Msg("create perf file") + file, err := os.OpenFile(dev.perfFile, + os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0o755) + if err != nil { + return err + } + + dev.perfStop = make(chan struct{}) + // start performance monitor + go func() { + for { + select { + case <-dev.perfStop: + file.Close() + dev.d.PerfStop() + return + case d := <-data: + _, err = file.WriteString(string(d) + "\n") + if err != nil { + log.Error().Err(err). + Str("line", string(d)). + Msg("write perf data failed") + } + } + } + }() + return nil +} + +func (dev *IOSDevice) StopPerf() string { + if dev.perfStop == nil { + return "" + } + close(dev.perfStop) + return dev.perfFile +} + func (dev *IOSDevice) StartPcap() error { packets, err := dev.d.PcapStart() if err != nil { return err } - rootDir, _ := os.Getwd() - dev.pcapFile = filepath.Join(rootDir, + dev.pcapFile = filepath.Join(env.ResultsPath, fmt.Sprintf("dump_%s.pcap", time.Now().Format("20060102150405"))) log.Info().Str("pcapFile", dev.pcapFile).Msg("create pcap file") diff --git a/hrp/runner.go b/hrp/runner.go index d97cf93c..aa4665ea 100644 --- a/hrp/runner.go +++ b/hrp/runner.go @@ -635,7 +635,7 @@ func (r *SessionRunner) GetSummary() (*TestCaseSummary, error) { } // stop performance monitor - logs["performance"] = client.GetPerfData() + logs["performance"] = client.Device.StopPerf() logs["pcap"] = client.Device.StopPcap() caseSummary.Logs = append(caseSummary.Logs, logs)