Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit dac5c9a

Browse files
author
Ivan Mirić
authored
Add metrics emission test (#167)
* Add metrics emission test This adds a missing test for #40, and all other metrics emitted for page.goto(). It uses a slightly modified version of the assertRequestMetricsEmittedSingle helper from k6[1]. [1]: https://github.com/grafana/k6/blob/804203bcf574da57cd05d67b39f03d05c54523ab/js/modules/k6/http/request_test.go#L109 * Replace sleep in test with Page.WaitForLoadState() Resolves #167 (comment) * Make NetworkManager tests parallel
1 parent 758d14b commit dac5c9a

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed

tests/metrics_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
*
3+
* xk6-browser - a browser automation extension for k6
4+
* Copyright (C) 2021 Load Impact
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Affero General Public License as
8+
* published by the Free Software Foundation, either version 3 of the
9+
* License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Affero General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Affero General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*
19+
*/
20+
21+
package tests
22+
23+
import (
24+
"testing"
25+
26+
"github.com/stretchr/testify/assert"
27+
k6stats "go.k6.io/k6/stats"
28+
)
29+
30+
func assertMetricsEmitted(t *testing.T, samples []k6stats.SampleContainer,
31+
expMetricTags map[string]map[string]string, callback func(sample k6stats.Sample)) {
32+
t.Helper()
33+
34+
metricMap := make(map[string]bool, len(expMetricTags))
35+
for m := range expMetricTags {
36+
metricMap[m] = false
37+
}
38+
39+
for _, container := range samples {
40+
for _, sample := range container.GetSamples() {
41+
tags := sample.Tags.CloneTags()
42+
v, ok := metricMap[sample.Metric.Name]
43+
assert.True(t, ok, "unexpected metric %s", sample.Metric.Name)
44+
// Already seen this metric, skip it.
45+
// TODO: Fail on repeated metrics?
46+
if v {
47+
continue
48+
}
49+
metricMap[sample.Metric.Name] = true
50+
expTags := expMetricTags[sample.Metric.Name]
51+
assert.EqualValues(t, expTags, tags,
52+
"tags for metric %s don't match", sample.Metric.Name)
53+
if callback != nil {
54+
callback(sample)
55+
}
56+
}
57+
}
58+
59+
for k, v := range metricMap {
60+
assert.True(t, v, "didn't emit %s", k)
61+
}
62+
}

tests/network_manager_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,15 @@ package tests
2323
import (
2424
"testing"
2525

26+
"github.com/grafana/xk6-browser/common"
2627
"github.com/stretchr/testify/assert"
28+
"github.com/stretchr/testify/require"
29+
k6metrics "go.k6.io/k6/lib/metrics"
30+
k6stats "go.k6.io/k6/stats"
2731
)
2832

2933
func TestDataURLSkipRequest(t *testing.T) {
34+
t.Parallel()
3035
tb := newTestBrowser(t)
3136
p := tb.NewPage(nil)
3237

@@ -36,3 +41,63 @@ func TestDataURLSkipRequest(t *testing.T) {
3641

3742
assert.True(t, lc.contains("skipped request handling of data URL"))
3843
}
44+
45+
func TestMetricsEmission(t *testing.T) {
46+
t.Parallel()
47+
tb := newTestBrowser(t, withHTTPServer())
48+
49+
url := tb.URL("/get")
50+
browserTags := map[string]string{
51+
"group": "",
52+
"url": "about:blank",
53+
}
54+
httpTags := map[string]string{
55+
"method": "GET",
56+
"url": url,
57+
"status": "200",
58+
"group": "",
59+
"proto": "http/1.1",
60+
"from_cache": "false",
61+
"from_prefetch_cache": "false",
62+
"from_service_worker": "false",
63+
}
64+
expMetricTags := map[string]map[string]string{
65+
common.BrowserDOMContentLoaded.Name: browserTags,
66+
common.BrowserLoaded.Name: browserTags,
67+
k6metrics.DataSentName: map[string]string{
68+
"group": "",
69+
"method": "GET",
70+
"url": url,
71+
},
72+
k6metrics.HTTPReqsName: httpTags,
73+
k6metrics.HTTPReqDurationName: httpTags,
74+
k6metrics.DataReceivedName: httpTags,
75+
k6metrics.HTTPReqConnectingName: httpTags,
76+
k6metrics.HTTPReqTLSHandshakingName: httpTags,
77+
k6metrics.HTTPReqSendingName: httpTags,
78+
k6metrics.HTTPReqReceivingName: httpTags,
79+
}
80+
81+
p := tb.NewPage(nil)
82+
resp := p.Goto(url, nil)
83+
require.NotNil(t, resp)
84+
85+
// Wait for all metrics to be emitted
86+
p.WaitForLoadState("networkidle", nil)
87+
88+
bufSamples := k6stats.GetBufferedSamples(tb.samples)
89+
90+
var reqsCount int
91+
cb := func(sample k6stats.Sample) {
92+
switch sample.Metric.Name {
93+
case k6metrics.HTTPReqsName:
94+
reqsCount += int(sample.Value)
95+
case k6metrics.DataSentName, k6metrics.DataReceivedName:
96+
assert.Greaterf(t, int(sample.Value), 0,
97+
"metric %s", sample.Metric.Name)
98+
}
99+
}
100+
101+
assertMetricsEmitted(t, bufSamples, expMetricTags, cb)
102+
assert.Equal(t, 1, reqsCount)
103+
}

0 commit comments

Comments
 (0)