Example #1
0
func BenchmarkRedisPool(b *testing.B) {
	b.StopTimer()
	p := redis.Pool{
		Dial: func() (redis.Conn, error) {
			//			c, err := redis.Dial("tcp", cfg.RedisAddr())
			c, err := redis.Dial("tcp", ":6379")
			if err != nil {
				return nil, err
			}
			// 选择db
			c.Do("SELECT", 11)
			return c, nil
		},

		MaxIdle:   30,
		MaxActive: 30}
	c := p.Get()
	if err := c.Err(); err != nil {
		b.Fatal(err)
	}
	c.Close()
	defer p.Close()
	//	c = p.Get()
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		c = p.Get()
		if _, err := c.Do("PING"); err != nil {
			b.Fatal(err)
		}
		c.Close()
	}
	b.StopTimer()
	c.Close()

}
Example #2
0
func BenchmarkPoolGet(b *testing.B) {
	b.StopTimer()
	p := redis.Pool{Dial: redistest.Dial, MaxIdle: 2}
	c := p.Get()
	if err := c.Err(); err != nil {
		b.Fatal(err)
	}
	c.Close()
	defer p.Close()
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		c = p.Get()
		c.Close()
	}
}
Example #3
0
func BenchmarkPoolGetPing(b *testing.B) {
	b.StopTimer()
	p := redis.Pool{Dial: redis.DialDefaultServer, MaxIdle: 2}
	c := p.Get()
	if err := c.Err(); err != nil {
		b.Fatal(err)
	}
	c.Close()
	defer p.Close()
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		c = p.Get()
		if _, err := c.Do("PING"); err != nil {
			b.Fatal(err)
		}
		c.Close()
	}
}
Example #4
0
func BenchmarkPoolConcurrent(b *testing.B) {
	b.StopTimer()

	p := redis.Pool{Dial: redistest.Dial, MaxIdle: numConcurrent}
	defer p.Close()

	// Fill the pool.
	conns := make([]redis.Conn, numConcurrent)
	for i := range conns {
		c := p.Get()
		if err := c.Err(); err != nil {
			b.Fatal(err)
		}
		conns[i] = c
	}
	for _, c := range conns {
		c.Close()
	}

	var wg sync.WaitGroup
	wg.Add(numConcurrent)

	b.StartTimer()

	for i := 0; i < numConcurrent; i++ {
		go func() {
			defer wg.Done()
			for i := 0; i < b.N; i++ {
				c := p.Get()
				if _, err := c.Do("PING"); err != nil {
					b.Fatal(err)
				}
				c.Close()
			}
		}()
	}
	wg.Wait()
}
Example #5
0
func BenchmarkPool(b *testing.B) {
	b.StopTimer()

	p := redis.Pool{Dial: redistest.Dial, MaxIdle: 1}
	defer p.Close()

	// Fill the pool.
	c := p.Get()
	if err := c.Err(); err != nil {
		b.Fatal(err)
	}
	c.Close()

	b.StartTimer()

	for i := 0; i < b.N; i++ {
		c := p.Get()
		if _, err := c.Do("PING"); err != nil {
			b.Fatal(err)
		}
		c.Close()
	}
}