prometheus/util/stats/stats_test.go
Julien Pivotto 9a2e93228e
Switch to grafana/regexp everywhere (#10268)
Let's have a consistent library for regexp.

Signed-off-by: Julien Pivotto <roidelapluie@inuits.eu>
2022-02-13 00:58:27 +01:00

93 lines
3.3 KiB
Go

// Copyright 2017 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package stats
import (
"encoding/json"
"testing"
"time"
"github.com/grafana/regexp"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/require"
"github.com/prometheus/prometheus/util/testutil"
)
func TestTimerGroupNewTimer(t *testing.T) {
tg := NewTimerGroup()
timer := tg.GetTimer(ExecTotalTime)
duration := timer.Duration()
require.Equal(t, 0.0, duration, "Expected duration equal 0")
minimum := 2 * time.Millisecond
timer.Start()
time.Sleep(minimum)
timer.Stop()
duration = timer.Duration()
require.Greater(t, duration, 0.0, "Expected duration greater than 0")
elapsed := timer.ElapsedTime()
require.GreaterOrEqual(t, elapsed, minimum,
"Expected elapsed time to be greater than time slept.")
}
func TestQueryStatsWithTimers(t *testing.T) {
qt := NewQueryTimers()
timer := qt.GetTimer(ExecTotalTime)
timer.Start()
time.Sleep(2 * time.Millisecond)
timer.Stop()
qs := NewQueryStats(qt)
actual, err := json.Marshal(qs)
require.NoError(t, err, "unexpected error during serialization")
// Timing value is one of multiple fields, unit is seconds (float).
match, err := regexp.MatchString(`[,{]"execTotalTime":\d+\.\d+[,}]`, string(actual))
require.NoError(t, err, "unexpected error while matching string")
require.True(t, match, "Expected timings with one non-zero entry.")
}
func TestQueryStatsWithSpanTimers(t *testing.T) {
qt := NewQueryTimers()
ctx := &testutil.MockContext{DoneCh: make(chan struct{})}
qst, _ := qt.GetSpanTimer(ctx, ExecQueueTime, prometheus.NewSummary(prometheus.SummaryOpts{}))
time.Sleep(5 * time.Millisecond)
qst.Finish()
qs := NewQueryStats(qt)
actual, err := json.Marshal(qs)
require.NoError(t, err, "unexpected error during serialization")
// Timing value is one of multiple fields, unit is seconds (float).
match, err := regexp.MatchString(`[,{]"execQueueTime":\d+\.\d+[,}]`, string(actual))
require.NoError(t, err, "unexpected error while matching string")
require.True(t, match, "Expected timings with one non-zero entry.")
}
func TestTimerGroup(t *testing.T) {
tg := NewTimerGroup()
require.Equal(t, "Exec total time: 0s", tg.GetTimer(ExecTotalTime).String())
require.Equal(t, "Exec queue wait time: 0s", tg.GetTimer(ExecQueueTime).String())
require.Equal(t, "Inner eval time: 0s", tg.GetTimer(InnerEvalTime).String())
require.Equal(t, "Query preparation time: 0s", tg.GetTimer(QueryPreparationTime).String())
require.Equal(t, "Result sorting time: 0s", tg.GetTimer(ResultSortTime).String())
require.Equal(t, "Eval total time: 0s", tg.GetTimer(EvalTotalTime).String())
actual := tg.String()
expected := "Exec total time: 0s\nExec queue wait time: 0s\nInner eval time: 0s\nQuery preparation time: 0s\nResult sorting time: 0s\nEval total time: 0s\n"
require.Equal(t, expected, actual)
}