Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add benchmark for network/client
Browse files Browse the repository at this point in the history
mostafa committed Sep 20, 2023
1 parent 2858fe4 commit b8017ba
Showing 1 changed file with 112 additions and 0 deletions.
112 changes: 112 additions & 0 deletions network/client_test.go
Original file line number Diff line number Diff line change
@@ -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++ {
c := 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)
c.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)
}
}

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)
for i := 0; i < b.N; i++ {
client.Receive()
}
}

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()
}
}

0 comments on commit b8017ba

Please sign in to comment.