github.com/cockroachdb/pebble@v1.1.1-0.20240513155919-3622ade60459/internal/base/metrics_test.go (about) 1 // Copyright 2022 The LevelDB-Go and Pebble Authors. All rights reserved. Use 2 // of this source code is governed by a BSD-style license that can be found in 3 // the LICENSE file. 4 5 package base 6 7 import ( 8 "testing" 9 "time" 10 11 "github.com/stretchr/testify/require" 12 ) 13 14 func TestThroughputMetric(t *testing.T) { 15 m1 := ThroughputMetric{ 16 Bytes: 10, 17 WorkDuration: time.Millisecond, 18 IdleDuration: 9 * time.Millisecond, 19 } 20 var m2 ThroughputMetric 21 m2.Merge(m1) 22 require.Equal(t, m1, m2) 23 m2.Merge(m1) 24 doubleM1 := ThroughputMetric{ 25 Bytes: 2 * m1.Bytes, 26 WorkDuration: 2 * m1.WorkDuration, 27 IdleDuration: 2 * m1.IdleDuration, 28 } 29 require.Equal(t, doubleM1, m2) 30 require.EqualValues(t, 10*100, m1.Rate()) 31 require.EqualValues(t, 10*1000, m1.PeakRate()) 32 } 33 34 func TestThroughputMetric_Subtract(t *testing.T) { 35 m1 := ThroughputMetric{ 36 Bytes: 10, 37 WorkDuration: time.Millisecond, 38 IdleDuration: 9 * time.Millisecond, 39 } 40 m2 := ThroughputMetric{ 41 Bytes: 100, 42 WorkDuration: time.Millisecond, 43 IdleDuration: 90 * time.Millisecond, 44 } 45 46 m2.Subtract(m1) 47 require.Equal(t, int64(90), m2.Bytes) 48 require.Equal(t, 0*time.Millisecond, m2.WorkDuration) 49 require.Equal(t, 81*time.Millisecond, m2.IdleDuration) 50 } 51 52 func TestGaugeSampleMetric(t *testing.T) { 53 g1 := GaugeSampleMetric{} 54 g1.AddSample(10) 55 g1.AddSample(20) 56 g2 := GaugeSampleMetric{} 57 g2.Merge(g1) 58 g2.AddSample(60) 59 require.EqualValues(t, 30, g2.Mean()) 60 require.EqualValues(t, 3, g2.count) 61 require.EqualValues(t, 15, g1.Mean()) 62 require.EqualValues(t, 2, g1.count) 63 } 64 65 func TestGaugeSampleMetricSubtract(t *testing.T) { 66 g1 := GaugeSampleMetric{} 67 g2 := GaugeSampleMetric{} 68 g1.AddSample(10) 69 g1.AddSample(20) 70 g1.AddSample(0) 71 72 g2.AddSample(10) 73 74 g1.Subtract(g2) 75 76 require.Equal(t, int64(20), g1.sampleSum) 77 require.Equal(t, int64(2), g1.count) 78 79 }