Skip to content

Commit 24b80df

Browse files
Added initial support for -t flag to show timings (#428)
* Added initial support for -t flag to show timings Shows very basic timing data for the Dial stage (TLS setup and BlockingDial) and InvokeRPC method as well as the total time. * Made timing data part of the very verbose functionality * cleanup * fix --------- Co-authored-by: Scott Blum <[email protected]>
1 parent 334e3f5 commit 24b80df

File tree

1 file changed

+56
-1
lines changed

1 file changed

+56
-1
lines changed

cmd/grpcurl/grpcurl.go

+56-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ var (
155155
verbose = flags.Bool("v", false, prettify(`
156156
Enable verbose output.`))
157157
veryVerbose = flags.Bool("vv", false, prettify(`
158-
Enable very verbose output.`))
158+
Enable very verbose output (includes timing data).`))
159159
serverName = flags.String("servername", "", prettify(`
160160
Override server name when validating TLS certificate. This flag is
161161
ignored if -plaintext or -insecure is used.
@@ -276,6 +276,32 @@ func (cs compositeSource) AllExtensionsForType(typeName string) ([]*desc.FieldDe
276276
return exts, nil
277277
}
278278

279+
type timingData struct {
280+
Title string
281+
Start time.Time
282+
Value time.Duration
283+
Parent *timingData
284+
Sub []*timingData
285+
}
286+
287+
func (d *timingData) Child(title string) *timingData {
288+
if d == nil {
289+
return nil
290+
}
291+
child := &timingData{Title: title, Start: time.Now()}
292+
d.Sub = append(d.Sub, child)
293+
return child
294+
}
295+
296+
func (d *timingData) Done() {
297+
if d == nil {
298+
return
299+
}
300+
if d.Value == 0 {
301+
d.Value = time.Since(d.Start)
302+
}
303+
}
304+
279305
func main() {
280306
flags.Usage = usage
281307
flags.Parse(os.Args[1:])
@@ -361,8 +387,16 @@ func main() {
361387
if *verbose {
362388
verbosityLevel = 1
363389
}
390+
391+
var rootTiming *timingData
364392
if *veryVerbose {
365393
verbosityLevel = 2
394+
395+
rootTiming = &timingData{Title: "Timing Data", Start: time.Now()}
396+
defer func() {
397+
rootTiming.Done()
398+
dumpTiming(rootTiming, 0)
399+
}()
366400
}
367401

368402
var symbol string
@@ -421,6 +455,8 @@ func main() {
421455
}
422456

423457
dial := func() *grpc.ClientConn {
458+
dialTiming := rootTiming.Child("Dial")
459+
defer dialTiming.Done()
424460
dialTime := 10 * time.Second
425461
if *connectTimeout > 0 {
426462
dialTime = time.Duration(*connectTimeout * float64(time.Second))
@@ -453,6 +489,9 @@ func main() {
453489
}
454490
creds = alts.NewClientCreds(clientOptions)
455491
} else if usetls {
492+
tlsTiming := dialTiming.Child("TLS Setup")
493+
defer tlsTiming.Done()
494+
456495
tlsConf, err := grpcurl.ClientTLSConfig(*insecure, *cacert, *cert, *key)
457496
if err != nil {
458497
fail(err, "Failed to create TLS config")
@@ -485,6 +524,7 @@ func main() {
485524
if overrideName != "" {
486525
opts = append(opts, grpc.WithAuthority(overrideName))
487526
}
527+
tlsTiming.Done()
488528
} else {
489529
panic("Should have defaulted to use TLS.")
490530
}
@@ -502,6 +542,8 @@ func main() {
502542
if isUnixSocket != nil && isUnixSocket() {
503543
network = "unix"
504544
}
545+
blockingDialTiming := dialTiming.Child("BlockingDial")
546+
defer blockingDialTiming.Done()
505547
cc, err := grpcurl.BlockingDial(ctx, network, target, creds, opts...)
506548
if err != nil {
507549
fail(err, "Failed to dial target host %q", target)
@@ -749,7 +791,9 @@ func main() {
749791
VerbosityLevel: verbosityLevel,
750792
}
751793

794+
invokeTiming := rootTiming.Child("InvokeRPC")
752795
err = grpcurl.InvokeRPC(ctx, descSource, cc, symbol, append(addlHeaders, rpcHeaders...), h, rf.Next)
796+
invokeTiming.Done()
753797
if err != nil {
754798
if errStatus, ok := status.FromError(err); ok && *formatError {
755799
h.Status = errStatus
@@ -780,6 +824,17 @@ func main() {
780824
}
781825
}
782826

827+
func dumpTiming(td *timingData, lvl int) {
828+
ind := ""
829+
for x := 0; x < lvl; x++ {
830+
ind += " "
831+
}
832+
fmt.Printf("%s%s: %s\n", ind, td.Title, td.Value)
833+
for _, sd := range td.Sub {
834+
dumpTiming(sd, lvl+1)
835+
}
836+
}
837+
783838
func usage() {
784839
fmt.Fprintf(os.Stderr, `Usage:
785840
%s [flags] [address] [list|describe] [symbol]

0 commit comments

Comments
 (0)