Go Redis篇

所有关于Golang的文章都只是作为记录而已.. 正在慢慢的学Go

使用的包

建立Redis连接池

package handler

import (
    "github.com/gomodule/redigo/redis"
)

func RdsPool(server, auth string) *redis.Pool {
    return &redis.Pool{
        //最大空闲
        MaxIdle: 100,
        //最大活动量
        MaxActive: 30,
        //空闲超时
        IdleTimeout: 60 * time.Second,
        Dial: func() (redis.Conn, error) {
            c, err := redis.Dial("tcp", server)
            if err != nil {
                return nil, err
            }
            if auth == "" {
                return c, err
            }
            if _, err := c.Do("AUTH", auth); err != nil {
                c.Close()
                return nil, err
            }
            return c, err
        },
        TestOnBorrow: func(c redis.Conn, t time.Time) error {
            _, err := c.Do("PING")
            return err
        },
    }
}

连接池 VS 不用连接池

不使用连接池

rd := g.Group("/redis")
    {
        rd.GET("/set", func(context *gin.Context) {
            runtime.GOMAXPROCS(runtime.NumCPU())
            //普通方式操作Redis
            //连接redis
            normalRd, _ := redis.Dial("tcp", "localhost:6379")
            _, _ = normalRd.Do("SET", "username", "nick")
            username, _ := redis.String(normalRd.Do("GET", "username"))
            _, _ = normalRd.Do("DEL", "username")
            //读取key
            defer normalRd.Close()
            SendResponse(context, nil, username)
        })
    }

ab结果

Server Software:
Server Hostname:        127.0.0.1
Server Port:            9999

Document Path:          /redis/set
Document Length:        39 bytes

Concurrency Level:      4
Time taken for tests:   1.138 seconds
Complete requests:      2000
Failed requests:        32
   (Connect: 0, Receive: 0, Length: 32, Exceptions: 0)
Total transferred:      877872 bytes
HTML transferred:       77872 bytes
Requests per second:    1756.90 [#/sec] (mean)
Time per request:       2.277 [ms] (mean)
Time per request:       0.569 [ms] (mean, across all concurrent requests)
Transfer rate:          753.09 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       2
Processing:     1    2   1.2      2      24
Waiting:        1    2   1.2      2      24
Total:          1    2   1.2      2      25

Percentage of the requests served within a certain time (ms)
  50%      2
  66%      2
  75%      2
  80%      3
  90%      3
  95%      3
  98%      4
  99%      5
 100%     25 (longest request)

连接池版本

rd := g.Group("/redis")
    {
        rd.GET("/set", func(context *gin.Context) {
            runtime.GOMAXPROCS(runtime.NumCPU())
            //连接池操作Redis
            var poolRc redis.Conn = RdsPool(`127.0.0.1:6379`, ``).Get()
            //写入
            _, _ = poolRc.Do("SET", "username", "nick")
            //读取
            username, _ := redis.String(poolRc.Do("GET", "username"))
            //删除
            _, _ = poolRc.Do("DEL", "username")
            defer poolRc.Close()
            SendResponse(context, nil, username)
        })
    }

ab结果

Concurrency Level:      4
Time taken for tests:   0.674 seconds
Complete requests:      2000
Failed requests:        337
   (Connect: 0, Receive: 0, Length: 337, Exceptions: 0)
Total transferred:      876652 bytes
HTML transferred:       76652 bytes
Requests per second:    2966.56 [#/sec] (mean)
Time per request:       1.348 [ms] (mean)
Time per request:       0.337 [ms] (mean, across all concurrent requests)
Transfer rate:          1269.85 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.2      0       4
Processing:     0    1   0.7      1       9
Waiting:        0    1   0.7      1       9
Total:          1    1   0.8      1      10

Percentage of the requests served within a certain time (ms)
  50%      1
  66%      1
  75%      1
  80%      1
  90%      2
  95%      3
  98%      4
  99%      4
 100%     10 (longest request)

其实效果还是蛮明显的..因为我本地电脑的问题 request太多会卡... 所以就只给了 2000 个请求

参考文章