redis_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. package redis
  2. import (
  3. "encoding/json"
  4. "log"
  5. "math/rand"
  6. "strings"
  7. "testing"
  8. "time"
  9. "unsafe"
  10. //"qfw/util/redis"
  11. )
  12. func TestInit(t *testing.T) {
  13. // regAddr := regexp.MustCompile("[0-9.a-zA-Z/]+:[0-9]+.*")
  14. // log.Println(regAddr.MatchString("fdfsd.uyuyds.ii:2203&dsds"))
  15. r1 := &GoRedis{}
  16. // r1.Init("other=127.0.0.1:2203|127.0.0.1:2204=0-14=1-10=400")
  17. // r1.Init("other=127.0.0.1:2203=0-1=1-10=400")
  18. // r1.Init("other=127.0.0.1:2203=1=1")
  19. r1.Init("192.168.3.207:6379=0=2=600")
  20. res, err := r1.GetByPattern(`dd*`)
  21. log.Println(res, err)
  22. r1.BulkPut(120, map[string]interface{}{"a1": 1, "a2": "222", "a3": 456, "a4": "fdsfdsfds"})
  23. r1.BulkPut(220, []interface{}{[]interface{}{"b1", 1}, []interface{}{"b2", "222"}, []interface{}{"b3", 5666}})
  24. b, _ := json.Marshal(map[string]interface{}{"key": "值val1"})
  25. log.Println(b)
  26. b1 := make([]byte, 1000)
  27. strings.NewReader("aabbccdd").Read(b1)
  28. r1.Set("aabb", b1, 400)
  29. v, e := r1.GetBytes("aabb")
  30. log.Println("ffff", v, e)
  31. r1.Set("aaa", "ddddddd", 500)
  32. r1.SetExpire("aaa", 900)
  33. log.Println("exists", r1.Exists("aaa"))
  34. r1.Set("bbb", "ddddddd", 500)
  35. r1.Del("bbb")
  36. r1.Set("bbcc", "ddddddd", 500)
  37. r1.Set("bbcc1", "ddddddd", 500)
  38. r1.DelByPattern("bbcc*")
  39. r1.Incr("aa1")
  40. r1.Incr("aa1")
  41. r1.Incr("aa1")
  42. r1.Incr("aa1")
  43. r1.Decrby("aa1", 2)
  44. log.Println(hashCode("aaaaOppkdlk"))
  45. }
  46. func Test_bench(t *testing.T) {
  47. //r1.Init("192.168.3.207:6379=0=20=600")
  48. }
  49. /*
  50. 在700万以内只用1个库旧工具类速度快于新工具类
  51. */
  52. const letters = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  53. var src = rand.NewSource(time.Now().UnixNano())
  54. const (
  55. // 6 bits to represent a letter index
  56. letterIdBits = 6
  57. // All 1-bits as many as letterIdBits
  58. letterIdMask = 1<<letterIdBits - 1
  59. letterIdMax = 63 / letterIdBits
  60. )
  61. func randStr(n int) string {
  62. b := make([]byte, n)
  63. // A rand.Int63() generates 63 random bits, enough for letterIdMax letters!
  64. for i, cache, remain := n-1, src.Int63(), letterIdMax; i >= 0; {
  65. if remain == 0 {
  66. cache, remain = src.Int63(), letterIdMax
  67. }
  68. if idx := int(cache & letterIdMask); idx < len(letters) {
  69. b[i] = letters[idx]
  70. i--
  71. }
  72. cache >>= letterIdBits
  73. remain--
  74. }
  75. return *(*string)(unsafe.Pointer(&b))
  76. }
  77. // func Test_old_redis(t *testing.T) {
  78. // redis.InitRedis("other=127.0.0.1:6379")
  79. // chanKey := make(chan string, 50000)
  80. // go func() {
  81. // for i := 0; i < 2000000; i++ {
  82. // chanKey <- randStr(10)
  83. // }
  84. // }()
  85. // go func() {
  86. // ch := make(chan bool, 30)
  87. // t1 := time.Now().Unix()
  88. // for i := 0; i < 2000000; i++ {
  89. // k := <-chanKey
  90. // ch <- true
  91. // go func() {
  92. // defer func() {
  93. // <-ch
  94. // }()
  95. // redis.Put("other", k, 1, 864000)
  96. // }()
  97. // if i%20000 == 0 {
  98. // log.Println("current:", i)
  99. // }
  100. // }
  101. // log.Println("set", time.Now().Unix()-t1)
  102. // }()
  103. // select {}
  104. // }
  105. // func Test_old_redis_get(t *testing.T) {
  106. // redis.InitRedis("other=127.0.0.1:6379")
  107. // chanKey := make(chan string, 50000)
  108. // go func() {
  109. // for i := 0; i < 100000; i++ {
  110. // chanKey <- randStr(10)
  111. // }
  112. // }()
  113. // go func() {
  114. // ch := make(chan bool, 10)
  115. // t1 := time.Now().Unix()
  116. // for i := 0; i < 100000; i++ {
  117. // k := <-chanKey
  118. // ch <- true
  119. // go func() {
  120. // defer func() {
  121. // <-ch
  122. // }()
  123. // redis.Get("other", k)
  124. // }()
  125. // if i%20000 == 0 {
  126. // log.Println("current:", i)
  127. // }
  128. // }
  129. // log.Println("get", time.Now().Unix()-t1)
  130. // }()
  131. // select {}
  132. // }
  133. func Test_new_redis(t *testing.T) {
  134. r1 := &GoRedis{}
  135. r1.Init("127.0.0.1:6379=0-15=30=600")
  136. chanKey := make(chan string, 50000)
  137. go func() {
  138. for i := 0; i < 7000000; i++ {
  139. chanKey <- randStr(10)
  140. }
  141. }()
  142. go func() {
  143. ch := make(chan bool, 30)
  144. t1 := time.Now().Unix()
  145. for i := 0; i < 7000000; i++ {
  146. k := <-chanKey
  147. ch <- true
  148. go func() {
  149. defer func() {
  150. <-ch
  151. }()
  152. r1.Set(k, 1, 864000)
  153. }()
  154. if i%20000 == 0 {
  155. log.Println("current:", i)
  156. }
  157. }
  158. log.Println("set", time.Now().Unix()-t1)
  159. }()
  160. // go func() {
  161. // t1 := time.Now()
  162. // for i := 0; i < 1000000; i++ {
  163. // k := <-chanKey
  164. // r1.Get(k)
  165. // }
  166. // log.Println("get", time.Now()-t1)
  167. // }()
  168. select {}
  169. }
  170. func Test_new_redis_get(t *testing.T) {
  171. r1 := &GoRedis{}
  172. r1.Init("127.0.0.1:6379=0-15=30=600")
  173. chanKey := make(chan string, 50000)
  174. go func() {
  175. for i := 0; i < 100000; i++ {
  176. chanKey <- randStr(10)
  177. }
  178. }()
  179. go func() {
  180. ch := make(chan bool, 10)
  181. t1 := time.Now().Unix()
  182. for i := 0; i < 100000; i++ {
  183. k := <-chanKey
  184. ch <- true
  185. go func() {
  186. defer func() {
  187. <-ch
  188. }()
  189. r1.Get(k)
  190. }()
  191. if i%20000 == 0 {
  192. log.Println("current:", i)
  193. }
  194. }
  195. log.Println("set", time.Now().Unix()-t1)
  196. }()
  197. // go func() {
  198. // t1 := time.Now()
  199. // for i := 0; i < 1000000; i++ {
  200. // k := <-chanKey
  201. // r1.Get(k)
  202. // }
  203. // log.Println("get", time.Now()-t1)
  204. // }()
  205. select {}
  206. }