From b8017bac290212b05945681ba6ed2cd4bb30cfc1 Mon Sep 17 00:00:00 2001 From: Mostafa Moradian Date: Wed, 20 Sep 2023 22:44:25 +0200 Subject: [PATCH] Add benchmark for network/client --- network/client_test.go | 112 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) diff --git a/network/client_test.go b/network/client_test.go index 99bc70c5..7e7a42aa 100644 --- a/network/client_test.go +++ b/network/client_test.go @@ -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() + } +}