11package metrics
22
33import (
4+ "net"
45 "testing"
56 "time"
7+
8+ geoip2 "github.com/oschwald/geoip2-golang"
9+ "github.com/prometheus/client_golang/prometheus"
610)
711
812func TestMethodsDontPanic (t * testing.T ) {
9- ssMetrics := NewShadowsocksMetrics (nil )
13+ ssMetrics := NewPrometheusShadowsocksMetrics (nil , prometheus . NewPedanticRegistry () )
1014 proxyMetrics := ProxyMetrics {
1115 ClientProxy : 1 ,
1216 ProxyTarget : 2 ,
@@ -22,3 +26,94 @@ func TestMethodsDontPanic(t *testing.T) {
2226 ssMetrics .AddUDPNatEntry ()
2327 ssMetrics .RemoveUDPNatEntry ()
2428}
29+
30+ func BenchmarkGetLocation (b * testing.B ) {
31+ var ipCountryDB * geoip2.Reader
32+ // The test data is in a git submodule that must be initialized before running the test.
33+ dbPath := "../third_party/maxmind/test-data/GeoIP2-Country-Test.mmdb"
34+ ipCountryDB , err := geoip2 .Open (dbPath )
35+ if err != nil {
36+ b .Fatalf ("Could not open geoip database at %v: %v" , dbPath , err )
37+ }
38+ defer ipCountryDB .Close ()
39+
40+ ssMetrics := NewPrometheusShadowsocksMetrics (ipCountryDB , prometheus .NewRegistry ())
41+ testIP := net .ParseIP ("217.65.48.1" )
42+ testAddr := & net.TCPAddr {IP : testIP , Port : 12345 }
43+ b .ResetTimer ()
44+ // Repeatedly check the country for the same address. This is realistic, because
45+ // servers call this method for each new connection, but typically many connections
46+ // come from a single user in succession.
47+ for i := 0 ; i < b .N ; i ++ {
48+ ssMetrics .GetLocation (testAddr )
49+ }
50+ }
51+
52+ func BenchmarkOpenTCP (b * testing.B ) {
53+ ssMetrics := NewPrometheusShadowsocksMetrics (nil , prometheus .NewRegistry ())
54+ b .ResetTimer ()
55+ for i := 0 ; i < b .N ; i ++ {
56+ ssMetrics .AddOpenTCPConnection ("ZZ" )
57+ }
58+ }
59+
60+ func BenchmarkCloseTCP (b * testing.B ) {
61+ ssMetrics := NewPrometheusShadowsocksMetrics (nil , prometheus .NewRegistry ())
62+ clientLocation := "ZZ"
63+ accessKey := "key 1"
64+ status := "OK"
65+ data := ProxyMetrics {}
66+ timeToCipher := time .Microsecond
67+ duration := time .Minute
68+ b .ResetTimer ()
69+ for i := 0 ; i < b .N ; i ++ {
70+ ssMetrics .AddClosedTCPConnection (clientLocation , accessKey , status , data , timeToCipher , duration )
71+ }
72+ }
73+
74+ func BenchmarkProbe (b * testing.B ) {
75+ ssMetrics := NewPrometheusShadowsocksMetrics (nil , prometheus .NewRegistry ())
76+ clientLocation := "ZZ"
77+ status := "ERR_REPLAY"
78+ drainResult := "other"
79+ port := 12345
80+ data := ProxyMetrics {}
81+ b .ResetTimer ()
82+ for i := 0 ; i < b .N ; i ++ {
83+ ssMetrics .AddTCPProbe (clientLocation , status , drainResult , port , data )
84+ }
85+ }
86+
87+ func BenchmarkClientUDP (b * testing.B ) {
88+ ssMetrics := NewPrometheusShadowsocksMetrics (nil , prometheus .NewRegistry ())
89+ clientLocation := "ZZ"
90+ accessKey := "key 1"
91+ status := "OK"
92+ size := 1000
93+ timeToCipher := time .Microsecond
94+ b .ResetTimer ()
95+ for i := 0 ; i < b .N ; i ++ {
96+ ssMetrics .AddUDPPacketFromClient (clientLocation , accessKey , status , size , size , timeToCipher )
97+ }
98+ }
99+
100+ func BenchmarkTargetUDP (b * testing.B ) {
101+ ssMetrics := NewPrometheusShadowsocksMetrics (nil , prometheus .NewRegistry ())
102+ clientLocation := "ZZ"
103+ accessKey := "key 1"
104+ status := "OK"
105+ size := 1000
106+ b .ResetTimer ()
107+ for i := 0 ; i < b .N ; i ++ {
108+ ssMetrics .AddUDPPacketFromTarget (clientLocation , accessKey , status , size , size )
109+ }
110+ }
111+
112+ func BenchmarkNAT (b * testing.B ) {
113+ ssMetrics := NewPrometheusShadowsocksMetrics (nil , prometheus .NewRegistry ())
114+ b .ResetTimer ()
115+ for i := 0 ; i < b .N ; i ++ {
116+ ssMetrics .AddUDPNatEntry ()
117+ ssMetrics .RemoveUDPNatEntry ()
118+ }
119+ }
0 commit comments