bench_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. package redis_test
  2. import (
  3. "bytes"
  4. "testing"
  5. "time"
  6. "gopkg.in/redis.v5"
  7. )
  8. func benchmarkRedisClient(poolSize int) *redis.Client {
  9. client := redis.NewClient(&redis.Options{
  10. Addr: ":6379",
  11. DialTimeout: time.Second,
  12. ReadTimeout: time.Second,
  13. WriteTimeout: time.Second,
  14. PoolSize: poolSize,
  15. })
  16. if err := client.FlushDb().Err(); err != nil {
  17. panic(err)
  18. }
  19. return client
  20. }
  21. func BenchmarkRedisPing(b *testing.B) {
  22. client := benchmarkRedisClient(10)
  23. defer client.Close()
  24. b.ResetTimer()
  25. b.RunParallel(func(pb *testing.PB) {
  26. for pb.Next() {
  27. if err := client.Ping().Err(); err != nil {
  28. b.Fatal(err)
  29. }
  30. }
  31. })
  32. }
  33. func BenchmarkRedisSetString(b *testing.B) {
  34. client := benchmarkRedisClient(10)
  35. defer client.Close()
  36. value := string(bytes.Repeat([]byte{'1'}, 10000))
  37. b.ResetTimer()
  38. b.RunParallel(func(pb *testing.PB) {
  39. for pb.Next() {
  40. if err := client.Set("key", value, 0).Err(); err != nil {
  41. b.Fatal(err)
  42. }
  43. }
  44. })
  45. }
  46. func BenchmarkRedisGetNil(b *testing.B) {
  47. client := benchmarkRedisClient(10)
  48. defer client.Close()
  49. b.ResetTimer()
  50. b.RunParallel(func(pb *testing.PB) {
  51. for pb.Next() {
  52. if err := client.Get("key").Err(); err != redis.Nil {
  53. b.Fatal(err)
  54. }
  55. }
  56. })
  57. }
  58. func benchmarkSetRedis(b *testing.B, poolSize, payloadSize int) {
  59. client := benchmarkRedisClient(poolSize)
  60. defer client.Close()
  61. value := string(bytes.Repeat([]byte{'1'}, payloadSize))
  62. b.ResetTimer()
  63. b.RunParallel(func(pb *testing.PB) {
  64. for pb.Next() {
  65. if err := client.Set("key", value, 0).Err(); err != nil {
  66. b.Fatal(err)
  67. }
  68. }
  69. })
  70. }
  71. func BenchmarkSetRedis10Conns64Bytes(b *testing.B) {
  72. benchmarkSetRedis(b, 10, 64)
  73. }
  74. func BenchmarkSetRedis100Conns64Bytes(b *testing.B) {
  75. benchmarkSetRedis(b, 100, 64)
  76. }
  77. func BenchmarkSetRedis10Conns1KB(b *testing.B) {
  78. benchmarkSetRedis(b, 10, 1024)
  79. }
  80. func BenchmarkSetRedis100Conns1KB(b *testing.B) {
  81. benchmarkSetRedis(b, 100, 1024)
  82. }
  83. func BenchmarkSetRedis10Conns10KB(b *testing.B) {
  84. benchmarkSetRedis(b, 10, 10*1024)
  85. }
  86. func BenchmarkSetRedis100Conns10KB(b *testing.B) {
  87. benchmarkSetRedis(b, 100, 10*1024)
  88. }
  89. func BenchmarkSetRedis10Conns1MB(b *testing.B) {
  90. benchmarkSetRedis(b, 10, 1024*1024)
  91. }
  92. func BenchmarkSetRedis100Conns1MB(b *testing.B) {
  93. benchmarkSetRedis(b, 100, 1024*1024)
  94. }
  95. func BenchmarkRedisSetGetBytes(b *testing.B) {
  96. client := benchmarkRedisClient(10)
  97. defer client.Close()
  98. value := bytes.Repeat([]byte{'1'}, 10000)
  99. b.ResetTimer()
  100. b.RunParallel(func(pb *testing.PB) {
  101. for pb.Next() {
  102. if err := client.Set("key", value, 0).Err(); err != nil {
  103. b.Fatal(err)
  104. }
  105. got, err := client.Get("key").Bytes()
  106. if err != nil {
  107. b.Fatal(err)
  108. }
  109. if !bytes.Equal(got, value) {
  110. b.Fatalf("got != value")
  111. }
  112. }
  113. })
  114. }
  115. func BenchmarkRedisMGet(b *testing.B) {
  116. client := benchmarkRedisClient(10)
  117. defer client.Close()
  118. if err := client.MSet("key1", "hello1", "key2", "hello2").Err(); err != nil {
  119. b.Fatal(err)
  120. }
  121. b.ResetTimer()
  122. b.RunParallel(func(pb *testing.PB) {
  123. for pb.Next() {
  124. if err := client.MGet("key1", "key2").Err(); err != nil {
  125. b.Fatal(err)
  126. }
  127. }
  128. })
  129. }
  130. func BenchmarkSetExpire(b *testing.B) {
  131. client := benchmarkRedisClient(10)
  132. defer client.Close()
  133. b.ResetTimer()
  134. b.RunParallel(func(pb *testing.PB) {
  135. for pb.Next() {
  136. if err := client.Set("key", "hello", 0).Err(); err != nil {
  137. b.Fatal(err)
  138. }
  139. if err := client.Expire("key", time.Second).Err(); err != nil {
  140. b.Fatal(err)
  141. }
  142. }
  143. })
  144. }
  145. func BenchmarkPipeline(b *testing.B) {
  146. client := benchmarkRedisClient(10)
  147. defer client.Close()
  148. b.ResetTimer()
  149. b.RunParallel(func(pb *testing.PB) {
  150. for pb.Next() {
  151. _, err := client.Pipelined(func(pipe *redis.Pipeline) error {
  152. pipe.Set("key", "hello", 0)
  153. pipe.Expire("key", time.Second)
  154. return nil
  155. })
  156. if err != nil {
  157. b.Fatal(err)
  158. }
  159. }
  160. })
  161. }
  162. func BenchmarkZAdd(b *testing.B) {
  163. client := benchmarkRedisClient(10)
  164. defer client.Close()
  165. b.ResetTimer()
  166. b.RunParallel(func(pb *testing.PB) {
  167. for pb.Next() {
  168. if err := client.ZAdd("key", redis.Z{float64(1), "hello"}).Err(); err != nil {
  169. b.Fatal(err)
  170. }
  171. }
  172. })
  173. }