github.com/zuoyebang/bitalostable@v1.0.1-0.20240229032404-e3b99a834294/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 TestGaugeSampleMetric(t *testing.T) { 35 g1 := GaugeSampleMetric{} 36 g1.AddSample(10) 37 g1.AddSample(20) 38 g2 := GaugeSampleMetric{} 39 g2.Merge(g1) 40 g2.AddSample(60) 41 require.EqualValues(t, 30, g2.Mean()) 42 require.EqualValues(t, 3, g2.count) 43 require.EqualValues(t, 15, g1.Mean()) 44 require.EqualValues(t, 2, g1.count) 45 }