github.com/TeaOSLab/EdgeNode@v1.3.8/internal/stats/user_agent_parser_test.go (about) 1 // Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. 2 3 package stats_test 4 5 import ( 6 "github.com/TeaOSLab/EdgeNode/internal/stats" 7 "github.com/TeaOSLab/EdgeNode/internal/utils/testutils" 8 "github.com/iwind/TeaGo/assert" 9 "github.com/iwind/TeaGo/rands" 10 "github.com/iwind/TeaGo/types" 11 "runtime" 12 "runtime/debug" 13 "testing" 14 "time" 15 ) 16 17 func TestUserAgentParser_Parse(t *testing.T) { 18 var parser = stats.NewUserAgentParser() 19 for i := 0; i < 4; i++ { 20 t.Log(parser.Parse("Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36 Test/1")) 21 t.Log(parser.Parse("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36")) 22 } 23 } 24 25 func TestUserAgentParser_Parse_Unknown(t *testing.T) { 26 var parser = stats.NewUserAgentParser() 27 t.Log(parser.Parse("Mozilla/5.0 (Wind 10.0; WOW64; rv:49.0) Apple/537.36 (KHTML, like Gecko) Chr/88.0.4324.96 Sa/537.36 Test/1")) 28 t.Log(parser.Parse("")) 29 } 30 31 func TestUserAgentParser_Memory(t *testing.T) { 32 var stat1 = &runtime.MemStats{} 33 runtime.ReadMemStats(stat1) 34 35 var parser = stats.NewUserAgentParser() 36 37 for i := 0; i < 1_000_000; i++ { 38 parser.Parse("Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36 Test/" + types.String(rands.Int(0, 1_000_000))) 39 } 40 41 runtime.GC() 42 debug.FreeOSMemory() 43 44 var stat2 = &runtime.MemStats{} 45 runtime.ReadMemStats(stat2) 46 47 t.Log("max cache items:", parser.MaxCacheItems()) 48 t.Log("cache:", parser.Len(), "usage:", (stat2.HeapInuse-stat1.HeapInuse)>>20, "MB") 49 } 50 51 func TestNewUserAgentParser_GC(t *testing.T) { 52 if !testutils.IsSingleTesting() { 53 return 54 } 55 56 var parser = stats.NewUserAgentParser() 57 58 for i := 0; i < 1_000_000; i++ { 59 parser.Parse("Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36 Test/" + types.String(rands.Int(0, 100_000))) 60 } 61 62 time.Sleep(60 * time.Second) // wait to gc 63 t.Log(parser.Len(), "cache items") 64 } 65 66 func TestNewUserAgentParser_Mobile(t *testing.T) { 67 var a = assert.NewAssertion(t) 68 var parser = stats.NewUserAgentParser() 69 for _, userAgent := range []string{ 70 "Mozilla/5.0 (iPhone; CPU iPhone OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148", 71 "Mozilla/5.0 (Linux; U; Android 2.2.1; en-us; Nexus One Build/FRG83) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1", 72 } { 73 a.IsTrue(parser.Parse(userAgent).IsMobile) 74 } 75 } 76 77 func BenchmarkUserAgentParser_Parse_Many_LimitCPU(b *testing.B) { 78 runtime.GOMAXPROCS(4) 79 80 var parser = stats.NewUserAgentParser() 81 b.RunParallel(func(pb *testing.PB) { 82 for pb.Next() { 83 parser.Parse("Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36 Test/" + types.String(rands.Int(0, 1_000_000))) 84 } 85 }) 86 b.Log(parser.Len()) 87 } 88 89 func BenchmarkUserAgentParser_Parse_Many(b *testing.B) { 90 var parser = stats.NewUserAgentParser() 91 b.RunParallel(func(pb *testing.PB) { 92 for pb.Next() { 93 parser.Parse("Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36 Test/" + types.String(rands.Int(0, 1_000_000))) 94 } 95 }) 96 b.Log(parser.Len()) 97 } 98 99 func BenchmarkUserAgentParser_Parse_Few_LimitCPU(b *testing.B) { 100 runtime.GOMAXPROCS(4) 101 102 var parser = stats.NewUserAgentParser() 103 b.RunParallel(func(pb *testing.PB) { 104 for pb.Next() { 105 parser.Parse("Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36 Test/" + types.String(rands.Int(0, 100_000))) 106 } 107 }) 108 b.Log(parser.Len()) 109 } 110 111 func BenchmarkUserAgentParser_Parse_Few(b *testing.B) { 112 var parser = stats.NewUserAgentParser() 113 b.RunParallel(func(pb *testing.PB) { 114 for pb.Next() { 115 parser.Parse("Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36 Test/" + types.String(rands.Int(0, 100_000))) 116 } 117 }) 118 b.Log(parser.Len()) 119 }