mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-06 16:07:45 +08:00
refactor: replace GA with GA4
This commit is contained in:
+7
-10
@@ -93,17 +93,14 @@ func (b *HRPBoomer) SetPython3Venv(venv string) *HRPBoomer {
|
|||||||
|
|
||||||
// Run starts to run load test for one or multiple testcases.
|
// Run starts to run load test for one or multiple testcases.
|
||||||
func (b *HRPBoomer) Run(testcases ...ITestCase) {
|
func (b *HRPBoomer) Run(testcases ...ITestCase) {
|
||||||
event := sdk.EventTracking{
|
startTime := time.Now()
|
||||||
Category: "RunLoadTests",
|
|
||||||
Action: "hrp boom",
|
|
||||||
}
|
|
||||||
// report start event
|
|
||||||
go sdk.SendEvent(event)
|
|
||||||
// report execution timing event
|
|
||||||
defer sdk.SendEvent(event.StartTiming("execution"))
|
|
||||||
|
|
||||||
// quit all plugins
|
|
||||||
defer func() {
|
defer func() {
|
||||||
|
// report boom event
|
||||||
|
sdk.SendGA4Event("hrp_boom", map[string]interface{}{
|
||||||
|
"engagement_time_msec": time.Since(startTime).Milliseconds(),
|
||||||
|
})
|
||||||
|
|
||||||
|
// quit all plugins
|
||||||
pluginMap.Range(func(key, value interface{}) bool {
|
pluginMap.Range(func(key, value interface{}) bool {
|
||||||
if plugin, ok := value.(funplugin.IPlugin); ok {
|
if plugin, ok := value.(funplugin.IPlugin); ok {
|
||||||
plugin.Quit()
|
plugin.Quit()
|
||||||
|
|||||||
@@ -6,10 +6,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func RunPytest(args []string) error {
|
func RunPytest(args []string) error {
|
||||||
sdk.SendEvent(sdk.EventTracking{
|
sdk.SendGA4Event("hrp_pytest", nil)
|
||||||
Category: "RunAPITests",
|
|
||||||
Action: "hrp pytest",
|
|
||||||
})
|
|
||||||
|
|
||||||
args = append([]string{"run"}, args...)
|
args = append([]string{"run"}, args...)
|
||||||
return myexec.ExecPython3Command("httprunner", args...)
|
return myexec.ExecPython3Command("httprunner", args...)
|
||||||
|
|||||||
@@ -55,10 +55,7 @@ func CopyFile(templateFile, targetFile string) error {
|
|||||||
|
|
||||||
func CreateScaffold(projectName string, pluginType PluginType, venv string, force bool) error {
|
func CreateScaffold(projectName string, pluginType PluginType, venv string, force bool) error {
|
||||||
// report event
|
// report event
|
||||||
sdk.SendEvent(sdk.EventTracking{
|
sdk.SendGA4Event("hrp_startproject", nil)
|
||||||
Category: "Scaffold",
|
|
||||||
Action: "hrp startproject",
|
|
||||||
})
|
|
||||||
|
|
||||||
log.Info().
|
log.Info().
|
||||||
Str("projectName", projectName).
|
Str("projectName", projectName).
|
||||||
|
|||||||
+17
-10
@@ -3,7 +3,6 @@ package sdk
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
@@ -13,6 +12,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/denisbrodbeck/machineid"
|
"github.com/denisbrodbeck/machineid"
|
||||||
|
"github.com/pkg/errors"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
uuid "github.com/satori/go.uuid"
|
uuid "github.com/satori/go.uuid"
|
||||||
|
|
||||||
@@ -159,18 +159,17 @@ func (g *GA4Client) SendEvent(event Event) error {
|
|||||||
Msg("send GA4 event")
|
Msg("send GA4 event")
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return errors.Wrap(err, "marshal GA4 request payload failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
body := bytes.NewReader(bs)
|
body := bytes.NewReader(bs)
|
||||||
res, err := g.httpClient.Post(uri, "application/json", body)
|
res, err := g.httpClient.Post(uri, "application/json", body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return errors.Wrap(err, "request GA4 failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
if res.StatusCode >= 300 {
|
if res.StatusCode >= 300 {
|
||||||
log.Error().Int("statusCode", res.StatusCode).Msg("validation response got unexpected status")
|
return fmt.Errorf("validation response got unexpected status %d", res.StatusCode)
|
||||||
return errors.New("validation response got unexpected status")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if !g.debug {
|
if !g.debug {
|
||||||
@@ -179,13 +178,13 @@ func (g *GA4Client) SendEvent(event Event) error {
|
|||||||
|
|
||||||
bs, err = ioutil.ReadAll(res.Body)
|
bs, err = ioutil.ReadAll(res.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return errors.Wrap(err, "read GA4 response body failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
validationResponse := ValidationResponse{}
|
validationResponse := ValidationResponse{}
|
||||||
err = json.Unmarshal(bs, &validationResponse)
|
err = json.Unmarshal(bs, &validationResponse)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unmarshal response body error: %w", err)
|
return errors.Wrap(err, "unmarshal GA4 response body failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debug().
|
log.Debug().
|
||||||
@@ -195,10 +194,18 @@ func (g *GA4Client) SendEvent(event Event) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func SendGA4Event(e IEvent) error {
|
func SendGA4Event(name string, params map[string]interface{}) {
|
||||||
if env.DISABLE_GA == "true" {
|
if env.DISABLE_GA == "true" {
|
||||||
// do not send GA4 events in CI environment
|
// do not send GA4 events in CI environment
|
||||||
return nil
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
event := Event{
|
||||||
|
Name: name,
|
||||||
|
Params: params,
|
||||||
|
}
|
||||||
|
err := ga4Client.SendEvent(event)
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).Msg("send GA4 event failed")
|
||||||
}
|
}
|
||||||
return gaClient.SendEvent(e)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,10 +8,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func OpenWiki() error {
|
func OpenWiki() error {
|
||||||
sdk.SendEvent(sdk.EventTracking{
|
sdk.SendGA4Event("hrp_wiki", nil)
|
||||||
Category: "OpenWiki",
|
|
||||||
Action: "hrp wiki",
|
|
||||||
})
|
|
||||||
log.Info().Msgf("%s https://httprunner.com", openCmd)
|
log.Info().Msgf("%s https://httprunner.com", openCmd)
|
||||||
return myexec.RunCommand(openCmd, "https://httprunner.com")
|
return myexec.RunCommand(openCmd, "https://httprunner.com")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package convert
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
_ "embed"
|
_ "embed"
|
||||||
"fmt"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
@@ -141,10 +140,13 @@ func (c *TCaseConverter) loadCase(casePath string, fromType FromType) error {
|
|||||||
|
|
||||||
func (c *TCaseConverter) Convert(casePath string, fromType FromType, outputType OutputType) error {
|
func (c *TCaseConverter) Convert(casePath string, fromType FromType, outputType OutputType) error {
|
||||||
// report event
|
// report event
|
||||||
sdk.SendEvent(sdk.EventTracking{
|
sdk.SendGA4Event(
|
||||||
Category: "ConvertTests",
|
"hrp_convert",
|
||||||
Action: fmt.Sprintf("hrp convert --to-%s", outputType.String()),
|
map[string]interface{}{
|
||||||
})
|
"from": fromType.String(),
|
||||||
|
"to": outputType.String(),
|
||||||
|
},
|
||||||
|
)
|
||||||
log.Info().Str("path", casePath).
|
log.Info().Str("path", casePath).
|
||||||
Str("fromType", fromType.String()).
|
Str("fromType", fromType.String()).
|
||||||
Str("outputType", outputType.String()).
|
Str("outputType", outputType.String()).
|
||||||
|
|||||||
+5
-6
@@ -90,15 +90,14 @@ func initPlugin(path, venv string, logOn bool) (plugin funplugin.IPlugin, err er
|
|||||||
pluginMap.Store(pluginPath, plugin)
|
pluginMap.Store(pluginPath, plugin)
|
||||||
|
|
||||||
// report event for initializing plugin
|
// report event for initializing plugin
|
||||||
event := sdk.EventTracking{
|
params := map[string]interface{}{
|
||||||
Category: "InitPlugin",
|
"type": plugin.Type(),
|
||||||
Action: fmt.Sprintf("Init %s plugin", plugin.Type()),
|
"result": "success",
|
||||||
Value: 0, // success
|
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
event.Value = 1 // failed
|
params["result"] = "failed"
|
||||||
}
|
}
|
||||||
go sdk.SendEvent(event)
|
go sdk.SendGA4Event("init_plugin", params)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
+9
-8
@@ -194,14 +194,15 @@ func (r *HRPRunner) GenHTMLReport() *HRPRunner {
|
|||||||
// Run starts to execute one or multiple testcases.
|
// Run starts to execute one or multiple testcases.
|
||||||
func (r *HRPRunner) Run(testcases ...ITestCase) (err error) {
|
func (r *HRPRunner) Run(testcases ...ITestCase) (err error) {
|
||||||
log.Info().Str("hrp_version", version.VERSION).Msg("start running")
|
log.Info().Str("hrp_version", version.VERSION).Msg("start running")
|
||||||
event := sdk.EventTracking{
|
|
||||||
Category: "RunAPITests",
|
startTime := time.Now()
|
||||||
Action: "hrp run",
|
defer func() {
|
||||||
}
|
// report run event
|
||||||
// report start event
|
sdk.SendGA4Event("hrp_run", map[string]interface{}{
|
||||||
go sdk.SendEvent(event)
|
"engagement_time_msec": time.Since(startTime).Milliseconds(),
|
||||||
// report execution timing event
|
})
|
||||||
defer sdk.SendEvent(event.StartTiming("execution"))
|
}()
|
||||||
|
|
||||||
// record execution data to summary
|
// record execution data to summary
|
||||||
s := newOutSummary()
|
s := newOutSummary()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user