2222Package main provides benchmark with setting flags.
2323To run a certain benchmark with profile usage, the command is
2424go run benchmark/benchmain/main.go -kbps=0 -mtu=0 -maxConcurrentCalls=1 \
25- reqSizeBytes=1,1048576 -reqspSizeBytes=1,1048576 -runUnary=true -runStream=true \
26- -traceMode=true -compressionMode =true -latency=0s,5ms -timeout =10s \
25+ - reqSizeBytes=1,1048576 -reqspSizeBytes=1,1048576 -runUnary=true -runStream=true \
26+ -traceMode=true -compressorMode =true -latency=0s,5ms -benchtime =10s \
2727-cpuProfile=cpuProf -memProfile=memProf -memProfileRate=10000
2828*/
2929package main
@@ -32,6 +32,7 @@ import (
3232 "errors"
3333 "flag"
3434 "fmt"
35+ "log"
3536 "net"
3637 "os"
3738 "reflect"
@@ -65,22 +66,22 @@ var (
6566 maxConcurrentCalls = []int {1 , 8 , 64 , 512 }
6667 reqSizeBytes = []int {1 , 1024 , 1024 * 1024 }
6768 respSizeBytes = []int {1 , 1024 , 1024 * 1024 }
68- timeout = [] time.Duration { 1 * time . Second }
69+ benchtime time.Duration
6970 memProfile , cpuProfile string
7071 memProfileRate int
7172 enableCompressor = []bool {false }
7273)
7374
74- func unaryBenchmark (startTimer func (), stopTimer func (int32 ), benchFeatures bm.Features , timeout time.Duration , s * stats.Stats ) {
75+ func unaryBenchmark (startTimer func (), stopTimer func (int32 ), benchFeatures bm.Features , benchtime time.Duration , s * stats.Stats ) {
7576 caller , close := makeFuncUnary (benchFeatures )
7677 defer close ()
77- runBenchmark (caller , startTimer , stopTimer , benchFeatures , timeout , s )
78+ runBenchmark (caller , startTimer , stopTimer , benchFeatures , benchtime , s )
7879}
7980
80- func streamBenchmark (startTimer func (), stopTimer func (int32 ), benchFeatures bm.Features , timeout time.Duration , s * stats.Stats ) {
81+ func streamBenchmark (startTimer func (), stopTimer func (int32 ), benchFeatures bm.Features , benchtime time.Duration , s * stats.Stats ) {
8182 caller , close := makeFuncStream (benchFeatures )
8283 defer close ()
83- runBenchmark (caller , startTimer , stopTimer , benchFeatures , timeout , s )
84+ runBenchmark (caller , startTimer , stopTimer , benchFeatures , benchtime , s )
8485}
8586
8687func makeFuncUnary (benchFeatures bm.Features ) (func (int ), func ()) {
@@ -166,7 +167,7 @@ func streamCaller(stream testpb.BenchmarkService_StreamingCallClient, reqSize, r
166167 }
167168}
168169
169- func runBenchmark (caller func (int ), startTimer func (), stopTimer func (int32 ), benchFeatures bm.Features , timeout time.Duration , s * stats.Stats ) {
170+ func runBenchmark (caller func (int ), startTimer func (), stopTimer func (int32 ), benchFeatures bm.Features , benchtime time.Duration , s * stats.Stats ) {
170171 // Warm up connection.
171172 for i := 0 ; i < 10 ; i ++ {
172173 caller (0 )
@@ -178,7 +179,7 @@ func runBenchmark(caller func(int), startTimer func(), stopTimer func(int32), be
178179 wg sync.WaitGroup
179180 )
180181 wg .Add (benchFeatures .MaxConcurrentCalls )
181- bmEnd := time .Now ().Add (timeout )
182+ bmEnd := time .Now ().Add (benchtime )
182183 var count int32
183184 for i := 0 ; i < benchFeatures .MaxConcurrentCalls ; i ++ {
184185 go func (pos int ) {
@@ -206,13 +207,13 @@ func runBenchmark(caller func(int), startTimer func(), stopTimer func(int32), be
206207func init () {
207208 var runUnary , runStream bool
208209 var traceMode , compressorMode bool
209- var readLatency , readTimeout string
210+ var readLatency string
210211 var readKbps , readMtu , readMaxConcurrentCalls , readReqSizeBytes , readReqspSizeBytes intSliceType
211212 flag .BoolVar (& runUnary , "runUnary" , false , "runUnary" )
212213 flag .BoolVar (& runStream , "runStream" , false , "runStream" )
213214 flag .BoolVar (& traceMode , "traceMode" , false , "traceMode" )
214215 flag .StringVar (& readLatency , "latency" , "" , "latency" )
215- flag .StringVar ( & readTimeout , "timeout " , "" , "timeout " )
216+ flag .DurationVar ( & benchtime , "benchtime " , time . Second , "benchtime " )
216217 flag .Var (& readKbps , "kbps" , "kbps" )
217218 flag .Var (& readMtu , "mtu" , "mtu" )
218219 flag .Var (& readMaxConcurrentCalls , "maxConcurrentCalls" , "maxConcurrentCalls" )
@@ -223,6 +224,9 @@ func init() {
223224 flag .StringVar (& cpuProfile , "cpuProfile" , "" , "cpuProfile" )
224225 flag .BoolVar (& compressorMode , "compressorMode" , false , "compressorMode" )
225226 flag .Parse ()
227+ if flag .NArg () != 0 {
228+ log .Fatal ("Error: unparsed arguments: " , flag .Args ())
229+ }
226230 // If no flags related to mode are set, it runs both by default.
227231 if runUnary || runStream {
228232 runMode [0 ] = runUnary
@@ -236,7 +240,6 @@ func init() {
236240 }
237241 // Time input formats as (time + unit).
238242 readTimeFromInput (& ltc , readLatency )
239- readTimeFromInput (& timeout , readTimeout )
240243 readIntFromIntSlice (& kbps , readKbps )
241244 readIntFromIntSlice (& mtu , readMtu )
242245 readIntFromIntSlice (& maxConcurrentCalls , readMaxConcurrentCalls )
@@ -278,8 +281,7 @@ func readTimeFromInput(values *[]time.Duration, replace string) {
278281 for _ , ltc := range strings .Split (replace , "," ) {
279282 duration , err := time .ParseDuration (ltc )
280283 if err != nil {
281- fmt .Println (err )
282- return
284+ log .Fatal (err .Error ())
283285 }
284286 * values = append (* values , duration )
285287 }
@@ -293,7 +295,7 @@ func main() {
293295 featuresNum := []int {len (enableTrace ), len (ltc ), len (kbps ), len (mtu ),
294296 len (maxConcurrentCalls ), len (reqSizeBytes ), len (respSizeBytes ), len (enableCompressor )}
295297 initalPos := make ([]int , len (featuresPos ))
296- s := stats .NewStats (38 )
298+ s := stats .NewStats (10 )
297299 var memStats runtime.MemStats
298300 var results testing.BenchmarkResult
299301 var startAllocs , startBytes uint64
@@ -331,15 +333,15 @@ func main() {
331333 grpc .EnableTracing = enableTrace [featuresPos [0 ]]
332334 if runMode [0 ] {
333335 fmt .Printf ("Unary-%s-%s:\n " , tracing , benchFeature .String ())
334- unaryBenchmark (startTimer , stopTimer , benchFeature , timeout [ 0 ] , s )
336+ unaryBenchmark (startTimer , stopTimer , benchFeature , benchtime , s )
335337 fmt .Println (results .String (), results .MemString ())
336338 fmt .Println (s .String ())
337339 s .Clear ()
338340 }
339341
340342 if runMode [1 ] {
341343 fmt .Printf ("Stream-%s-%s\n " , tracing , benchFeature .String ())
342- streamBenchmark (startTimer , stopTimer , benchFeature , timeout [ 0 ] , s )
344+ streamBenchmark (startTimer , stopTimer , benchFeature , benchtime , s )
343345 fmt .Println (results .String (), results .MemString ())
344346 fmt .Println (s .String ())
345347 s .Clear ()
0 commit comments