2023-07-20 10:10:55 +02:00
|
|
|
package loki
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logstorage"
|
|
|
|
)
|
|
|
|
|
2023-07-21 01:21:47 +02:00
|
|
|
func BenchmarkParseJSONRequest(b *testing.B) {
|
2023-07-20 10:10:55 +02:00
|
|
|
for _, streams := range []int{5, 10} {
|
|
|
|
for _, rows := range []int{100, 1000} {
|
|
|
|
for _, labels := range []int{10, 50} {
|
|
|
|
b.Run(fmt.Sprintf("streams_%d/rows_%d/labels_%d", streams, rows, labels), func(b *testing.B) {
|
2023-07-21 01:21:47 +02:00
|
|
|
benchmarkParseJSONRequest(b, streams, rows, labels)
|
2023-07-20 10:10:55 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-21 01:21:47 +02:00
|
|
|
func benchmarkParseJSONRequest(b *testing.B, streams, rows, labels int) {
|
2023-07-20 10:10:55 +02:00
|
|
|
b.ReportAllocs()
|
2023-07-21 01:21:47 +02:00
|
|
|
b.SetBytes(int64(streams * rows))
|
2023-07-20 10:10:55 +02:00
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
2023-07-21 01:21:47 +02:00
|
|
|
data := getJSONBody(streams, rows, labels)
|
2023-07-20 10:10:55 +02:00
|
|
|
for pb.Next() {
|
2023-09-29 11:55:38 +02:00
|
|
|
_, err := parseJSONRequest(data, func(timestamp int64, fields []logstorage.Field) error { return nil })
|
2023-07-20 10:10:55 +02:00
|
|
|
if err != nil {
|
2023-07-21 01:21:47 +02:00
|
|
|
panic(fmt.Errorf("unexpected error: %s", err))
|
2023-07-20 10:10:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-07-21 01:21:47 +02:00
|
|
|
func getJSONBody(streams, rows, labels int) []byte {
|
|
|
|
body := append([]byte{}, `{"streams":[`...)
|
2023-07-20 10:10:55 +02:00
|
|
|
now := time.Now().UnixNano()
|
|
|
|
valuePrefix := fmt.Sprintf(`["%d","value_`, now)
|
|
|
|
|
|
|
|
for i := 0; i < streams; i++ {
|
2023-07-21 01:21:47 +02:00
|
|
|
body = append(body, `{"stream":{`...)
|
2023-07-20 10:10:55 +02:00
|
|
|
|
|
|
|
for j := 0; j < labels; j++ {
|
2023-07-21 01:21:47 +02:00
|
|
|
body = append(body, `"label_`...)
|
|
|
|
body = strconv.AppendInt(body, int64(j), 10)
|
|
|
|
body = append(body, `":"value_`...)
|
|
|
|
body = strconv.AppendInt(body, int64(j), 10)
|
|
|
|
body = append(body, '"')
|
2023-07-20 10:10:55 +02:00
|
|
|
if j < labels-1 {
|
2023-07-21 01:21:47 +02:00
|
|
|
body = append(body, ',')
|
2023-07-20 10:10:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2023-07-21 01:21:47 +02:00
|
|
|
body = append(body, `}, "values":[`...)
|
2023-07-20 10:10:55 +02:00
|
|
|
|
|
|
|
for j := 0; j < rows; j++ {
|
2023-07-21 01:21:47 +02:00
|
|
|
body = append(body, valuePrefix...)
|
|
|
|
body = strconv.AppendInt(body, int64(j), 10)
|
|
|
|
body = append(body, `"]`...)
|
2023-07-20 10:10:55 +02:00
|
|
|
if j < rows-1 {
|
2023-07-21 01:21:47 +02:00
|
|
|
body = append(body, ',')
|
2023-07-20 10:10:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-21 01:21:47 +02:00
|
|
|
body = append(body, `]}`...)
|
2023-07-20 10:10:55 +02:00
|
|
|
if i < streams-1 {
|
2023-07-21 01:21:47 +02:00
|
|
|
body = append(body, ',')
|
2023-07-20 10:10:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-07-21 01:21:47 +02:00
|
|
|
body = append(body, `]}`...)
|
2023-07-20 10:10:55 +02:00
|
|
|
|
|
|
|
return body
|
|
|
|
}
|