From f0acd05e9ca587fc03b8facabae86ed849858b6e Mon Sep 17 00:00:00 2001 From: Bojan Djurkovic Date: Thu, 15 Nov 2018 10:29:28 -0400 Subject: [PATCH] Slightly improve average calculation by using length of lats. Use ephemeral port for tests --- reporter.go | 4 +++- requester_test.go | 12 +++++++++--- stats_handler_test.go | 4 +--- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/reporter.go b/reporter.go index 51f49b0c..3187f8e6 100644 --- a/reporter.go +++ b/reporter.go @@ -83,7 +83,9 @@ type ResultDetail struct { } func newReporter(results chan *callResult, options *Options) *Reporter { + cap := min(options.N, maxResult) + return &Reporter{ options: options, results: results, @@ -123,7 +125,7 @@ func (r *Reporter) Run() { // Finalize all the gathered data into a final report func (r *Reporter) Finalize(total time.Duration) *Report { - average := r.avgTotal / float64(r.totalCount) + average := r.avgTotal / float64(len(r.lats)) avgDuration := time.Duration(average * float64(time.Second)) rps := float64(r.totalCount) / total.Seconds() diff --git a/requester_test.go b/requester_test.go index 59a25a00..d61f4fcf 100644 --- a/requester_test.go +++ b/requester_test.go @@ -2,6 +2,7 @@ package ghz import ( "net" + "strconv" "sync" "testing" "time" @@ -15,11 +16,11 @@ import ( "google.golang.org/grpc/credentials" ) -const port = ":50051" -const localhost = "localhost:50051" +var port string +var localhost string func startServer(secure bool) (*helloworld.Greeter, *grpc.Server, error) { - lis, err := net.Listen("tcp", port) + lis, err := net.Listen("tcp", ":0") if err != nil { return nil, nil, err } @@ -39,9 +40,14 @@ func startServer(secure bool) (*helloworld.Greeter, *grpc.Server, error) { gs := helloworld.NewGreeter() helloworld.RegisterGreeterServer(s, gs) // reflection.Register(s) + + port = strconv.Itoa(lis.Addr().(*net.TCPAddr).Port) + localhost = "localhost:" + port + go func() { s.Serve(lis) }() + return gs, s, err } diff --git a/stats_handler_test.go b/stats_handler_test.go index e428d7c4..99a90641 100644 --- a/stats_handler_test.go +++ b/stats_handler_test.go @@ -10,8 +10,6 @@ import ( "google.golang.org/grpc" ) -const address = "localhost:50051" - func TestStatsHandler(t *testing.T) { _, s, err := startServer(false) @@ -32,7 +30,7 @@ func TestStatsHandler(t *testing.T) { done <- true }() - conn, err := grpc.Dial(address, grpc.WithInsecure(), grpc.WithStatsHandler(&statsHandler{rChan})) + conn, err := grpc.Dial(localhost, grpc.WithInsecure(), grpc.WithStatsHandler(&statsHandler{rChan})) if err != nil { assert.FailNow(t, err.Error())