Commit 24ead3a4 by ryan

add random_walk_table scenario

parent 2b5ac6ba
...@@ -2,6 +2,7 @@ package testdata ...@@ -2,6 +2,7 @@ package testdata
import ( import (
"encoding/json" "encoding/json"
"math"
"math/rand" "math/rand"
"strconv" "strconv"
"strings" "strings"
...@@ -101,6 +102,15 @@ func init() { ...@@ -101,6 +102,15 @@ func init() {
}) })
registerScenario(&Scenario{ registerScenario(&Scenario{
Id: "random_walk_table",
Name: "Random Walk Table",
Handler: func(query *tsdb.Query, context *tsdb.TsdbQuery) *tsdb.QueryResult {
return getRandomWalkTable(query, context)
},
})
registerScenario(&Scenario{
Id: "slow_query", Id: "slow_query",
Name: "Slow Query", Name: "Slow Query",
StringInput: "5s", StringInput: "5s",
...@@ -267,6 +277,64 @@ func getRandomWalk(query *tsdb.Query, tsdbQuery *tsdb.TsdbQuery) *tsdb.QueryResu ...@@ -267,6 +277,64 @@ func getRandomWalk(query *tsdb.Query, tsdbQuery *tsdb.TsdbQuery) *tsdb.QueryResu
return queryRes return queryRes
} }
func getRandomWalkTable(query *tsdb.Query, tsdbQuery *tsdb.TsdbQuery) *tsdb.QueryResult {
timeWalkerMs := tsdbQuery.TimeRange.GetFromAsMsEpoch()
to := tsdbQuery.TimeRange.GetToAsMsEpoch()
table := tsdb.Table{
Columns: []tsdb.TableColumn{
{Text: "Time"},
{Text: "Value"},
{Text: "Min"},
{Text: "Max"},
{Text: "Info"},
},
Rows: []tsdb.RowValues{},
}
withNil := query.Model.Get("withNil").MustBool(false)
walker := query.Model.Get("startValue").MustFloat64(rand.Float64() * 100)
spread := 2.5
var info strings.Builder
for i := int64(0); i < query.MaxDataPoints && timeWalkerMs < to; i++ {
delta := rand.Float64() - 0.5
walker += delta
info.Reset()
if delta > 0 {
info.WriteString("up")
} else {
info.WriteString("down")
}
if math.Abs(delta) > .4 {
info.WriteString(" fast")
}
row := tsdb.RowValues{
float64(timeWalkerMs),
walker,
walker - ((rand.Float64() * spread) + 0.01), // Min
walker + ((rand.Float64() * spread) + 0.01), // Max
info.String(),
}
// Add some random null values
if withNil && rand.Float64() > 0.8 {
for i := 1; i < 4; i++ {
if rand.Float64() > .2 {
row[i] = nil
}
}
}
table.Rows = append(table.Rows, row)
timeWalkerMs += query.IntervalMs
}
queryRes := tsdb.NewQueryResult()
queryRes.Tables = append(queryRes.Tables, &table)
return queryRes
}
func registerScenario(scenario *Scenario) { func registerScenario(scenario *Scenario) {
ScenarioRegistry[scenario.Id] = scenario ScenarioRegistry[scenario.Id] = scenario
} }
......
...@@ -11,27 +11,86 @@ import ( ...@@ -11,27 +11,86 @@ import (
func TestTestdataScenarios(t *testing.T) { func TestTestdataScenarios(t *testing.T) {
Convey("random walk ", t, func() { Convey("random walk ", t, func() {
if scenario, exist := ScenarioRegistry["random_walk"]; exist { scenario, exist := ScenarioRegistry["random_walk"]
So(exist, ShouldBeTrue)
Convey("Should start at the requested value", func() {
req := &tsdb.TsdbQuery{ Convey("Should start at the requested value", func() {
TimeRange: tsdb.NewFakeTimeRange("5m", "now", time.Now()), req := &tsdb.TsdbQuery{
Queries: []*tsdb.Query{ TimeRange: tsdb.NewFakeTimeRange("5m", "now", time.Now()),
{RefId: "A", IntervalMs: 100, MaxDataPoints: 10, Model: simplejson.New()}, Queries: []*tsdb.Query{
}, {RefId: "A", IntervalMs: 100, MaxDataPoints: 100, Model: simplejson.New()},
} },
query := req.Queries[0] }
query.Model.Set("startValue", 1.234) query := req.Queries[0]
query.Model.Set("startValue", 1.234)
result := scenario.Handler(req.Queries[0], req)
points := result.Series[0].Points
So(result.Series, ShouldNotBeNil)
So(points[0][0].Float64, ShouldEqual, 1.234)
})
})
Convey("random walk table", t, func() {
scenario, exist := ScenarioRegistry["random_walk_table"]
So(exist, ShouldBeTrue)
Convey("Should return a table that looks like value/min/max", func() {
req := &tsdb.TsdbQuery{
TimeRange: tsdb.NewFakeTimeRange("5m", "now", time.Now()),
Queries: []*tsdb.Query{
{RefId: "A", IntervalMs: 100, MaxDataPoints: 100, Model: simplejson.New()},
},
}
result := scenario.Handler(req.Queries[0], req)
table := result.Tables[0]
result := scenario.Handler(req.Queries[0], req) So(len(table.Rows), ShouldBeGreaterThan, 50)
points := result.Series[0].Points for _, row := range table.Rows {
value := row[1]
min := row[2]
max := row[3]
So(result.Series, ShouldNotBeNil) So(min, ShouldBeLessThan, value)
So(points[0][0].Float64, ShouldEqual, 1.234) So(max, ShouldBeGreaterThan, value)
}) }
})
Convey("Should return a table with some nil values", func() {
req := &tsdb.TsdbQuery{
TimeRange: tsdb.NewFakeTimeRange("5m", "now", time.Now()),
Queries: []*tsdb.Query{
{RefId: "A", IntervalMs: 100, MaxDataPoints: 100, Model: simplejson.New()},
},
}
query := req.Queries[0]
query.Model.Set("withNil", true)
result := scenario.Handler(req.Queries[0], req)
table := result.Tables[0]
nil1 := false
nil2 := false
nil3 := false
So(len(table.Rows), ShouldBeGreaterThan, 50)
for _, row := range table.Rows {
if row[1] == nil {
nil1 = true
}
if row[2] == nil {
nil2 = true
}
if row[3] == nil {
nil3 = true
}
}
} else { So(nil1, ShouldBeTrue)
t.Fail() So(nil2, ShouldBeTrue)
} So(nil3, ShouldBeTrue)
})
}) })
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment