github.com/artisanhe/tools@v1.0.1-0.20210607022958-19a8fef2eb04/catgo/cat-go/test/test.go (about) 1 package main 2 3 import ( 4 "errors" 5 "math/rand" 6 "sync" 7 "time" 8 9 "github.com/artisanhe/tools/catgo/cat-go/cat" 10 ) 11 12 const TestType = "foo" 13 14 var wg = sync.WaitGroup{} 15 16 func init() { 17 cat.DebugOn() 18 cat.Init("gocat.v2") 19 } 20 21 // send transaction 22 func case1() { 23 t := cat.NewTransaction(TestType, "test") 24 defer t.Complete() 25 26 if rand.Int31n(100) == 0 { 27 t.SetStatus(cat.FAIL) 28 } 29 30 t.AddData("foo", "bar") 31 32 t.NewEvent(TestType, "event-1") 33 t.Complete() 34 35 if rand.Int31n(100) == 0 { 36 t.LogEvent(TestType, "event-2", cat.FAIL) 37 } else { 38 t.LogEvent(TestType, "event-2") 39 } 40 t.LogEvent(TestType, "event-3", cat.SUCCESS, "k=v") 41 42 t.SetDurationStart(time.Now().Add(-5 * time.Second)) 43 t.SetTime(time.Now().Add(-5 * time.Second)) 44 t.SetDuration(time.Millisecond * 500) 45 } 46 47 // send completed transaction with duration 48 func case2() { 49 cat.NewCompletedTransactionWithDuration(TestType, "completed", time.Second*24) 50 cat.NewCompletedTransactionWithDuration(TestType, "completed-over-60s", time.Second*65) 51 } 52 53 // send event 54 func case3() { 55 // way 1 56 e := cat.NewEvent(TestType, "event-4") 57 e.Complete() 58 // way 2 59 60 if rand.Int31n(100) == 0 { 61 cat.LogEvent(TestType, "event-5", cat.FAIL) 62 } else { 63 cat.LogEvent(TestType, "event-5") 64 } 65 cat.LogEvent(TestType, "event-6", cat.SUCCESS, "foobar") 66 } 67 68 // send error with backtrace 69 func case4() { 70 if rand.Int31n(100) == 0 { 71 err := errors.New("error") 72 cat.LogError(err) 73 } 74 } 75 76 // send metric 77 func case5() { 78 cat.LogMetricForCount("metric-1") 79 cat.LogMetricForCount("metric-2", 3) 80 cat.LogMetricForDuration("metric-3", 150*time.Millisecond) 81 cat.NewMetricHelper("metric-4").Count(7) 82 cat.NewMetricHelper("metric-5").Duration(time.Second) 83 } 84 85 func run(f func()) { 86 defer wg.Done() 87 88 for i := 0; i < 100000000; i++ { 89 f() 90 time.Sleep(time.Microsecond * 100) 91 } 92 } 93 94 func start(f func()) { 95 wg.Add(1) 96 go run(f) 97 } 98 99 func main() { 100 start(case1) 101 start(case2) 102 start(case3) 103 start(case4) 104 start(case5) 105 106 wg.Wait() 107 108 cat.Shutdown() 109 }