From 84192d489080fe157841227bf727fc63afde92af Mon Sep 17 00:00:00 2001 From: luohoufu Date: Mon, 9 Dec 2024 15:07:53 +0800 Subject: [PATCH] style: format code by go fmt --- .gitignore | 1 + domain.go | 28 ++++++------ loader.go | 122 +++++++++++++++++++++++++---------------------------- main.go | 41 +++++++++--------- runner.go | 18 ++++---- 5 files changed, 101 insertions(+), 109 deletions(-) diff --git a/.gitignore b/.gitignore index b8b4533..98bd9dd 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ bin vendor generated_*.go testing +.idea \ No newline at end of file diff --git a/domain.go b/domain.go index 3d65400..ffcd675 100644 --- a/domain.go +++ b/domain.go @@ -57,10 +57,10 @@ func (m valuesMap) GetValue(key string) (interface{}, error) { } type Request struct { - Method string `config:"method"` - Url string `config:"url"` - Body string `config:"body"` - SimpleMode bool `config:"simple_mode"` + Method string `config:"method"` + Url string `config:"url"` + Body string `config:"body"` + SimpleMode bool `config:"simple_mode"` RepeatBodyNTimes int `config:"body_repeat_times"` Headers []map[string]string `config:"headers"` @@ -72,11 +72,11 @@ type Request struct { RuntimeVariables map[string]string `config:"runtime_variables"` RuntimeBodyLineVariables map[string]string `config:"runtime_body_line_variables"` - ExecuteRepeatTimes int `config:"execute_repeat_times"` + ExecuteRepeatTimes int `config:"execute_repeat_times"` - urlHasTemplate bool - headerHasTemplate bool - bodyHasTemplate bool + urlHasTemplate bool + headerHasTemplate bool + bodyHasTemplate bool headerTemplates map[string]*fasttemplate.Template urlTemplate *fasttemplate.Template @@ -142,11 +142,11 @@ type RunnerConfig struct { // Print the request sent to server LogRequests bool `config:"log_requests"` - BenchmarkOnly bool `config:"benchmark_only"` - DurationInUs bool `config:"duration_in_us"` - NoStats bool `config:"no_stats"` - NoSizeStats bool`config:"no_size_stats"` - MetricSampleSize int`config:"metric_sample_size"` + BenchmarkOnly bool `config:"benchmark_only"` + DurationInUs bool `config:"duration_in_us"` + NoStats bool `config:"no_stats"` + NoSizeStats bool `config:"no_size_stats"` + MetricSampleSize int `config:"metric_sample_size"` // Print the request sent to server if status code matched LogStatusCodes []int `config:"log_status_codes"` @@ -449,7 +449,7 @@ type SleepAction struct { } type RequestResult struct { - RequestCount int + RequestCount int RequestSize int ResponseSize int Status int diff --git a/loader.go b/loader.go index 0d06842..fa4ec5e 100644 --- a/loader.go +++ b/loader.go @@ -90,22 +90,22 @@ func NewLoadGenerator(duration int, goroutines int, statsAggregator chan *LoadSt } httpClient = fasthttp.Client{ - MaxConnsPerHost: goroutines, + MaxConnsPerHost: goroutines, //MaxConns: goroutines, NoDefaultUserAgentHeader: false, DisableHeaderNamesNormalizing: disableHeaderNamesNormalizing, - Name: global.Env().GetAppLowercaseName() + "/" + global.Env().GetVersion() + "/" + global.Env().GetBuildNumber(), - TLSConfig: &tls.Config{InsecureSkipVerify: true}, + Name: global.Env().GetAppLowercaseName() + "/" + global.Env().GetVersion() + "/" + global.Env().GetBuildNumber(), + TLSConfig: &tls.Config{InsecureSkipVerify: true}, } - if readTimeout>0{ - httpClient.ReadTimeout= time.Second * time.Duration(readTimeout) + if readTimeout > 0 { + httpClient.ReadTimeout = time.Second * time.Duration(readTimeout) } - if writeTimeout>0{ - httpClient.WriteTimeout= time.Second * time.Duration(writeTimeout) + if writeTimeout > 0 { + httpClient.WriteTimeout = time.Second * time.Duration(writeTimeout) } - if dialTimeout>0{ - httpClient.Dial=func(addr string) (net.Conn, error) { + if dialTimeout > 0 { + httpClient.Dial = func(addr string) (net.Conn, error) { return fasthttp.DialTimeout(addr, time.Duration(dialTimeout)*time.Second) } } @@ -116,41 +116,37 @@ func NewLoadGenerator(duration int, goroutines int, statsAggregator chan *LoadSt var defaultHTTPPool = fasthttp.NewRequestResponsePool("default_http") - - -func doRequest(config *LoaderConfig,globalCtx util.MapStr,req *fasthttp.Request,resp *fasthttp.Response, item *RequestItem, loadStats *LoadStats,timer *tachymeter.Tachymeter) (continueNext bool,err error) { +func doRequest(config *LoaderConfig, globalCtx util.MapStr, req *fasthttp.Request, resp *fasthttp.Response, item *RequestItem, loadStats *LoadStats, timer *tachymeter.Tachymeter) (continueNext bool, err error) { if item.Request != nil { - - if item.Request.ExecuteRepeatTimes<1{ - item.Request.ExecuteRepeatTimes=1 + if item.Request.ExecuteRepeatTimes < 1 { + item.Request.ExecuteRepeatTimes = 1 } - for i:=0;i0{ + if timeout > 0 { err = httpClient.DoTimeout(req, resp, time.Duration(timeout)*time.Second) - }else{ + } else { err = httpClient.Do(req, resp) } - if global.Env().IsDebug{ + if global.Env().IsDebug { log.Info(resp.String()) } - duration:=time.Since(start) - statsCode:=resp.StatusCode() - + duration := time.Since(start) + statsCode := resp.StatusCode() - if !config.RunnerConfig.BenchmarkOnly && timer!=nil{ + if !config.RunnerConfig.BenchmarkOnly && timer != nil { timer.AddTime(duration) } @@ -164,33 +160,33 @@ func doRequest(config *LoaderConfig,globalCtx util.MapStr,req *fasthttp.Request, stats.Increment("request", "total") stats.Increment("request", strconv.Itoa(resp.StatusCode())) - if err!=nil { + if err != nil { loadStats.NumErrs++ - loadStats.NumInvalid ++ + loadStats.NumInvalid++ } - if !config.RunnerConfig.NoSizeStats{ - loadStats.TotReqSize += int64(req.GetRequestLength()) //TODO inaccurate + if !config.RunnerConfig.NoSizeStats { + loadStats.TotReqSize += int64(req.GetRequestLength()) //TODO inaccurate loadStats.TotRespSize += int64(resp.GetResponseLength()) //TODO inaccurate } - loadStats.NumRequests ++ - loadStats.TotDuration+=duration + loadStats.NumRequests++ + loadStats.TotDuration += duration loadStats.MaxRequestTime = util.MaxDuration(duration, loadStats.MaxRequestTime) loadStats.MinRequestTime = util.MinDuration(duration, loadStats.MinRequestTime) - loadStats.StatusCode[statsCode]+=1 + loadStats.StatusCode[statsCode] += 1 } if config.RunnerConfig.BenchmarkOnly { - return true,err + return true, err } - if item.Register!=nil || item.Assert!=nil ||config.RunnerConfig.LogRequests{ + if item.Register != nil || item.Assert != nil || config.RunnerConfig.LogRequests { //only use last request and response reqBody := req.GetRawBody() respBody := resp.GetRawBody() - if global.Env().IsDebug{ - log.Debugf("final response code: %v, body: %s", resp.StatusCode(),string(respBody)) + if global.Env().IsDebug { + log.Debugf("final response code: %v, body: %s", resp.StatusCode(), string(respBody)) } if item.Request != nil && config.RunnerConfig.LogRequests || util.ContainsInAnyInt32Array(statsCode, config.RunnerConfig.LogStatusCodes) { @@ -198,7 +194,7 @@ func doRequest(config *LoaderConfig,globalCtx util.MapStr,req *fasthttp.Request, log.Infof("status: %v, error: %v, response: %v", statsCode, err, util.SubString(string(respBody), 0, 512)) } - if err!=nil{ + if err != nil { continue } @@ -226,16 +222,16 @@ func doRequest(config *LoaderConfig,globalCtx util.MapStr,req *fasthttp.Request, condition, buildErr := conditions.NewCondition(item.Assert) if buildErr != nil { log.Errorf("failed to build conditions whilte assert existed, error: %+v", buildErr) - loadStats.NumInvalid ++ + loadStats.NumInvalid++ return } if !condition.Check(event) { - loadStats.NumInvalid ++ + loadStats.NumInvalid++ if item.Request != nil { log.Errorf("%s %s, assertion failed, skipping subsequent requests", item.Request.Method, item.Request.Url) } - if !config.RunnerConfig.ContinueOnAssertInvalid{ + if !config.RunnerConfig.ContinueOnAssertInvalid { return false, err } } @@ -279,7 +275,7 @@ func buildCtx(resp *fasthttp.Response, respBody []byte, duration time.Duration) return event } -func (cfg *LoadGenerator) Run(config *LoaderConfig, countLimit int,timer *tachymeter.Tachymeter) { +func (cfg *LoadGenerator) Run(config *LoaderConfig, countLimit int, timer *tachymeter.Tachymeter) { loadStats := &LoadStats{MinRequestTime: time.Millisecond, StatusCode: map[int]int{}} start := time.Now() @@ -292,7 +288,6 @@ func (cfg *LoadGenerator) Run(config *LoaderConfig, countLimit int,timer *tachym resp := defaultHTTPPool.AcquireResponse() defer defaultHTTPPool.ReleaseResponse(resp) - totalRequests := 0 totalRounds := 0 @@ -304,7 +299,7 @@ func (cfg *LoadGenerator) Run(config *LoaderConfig, countLimit int,timer *tachym for _, item := range config.Requests { - if !config.RunnerConfig.BenchmarkOnly{ + if !config.RunnerConfig.BenchmarkOnly { if countLimit > 0 && totalRequests >= countLimit { goto END } @@ -319,12 +314,10 @@ func (cfg *LoadGenerator) Run(config *LoaderConfig, countLimit int,timer *tachym } } + item.prepareRequest(config, globalCtx, req) - - item.prepareRequest(config,globalCtx, req) - - next,_:=doRequest(config,globalCtx, req,resp,&item, loadStats,timer) - if !next{ + next, _ := doRequest(config, globalCtx, req, resp, &item, loadStats, timer) + if !next { break } @@ -340,16 +333,16 @@ func (v *RequestItem) prepareRequest(config *LoaderConfig, globalCtx util.MapStr req.Reset() req.ResetBody() - if v.Request.BasicAuth!=nil && v.Request.BasicAuth.Username != "" { + if v.Request.BasicAuth != nil && v.Request.BasicAuth.Username != "" { req.SetBasicAuth(v.Request.BasicAuth.Username, v.Request.BasicAuth.Password) - }else{ + } else { //try use default auth - if config.RunnerConfig.DefaultBasicAuth!=nil&&config.RunnerConfig.DefaultBasicAuth.Username!=""{ + if config.RunnerConfig.DefaultBasicAuth != nil && config.RunnerConfig.DefaultBasicAuth.Username != "" { req.SetBasicAuth(config.RunnerConfig.DefaultBasicAuth.Username, config.RunnerConfig.DefaultBasicAuth.Password) } } - if v.Request.SimpleMode{ + if v.Request.SimpleMode { req.Header.SetMethod(v.Request.Method) req.SetRequestURI(v.Request.Url) return @@ -383,7 +376,6 @@ func (v *RequestItem) prepareRequest(config *LoaderConfig, globalCtx util.MapStr } } - } //prepare url @@ -397,14 +389,14 @@ func (v *RequestItem) prepareRequest(config *LoaderConfig, globalCtx util.MapStr //set default endpoint parsedUrl := fasthttp.URI{} - err:=parsedUrl.Parse(nil, []byte(url)) - if err!=nil{ + err := parsedUrl.Parse(nil, []byte(url)) + if err != nil { panic(err) } if parsedUrl.Host() == nil || len(parsedUrl.Host()) == 0 { - path,err:=config.RunnerConfig.parseDefaultEndpoint() + path, err := config.RunnerConfig.parseDefaultEndpoint() //log.Infof("default endpoint: %v, %v",path,err) - if err==nil{ + if err == nil { parsedUrl.SetSchemeBytes(path.Scheme()) parsedUrl.SetHostBytes(path.Host()) } @@ -413,8 +405,8 @@ func (v *RequestItem) prepareRequest(config *LoaderConfig, globalCtx util.MapStr req.SetRequestURI(url) - if global.Env().IsDebug{ - log.Debugf("final request url: %v %s", v.Request.Method,url) + if global.Env().IsDebug { + log.Debugf("final request url: %v %s", v.Request.Method, url) } //prepare method @@ -478,22 +470,22 @@ func (cfg *LoadGenerator) Warmup(config *LoaderConfig) int { defer defaultHTTPPool.ReleaseResponse(resp) globalCtx := util.MapStr{} for _, v := range config.Requests { - v.prepareRequest(config,globalCtx, req) - next,err:=doRequest(config,globalCtx,req,resp, &v, loadStats,nil) - for k,_:=range loadStats.StatusCode{ - if len(config.RunnerConfig.ValidStatusCodesDuringWarmup)>0{ - if util.ContainsInAnyInt32Array(k,config.RunnerConfig.ValidStatusCodesDuringWarmup){ + v.prepareRequest(config, globalCtx, req) + next, err := doRequest(config, globalCtx, req, resp, &v, loadStats, nil) + for k, _ := range loadStats.StatusCode { + if len(config.RunnerConfig.ValidStatusCodesDuringWarmup) > 0 { + if util.ContainsInAnyInt32Array(k, config.RunnerConfig.ValidStatusCodesDuringWarmup) { continue } } if k >= 400 || k == 0 || err != nil { - log.Infof("requests seems failed to process, err: %v, are you sure to continue?\nPress `Ctrl+C` to skip or press 'Enter' to continue...",err) + log.Infof("requests seems failed to process, err: %v, are you sure to continue?\nPress `Ctrl+C` to skip or press 'Enter' to continue...", err) reader := bufio.NewReader(os.Stdin) reader.ReadString('\n') break } } - if !next{ + if !next { break } } diff --git a/main.go b/main.go index f25cde4..18fc795 100644 --- a/main.go +++ b/main.go @@ -92,8 +92,8 @@ func startLoader(cfg *LoaderConfig) *LoadStats { flag.Parse() - if cfg.RunnerConfig.MetricSampleSize<=0{ - cfg.RunnerConfig.MetricSampleSize=10000 + if cfg.RunnerConfig.MetricSampleSize <= 0 { + cfg.RunnerConfig.MetricSampleSize = 10000 } // Initialize tachymeter. @@ -136,7 +136,7 @@ func startLoader(cfg *LoaderConfig) *LoadStats { leftDoc -= thisDoc } - go loadGen.Run(cfg, thisDoc,timer) + go loadGen.Run(cfg, thisDoc, timer) } responders := 0 @@ -173,7 +173,7 @@ func startLoader(cfg *LoaderConfig) *LoadStats { return nil } - finalDuration:=time.Since(wallTimeStart) + finalDuration := time.Since(wallTimeStart) // When finished, set elapsed wall time. timer.SetWallTime(finalDuration) @@ -191,33 +191,32 @@ func startLoader(cfg *LoaderConfig) *LoadStats { // Flush before printing stats to avoid logging mixing with stats log.Flush() - if cfg.RunnerConfig.NoSizeStats{ + if cfg.RunnerConfig.NoSizeStats { fmt.Printf("\n%v requests finished in %v\n", aggStats.NumRequests, avgThreadDur) - }else{ + } else { fmt.Printf("\n%v requests finished in %v, %v sent, %v received\n", aggStats.NumRequests, avgThreadDur, util.ByteValue{float64(aggStats.TotReqSize)}, util.ByteValue{float64(aggStats.TotRespSize)}) } fmt.Println("\n[Loadgen Client Metrics]") - fmt.Printf("Requests/sec:\t\t%.2f\n",roughReqRate) + fmt.Printf("Requests/sec:\t\t%.2f\n", roughReqRate) - if !cfg.RunnerConfig.BenchmarkOnly&& !cfg.RunnerConfig.NoSizeStats { + if !cfg.RunnerConfig.BenchmarkOnly && !cfg.RunnerConfig.NoSizeStats { fmt.Printf( "Request Traffic/sec:\t%v\n"+ - "Total Transfer/sec:\t%v\n", + "Total Transfer/sec:\t%v\n", util.ByteValue{roughReqBytesRate}, util.ByteValue{roughBytesRate}) } - fmt.Printf("Fastest Request:\t%v\n", aggStats.MinRequestTime) fmt.Printf("Slowest Request:\t%v\n", aggStats.MaxRequestTime) - if cfg.RunnerConfig.AssertError{ + if cfg.RunnerConfig.AssertError { fmt.Printf("Number of Errors:\t%v\n", aggStats.NumErrs) } - if cfg.RunnerConfig.AssertInvalid{ + if cfg.RunnerConfig.AssertInvalid { fmt.Printf("Number of Invalid:\t%v\n", aggStats.NumInvalid) } @@ -225,7 +224,7 @@ func startLoader(cfg *LoaderConfig) *LoadStats { fmt.Printf("Status %v:\t\t%v\n", k, v) } - if !cfg.RunnerConfig.BenchmarkOnly&& !cfg.RunnerConfig.NoStats{ + if !cfg.RunnerConfig.BenchmarkOnly && !cfg.RunnerConfig.NoStats { // Rate outputs will be accurate. fmt.Println("\n[Latency Metrics]") fmt.Println(timer.Calc().String()) @@ -234,9 +233,9 @@ func startLoader(cfg *LoaderConfig) *LoadStats { fmt.Println(timer.Calc().Histogram.String(30)) } - fmt.Printf("\n[Estimated Server Metrics]\nRequests/sec:\t\t%.2f\nAvg Req Time:\t\t%v\n", reqRate, avgReqTime) - if !cfg.RunnerConfig.BenchmarkOnly&& !cfg.RunnerConfig.NoSizeStats { - fmt.Printf("Transfer/sec:\t\t%v\n", util.ByteValue{bytesRate}) + fmt.Printf("\n[Estimated Server Metrics]\nRequests/sec:\t\t%.2f\nAvg Req Time:\t\t%v\n", reqRate, avgReqTime) + if !cfg.RunnerConfig.BenchmarkOnly && !cfg.RunnerConfig.NoSizeStats { + fmt.Printf("Transfer/sec:\t\t%v\n", util.ByteValue{bytesRate}) } fmt.Println("") @@ -429,7 +428,7 @@ func runDSLFile(appConfig *AppConfig, path string) int { } func runDSL(appConfig *AppConfig, dsl string) int { - loaderConfig := parseDSL(appConfig,dsl) + loaderConfig := parseDSL(appConfig, dsl) return runLoaderConfig(&loaderConfig) } @@ -453,10 +452,10 @@ func runLoaderConfig(config *LoaderConfig) int { } // parseDSL parses a DSL string to LoaderConfig. -func parseDSL(appConfig *AppConfig,input string) (output LoaderConfig) { - output= LoaderConfig{} - output.RunnerConfig=appConfig.RunnerConfig - output.Variable=appConfig.Variable +func parseDSL(appConfig *AppConfig, input string) (output LoaderConfig) { + output = LoaderConfig{} + output.RunnerConfig = appConfig.RunnerConfig + output.Variable = appConfig.Variable outputStr, err := loadPlugins([][]byte{loadgenDSL}, input) if err != nil { diff --git a/runner.go b/runner.go index 863ee87..633bad3 100644 --- a/runner.go +++ b/runner.go @@ -119,18 +119,18 @@ func runTest(config *AppConfig, cwd string, test Test) (*TestResult, error) { loaderConfigPath := path.Join(testPath, "loadgen.dsl") //auto resolve the loaderConfigPath - if !util.FileExists(loaderConfigPath){ - temp:=path.Join(filepath.Dir(global.Env().GetConfigFile()),loaderConfigPath) - if util.FileExists(temp){ - loaderConfigPath=temp - }else{ - temp:=path.Join(filepath.Dir(global.Env().GetConfigDir()),loaderConfigPath) - if util.FileExists(temp){ - loaderConfigPath=temp + if !util.FileExists(loaderConfigPath) { + temp := path.Join(filepath.Dir(global.Env().GetConfigFile()), loaderConfigPath) + if util.FileExists(temp) { + loaderConfigPath = temp + } else { + temp := path.Join(filepath.Dir(global.Env().GetConfigDir()), loaderConfigPath) + if util.FileExists(temp) { + loaderConfigPath = temp } } } - loaderConfigPath,_=filepath.Abs(loaderConfigPath) + loaderConfigPath, _ = filepath.Abs(loaderConfigPath) //log.Debugf("Executing gateway within %s", testPath) //if err := os.Chdir(filepath.Dir(loaderConfigPath)); err != nil {