From ee7249e2914149f3c10f51ac5b986ecce49dc560 Mon Sep 17 00:00:00 2001 From: six-ddc Date: Tue, 22 Jun 2021 22:39:02 +0800 Subject: [PATCH] fix golangci-lint --- charts.go | 16 ++++++++-------- print.go | 35 ++++++++++++++++++----------------- report.go | 12 ++---------- requester.go | 43 ++++++++++++++++++++----------------------- 4 files changed, 48 insertions(+), 58 deletions(-) diff --git a/charts.go b/charts.go index 1462111..face56e 100644 --- a/charts.go +++ b/charts.go @@ -31,7 +31,7 @@ $(function () { setInterval({{ .ViewID }}_sync, {{ .Interval }}); }); function {{ .ViewID }}_sync() { $.ajax({ type: "GET", - url: "http://{{ .Addr }}{{ .ApiPath }}/{{ .Route }}", + url: "http://{{ .Addr }}{{ .APIPath }}/{{ .Route }}", dataType: "json", success: function (result) { let opt = goecharts_{{ .ViewID }}.getOption(); @@ -71,13 +71,13 @@ func (c *Charts) genViewTemplate(vid, route string) string { var d = struct { Interval int Addr string - ApiPath string + APIPath string Route string ViewID string }{ Interval: int(refreshInterval.Milliseconds()), Addr: c.linkAddr, - ApiPath: apiPath, + APIPath: apiPath, Route: route, ViewID: vid, } @@ -166,12 +166,12 @@ func (c *Charts) Handler(ctx *fasthttp.RequestCtx) { path := string(ctx.Path()) switch path { case assertsPath + "echarts.min.js": - ctx.WriteString(EchartJS) + _, _ = ctx.WriteString(EchartJS) case assertsPath + "jquery.min.js": - ctx.WriteString(JqueryJS) + _, _ = ctx.WriteString(JqueryJS) case "/": ctx.SetContentType("text/html") - c.page.Render(ctx) + _ = c.page.Render(ctx) default: if strings.HasPrefix(path, apiPath) { view := path[len(apiPath)+1:] @@ -197,7 +197,7 @@ func (c *Charts) Handler(ctx *fasthttp.RequestCtx) { Time: time.Now().Format(timeFormat), Values: values, } - json.NewEncoder(ctx).Encode(metrics) + _ = json.NewEncoder(ctx).Encode(metrics) } else { ctx.Error("NotFound", fasthttp.StatusNotFound) } @@ -208,5 +208,5 @@ func (c *Charts) Serve() { server := fasthttp.Server{ Handler: cors.DefaultHandler().CorsMiddleware(c.Handler), } - server.Serve(c.ln) + _ = server.Serve(c.ln) } diff --git a/print.go b/print.go index 1607f4f..344f085 100644 --- a/print.go +++ b/print.go @@ -21,7 +21,7 @@ var ( barEnd = "|" barSpinner = []string{"|", "/", "-", "\\"} clearLine = []byte("\r\033[K") - isTerminal = isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsTerminal(os.Stdout.Fd()) + isTerminal = isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()) ) type Printer struct { @@ -39,7 +39,7 @@ func NewPrinter(maxNum int64, maxDuration time.Duration) *Printer { } func (p *Printer) updateProgressValue(rs *SnapshotReport) { - p.pbInc += 1 + p.pbInc++ if p.maxDuration > 0 { n := rs.Elapsed if n > p.maxDuration { @@ -108,6 +108,7 @@ func (p *Printer) PrintLoop(snapshot func() *SnapshotReport, interval time.Durat echo(true) } +//nolint const ( FgBlackColor int = iota + 30 FgRedColor @@ -147,7 +148,7 @@ func alignBulk(bulk [][]string, aligns ...int) { for _, b := range bulk { for i, ali := range aligns { if len(b) >= i+1 { - if i == len(aligns)-1 && ali == ALIGN_LEFT { + if i == len(aligns)-1 && ali == AlignLeft { continue } b[i] = padString(b[i], " ", maxLen[i], ali) @@ -228,9 +229,9 @@ func (p *Printer) buildHistogram(snapshot *SnapshotReport, useSeconds bool, isFi hisBulk = append(hisBulk, row) } if isFinal { - alignBulk(hisBulk, ALIGN_LEFT, ALIGN_RIGHT, ALIGN_RIGHT) + alignBulk(hisBulk, AlignLeft, AlignRight, AlignRight) } else { - alignBulk(hisBulk, ALIGN_LEFT, ALIGN_RIGHT, ALIGN_LEFT) + alignBulk(hisBulk, AlignLeft, AlignRight, AlignLeft) } return hisBulk } @@ -242,9 +243,9 @@ func (p *Printer) buildPercentile(snapshot *SnapshotReport, useSeconds bool) [][ perc := formatFloat64(percentile.Percentile * 100) percBulk[0] = append(percBulk[0], "P"+perc) percBulk[1] = append(percBulk[1], durationToString(percentile.Latency, useSeconds)) - percAligns = append(percAligns, ALIGN_CENTER) + percAligns = append(percAligns, AlignCenter) } - percAligns[0] = ALIGN_LEFT + percAligns[0] = AlignLeft alignBulk(percBulk, percAligns...) return percBulk } @@ -272,7 +273,7 @@ func (p *Printer) buildStats(snapshot *SnapshotReport, useSeconds bool) [][]stri }, ) } - alignBulk(statsBulk, ALIGN_LEFT, ALIGN_CENTER, ALIGN_CENTER, ALIGN_CENTER, ALIGN_CENTER) + alignBulk(statsBulk, AlignLeft, AlignCenter, AlignCenter, AlignCenter, AlignCenter) return statsBulk } @@ -285,7 +286,7 @@ func (p *Printer) buildErrors(snapshot *SnapshotReport) [][]string { if errorsBulks != nil { sort.Slice(errorsBulks, func(i, j int) bool { return errorsBulks[i][1] < errorsBulks[j][1] }) } - alignBulk(errorsBulks, ALIGN_LEFT, ALIGN_LEFT) + alignBulk(errorsBulks, AlignLeft, AlignLeft) return errorsBulks } @@ -320,7 +321,7 @@ func (p *Printer) buildSummary(snapshot *SnapshotReport, isFinal bool) [][]strin []string{"Reads", fmt.Sprintf("%.3fMB/s", snapshot.ReadThroughput)}, []string{"Writes", fmt.Sprintf("%.3fMB/s", snapshot.WriteThroughput)}, ) - alignBulk(summarybulk, ALIGN_LEFT, ALIGN_RIGHT) + alignBulk(summarybulk, AlignLeft, AlignRight) return summarybulk } @@ -331,20 +332,20 @@ func displayWidth(str string) int { } const ( - ALIGN_LEFT = iota - ALIGN_RIGHT - ALIGN_CENTER + AlignLeft = iota + AlignRight + AlignCenter ) func padString(s, pad string, width int, align int) string { gap := width - displayWidth(s) if gap > 0 { - if align == ALIGN_LEFT { + if align == AlignLeft { return s + strings.Repeat(pad, gap) - } else if align == ALIGN_RIGHT { + } else if align == AlignRight { return strings.Repeat(pad, gap) + s - } else if align == ALIGN_CENTER { - gapLeft := int(math.Ceil(float64(gap / 2))) + } else if align == AlignCenter { + gapLeft := gap / 2 gapRight := gap - gapLeft return strings.Repeat(pad, gapLeft) + s + strings.Repeat(pad, gapRight) } diff --git a/report.go b/report.go index 46c1e69..2e25295 100644 --- a/report.go +++ b/report.go @@ -104,14 +104,6 @@ func (s *StreamReport) insert(v float64) { s.latencyStats.Update(v) } -func (s *StreamReport) percentiles() ([]float64, []float64) { - result := make([]float64, len(quantiles)) - for i, f := range quantiles { - result[i] = s.latencyQuantile.Query(f) - } - return quantiles, result -} - func (s *StreamReport) Collect(records <-chan *ReportRecord) { latencyWithinSecTemp := &Stats{} go func() { @@ -153,10 +145,10 @@ func (s *StreamReport) Collect(records <-chan *ReportRecord) { latencyWithinSecTemp.Update(float64(r.cost)) s.insert(float64(r.cost)) if r.code != "" { - s.codes[r.code] += 1 + s.codes[r.code] ++ } if r.error != "" { - s.errors[r.error] += 1 + s.errors[r.error] ++ } s.readBytes = r.readBytes s.writeBytes = r.writeBytes diff --git a/requester.go b/requester.go index 3ddacda..d47151e 100644 --- a/requester.go +++ b/requester.go @@ -97,8 +97,6 @@ type Requester struct { recordChan chan *ReportRecord closeOnce sync.Once - report *StreamReport - errCount int64 wg sync.WaitGroup readBytes int64 @@ -173,7 +171,7 @@ func buildRequestClient(opt *ClientOpt, r *int64, w *int64) (*fasthttp.HostClien DisableHeaderNamesNormalizing: true, } if opt.socks5Proxy != "" { - if strings.Index(opt.socks5Proxy, "://") == -1 { + if !strings.Contains(opt.socks5Proxy, "://") { opt.socks5Proxy = "socks5://" + opt.socks5Proxy } httpClient.Dial = fasthttpproxy.FasthttpSocksDialer(opt.socks5Proxy) @@ -233,26 +231,25 @@ func (r *Requester) DoRequest(req *fasthttp.Request, resp *fasthttp.Response, rr rr.code = "" rr.error = err.Error() return - } else { - switch resp.StatusCode() / 100 { - case 1: - code = "1xx" - case 2: - code = "2xx" - case 3: - code = "3xx" - case 4: - code = "4xx" - case 5: - code = "5xx" - } - err = resp.BodyWriteTo(ioutil.Discard) - if err != nil { - rr.cost = time.Since(startTime) - t1 - rr.code = "" - rr.error = err.Error() - return - } + } + switch resp.StatusCode() / 100 { + case 1: + code = "1xx" + case 2: + code = "2xx" + case 3: + code = "3xx" + case 4: + code = "4xx" + case 5: + code = "5xx" + } + err = resp.BodyWriteTo(ioutil.Discard) + if err != nil { + rr.cost = time.Since(startTime) - t1 + rr.code = "" + rr.error = err.Error() + return } rr.cost = time.Since(startTime) - t1