Use goroutine to speed up `dashboard global` command (#72)
diff --git a/graphql/dashboard/global.go b/graphql/dashboard/global.go
index 3dbf31a..c004acd 100644
--- a/graphql/dashboard/global.go
+++ b/graphql/dashboard/global.go
@@ -21,6 +21,7 @@
"bytes"
"io/ioutil"
"strings"
+ "sync"
"github.com/spf13/viper"
"github.com/urfave/cli"
@@ -193,11 +194,36 @@
}
func Global(ctx *cli.Context, duration schema.Duration) *GlobalData {
- var globalData GlobalData
+ // Load template to `globalTemplate`, so the subsequent three calls can ues it directly.
+ _, err := LoadTemplate(ctx.String("template"))
+ if err != nil {
+ return nil
+ }
- globalData.Metrics = Metrics(ctx, duration)
- globalData.ResponseLatency = responseLatency(ctx, duration)
- globalData.HeatMap = heatMap(ctx, duration)
+ // Use three goroutines to enable concurrent execution of three graphql queries.
+ var wg sync.WaitGroup
+ wg.Add(3)
+ var m [][]*schema.SelectedRecord
+ go func() {
+ m = Metrics(ctx, duration)
+ wg.Done()
+ }()
+ var rl []map[string]float64
+ go func() {
+ rl = responseLatency(ctx, duration)
+ wg.Done()
+ }()
+ var hm schema.HeatMap
+ go func() {
+ hm = heatMap(ctx, duration)
+ wg.Done()
+ }()
+ wg.Wait()
+
+ var globalData GlobalData
+ globalData.Metrics = m
+ globalData.ResponseLatency = rl
+ globalData.HeatMap = hm
return &globalData
}