Skip to content

Commit

Permalink
Merge pull request #113 from bojand/golint
Browse files Browse the repository at this point in the history
Improve makefile and add golangci-lint
  • Loading branch information
bojand authored Jul 25, 2019
2 parents 741b19e + a91345f commit 8d3e42d
Show file tree
Hide file tree
Showing 28 changed files with 208 additions and 203 deletions.
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ jobs:
- image: circleci/golang:1.11
steps:
- checkout
- run: make setup
- run: make
workflows:
version: 2
Expand Down
16 changes: 16 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# TODO Look into enabling all with some exception where practical
# linters:
# enable-all: true
# disable:
# - gochecknoglobals
# - dupl
# - lll
issues:
exclude-rules:
# Exclude lostcancel govet rule specifically for requester.go
# Since we purpocefully do that. See comments in code.
- path: runner/requester.go
text: "lostcancel"
# TODO Look into fixing time.Tick() usage SA1015 in worker.go
- path: runner/worker.go
text: "SA1015"
35 changes: 15 additions & 20 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ unexport GOROOT
export GO111MODULE := on
# Set the location where we install Golang binaries via go install to TMP_BIN.
export GOBIN := $(abspath $(TMP_BIN))
# Set BINDIR location needed by golangci-lint install script, otherwise it goes to ./bin
export BINDIR := $(GOBIN)
# Add GOBIN to to the front of the PATH. This allows us to invoke binaries we install.
export PATH := :$(GOBIN):$(PATH)

Expand All @@ -70,6 +72,12 @@ GO_MODULE := $(shell grep '^module ' go.mod | cut -f 2 -d ' ')
# Run all by default when "make" is invoked.
.DEFAULT_GOAL := all

# Install all the build and lint dependencies
setup:
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh
go mod download
.PHONY: setup

# All runs the default lint, test, and code coverage targets.
.PHONY: all
all: lint cover
Expand All @@ -79,34 +87,21 @@ all: lint cover
clean:
rm -rf $(TMP_BASE)

# Golint runs the golint linter.
.PHONY: golint
golint:
$(AT) go install golang.org/x/lint/golint
golint -set_exit_status $(GO_PKGS)

# Errcheck runs the errcheck linter.
.PHONY: errcheck
errcheck:
$(AT) go install github.com/kisielk/errcheck
errcheck -ignoretests $(GO_PKGS)

# Staticcheck runs the staticcheck linter.
.PHONY: staticcheck
staticcheck:
$(AT) go install honnef.co/go/tools/cmd/staticcheck
staticcheck --tests=false $(GO_PKGS)

# Lint runs all linters. This is the main lint target to run.
# TODO: add errcheck and staticcheck when the code is updated to pass them
.PHONY: lint
lint: golint errcheck
lint:
golangci-lint run ./...

# Test runs go test on GO_PKGS. This does not produce code coverage.
.PHONY: test
test:
go test $(GO_TEST_FLAGS) $(GO_PKGS)

# gofmt and goimports all go files
fmt:
find . -name '*.go' -not -wholename './vendor/*' | while read -r file; do gofmt -w -s "$$file"; goimports -w "$$file"; done
.PHONY: fmt

# Cover runs go_test on GO_PKGS and produces code coverage in multiple formats.
# A coverage.html file for human viewing will be at $(TMP_COVERAGE)/coverage.html
# This target will echo "open $(TMP_COVERAGE)/coverage.html" with TMP_COVERAGE
Expand Down
12 changes: 6 additions & 6 deletions internal/helloworld/greeter_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type Greeter struct {

mutex *sync.RWMutex
callCounts map[CallType]int
calls map[CallType][][]*HelloRequest
calls map[CallType][][]*HelloRequest
}

func randomSleep() {
Expand Down Expand Up @@ -188,10 +188,10 @@ func (s *Greeter) GetConnectionCount() int {
// NewGreeter creates new greeter server
func NewGreeter() *Greeter {
streamData := []*HelloReply{
&HelloReply{Message: "Hello Bob"},
&HelloReply{Message: "Hello Kate"},
&HelloReply{Message: "Hello Jim"},
&HelloReply{Message: "Hello Sara"},
{Message: "Hello Bob"},
{Message: "Hello Kate"},
{Message: "Hello Jim"},
{Message: "Hello Sara"},
}

greeter := &Greeter{streamData: streamData, mutex: &sync.RWMutex{}}
Expand Down Expand Up @@ -245,4 +245,4 @@ func (c *HWStatsHandler) HandleRPC(ctx context.Context, rs stats.RPCStats) {
// TagRPC implements per-RPC context management.
func (c *HWStatsHandler) TagRPC(ctx context.Context, info *stats.RPCTagInfo) context.Context {
return ctx
}
}
2 changes: 1 addition & 1 deletion printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func jsonify(v interface{}, pretty bool) string {
return string(d)
}

return string(out.Bytes())
return out.String()
}

func formatNanoUnit(d time.Duration) string {
Expand Down
51 changes: 26 additions & 25 deletions printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,59 +57,59 @@ func TestPrinter_getInfluxLine(t *testing.T) {
},
},
LatencyDistribution: []runner.LatencyDistribution{
runner.LatencyDistribution{
{
Percentage: 25,
Latency: time.Duration(1 * time.Millisecond),
},
runner.LatencyDistribution{
{
Percentage: 50,
Latency: time.Duration(5 * time.Millisecond),
},
runner.LatencyDistribution{
{
Percentage: 75,
Latency: time.Duration(10 * time.Millisecond),
},
runner.LatencyDistribution{
{
Percentage: 90,
Latency: time.Duration(15 * time.Millisecond),
},
runner.LatencyDistribution{
{
Percentage: 95,
Latency: time.Duration(20 * time.Millisecond),
},
runner.LatencyDistribution{
{
Percentage: 99,
Latency: time.Duration(25 * time.Millisecond),
}},
Histogram: []runner.Bucket{
runner.Bucket{
{
Mark: 0.01,
Count: 1,
Frequency: 0.005,
},
runner.Bucket{
{
Mark: 0.02,
Count: 10,
Frequency: 0.01,
},
runner.Bucket{
{
Mark: 0.03,
Count: 50,
Frequency: 0.1,
},
runner.Bucket{
{
Mark: 0.05,
Count: 60,
Frequency: 0.15,
},
runner.Bucket{
{
Mark: 0.1,
Count: 15,
Frequency: 0.07,
},
},
Details: []runner.ResultDetail{
runner.ResultDetail{
{
Timestamp: date,
Latency: time.Duration(1 * time.Millisecond),
Status: "OK",
Expand Down Expand Up @@ -176,59 +176,59 @@ func TestPrinter_printInfluxDetails(t *testing.T) {
},
},
LatencyDistribution: []runner.LatencyDistribution{
runner.LatencyDistribution{
{
Percentage: 25,
Latency: time.Duration(1 * time.Millisecond),
},
runner.LatencyDistribution{
{
Percentage: 50,
Latency: time.Duration(5 * time.Millisecond),
},
runner.LatencyDistribution{
{
Percentage: 75,
Latency: time.Duration(10 * time.Millisecond),
},
runner.LatencyDistribution{
{
Percentage: 90,
Latency: time.Duration(15 * time.Millisecond),
},
runner.LatencyDistribution{
{
Percentage: 95,
Latency: time.Duration(20 * time.Millisecond),
},
runner.LatencyDistribution{
{
Percentage: 99,
Latency: time.Duration(25 * time.Millisecond),
}},
Histogram: []runner.Bucket{
runner.Bucket{
{
Mark: 0.01,
Count: 1,
Frequency: 0.005,
},
runner.Bucket{
{
Mark: 0.02,
Count: 10,
Frequency: 0.01,
},
runner.Bucket{
{
Mark: 0.03,
Count: 50,
Frequency: 0.1,
},
runner.Bucket{
{
Mark: 0.05,
Count: 60,
Frequency: 0.15,
},
runner.Bucket{
{
Mark: 0.1,
Count: 15,
Frequency: 0.07,
},
},
Details: []runner.ResultDetail{
runner.ResultDetail{
{
Timestamp: date,
Latency: time.Duration(1 * time.Millisecond),
Status: "OK",
Expand All @@ -244,7 +244,8 @@ func TestPrinter_printInfluxDetails(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
buf := bytes.NewBufferString("")
p := ReportPrinter{Report: &tt.report, Out: buf}
p.printInfluxDetails()
err := p.printInfluxDetails()
assert.NoError(t, err)
actual := buf.String()
assert.Equal(t, tt.expected, actual)
})
Expand Down
6 changes: 4 additions & 2 deletions runner/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package runner

import (
"encoding/json"
"github.com/bojand/ghz/testdata"
"github.com/golang/protobuf/proto"
"testing"

"github.com/golang/protobuf/proto"

"github.com/bojand/ghz/internal/helloworld"
"github.com/bojand/ghz/protodesc"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -208,6 +209,7 @@ func TestData_createPayloads(t *testing.T) {
msg1.Name = "bob"

binData, err := proto.Marshal(msg1)
assert.NoError(t, err)

inputs, err := createPayloadsFromBin(binData, mtdUnary)

Expand Down
2 changes: 1 addition & 1 deletion runner/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ func createClientTransportCredentials(skipVerify bool, cacertFile, clientCertFil
tlsConf.Certificates = []tls.Certificate{certificate}
}

if skipVerify == true {
if skipVerify {
tlsConf.InsecureSkipVerify = true
} else if cacertFile != "" {
// Create a certificate pool from the certificate authority
Expand Down
2 changes: 1 addition & 1 deletion runner/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (r *Reporter) Run() {
errStr = res.err.Error()
r.errorDist[errStr]++
}

if len(r.details) < maxResult {
r.details = append(r.details, ResultDetail{
Latency: res.duration,
Expand Down
2 changes: 1 addition & 1 deletion runner/requester.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func (b *Requester) Stop(reason StopReason) {
// Finish finishes the test run
func (b *Requester) Finish() *Report {
close(b.results)
total := time.Now().Sub(b.start)
total := time.Since(b.start)

// Wait until the reporter is done.
<-b.reporter.done
Expand Down
Loading

0 comments on commit 8d3e42d

Please sign in to comment.