Skip to content

Commit c2cf545

Browse files
committed
fix remaining unit tests
1 parent d2b61b7 commit c2cf545

File tree

8 files changed

+1322
-88
lines changed

8 files changed

+1322
-88
lines changed

wasp/benchspy/basic_test.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ func TestBenchSpy_NewBasicData(t *testing.T) {
1313
testName := t.Name()
1414
gen := &wasp.Generator{
1515
Cfg: &wasp.Config{
16-
T: t,
17-
GenName: "gen1",
16+
T: t,
17+
GenName: "gen1",
18+
Schedule: []*wasp.Segment{{From: 1, Duration: time.Hour, StartTime: time.Now().Add(-time.Hour), EndTime: time.Now()}},
1819
},
1920
}
2021

@@ -68,6 +69,9 @@ func TestBenchSpy_MustNewBasicData(t *testing.T) {
6869
Cfg: &wasp.Config{
6970
T: t,
7071
GenName: "gen1",
72+
Schedule: []*wasp.Segment{
73+
{From: 1, Duration: time.Hour, StartTime: time.Now().Add(-time.Hour), EndTime: time.Now()},
74+
},
7175
},
7276
}
7377

@@ -115,15 +119,13 @@ func TestBenchSpy_BasicData_FillStartEndTimes(t *testing.T) {
115119
T: t,
116120
GenName: "gen1",
117121
Schedule: []*wasp.Segment{
118-
{EndTime: now.Add(time.Hour)},
122+
{EndTime: now.Add(-time.Hour), Type: wasp.SegmentType_Plain, From: 1, Duration: time.Hour},
119123
},
120124
},
121125
}
122126

123127
bd, err := NewBasicData("abc123", gen)
124-
require.NoError(t, err)
125-
126-
err = bd.FillStartEndTimes()
128+
require.Nil(t, bd)
127129
require.Error(t, err)
128130
assert.Contains(t, err.Error(), "start time is missing")
129131
})
@@ -140,9 +142,7 @@ func TestBenchSpy_BasicData_FillStartEndTimes(t *testing.T) {
140142
}
141143

142144
bd, err := NewBasicData("abc123", gen)
143-
require.NoError(t, err)
144-
145-
err = bd.FillStartEndTimes()
145+
require.Nil(t, bd)
146146
require.Error(t, err)
147147
assert.Contains(t, err.Error(), "end time is missing")
148148
})
@@ -156,9 +156,7 @@ func TestBenchSpy_BasicData_FillStartEndTimes(t *testing.T) {
156156
}
157157

158158
bd, err := NewBasicData("abc123", gen)
159-
require.NoError(t, err)
160-
161-
err = bd.FillStartEndTimes()
159+
require.Nil(t, bd)
162160
require.Error(t, err)
163161
assert.Contains(t, err.Error(), "schedule is empty for generator gen1")
164162
})

wasp/benchspy/generator_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ func TestBenchSpy_GeneratorQueryExecutor_Execute(t *testing.T) {
271271

272272
fakeGun := &fakeGun{
273273
maxSuccesses: 0,
274-
maxFailures: 6,
274+
maxFailures: 10,
275275
schedule: cfg.Schedule[0],
276276
}
277277

@@ -296,7 +296,7 @@ func TestBenchSpy_GeneratorQueryExecutor_Execute(t *testing.T) {
296296

297297
errorRate, exists := results[string(ErrorRate)]
298298
assert.True(t, exists)
299-
assert.Equal(t, []string{"1.0000"}, errorRate)
299+
assert.Equal(t, "1.0000", errorRate)
300300
})
301301

302302
t.Run("no responses", func(t *testing.T) {

wasp/benchspy/prometheus.go

Lines changed: 75 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,26 +41,14 @@ func NewPrometheusQueryExecutor(url string, startTime, endTime time.Time, querie
4141
}
4242

4343
func NewStandardPrometheusQueryExecutor(url string, startTime, endTime time.Time, nameRegexPattern string) (*PrometheusQueryExecutor, error) {
44-
c, err := client.NewPrometheusClient(url)
45-
if err != nil {
46-
return nil, errors.Wrapf(err, "failed to create Prometheus client")
47-
}
44+
p := &PrometheusQueryExecutor{}
4845

49-
pr := &PrometheusQueryExecutor{
50-
client: c,
51-
startTime: startTime,
52-
endTime: endTime,
53-
QueryResults: make(map[string]interface{}),
54-
}
55-
56-
standardQueries, queryErr := pr.generateStandardQueries(nameRegexPattern, startTime, endTime)
46+
standardQueries, queryErr := p.generateStandardQueries(nameRegexPattern, startTime, endTime)
5747
if queryErr != nil {
5848
return nil, errors.Wrapf(queryErr, "failed to generate standard queries for %s", nameRegexPattern)
5949
}
6050

61-
pr.Queries = standardQueries
62-
63-
return pr, nil
51+
return NewPrometheusQueryExecutor(url, startTime, endTime, standardQueries)
6452
}
6553

6654
func (r *PrometheusQueryExecutor) Execute(ctx context.Context) error {
@@ -189,26 +177,92 @@ func (r *PrometheusQueryExecutor) generateStandardQueries(nameRegexPattern strin
189177
return standardQueries, nil
190178
}
191179

180+
type TypedMetric struct {
181+
model.Value
182+
MetricType string `json:"metric_type"`
183+
}
184+
185+
func (g *PrometheusQueryExecutor) MarshalJSON() ([]byte, error) {
186+
// we need custom marshalling to only include some parts of the metrics
187+
type QueryExecutor struct {
188+
Kind string `json:"kind"`
189+
Queries map[string]string `json:"queries"`
190+
QueryResults map[string]TypedMetric `json:"query_results"`
191+
}
192+
193+
q := &QueryExecutor{
194+
Kind: g.KindName,
195+
Queries: g.Queries,
196+
QueryResults: func() map[string]TypedMetric {
197+
simplifiedMetrics := make(map[string]TypedMetric)
198+
for name, value := range g.MustResultsAsValue() {
199+
simplifiedMetrics[name] = TypedMetric{
200+
MetricType: value.Type().String(),
201+
Value: value,
202+
}
203+
}
204+
return simplifiedMetrics
205+
}(),
206+
}
207+
208+
return json.Marshal(q)
209+
}
210+
192211
func (r *PrometheusQueryExecutor) UnmarshalJSON(data []byte) error {
193212
// helper struct with QueryResults map[string]interface{}
194213
type Alias PrometheusQueryExecutor
195214
var raw struct {
196215
Alias
197-
QueryResults map[string]interface{} `json:"query_results"`
216+
QueryResults map[string]json.RawMessage `json:"query_results"`
198217
}
199218

200219
// unmarshal into the helper struct to populate other fields automatically
201220
if err := json.Unmarshal(data, &raw); err != nil {
202221
return err
203222
}
204223

205-
// convert map[string]interface{} to map[string]actualType
206-
convertedTypes, conversionErr := convertQueryResults(raw.QueryResults)
207-
if conversionErr != nil {
208-
return conversionErr
224+
var convertedQueryResults = make(map[string]interface{})
225+
226+
for name, rawResult := range raw.QueryResults {
227+
var rawTypedMetric struct {
228+
MetricType string `json:"metric_type"`
229+
Value json.RawMessage `json:"Value"`
230+
}
231+
if err := json.Unmarshal(rawResult, &rawTypedMetric); err != nil {
232+
return errors.Wrapf(err, "failed to unmarshal query result for %s", name)
233+
}
234+
235+
switch rawTypedMetric.MetricType {
236+
case "scalar":
237+
var scalar model.Scalar
238+
if err := json.Unmarshal(rawTypedMetric.Value, &scalar); err != nil {
239+
return errors.Wrapf(err, "failed to unmarshal scalar value for %s", name)
240+
}
241+
convertedQueryResults[name] = scalar
242+
case "vector":
243+
var vector model.Vector
244+
if err := json.Unmarshal(rawTypedMetric.Value, &vector); err != nil {
245+
return errors.Wrapf(err, "failed to unmarshal vector value for %s", name)
246+
}
247+
convertedQueryResults[name] = vector
248+
case "matrix":
249+
var matrix model.Matrix
250+
if err := json.Unmarshal(rawTypedMetric.Value, &matrix); err != nil {
251+
return errors.Wrapf(err, "failed to unmarshal matrix value for %s", name)
252+
}
253+
convertedQueryResults[name] = matrix
254+
case "string":
255+
var str model.String
256+
if err := json.Unmarshal(rawTypedMetric.Value, &str); err != nil {
257+
return errors.Wrapf(err, "failed to unmarshal string value for %s", name)
258+
}
259+
convertedQueryResults[name] = str
260+
default:
261+
return fmt.Errorf("unknown metric type %s", rawTypedMetric.MetricType)
262+
}
209263
}
210264

211265
*r = PrometheusQueryExecutor(raw.Alias)
212-
r.QueryResults = convertedTypes
266+
r.QueryResults = convertedQueryResults
213267
return nil
214268
}

0 commit comments

Comments
 (0)