github.com/cloudwego/hertz@v0.9.3/internal/stats/stats_util_test.go (about) 1 /* 2 * Copyright 2022 CloudWeGo Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package stats 18 19 import ( 20 "testing" 21 "time" 22 23 "github.com/cloudwego/hertz/pkg/common/test/assert" 24 "github.com/cloudwego/hertz/pkg/common/tracer/stats" 25 "github.com/cloudwego/hertz/pkg/common/tracer/traceinfo" 26 ) 27 28 func TestUtil(t *testing.T) { 29 assert.Assert(t, CalcEventCostUs(nil, nil) == 0) 30 31 ti := traceinfo.NewTraceInfo() 32 33 // nil context 34 Record(ti, stats.HTTPStart, nil) 35 Record(ti, stats.HTTPFinish, nil) 36 37 st := ti.Stats() 38 assert.Assert(t, st != nil) 39 40 s, e := st.GetEvent(stats.HTTPStart), st.GetEvent(stats.HTTPFinish) 41 assert.Assert(t, s == nil) 42 assert.Assert(t, e == nil) 43 44 // stats disabled 45 Record(ti, stats.HTTPStart, nil) 46 time.Sleep(time.Millisecond) 47 Record(ti, stats.HTTPFinish, nil) 48 49 st = ti.Stats() 50 assert.Assert(t, st != nil) 51 52 s, e = st.GetEvent(stats.HTTPStart), st.GetEvent(stats.HTTPFinish) 53 assert.Assert(t, s == nil) 54 assert.Assert(t, e == nil) 55 56 // stats enabled 57 st = ti.Stats() 58 st.(interface{ SetLevel(stats.Level) }).SetLevel(stats.LevelBase) 59 60 Record(ti, stats.HTTPStart, nil) 61 time.Sleep(time.Millisecond) 62 Record(ti, stats.HTTPFinish, nil) 63 64 s, e = st.GetEvent(stats.HTTPStart), st.GetEvent(stats.HTTPFinish) 65 assert.Assert(t, s != nil, s) 66 assert.Assert(t, e != nil, e) 67 assert.Assert(t, CalcEventCostUs(s, e) > 0) 68 }