2014-02-26 14:47:25 -08:00
|
|
|
package metric
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
clientmodel "github.com/prometheus/client_golang/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
const numTestValues = 5000
|
|
|
|
|
|
|
|
func TestValuesMarshalAndUnmarshal(t *testing.T) {
|
|
|
|
values := randomValues(numTestValues)
|
|
|
|
|
|
|
|
marshalled := values.marshal()
|
|
|
|
unmarshalled := unmarshalValues(marshalled)
|
|
|
|
|
|
|
|
for i, expected := range values {
|
|
|
|
actual := unmarshalled[i]
|
2014-03-10 09:55:17 -07:00
|
|
|
if !actual.Equal(&expected) {
|
2014-02-26 14:47:25 -08:00
|
|
|
t.Fatalf("%d. got: %v, expected: %v", i, actual, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func randomValues(numSamples int) Values {
|
|
|
|
v := make(Values, 0, numSamples)
|
|
|
|
for i := 0; i < numSamples; i++ {
|
2014-03-10 09:55:17 -07:00
|
|
|
v = append(v, SamplePair{
|
2014-02-26 14:47:25 -08:00
|
|
|
Timestamp: clientmodel.Timestamp(rand.Int63()),
|
|
|
|
Value: clientmodel.SampleValue(rand.NormFloat64()),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkMarshal(b *testing.B) {
|
|
|
|
v := randomValues(numTestValues)
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
v.marshal()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkUnmarshal(b *testing.B) {
|
|
|
|
v := randomValues(numTestValues)
|
|
|
|
marshalled := v.marshal()
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
unmarshalValues(marshalled)
|
|
|
|
}
|
|
|
|
}
|