bench_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package pool_test
  2. import (
  3. "context"
  4. "fmt"
  5. "testing"
  6. "time"
  7. "github.com/go-redis/redis/v7/internal/pool"
  8. )
  9. type poolGetPutBenchmark struct {
  10. poolSize int
  11. }
  12. func (bm poolGetPutBenchmark) String() string {
  13. return fmt.Sprintf("pool=%d", bm.poolSize)
  14. }
  15. func BenchmarkPoolGetPut(b *testing.B) {
  16. benchmarks := []poolGetPutBenchmark{
  17. {1},
  18. {2},
  19. {8},
  20. {32},
  21. {64},
  22. {128},
  23. }
  24. for _, bm := range benchmarks {
  25. b.Run(bm.String(), func(b *testing.B) {
  26. connPool := pool.NewConnPool(&pool.Options{
  27. Dialer: dummyDialer,
  28. PoolSize: bm.poolSize,
  29. PoolTimeout: time.Second,
  30. IdleTimeout: time.Hour,
  31. IdleCheckFrequency: time.Hour,
  32. })
  33. b.ResetTimer()
  34. b.RunParallel(func(pb *testing.PB) {
  35. for pb.Next() {
  36. cn, err := connPool.Get(context.Background())
  37. if err != nil {
  38. b.Fatal(err)
  39. }
  40. connPool.Put(cn)
  41. }
  42. })
  43. })
  44. }
  45. }
  46. type poolGetRemoveBenchmark struct {
  47. poolSize int
  48. }
  49. func (bm poolGetRemoveBenchmark) String() string {
  50. return fmt.Sprintf("pool=%d", bm.poolSize)
  51. }
  52. func BenchmarkPoolGetRemove(b *testing.B) {
  53. benchmarks := []poolGetRemoveBenchmark{
  54. {1},
  55. {2},
  56. {8},
  57. {32},
  58. {64},
  59. {128},
  60. }
  61. for _, bm := range benchmarks {
  62. b.Run(bm.String(), func(b *testing.B) {
  63. connPool := pool.NewConnPool(&pool.Options{
  64. Dialer: dummyDialer,
  65. PoolSize: bm.poolSize,
  66. PoolTimeout: time.Second,
  67. IdleTimeout: time.Hour,
  68. IdleCheckFrequency: time.Hour,
  69. })
  70. b.ResetTimer()
  71. b.RunParallel(func(pb *testing.PB) {
  72. for pb.Next() {
  73. cn, err := connPool.Get(context.Background())
  74. if err != nil {
  75. b.Fatal(err)
  76. }
  77. connPool.Remove(cn, nil)
  78. }
  79. })
  80. })
  81. }
  82. }