use gnet server for performance

This commit is contained in:
finley
2025-05-22 09:31:53 +08:00
parent f4a2c92fc1
commit 144b642fe7
12 changed files with 286 additions and 11 deletions

41
gnet/parser_test.go Normal file
View File

@@ -0,0 +1,41 @@
package gnet
import (
"bytes"
"fmt"
"testing"
)
func BenchmarkParseSETCommand(b *testing.B) {
valueSizes := []int{10, 100, 1000, 10000}
for _, size := range valueSizes {
value := bytes.Repeat([]byte("a"), size)
cmd := []byte(fmt.Sprintf("*3\r\n$3\r\nSET\r\n$3\r\nkey\r\n$%d\r\n%s\r\n", len(value), value))
b.Run("value_size_"+formatSize(size), func(subB *testing.B) {
subB.ResetTimer()
for i := 0; i < subB.N; i++ {
reader := bytes.NewReader(cmd)
_, err := Parse(reader)
if err != nil {
subB.Fatalf("解析失败: %v", err)
}
}
})
}
}
func formatSize(size int) string {
units := []string{"B", "KB", "MB"}
unitIndex := 0
floatSize := float64(size)
for floatSize >= 1024 && unitIndex < len(units)-1 {
floatSize /= 1024
unitIndex++
}
return fmt.Sprintf("%.0f%s", floatSize, units[unitIndex])
}