Skip to content

Commit cc1b034

Browse files
committed
asim: add TestExploreTDTV
This can be used to understand how the tdtv approaches its final value as it is repeatedly evaluated over longer prefixes of the underlying metric.
1 parent cc49d30 commit cc1b034

File tree

2 files changed

+37
-16
lines changed

2 files changed

+37
-16
lines changed

pkg/kv/kvserver/asim/history/history.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,28 +67,28 @@ func (h *History) ThrashingForStat(stat string) ThrashingSlice {
6767

6868
ths := make(ThrashingSlice, numStores)
6969
for storeIdx := range vsByStore {
70-
sl := vsByStore[storeIdx]
71-
// HACK: instead of the slice directly, we measure the thrashing of a slice
72-
// that has all leading zeroes removed. This works around the fact that some
73-
// timeseries only show sensible values after an initial period of
74-
// inactivity. For example, CPU usage is zero until the first stats tick.
75-
// Without this hack, the large initial jump from zero to the first value
76-
// would be interpreted as variation.
77-
noLeadingZeroes := sl
78-
for i := 0; i < len(sl); i++ {
79-
if sl[i] == 0 {
80-
noLeadingZeroes = sl[i+1:]
81-
continue
82-
}
83-
break
84-
}
85-
th := computeThrashing(noLeadingZeroes)
70+
// HACK: we remove leading zeroes before computing the thrashin This works
71+
// around the fact that some timeseries only show sensible values after an
72+
// initial period of inactivity. For example, CPU usage is zero until the
73+
// first stats tick. Without this hack, the large initial jump from zero to
74+
// the first value would be interpreted as variation.
75+
th := computeThrashing(stripLeaderingZeroes(vsByStore[storeIdx]))
8676
ths[storeIdx] = th
8777
}
8878
ths.normalize()
8979
return ths
9080
}
9181

82+
func stripLeaderingZeroes(vs []float64) []float64 {
83+
for i := range vs {
84+
if vs[i] == 0 {
85+
continue
86+
}
87+
return vs[i:]
88+
}
89+
return nil
90+
}
91+
9292
// Thrashing returns a string representation of the thrashing for the given
9393
// stat.
9494
func (h *History) Thrashing(stat string) string {

pkg/kv/kvserver/asim/history/trashing_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"testing"
1313
"testing/quick"
1414

15+
"github.com/cockroachdb/cockroach/pkg/testutils"
1516
"github.com/cockroachdb/cockroach/pkg/testutils/datapathutils"
1617
"github.com/cockroachdb/cockroach/pkg/testutils/echotest"
1718
"github.com/cockroachdb/cockroach/pkg/util/randutil"
@@ -169,3 +170,23 @@ func TestTDTV(t *testing.T) {
169170
_, _ = fmt.Fprint(&buf, asciigraph.Plot(sl, asciigraph.Width(2*N), asciigraph.Height(N)))
170171
echotest.Require(t, buf.String(), datapathutils.TestDataPath(t, t.Name()+".txt"))
171172
}
173+
174+
func TestExploreTDTV(t *testing.T) {
175+
// Not a real test, more a helper.
176+
sl := []float64{
177+
// You can paste a time series copied from viewer.html here to explore
178+
// the variation of its prefixes.
179+
0,
180+
0,
181+
}
182+
testutils.RunTrueAndFalse(t, "stripped", func(t *testing.T, stripped bool) {
183+
sl := sl
184+
if stripped {
185+
sl = stripLeaderingZeroes(sl)
186+
}
187+
for i := len(sl) / 100; i < len(sl); i += len(sl) / 100 {
188+
t.Logf("sl[:%d]: %+v", i, computeThrashing(sl[:i]))
189+
}
190+
191+
})
192+
}

0 commit comments

Comments
 (0)