2022-05-12 19:37:38 +00:00
|
|
|
package stats
|
|
|
|
|
|
|
|
import (
|
2024-01-23 15:21:51 +00:00
|
|
|
"strings"
|
2022-05-12 19:37:38 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"go.k6.io/k6/js/modules"
|
|
|
|
"go.k6.io/k6/metrics"
|
|
|
|
)
|
|
|
|
|
2024-01-23 15:21:51 +00:00
|
|
|
// RootModule is the global module object type. It is instantiated once per test
|
|
|
|
// run and will be used to create k6/x/frostfs/stats module instances for each VU.
|
|
|
|
type RootModule struct {
|
|
|
|
Instance string
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
tagSet *metrics.TagSet
|
|
|
|
|
|
|
|
Registry *metrics.Registry
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Registry = metrics.NewRegistry()
|
|
|
|
tagSet = Registry.RootTagSet()
|
|
|
|
modules.Register("k6/x/frostfs/stats", &RootModule{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTags sets additional tags to custom metrics.
|
|
|
|
// Format: "key1:value1;key2:value2".
|
|
|
|
// Panics if input has invalid format.
|
|
|
|
func (m *RootModule) SetTags(labels string) {
|
|
|
|
kv := make(map[string]string)
|
|
|
|
pairs := strings.Split(labels, ";")
|
|
|
|
for _, pair := range pairs {
|
|
|
|
items := strings.Split(pair, ":")
|
|
|
|
if len(items) != 2 {
|
|
|
|
panic("invalid labels format")
|
|
|
|
}
|
|
|
|
kv[strings.TrimSpace(items[0])] = strings.TrimSpace(items[1])
|
|
|
|
}
|
|
|
|
for k, v := range kv {
|
|
|
|
tagSet = tagSet.With(k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-12 19:37:38 +00:00
|
|
|
func Report(vu modules.VU, metric *metrics.Metric, value float64) {
|
|
|
|
metrics.PushIfNotDone(vu.Context(), vu.State().Samples, metrics.Sample{
|
2023-05-23 08:16:59 +00:00
|
|
|
TimeSeries: metrics.TimeSeries{
|
|
|
|
Metric: metric,
|
2024-01-23 15:21:51 +00:00
|
|
|
Tags: tagSet,
|
2023-05-23 08:16:59 +00:00
|
|
|
},
|
|
|
|
Time: time.Now(),
|
|
|
|
Value: value,
|
2022-05-12 19:37:38 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReportDataReceived(vu modules.VU, value float64) {
|
|
|
|
vu.State().BuiltinMetrics.DataReceived.Sink.Add(
|
|
|
|
metrics.Sample{
|
2023-05-23 08:16:59 +00:00
|
|
|
TimeSeries: metrics.TimeSeries{
|
|
|
|
Metric: &metrics.Metric{},
|
2024-01-23 15:21:51 +00:00
|
|
|
Tags: tagSet,
|
2023-05-23 08:16:59 +00:00
|
|
|
},
|
|
|
|
Value: value,
|
2024-01-23 15:21:51 +00:00
|
|
|
Time: time.Now(),
|
|
|
|
},
|
2022-05-12 19:37:38 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReportDataSent(vu modules.VU, value float64) {
|
|
|
|
state := vu.State()
|
|
|
|
state.BuiltinMetrics.DataSent.Sink.Add(
|
|
|
|
metrics.Sample{
|
2023-05-23 08:16:59 +00:00
|
|
|
TimeSeries: metrics.TimeSeries{
|
|
|
|
Metric: &metrics.Metric{},
|
2024-01-23 15:21:51 +00:00
|
|
|
Tags: tagSet,
|
2023-05-23 08:16:59 +00:00
|
|
|
},
|
|
|
|
Value: value,
|
2024-01-23 15:21:51 +00:00
|
|
|
Time: time.Now(),
|
|
|
|
},
|
2022-05-12 19:37:38 +00:00
|
|
|
)
|
|
|
|
}
|