123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- package redis
- import (
- "encoding/json"
- "log"
- "math/rand"
- "strings"
- "testing"
- "time"
- "unsafe"
- //"qfw/util/redis"
- )
- func TestInit(t *testing.T) {
- // regAddr := regexp.MustCompile("[0-9.a-zA-Z/]+:[0-9]+.*")
- // log.Println(regAddr.MatchString("fdfsd.uyuyds.ii:2203&dsds"))
- r1 := &GoRedis{}
- // r1.Init("other=127.0.0.1:2203|127.0.0.1:2204=0-14=1-10=400")
- // r1.Init("other=127.0.0.1:2203=0-1=1-10=400")
- // r1.Init("other=127.0.0.1:2203=1=1")
- r1.Init("192.168.3.207:6379=0=2=600")
- res, err := r1.GetByPattern(`dd*`)
- log.Println(res, err)
- r1.BulkPut(120, map[string]interface{}{"a1": 1, "a2": "222", "a3": 456, "a4": "fdsfdsfds"})
- r1.BulkPut(220, []interface{}{[]interface{}{"b1", 1}, []interface{}{"b2", "222"}, []interface{}{"b3", 5666}})
- b, _ := json.Marshal(map[string]interface{}{"key": "值val1"})
- log.Println(b)
- b1 := make([]byte, 1000)
- strings.NewReader("aabbccdd").Read(b1)
- r1.Set("aabb", b1, 400)
- v, e := r1.GetBytes("aabb")
- log.Println("ffff", v, e)
- r1.Set("aaa", "ddddddd", 500)
- r1.SetExpire("aaa", 900)
- log.Println("exists", r1.Exists("aaa"))
- r1.Set("bbb", "ddddddd", 500)
- r1.Del("bbb")
- r1.Set("bbcc", "ddddddd", 500)
- r1.Set("bbcc1", "ddddddd", 500)
- r1.DelByPattern("bbcc*")
- r1.Incr("aa1")
- r1.Incr("aa1")
- r1.Incr("aa1")
- r1.Incr("aa1")
- r1.Decrby("aa1", 2)
- log.Println(hashCode("aaaaOppkdlk"))
- }
- func Test_bench(t *testing.T) {
- //r1.Init("192.168.3.207:6379=0=20=600")
- }
- /*
- 在700万以内只用1个库旧工具类速度快于新工具类
- */
- const letters = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
- var src = rand.NewSource(time.Now().UnixNano())
- const (
- // 6 bits to represent a letter index
- letterIdBits = 6
- // All 1-bits as many as letterIdBits
- letterIdMask = 1<<letterIdBits - 1
- letterIdMax = 63 / letterIdBits
- )
- func randStr(n int) string {
- b := make([]byte, n)
- // A rand.Int63() generates 63 random bits, enough for letterIdMax letters!
- for i, cache, remain := n-1, src.Int63(), letterIdMax; i >= 0; {
- if remain == 0 {
- cache, remain = src.Int63(), letterIdMax
- }
- if idx := int(cache & letterIdMask); idx < len(letters) {
- b[i] = letters[idx]
- i--
- }
- cache >>= letterIdBits
- remain--
- }
- return *(*string)(unsafe.Pointer(&b))
- }
- // func Test_old_redis(t *testing.T) {
- // redis.InitRedis("other=127.0.0.1:6379")
- // chanKey := make(chan string, 50000)
- // go func() {
- // for i := 0; i < 2000000; i++ {
- // chanKey <- randStr(10)
- // }
- // }()
- // go func() {
- // ch := make(chan bool, 30)
- // t1 := time.Now().Unix()
- // for i := 0; i < 2000000; i++ {
- // k := <-chanKey
- // ch <- true
- // go func() {
- // defer func() {
- // <-ch
- // }()
- // redis.Put("other", k, 1, 864000)
- // }()
- // if i%20000 == 0 {
- // log.Println("current:", i)
- // }
- // }
- // log.Println("set", time.Now().Unix()-t1)
- // }()
- // select {}
- // }
- // func Test_old_redis_get(t *testing.T) {
- // redis.InitRedis("other=127.0.0.1:6379")
- // chanKey := make(chan string, 50000)
- // go func() {
- // for i := 0; i < 100000; i++ {
- // chanKey <- randStr(10)
- // }
- // }()
- // go func() {
- // ch := make(chan bool, 10)
- // t1 := time.Now().Unix()
- // for i := 0; i < 100000; i++ {
- // k := <-chanKey
- // ch <- true
- // go func() {
- // defer func() {
- // <-ch
- // }()
- // redis.Get("other", k)
- // }()
- // if i%20000 == 0 {
- // log.Println("current:", i)
- // }
- // }
- // log.Println("get", time.Now().Unix()-t1)
- // }()
- // select {}
- // }
- func Test_new_redis(t *testing.T) {
- r1 := &GoRedis{}
- r1.Init("127.0.0.1:6379=0-15=30=600")
- chanKey := make(chan string, 50000)
- go func() {
- for i := 0; i < 7000000; i++ {
- chanKey <- randStr(10)
- }
- }()
- go func() {
- ch := make(chan bool, 30)
- t1 := time.Now().Unix()
- for i := 0; i < 7000000; i++ {
- k := <-chanKey
- ch <- true
- go func() {
- defer func() {
- <-ch
- }()
- r1.Set(k, 1, 864000)
- }()
- if i%20000 == 0 {
- log.Println("current:", i)
- }
- }
- log.Println("set", time.Now().Unix()-t1)
- }()
- // go func() {
- // t1 := time.Now()
- // for i := 0; i < 1000000; i++ {
- // k := <-chanKey
- // r1.Get(k)
- // }
- // log.Println("get", time.Now()-t1)
- // }()
- select {}
- }
- func Test_new_redis_get(t *testing.T) {
- r1 := &GoRedis{}
- r1.Init("127.0.0.1:6379=0-15=30=600")
- chanKey := make(chan string, 50000)
- go func() {
- for i := 0; i < 100000; i++ {
- chanKey <- randStr(10)
- }
- }()
- go func() {
- ch := make(chan bool, 10)
- t1 := time.Now().Unix()
- for i := 0; i < 100000; i++ {
- k := <-chanKey
- ch <- true
- go func() {
- defer func() {
- <-ch
- }()
- r1.Get(k)
- }()
- if i%20000 == 0 {
- log.Println("current:", i)
- }
- }
- log.Println("set", time.Now().Unix()-t1)
- }()
- // go func() {
- // t1 := time.Now()
- // for i := 0; i < 1000000; i++ {
- // k := <-chanKey
- // r1.Get(k)
- // }
- // log.Println("get", time.Now()-t1)
- // }()
- select {}
- }
|