github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/workload/cli/format_test.go (about) 1 // Copyright 2019 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package cli 12 13 import ( 14 "bytes" 15 "os" 16 "testing" 17 "time" 18 19 "github.com/cockroachdb/cockroach/pkg/util/leaktest" 20 "github.com/cockroachdb/cockroach/pkg/workload/histogram" 21 "github.com/stretchr/testify/require" 22 ) 23 24 func Example_text_formatter() { 25 testFormatter(&textFormatter{}) 26 27 // output: 28 // _elapsed___errors__ops/sec(inst)___ops/sec(cum)__p50(ms)__p95(ms)__p99(ms)_pMax(ms) 29 // 0.5s 0 2.0 2.0 503.3 503.3 503.3 503.3 read 30 // 1.5s 0 0.7 1.3 335.5 335.5 335.5 335.5 read 31 // 32 // _elapsed___errors_____ops(total)___ops/sec(cum)__avg(ms)__p50(ms)__p95(ms)__p99(ms)_pMax(ms)__total 33 // 2.0s 0 2 1.0 411.0 335.5 503.3 503.3 503.3 read 34 // 35 // _elapsed___errors_____ops(total)___ops/sec(cum)__avg(ms)__p50(ms)__p95(ms)__p99(ms)_pMax(ms)__result 36 // 4.0s 0 2 0.5 411.0 335.5 503.3 503.3 503.3 woo 37 } 38 39 func Example_json_formatter() { 40 testFormatter(&jsonFormatter{w: os.Stdout}) 41 42 // output: 43 // {"time":"0001-01-01T00:00:00.5Z","errs":0,"avgt":2.0,"avgl":2.0,"p50l":503.3,"p95l":503.3,"p99l":503.3,"maxl":503.3,"type":"read"} 44 // {"time":"0001-01-01T00:00:01.5Z","errs":0,"avgt":0.7,"avgl":1.3,"p50l":335.5,"p95l":335.5,"p99l":335.5,"maxl":335.5,"type":"read"} 45 } 46 47 func testFormatter(formatter outputFormat) { 48 reg := histogram.NewRegistry(time.Second) 49 50 start := time.Time{} 51 52 reg.GetHandle().Get("read").Record(time.Second / 2) 53 reg.Tick(func(t histogram.Tick) { 54 // Make output deterministic. 55 t.Elapsed = time.Second / 2 56 t.Now = start.Add(t.Elapsed) 57 58 formatter.outputTick(t.Elapsed, t) 59 }) 60 61 reg.GetHandle().Get("read").Record(time.Second / 3) 62 reg.Tick(func(t histogram.Tick) { 63 // ditto. 64 t.Elapsed = 3 * time.Second / 2 65 t.Now = start.Add(t.Elapsed) 66 67 formatter.outputTick(t.Elapsed, t) 68 }) 69 70 resultTick := histogram.Tick{Name: "woo"} 71 reg.Tick(func(t histogram.Tick) { 72 // ditto. 73 t.Elapsed = 2 * time.Second 74 t.Now = start.Add(t.Elapsed) 75 76 formatter.outputTotal(t.Elapsed, t) 77 resultTick.Now = t.Now 78 resultTick.Cumulative = t.Cumulative 79 }) 80 formatter.outputResult(4*time.Second, resultTick) 81 } 82 83 // TestJSONStructure ensures that the JSON output is parsable. 84 func TestJSONStructure(t *testing.T) { 85 defer leaktest.AfterTest(t)() 86 87 var buf bytes.Buffer 88 f := jsonFormatter{w: &buf} 89 reg := histogram.NewRegistry(time.Second) 90 91 start := time.Time{} 92 93 reg.GetHandle().Get("read").Record(time.Second / 2) 94 reg.Tick(func(t histogram.Tick) { 95 // Make output deterministic. 96 t.Elapsed = time.Second 97 t.Now = start.Add(t.Elapsed) 98 99 f.outputTick(time.Second, t) 100 }) 101 102 const expected = ` 103 { 104 "time":"0001-01-01T00:00:01Z", 105 "avgl":1, 106 "avgt":1, 107 "errs":0, 108 "maxl":503.3, 109 "p50l":503.3, 110 "p95l":503.3, 111 "p99l":503.3, 112 "type":"read" 113 }` 114 require.JSONEq(t, expected, buf.String()) 115 }