connmux_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. // Copyright 2014 Gary Burd
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"): you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. // License for the specific language governing permissions and limitations
  13. // under the License.
  14. package redisx_test
  15. import (
  16. "net/textproto"
  17. "sync"
  18. "testing"
  19. "github.com/gomodule/redigo/redis"
  20. "github.com/gomodule/redigo/redisx"
  21. )
  22. func TestConnMux(t *testing.T) {
  23. c, err := redisx.DialTest()
  24. if err != nil {
  25. t.Fatalf("error connection to database, %v", err)
  26. }
  27. m := redisx.NewConnMux(c)
  28. defer m.Close()
  29. c1 := m.Get()
  30. c2 := m.Get()
  31. c1.Send("ECHO", "hello")
  32. c2.Send("ECHO", "world")
  33. c1.Flush()
  34. c2.Flush()
  35. s, err := redis.String(c1.Receive())
  36. if err != nil {
  37. t.Fatal(err)
  38. }
  39. if s != "hello" {
  40. t.Fatalf("echo returned %q, want %q", s, "hello")
  41. }
  42. s, err = redis.String(c2.Receive())
  43. if err != nil {
  44. t.Fatal(err)
  45. }
  46. if s != "world" {
  47. t.Fatalf("echo returned %q, want %q", s, "world")
  48. }
  49. c1.Close()
  50. c2.Close()
  51. }
  52. func TestConnMuxClose(t *testing.T) {
  53. c, err := redisx.DialTest()
  54. if err != nil {
  55. t.Fatalf("error connection to database, %v", err)
  56. }
  57. m := redisx.NewConnMux(c)
  58. defer m.Close()
  59. c1 := m.Get()
  60. c2 := m.Get()
  61. if err := c1.Send("ECHO", "hello"); err != nil {
  62. t.Fatal(err)
  63. }
  64. if err := c1.Close(); err != nil {
  65. t.Fatal(err)
  66. }
  67. if err := c2.Send("ECHO", "world"); err != nil {
  68. t.Fatal(err)
  69. }
  70. if err := c2.Flush(); err != nil {
  71. t.Fatal(err)
  72. }
  73. s, err := redis.String(c2.Receive())
  74. if err != nil {
  75. t.Fatal(err)
  76. }
  77. if s != "world" {
  78. t.Fatalf("echo returned %q, want %q", s, "world")
  79. }
  80. c2.Close()
  81. }
  82. func BenchmarkConn(b *testing.B) {
  83. b.StopTimer()
  84. c, err := redisx.DialTest()
  85. if err != nil {
  86. b.Fatalf("error connection to database, %v", err)
  87. }
  88. defer c.Close()
  89. b.StartTimer()
  90. for i := 0; i < b.N; i++ {
  91. if _, err := c.Do("PING"); err != nil {
  92. b.Fatal(err)
  93. }
  94. }
  95. }
  96. func BenchmarkConnMux(b *testing.B) {
  97. b.StopTimer()
  98. c, err := redisx.DialTest()
  99. if err != nil {
  100. b.Fatalf("error connection to database, %v", err)
  101. }
  102. m := redisx.NewConnMux(c)
  103. defer m.Close()
  104. b.StartTimer()
  105. for i := 0; i < b.N; i++ {
  106. c := m.Get()
  107. if _, err := c.Do("PING"); err != nil {
  108. b.Fatal(err)
  109. }
  110. c.Close()
  111. }
  112. }
  113. func BenchmarkPool(b *testing.B) {
  114. b.StopTimer()
  115. p := redis.Pool{Dial: redisx.DialTest, MaxIdle: 1}
  116. defer p.Close()
  117. // Fill the pool.
  118. c := p.Get()
  119. if err := c.Err(); err != nil {
  120. b.Fatal(err)
  121. }
  122. c.Close()
  123. b.StartTimer()
  124. for i := 0; i < b.N; i++ {
  125. c := p.Get()
  126. if _, err := c.Do("PING"); err != nil {
  127. b.Fatal(err)
  128. }
  129. c.Close()
  130. }
  131. }
  132. const numConcurrent = 10
  133. func BenchmarkConnMuxConcurrent(b *testing.B) {
  134. b.StopTimer()
  135. c, err := redisx.DialTest()
  136. if err != nil {
  137. b.Fatalf("error connection to database, %v", err)
  138. }
  139. defer c.Close()
  140. m := redisx.NewConnMux(c)
  141. var wg sync.WaitGroup
  142. wg.Add(numConcurrent)
  143. b.StartTimer()
  144. for i := 0; i < numConcurrent; i++ {
  145. go func() {
  146. defer wg.Done()
  147. for i := 0; i < b.N; i++ {
  148. c := m.Get()
  149. if _, err := c.Do("PING"); err != nil {
  150. b.Fatal(err)
  151. }
  152. c.Close()
  153. }
  154. }()
  155. }
  156. wg.Wait()
  157. }
  158. func BenchmarkPoolConcurrent(b *testing.B) {
  159. b.StopTimer()
  160. p := redis.Pool{Dial: redisx.DialTest, MaxIdle: numConcurrent}
  161. defer p.Close()
  162. // Fill the pool.
  163. conns := make([]redis.Conn, numConcurrent)
  164. for i := range conns {
  165. c := p.Get()
  166. if err := c.Err(); err != nil {
  167. b.Fatal(err)
  168. }
  169. conns[i] = c
  170. }
  171. for _, c := range conns {
  172. c.Close()
  173. }
  174. var wg sync.WaitGroup
  175. wg.Add(numConcurrent)
  176. b.StartTimer()
  177. for i := 0; i < numConcurrent; i++ {
  178. go func() {
  179. defer wg.Done()
  180. for i := 0; i < b.N; i++ {
  181. c := p.Get()
  182. if _, err := c.Do("PING"); err != nil {
  183. b.Fatal(err)
  184. }
  185. c.Close()
  186. }
  187. }()
  188. }
  189. wg.Wait()
  190. }
  191. func BenchmarkPipelineConcurrency(b *testing.B) {
  192. b.StopTimer()
  193. c, err := redisx.DialTest()
  194. if err != nil {
  195. b.Fatalf("error connection to database, %v", err)
  196. }
  197. defer c.Close()
  198. var wg sync.WaitGroup
  199. wg.Add(numConcurrent)
  200. var pipeline textproto.Pipeline
  201. b.StartTimer()
  202. for i := 0; i < numConcurrent; i++ {
  203. go func() {
  204. defer wg.Done()
  205. for i := 0; i < b.N; i++ {
  206. id := pipeline.Next()
  207. pipeline.StartRequest(id)
  208. c.Send("PING")
  209. c.Flush()
  210. pipeline.EndRequest(id)
  211. pipeline.StartResponse(id)
  212. _, err := c.Receive()
  213. if err != nil {
  214. b.Fatal(err)
  215. }
  216. pipeline.EndResponse(id)
  217. }
  218. }()
  219. }
  220. wg.Wait()
  221. }