Skip to content

Commit 5732a59

Browse files
authored
Merge pull request #328 from gatewayd-io/add-benchmarks
Add benchmarks
2 parents 6ed3b6e + 5e95582 commit 5732a59

File tree

12 files changed

+900
-1
lines changed

12 files changed

+900
-1
lines changed

.golangci.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ linters-settings:
7474
- "github.com/prometheus/common/expfmt"
7575
- "github.com/panjf2000/gnet/v2"
7676
- "github.com/spf13/cobra"
77+
- "github.com/knadh/koanf"
7778
tagalign:
7879
align: false
7980
sort: false

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ clean:
110110
test:
111111
@go test -v ./...
112112

113+
benchmark:
114+
@go test -bench=. -benchmem -run=^# ./...
115+
113116
update-all:
114117
@go get -u ./...
115118
@go mod tidy

config/config_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package config
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/knadh/koanf"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
// TestNewConfig tests the NewConfig function.
12+
func TestNewConfig(t *testing.T) {
13+
config := NewConfig(
14+
context.Background(), GlobalConfigFilename, PluginsConfigFilename)
15+
assert.NotNil(t, config)
16+
assert.Equal(t, config.globalConfigFile, GlobalConfigFilename)
17+
assert.Equal(t, config.pluginConfigFile, PluginsConfigFilename)
18+
assert.Equal(t, config.globalDefaults, GlobalConfig{})
19+
assert.Equal(t, config.pluginDefaults, PluginConfig{})
20+
assert.Equal(t, config.Global, GlobalConfig{})
21+
assert.Equal(t, config.Plugin, PluginConfig{})
22+
assert.Equal(t, config.GlobalKoanf, koanf.New("."))
23+
assert.Equal(t, config.PluginKoanf, koanf.New("."))
24+
}
25+
26+
// TestInitConfig tests the InitConfig function, which practically tests all
27+
// the other functions.
28+
func TestInitConfig(t *testing.T) {
29+
ctx := context.Background()
30+
config := NewConfig(ctx, "../"+GlobalConfigFilename, "../"+PluginsConfigFilename)
31+
config.InitConfig(ctx)
32+
assert.NotNil(t, config.Global)
33+
assert.NotEqual(t, config.Global, GlobalConfig{})
34+
assert.Contains(t, config.Global.Servers, Default)
35+
assert.NotNil(t, config.Plugin)
36+
assert.NotEqual(t, config.Plugin, PluginConfig{})
37+
assert.Len(t, config.Plugin.Plugins, 1)
38+
assert.NotNil(t, config.GlobalKoanf)
39+
assert.NotEqual(t, config.GlobalKoanf, koanf.New("."))
40+
assert.Equal(t, DefaultLogLevel, config.GlobalKoanf.String("loggers.default.level"))
41+
assert.NotNil(t, config.PluginKoanf)
42+
assert.NotEqual(t, config.PluginKoanf, koanf.New("."))
43+
assert.Equal(t, string(PassDown), config.PluginKoanf.String("verificationPolicy"))
44+
assert.NotNil(t, config.globalDefaults)
45+
assert.NotEqual(t, config.globalDefaults, GlobalConfig{})
46+
assert.Contains(t, config.globalDefaults.Servers, Default)
47+
assert.NotNil(t, config.pluginDefaults)
48+
assert.NotEqual(t, config.pluginDefaults, PluginConfig{})
49+
assert.Len(t, config.pluginDefaults.Plugins, 0)
50+
}
51+
52+
// TestMergeGlobalConfig tests the MergeGlobalConfig function.
53+
func TestMergeGlobalConfig(t *testing.T) {
54+
ctx := context.Background()
55+
config := NewConfig(ctx, "../"+GlobalConfigFilename, "../"+PluginsConfigFilename)
56+
config.InitConfig(ctx)
57+
// The default log level is info.
58+
assert.Equal(t, config.Global.Loggers[Default].Level, DefaultLogLevel)
59+
60+
// Merge a config that sets the log level to debug.
61+
config.MergeGlobalConfig(ctx, map[string]interface{}{
62+
"loggers": map[string]interface{}{
63+
"default": map[string]interface{}{
64+
"level": "debug",
65+
},
66+
},
67+
})
68+
assert.NotNil(t, config.Global)
69+
assert.NotEqual(t, config.Global, GlobalConfig{})
70+
// The log level should now be debug.
71+
assert.Equal(t, config.Global.Loggers[Default].Level, "debug")
72+
}

config/getters.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,11 @@ func (l Logger) GetOutput() []LogOutput {
207207
outputs = append(outputs, Console)
208208
}
209209
}
210+
211+
if len(outputs) == 0 {
212+
outputs = append(outputs, Console)
213+
}
214+
210215
return outputs
211216
}
212217

config/getters_test.go

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package config
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/panjf2000/gnet/v2"
8+
"github.com/rs/zerolog"
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
// TestGetVerificationPolicy tests the GetVerificationPolicy function.
13+
func TestGetVerificationPolicy(t *testing.T) {
14+
pluginConfig := PluginConfig{}
15+
assert.Equal(t, PassDown, pluginConfig.GetVerificationPolicy())
16+
}
17+
18+
// TestGetPluginCompatibilityPolicy tests the GetPluginCompatibilityPolicy function.
19+
func TestGetPluginCompatibilityPolicy(t *testing.T) {
20+
pluginConfig := PluginConfig{}
21+
assert.Equal(t, Strict, pluginConfig.GetPluginCompatibilityPolicy())
22+
}
23+
24+
// TestGetAcceptancePolicy tests the GetAcceptancePolicy function.
25+
func TestGetAcceptancePolicy(t *testing.T) {
26+
pluginConfig := PluginConfig{}
27+
assert.Equal(t, Accept, pluginConfig.GetAcceptancePolicy())
28+
}
29+
30+
// TestGetTerminationPolicy tests the GetTerminationPolicy function.
31+
func TestGetTerminationPolicy(t *testing.T) {
32+
pluginConfig := PluginConfig{}
33+
assert.Equal(t, Stop, pluginConfig.GetTerminationPolicy())
34+
}
35+
36+
// TestGetTCPKeepAlivePeriod tests the GetTCPKeepAlivePeriod function.
37+
func TestGetTCPKeepAlivePeriod(t *testing.T) {
38+
client := Client{}
39+
assert.Equal(t, DefaultTCPKeepAlivePeriod, client.GetTCPKeepAlivePeriod())
40+
}
41+
42+
// TestGetReceiveDeadline tests the GetReceiveDeadline function.
43+
func TestGetReceiveDeadline(t *testing.T) {
44+
client := Client{}
45+
assert.Equal(t, time.Duration(0), client.GetReceiveDeadline())
46+
}
47+
48+
// TestGetReceiveTimeout tests the GetReceiveTimeout function.
49+
func TestGetReceiveTimeout(t *testing.T) {
50+
client := Client{}
51+
assert.Equal(t, time.Duration(0), client.GetReceiveTimeout())
52+
}
53+
54+
// TestGetSendDeadline tests the GetSendDeadline function.
55+
func TestGetSendDeadline(t *testing.T) {
56+
client := Client{}
57+
assert.Equal(t, time.Duration(0), client.GetSendDeadline())
58+
}
59+
60+
// TestGetReceiveChunkSize tests the GetReceiveChunkSize function.
61+
func TestGetReceiveChunkSize(t *testing.T) {
62+
client := Client{}
63+
assert.Equal(t, DefaultChunkSize, client.GetReceiveChunkSize())
64+
}
65+
66+
// TestGetHealthCheckPeriod tests the GetHealthCheckPeriod function.
67+
func TestGetHealthCheckPeriod(t *testing.T) {
68+
proxy := Proxy{}
69+
assert.Equal(t, DefaultHealthCheckPeriod, proxy.GetHealthCheckPeriod())
70+
}
71+
72+
// TestGetTickInterval tests the GetTickInterval function.
73+
func TestGetTickInterval(t *testing.T) {
74+
server := Server{}
75+
assert.Equal(t, DefaultTickInterval, server.GetTickInterval())
76+
}
77+
78+
// TestGetLoadBalancer tests the GetLoadBalancer function.
79+
func TestGetLoadBalancer(t *testing.T) {
80+
server := Server{}
81+
assert.Equal(t, gnet.RoundRobin, server.GetLoadBalancer())
82+
}
83+
84+
// TestGetTCPNoDelay tests the GetTCPNoDelay function.
85+
func TestGetTCPNoDelay(t *testing.T) {
86+
server := Server{}
87+
assert.Equal(t, gnet.TCPDelay, server.GetTCPNoDelay())
88+
}
89+
90+
// TestGetSize tests the GetSize function.
91+
func TestGetSize(t *testing.T) {
92+
pool := Pool{}
93+
assert.Equal(t, DefaultPoolSize, pool.GetSize())
94+
}
95+
96+
// TestGetOutput tests the GetOutput function.
97+
func TestGetOutput(t *testing.T) {
98+
logger := Logger{}
99+
assert.Equal(t, []LogOutput{Console}, logger.GetOutput())
100+
}
101+
102+
// TestGetTimeFormat tests the GetTimeFormat function.
103+
func TestGetTimeFormat(t *testing.T) {
104+
logger := Logger{}
105+
assert.Equal(t, zerolog.TimeFormatUnix, logger.GetTimeFormat())
106+
}
107+
108+
// TestGetConsoleTimeFormat tests the GetConsoleTimeFormat function.
109+
func TestGetConsoleTimeFormat(t *testing.T) {
110+
logger := Logger{}
111+
assert.Equal(t, time.RFC3339, logger.GetConsoleTimeFormat())
112+
}
113+
114+
// TestGetLevel tests the GetLevel function.
115+
func TestGetLevel(t *testing.T) {
116+
logger := Logger{}
117+
assert.Equal(t, zerolog.InfoLevel, logger.GetLevel())
118+
}
119+
120+
// TestGetPlugins tests the GetPlugins function.
121+
func TestGetPlugins(t *testing.T) {
122+
plugin := Plugin{Name: "plugin1"}
123+
pluginConfig := PluginConfig{Plugins: []Plugin{plugin}}
124+
assert.Equal(t, []Plugin{plugin}, pluginConfig.GetPlugins("plugin1"))
125+
}
126+
127+
// TestGetDefaultConfigFilePath tests the GetDefaultConfigFilePath function.
128+
func TestGetDefaultConfigFilePath(t *testing.T) {
129+
assert.Equal(t, GlobalConfigFilename, GetDefaultConfigFilePath(GlobalConfigFilename))
130+
}

config/version_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package config
2+
3+
import (
4+
"runtime"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
// TestVersionInfo tests the VersionInfo function.
11+
func TestVersionInfo(t *testing.T) {
12+
versionInfo := VersionInfo()
13+
assert.Contains(t, versionInfo, "GatewayD")
14+
assert.Contains(t, versionInfo, "0.0.0")
15+
assert.Contains(t, versionInfo, "go")
16+
assert.Contains(t, versionInfo, runtime.GOOS)
17+
assert.Contains(t, versionInfo, runtime.GOARCH)
18+
}

network/client_test.go

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,115 @@ func TestIsConnected(t *testing.T) {
106106
client.Close()
107107
assert.False(t, client.IsConnected())
108108
}
109+
110+
func BenchmarkNewClient(b *testing.B) {
111+
cfg := logging.LoggerConfig{
112+
Output: []config.LogOutput{config.Console},
113+
TimeFormat: zerolog.TimeFormatUnix,
114+
ConsoleTimeFormat: time.RFC3339,
115+
Level: zerolog.DebugLevel,
116+
NoColor: true,
117+
}
118+
119+
logger := logging.NewLogger(context.Background(), cfg)
120+
for i := 0; i < b.N; i++ {
121+
client := NewClient(context.Background(), &config.Client{
122+
Network: "tcp",
123+
Address: "localhost:5432",
124+
ReceiveChunkSize: config.DefaultChunkSize,
125+
ReceiveDeadline: config.DefaultReceiveDeadline,
126+
SendDeadline: config.DefaultSendDeadline,
127+
TCPKeepAlive: false,
128+
TCPKeepAlivePeriod: config.DefaultTCPKeepAlivePeriod,
129+
}, logger)
130+
client.Close()
131+
}
132+
}
133+
134+
func BenchmarkSend(b *testing.B) {
135+
logger := logging.NewLogger(context.Background(), logging.LoggerConfig{
136+
Output: []config.LogOutput{config.Console},
137+
TimeFormat: zerolog.TimeFormatUnix,
138+
ConsoleTimeFormat: time.RFC3339,
139+
Level: zerolog.DebugLevel,
140+
NoColor: true,
141+
})
142+
143+
client := NewClient(
144+
context.Background(),
145+
&config.Client{
146+
Network: "tcp",
147+
Address: "localhost:5432",
148+
ReceiveChunkSize: config.DefaultChunkSize,
149+
ReceiveDeadline: config.DefaultReceiveDeadline,
150+
SendDeadline: config.DefaultSendDeadline,
151+
TCPKeepAlive: false,
152+
TCPKeepAlivePeriod: config.DefaultTCPKeepAlivePeriod,
153+
},
154+
logger)
155+
defer client.Close()
156+
157+
packet := CreatePgStartupPacket()
158+
for i := 0; i < b.N; i++ {
159+
client.Send(packet) //nolint:errcheck
160+
}
161+
}
162+
163+
func BenchmarkReceive(b *testing.B) {
164+
logger := logging.NewLogger(context.Background(), logging.LoggerConfig{
165+
Output: []config.LogOutput{config.Console},
166+
TimeFormat: zerolog.TimeFormatUnix,
167+
ConsoleTimeFormat: time.RFC3339,
168+
Level: zerolog.DebugLevel,
169+
NoColor: true,
170+
})
171+
172+
client := NewClient(
173+
context.Background(),
174+
&config.Client{
175+
Network: "tcp",
176+
Address: "localhost:5432",
177+
ReceiveChunkSize: config.DefaultChunkSize,
178+
ReceiveDeadline: config.DefaultReceiveDeadline,
179+
ReceiveTimeout: 1 * time.Millisecond,
180+
SendDeadline: config.DefaultSendDeadline,
181+
TCPKeepAlive: false,
182+
TCPKeepAlivePeriod: config.DefaultTCPKeepAlivePeriod,
183+
},
184+
logger)
185+
defer client.Close()
186+
187+
packet := CreatePgStartupPacket()
188+
client.Send(packet) //nolint:errcheck
189+
for i := 0; i < b.N; i++ {
190+
client.Receive() //nolint:errcheck
191+
}
192+
}
193+
194+
func BenchmarkIsConnected(b *testing.B) {
195+
logger := logging.NewLogger(context.Background(), logging.LoggerConfig{
196+
Output: []config.LogOutput{config.Console},
197+
TimeFormat: zerolog.TimeFormatUnix,
198+
ConsoleTimeFormat: time.RFC3339,
199+
Level: zerolog.DebugLevel,
200+
NoColor: true,
201+
})
202+
203+
client := NewClient(
204+
context.Background(),
205+
&config.Client{
206+
Network: "tcp",
207+
Address: "localhost:5432",
208+
ReceiveChunkSize: config.DefaultChunkSize,
209+
ReceiveDeadline: config.DefaultReceiveDeadline,
210+
SendDeadline: config.DefaultSendDeadline,
211+
TCPKeepAlive: false,
212+
TCPKeepAlivePeriod: config.DefaultTCPKeepAlivePeriod,
213+
},
214+
logger)
215+
defer client.Close()
216+
217+
for i := 0; i < b.N; i++ {
218+
client.IsConnected()
219+
}
220+
}

0 commit comments

Comments
 (0)