Skip to content

Commit

Permalink
Merge pull request #328 from gatewayd-io/add-benchmarks
Browse files Browse the repository at this point in the history
Add benchmarks
  • Loading branch information
mostafa authored Sep 21, 2023
2 parents 6ed3b6e + 5e95582 commit 5732a59
Show file tree
Hide file tree
Showing 12 changed files with 900 additions and 1 deletion.
1 change: 1 addition & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ linters-settings:
- "github.com/prometheus/common/expfmt"
- "github.com/panjf2000/gnet/v2"
- "github.com/spf13/cobra"
- "github.com/knadh/koanf"
tagalign:
align: false
sort: false
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ clean:
test:
@go test -v ./...

benchmark:
@go test -bench=. -benchmem -run=^# ./...

update-all:
@go get -u ./...
@go mod tidy
Expand Down
72 changes: 72 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package config

import (
"context"
"testing"

"github.com/knadh/koanf"
"github.com/stretchr/testify/assert"
)

// TestNewConfig tests the NewConfig function.
func TestNewConfig(t *testing.T) {
config := NewConfig(
context.Background(), GlobalConfigFilename, PluginsConfigFilename)
assert.NotNil(t, config)
assert.Equal(t, config.globalConfigFile, GlobalConfigFilename)
assert.Equal(t, config.pluginConfigFile, PluginsConfigFilename)
assert.Equal(t, config.globalDefaults, GlobalConfig{})
assert.Equal(t, config.pluginDefaults, PluginConfig{})
assert.Equal(t, config.Global, GlobalConfig{})
assert.Equal(t, config.Plugin, PluginConfig{})
assert.Equal(t, config.GlobalKoanf, koanf.New("."))
assert.Equal(t, config.PluginKoanf, koanf.New("."))
}

// TestInitConfig tests the InitConfig function, which practically tests all
// the other functions.
func TestInitConfig(t *testing.T) {
ctx := context.Background()
config := NewConfig(ctx, "../"+GlobalConfigFilename, "../"+PluginsConfigFilename)
config.InitConfig(ctx)
assert.NotNil(t, config.Global)
assert.NotEqual(t, config.Global, GlobalConfig{})
assert.Contains(t, config.Global.Servers, Default)
assert.NotNil(t, config.Plugin)
assert.NotEqual(t, config.Plugin, PluginConfig{})
assert.Len(t, config.Plugin.Plugins, 1)
assert.NotNil(t, config.GlobalKoanf)
assert.NotEqual(t, config.GlobalKoanf, koanf.New("."))
assert.Equal(t, DefaultLogLevel, config.GlobalKoanf.String("loggers.default.level"))
assert.NotNil(t, config.PluginKoanf)
assert.NotEqual(t, config.PluginKoanf, koanf.New("."))
assert.Equal(t, string(PassDown), config.PluginKoanf.String("verificationPolicy"))
assert.NotNil(t, config.globalDefaults)
assert.NotEqual(t, config.globalDefaults, GlobalConfig{})
assert.Contains(t, config.globalDefaults.Servers, Default)
assert.NotNil(t, config.pluginDefaults)
assert.NotEqual(t, config.pluginDefaults, PluginConfig{})
assert.Len(t, config.pluginDefaults.Plugins, 0)
}

// TestMergeGlobalConfig tests the MergeGlobalConfig function.
func TestMergeGlobalConfig(t *testing.T) {
ctx := context.Background()
config := NewConfig(ctx, "../"+GlobalConfigFilename, "../"+PluginsConfigFilename)
config.InitConfig(ctx)
// The default log level is info.
assert.Equal(t, config.Global.Loggers[Default].Level, DefaultLogLevel)

// Merge a config that sets the log level to debug.
config.MergeGlobalConfig(ctx, map[string]interface{}{
"loggers": map[string]interface{}{
"default": map[string]interface{}{
"level": "debug",
},
},
})
assert.NotNil(t, config.Global)
assert.NotEqual(t, config.Global, GlobalConfig{})
// The log level should now be debug.
assert.Equal(t, config.Global.Loggers[Default].Level, "debug")
}
5 changes: 5 additions & 0 deletions config/getters.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ func (l Logger) GetOutput() []LogOutput {
outputs = append(outputs, Console)
}
}

if len(outputs) == 0 {
outputs = append(outputs, Console)
}

return outputs
}

Expand Down
130 changes: 130 additions & 0 deletions config/getters_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package config

import (
"testing"
"time"

"github.com/panjf2000/gnet/v2"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
)

// TestGetVerificationPolicy tests the GetVerificationPolicy function.
func TestGetVerificationPolicy(t *testing.T) {
pluginConfig := PluginConfig{}
assert.Equal(t, PassDown, pluginConfig.GetVerificationPolicy())
}

// TestGetPluginCompatibilityPolicy tests the GetPluginCompatibilityPolicy function.
func TestGetPluginCompatibilityPolicy(t *testing.T) {
pluginConfig := PluginConfig{}
assert.Equal(t, Strict, pluginConfig.GetPluginCompatibilityPolicy())
}

// TestGetAcceptancePolicy tests the GetAcceptancePolicy function.
func TestGetAcceptancePolicy(t *testing.T) {
pluginConfig := PluginConfig{}
assert.Equal(t, Accept, pluginConfig.GetAcceptancePolicy())
}

// TestGetTerminationPolicy tests the GetTerminationPolicy function.
func TestGetTerminationPolicy(t *testing.T) {
pluginConfig := PluginConfig{}
assert.Equal(t, Stop, pluginConfig.GetTerminationPolicy())
}

// TestGetTCPKeepAlivePeriod tests the GetTCPKeepAlivePeriod function.
func TestGetTCPKeepAlivePeriod(t *testing.T) {
client := Client{}
assert.Equal(t, DefaultTCPKeepAlivePeriod, client.GetTCPKeepAlivePeriod())
}

// TestGetReceiveDeadline tests the GetReceiveDeadline function.
func TestGetReceiveDeadline(t *testing.T) {
client := Client{}
assert.Equal(t, time.Duration(0), client.GetReceiveDeadline())
}

// TestGetReceiveTimeout tests the GetReceiveTimeout function.
func TestGetReceiveTimeout(t *testing.T) {
client := Client{}
assert.Equal(t, time.Duration(0), client.GetReceiveTimeout())
}

// TestGetSendDeadline tests the GetSendDeadline function.
func TestGetSendDeadline(t *testing.T) {
client := Client{}
assert.Equal(t, time.Duration(0), client.GetSendDeadline())
}

// TestGetReceiveChunkSize tests the GetReceiveChunkSize function.
func TestGetReceiveChunkSize(t *testing.T) {
client := Client{}
assert.Equal(t, DefaultChunkSize, client.GetReceiveChunkSize())
}

// TestGetHealthCheckPeriod tests the GetHealthCheckPeriod function.
func TestGetHealthCheckPeriod(t *testing.T) {
proxy := Proxy{}
assert.Equal(t, DefaultHealthCheckPeriod, proxy.GetHealthCheckPeriod())
}

// TestGetTickInterval tests the GetTickInterval function.
func TestGetTickInterval(t *testing.T) {
server := Server{}
assert.Equal(t, DefaultTickInterval, server.GetTickInterval())
}

// TestGetLoadBalancer tests the GetLoadBalancer function.
func TestGetLoadBalancer(t *testing.T) {
server := Server{}
assert.Equal(t, gnet.RoundRobin, server.GetLoadBalancer())
}

// TestGetTCPNoDelay tests the GetTCPNoDelay function.
func TestGetTCPNoDelay(t *testing.T) {
server := Server{}
assert.Equal(t, gnet.TCPDelay, server.GetTCPNoDelay())
}

// TestGetSize tests the GetSize function.
func TestGetSize(t *testing.T) {
pool := Pool{}
assert.Equal(t, DefaultPoolSize, pool.GetSize())
}

// TestGetOutput tests the GetOutput function.
func TestGetOutput(t *testing.T) {
logger := Logger{}
assert.Equal(t, []LogOutput{Console}, logger.GetOutput())
}

// TestGetTimeFormat tests the GetTimeFormat function.
func TestGetTimeFormat(t *testing.T) {
logger := Logger{}
assert.Equal(t, zerolog.TimeFormatUnix, logger.GetTimeFormat())
}

// TestGetConsoleTimeFormat tests the GetConsoleTimeFormat function.
func TestGetConsoleTimeFormat(t *testing.T) {
logger := Logger{}
assert.Equal(t, time.RFC3339, logger.GetConsoleTimeFormat())
}

// TestGetLevel tests the GetLevel function.
func TestGetLevel(t *testing.T) {
logger := Logger{}
assert.Equal(t, zerolog.InfoLevel, logger.GetLevel())
}

// TestGetPlugins tests the GetPlugins function.
func TestGetPlugins(t *testing.T) {
plugin := Plugin{Name: "plugin1"}
pluginConfig := PluginConfig{Plugins: []Plugin{plugin}}
assert.Equal(t, []Plugin{plugin}, pluginConfig.GetPlugins("plugin1"))
}

// TestGetDefaultConfigFilePath tests the GetDefaultConfigFilePath function.
func TestGetDefaultConfigFilePath(t *testing.T) {
assert.Equal(t, GlobalConfigFilename, GetDefaultConfigFilePath(GlobalConfigFilename))
}
18 changes: 18 additions & 0 deletions config/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package config

import (
"runtime"
"testing"

"github.com/stretchr/testify/assert"
)

// TestVersionInfo tests the VersionInfo function.
func TestVersionInfo(t *testing.T) {
versionInfo := VersionInfo()
assert.Contains(t, versionInfo, "GatewayD")
assert.Contains(t, versionInfo, "0.0.0")
assert.Contains(t, versionInfo, "go")
assert.Contains(t, versionInfo, runtime.GOOS)
assert.Contains(t, versionInfo, runtime.GOARCH)
}
112 changes: 112 additions & 0 deletions network/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,115 @@ func TestIsConnected(t *testing.T) {
client.Close()
assert.False(t, client.IsConnected())
}

func BenchmarkNewClient(b *testing.B) {
cfg := logging.LoggerConfig{
Output: []config.LogOutput{config.Console},
TimeFormat: zerolog.TimeFormatUnix,
ConsoleTimeFormat: time.RFC3339,
Level: zerolog.DebugLevel,
NoColor: true,
}

logger := logging.NewLogger(context.Background(), cfg)
for i := 0; i < b.N; i++ {
client := NewClient(context.Background(), &config.Client{
Network: "tcp",
Address: "localhost:5432",
ReceiveChunkSize: config.DefaultChunkSize,
ReceiveDeadline: config.DefaultReceiveDeadline,
SendDeadline: config.DefaultSendDeadline,
TCPKeepAlive: false,
TCPKeepAlivePeriod: config.DefaultTCPKeepAlivePeriod,
}, logger)
client.Close()
}
}

func BenchmarkSend(b *testing.B) {
logger := logging.NewLogger(context.Background(), logging.LoggerConfig{
Output: []config.LogOutput{config.Console},
TimeFormat: zerolog.TimeFormatUnix,
ConsoleTimeFormat: time.RFC3339,
Level: zerolog.DebugLevel,
NoColor: true,
})

client := NewClient(
context.Background(),
&config.Client{
Network: "tcp",
Address: "localhost:5432",
ReceiveChunkSize: config.DefaultChunkSize,
ReceiveDeadline: config.DefaultReceiveDeadline,
SendDeadline: config.DefaultSendDeadline,
TCPKeepAlive: false,
TCPKeepAlivePeriod: config.DefaultTCPKeepAlivePeriod,
},
logger)
defer client.Close()

packet := CreatePgStartupPacket()
for i := 0; i < b.N; i++ {
client.Send(packet) //nolint:errcheck
}
}

func BenchmarkReceive(b *testing.B) {
logger := logging.NewLogger(context.Background(), logging.LoggerConfig{
Output: []config.LogOutput{config.Console},
TimeFormat: zerolog.TimeFormatUnix,
ConsoleTimeFormat: time.RFC3339,
Level: zerolog.DebugLevel,
NoColor: true,
})

client := NewClient(
context.Background(),
&config.Client{
Network: "tcp",
Address: "localhost:5432",
ReceiveChunkSize: config.DefaultChunkSize,
ReceiveDeadline: config.DefaultReceiveDeadline,
ReceiveTimeout: 1 * time.Millisecond,
SendDeadline: config.DefaultSendDeadline,
TCPKeepAlive: false,
TCPKeepAlivePeriod: config.DefaultTCPKeepAlivePeriod,
},
logger)
defer client.Close()

packet := CreatePgStartupPacket()
client.Send(packet) //nolint:errcheck
for i := 0; i < b.N; i++ {
client.Receive() //nolint:errcheck
}
}

func BenchmarkIsConnected(b *testing.B) {
logger := logging.NewLogger(context.Background(), logging.LoggerConfig{
Output: []config.LogOutput{config.Console},
TimeFormat: zerolog.TimeFormatUnix,
ConsoleTimeFormat: time.RFC3339,
Level: zerolog.DebugLevel,
NoColor: true,
})

client := NewClient(
context.Background(),
&config.Client{
Network: "tcp",
Address: "localhost:5432",
ReceiveChunkSize: config.DefaultChunkSize,
ReceiveDeadline: config.DefaultReceiveDeadline,
SendDeadline: config.DefaultSendDeadline,
TCPKeepAlive: false,
TCPKeepAlivePeriod: config.DefaultTCPKeepAlivePeriod,
},
logger)
defer client.Close()

for i := 0; i < b.N; i++ {
client.IsConnected()
}
}
Loading

0 comments on commit 5732a59

Please sign in to comment.