Rewrite benchmarks using std testing lib.

This commit is contained in:
Vladimir Mihailenco 2014-07-13 15:01:52 +03:00
parent 3c565a35a9
commit aff229a228
1 changed files with 77 additions and 40 deletions

View File

@ -3099,80 +3099,117 @@ func (t *RedisTest) TestCmdDebugObject(c *C) {
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
func (t *RedisTest) BenchmarkRedisPing(c *C) { func BenchmarkRedisPing(b *testing.B) {
for i := 0; i < c.N; i++ { b.StopTimer()
if err := t.client.Ping().Err(); err != nil { client := redis.NewTCPClient(&redis.Options{
Addr: redisAddr,
})
b.StartTimer()
for i := 0; i < b.N; i++ {
if err := client.Ping().Err(); err != nil {
panic(err) panic(err)
} }
} }
} }
func (t *RedisTest) BenchmarkRedisSet(c *C) { func BenchmarkRedisSet(b *testing.B) {
for i := 0; i < c.N; i++ { b.StopTimer()
if err := t.client.Set("key", "hello").Err(); err != nil { client := redis.NewTCPClient(&redis.Options{
Addr: redisAddr,
})
b.StartTimer()
for i := 0; i < b.N; i++ {
if err := client.Set("key", "hello").Err(); err != nil {
panic(err) panic(err)
} }
} }
} }
func (t *RedisTest) BenchmarkRedisGetNil(c *C) { func BenchmarkRedisGetNil(b *testing.B) {
for i := 0; i < c.N; i++ { b.StopTimer()
if err := t.client.Get("key").Err(); err != redis.Nil { client := redis.NewTCPClient(&redis.Options{
panic(err) Addr: redisAddr,
})
if err := client.FlushDb().Err(); err != nil {
b.Fatal(err)
}
b.StartTimer()
for i := 0; i < b.N; i++ {
if err := client.Get("key").Err(); err != redis.Nil {
b.Fatal(err)
} }
} }
} }
func (t *RedisTest) BenchmarkRedisGet(c *C) { func BenchmarkRedisGet(b *testing.B) {
c.StopTimer() b.StopTimer()
client := redis.NewTCPClient(&redis.Options{
Addr: redisAddr,
})
if err := client.Set("key", "hello").Err(); err != nil {
b.Fatal(err)
}
b.StartTimer()
set := t.client.Set("key", "hello") for i := 0; i < b.N; i++ {
c.Assert(set.Err(), IsNil) if err := client.Get("key").Err(); err != nil {
b.Fatal(err)
c.StartTimer()
for i := 0; i < c.N; i++ {
if err := t.client.Get("key").Err(); err != nil {
panic(err)
} }
} }
} }
func (t *RedisTest) BenchmarkRedisMGet(c *C) { func BenchmarkRedisMGet(b *testing.B) {
c.StopTimer() b.StopTimer()
client := redis.NewTCPClient(&redis.Options{
Addr: redisAddr,
})
if err := client.MSet("key1", "hello1", "key2", "hello2").Err(); err != nil {
b.Fatal(err)
}
b.StartTimer()
mset := t.client.MSet("key1", "hello1", "key2", "hello2") for i := 0; i < b.N; i++ {
c.Assert(mset.Err(), IsNil) if err := client.MGet("key1", "key2").Err(); err != nil {
b.Fatal(err)
c.StartTimer()
for i := 0; i < c.N; i++ {
if err := t.client.MGet("key1", "key2").Err(); err != nil {
panic(err)
} }
} }
} }
func (t *RedisTest) BenchmarkSetExpire(c *C) { func BenchmarkSetExpire(b *testing.B) {
for i := 0; i < c.N; i++ { b.StopTimer()
if err := t.client.Set("key", "hello").Err(); err != nil { client := redis.NewTCPClient(&redis.Options{
panic(err) Addr: redisAddr,
})
b.StartTimer()
for i := 0; i < b.N; i++ {
if err := client.Set("key", "hello").Err(); err != nil {
b.Fatal(err)
} }
if err := t.client.Expire("key", time.Second).Err(); err != nil { if err := client.Expire("key", time.Second).Err(); err != nil {
panic(err) b.Fatal(err)
} }
} }
} }
func (t *RedisTest) BenchmarkPipeline(c *C) { func BenchmarkPipeline(b *testing.B) {
for i := 0; i < c.N; i++ { b.StopTimer()
_, err := t.client.Pipelined(func(pipe *redis.Pipeline) error { client := redis.NewTCPClient(&redis.Options{
Addr: redisAddr,
})
b.StartTimer()
for i := 0; i < b.N; i++ {
_, err := client.Pipelined(func(pipe *redis.Pipeline) error {
pipe.Set("key", "hello") pipe.Set("key", "hello")
pipe.Expire("key", time.Second) pipe.Expire("key", time.Second)
return nil return nil
}) })
if err != nil { if err != nil {
panic(err) b.Fatal(err)
} }
} }
} }