- golang 手搓redis服務器
- 倉庫地址:
- 實現思路:
golang 手搓redis服務器
倉庫地址:
倉庫: https://github.com/dengjiayue/my-redis.git
實現思路:
● 協議: tcp通信
● 數據包: 長度(4byte)+方法(1byte)+數據json
● 數據處理: 單線程map讀寫
○ 依次處理待處理隊列的請求(chan)數據,處理并返回
■ 隊列大小: Max指定
■ 構建請求處理池: 不需要反復創建chan
性能壓測:
package srcimport ("fmt""testing""time"
)// 壓測my redis
func BenchmarkMyRedisWrite(b *testing.B) {c := NewClient("localhost:8080")go c.HandleResp()defer c.Close()//開始計時b.StartTimer()for i := 0; i < b.N; i++ {c.Set("name", "zhangsan")}// BenchmarkMyRedis-8 28090 40598 ns/op 642 B/op 14 allocs/op
}// 壓測my redis
func BenchmarkMyRedisRead(b *testing.B) {c := NewClient("localhost:8080")go c.HandleResp()defer c.Close()//開始計時b.StartTimer()for i := 0; i < b.N; i++ {c.Get("name")}// BenchmarkMyRedisRead-8 27771 44423 ns/op 588
}// 并發壓測(寫)
func BenchmarkMyRedisConcurrencyWrite(b *testing.B) {c := NewClient("localhost:8080")go c.HandleResp()defer c.Close()//開始計時b.StartTimer()b.RunParallel(func(pb *testing.PB) {for pb.Next() {c.Set("name", "zhangsan")}})// BenchmarkMyRedisConcurrencyWrite-8 90667 12439 ns/op 612 B/op 14 allocs/op
}// 并發壓測(讀)
func BenchmarkMyRedisConcurrencyRead(b *testing.B) {c := NewClient("localhost:8080")go c.HandleResp()defer c.Close()//開始計時b.StartTimer()b.RunParallel(func(pb *testing.PB) {for pb.Next() {c.Get("name")}})// BenchmarkMyRedisConcurrencyRead-8 89955 12198 ns/op 512 B/op 15 allocs/op
}
- 單tcp連接可以達到9w左右的讀寫的QPS