Skip to content

Instantly share code, notes, and snippets.

@ken39arg
Created June 8, 2021 09:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ken39arg/92f5a505d72b9995d399d78ab5d74d80 to your computer and use it in GitHub Desktop.
Save ken39arg/92f5a505d72b9995d399d78ab5d74d80 to your computer and use it in GitHub Desktop.
package main
import (
"context"
"crypto/sha1"
"encoding/hex"
"fmt"
"hash/crc32"
"log"
"math/rand"
"os"
"testing"
"time"
"github.com/go-redis/redis/v8"
)
var cli redis.UniversalClient
func init() {
cli = redis.NewClient(&redis.Options{
Addr: os.Getenv("REDIS_ADDRESS"),
})
rand.Seed(time.Now().UnixNano())
}
func set(key string) {
if err := cli.Set(context.Background(), key, 123, time.Second).Err(); err != nil {
log.Printf("set err: %s", err)
}
}
func get(key string) {
if _, err := cli.Get(context.Background(), key).Int(); err != nil && err != redis.Nil {
log.Printf("get err: %s", err)
}
}
const letterBytes = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
var letterSize = len(letterBytes)
var sizes = []int{32, 64, 128, 256, 512, 1024, 2048, 4096, 8192}
func genkey(size int) string {
r := make([]byte, size/2)
rand.Read(r)
return hex.EncodeToString(r)
}
func TestGenKey(t *testing.T) {
for _, size := range sizes {
key := genkey(size)
if len(key) != size {
t.Errorf("key size is differ")
}
}
}
func Benchmark_keygenenrate(b *testing.B) {
for _, size := range sizes {
b.Run(fmt.Sprintf("%04d", size), func(b *testing.B) {
for i := 0; i < b.N; i++ {
genkey(size)
}
})
}
}
func benchMark(b *testing.B, keyfilter func(string) string) {
log.Printf("generated: %s", keyfilter(genkey(128)))
for _, size := range sizes {
b.ResetTimer()
b.Run(fmt.Sprintf("%04d", size), func(b *testing.B) {
for i := 0; i < b.N; i++ {
key := genkey(size)
get(keyfilter(key))
set(keyfilter(key))
get(keyfilter(key))
}
})
}
}
func Benchmark_RedisKey_Raw(b *testing.B) {
benchMark(b, func(s string) string { return s })
}
func Benchmark_RedisKey_SHA1(b *testing.B) {
benchMark(b, func(s string) string {
h := sha1.New()
h.Write([]byte(s))
return hex.EncodeToString(h.Sum(nil))
})
}
func Benchmark_RedisKey_CRC32(b *testing.B) {
benchMark(b, func(s string) string {
h := crc32.NewIEEE()
h.Write([]byte(s))
return hex.EncodeToString(h.Sum(nil))
})
}
@ken39arg
Copy link
Author

ken39arg commented Jun 9, 2021

goos: darwin
goarch: amd64
Benchmark_keygenenrate/0032-4            9843356               148 ns/op              80 B/op          3 allocs/op
Benchmark_keygenenrate/0064-4            7310480               192 ns/op             160 B/op          3 allocs/op
Benchmark_keygenenrate/0128-4            4781851               304 ns/op             320 B/op          3 allocs/op
Benchmark_keygenenrate/0256-4            2806218               421 ns/op             640 B/op          3 allocs/op
Benchmark_keygenenrate/0512-4            1535560               887 ns/op            1280 B/op          3 allocs/op
Benchmark_keygenenrate/1024-4             819591              1446 ns/op            2560 B/op          3 allocs/op
Benchmark_keygenenrate/2048-4             423406              2873 ns/op            5120 B/op          3 allocs/op
Benchmark_keygenenrate/4096-4             197949              5691 ns/op           10240 B/op          3 allocs/op
Benchmark_keygenenrate/8192-4             103411             11214 ns/op           20480 B/op          3 allocs/op
2021/06/08 18:54:49 generated: fc6a0493af52e261885782d2282caad302875b414f72b3925144f5c14327405d87b7f6dffd0305e227b5577cb7fc3f6b99891c54de27a7812c4e47c32a4b2400
Benchmark_RedisKey_Raw/0032-4                282           4200548 ns/op             729 B/op         20 allocs/op
Benchmark_RedisKey_Raw/0064-4                330           4942915 ns/op             808 B/op         20 allocs/op
Benchmark_RedisKey_Raw/0128-4                258           7252478 ns/op             968 B/op         20 allocs/op
Benchmark_RedisKey_Raw/0256-4                332           4630280 ns/op            1288 B/op         20 allocs/op
Benchmark_RedisKey_Raw/0512-4                262           5903465 ns/op            1928 B/op         20 allocs/op
Benchmark_RedisKey_Raw/1024-4                294           4091465 ns/op            3208 B/op         20 allocs/op
Benchmark_RedisKey_Raw/2048-4                218           6021055 ns/op            5768 B/op         20 allocs/op
Benchmark_RedisKey_Raw/4096-4                183          12380521 ns/op           10888 B/op         20 allocs/op
Benchmark_RedisKey_Raw/8192-4                205           5023882 ns/op           21128 B/op         20 allocs/op
2021/06/08 18:55:08 generated: a16f55c1ffa1b1e8f02ac207846f12d14e54f024
Benchmark_RedisKey_SHA1/0032-4               332           4013168 ns/op            1544 B/op         35 allocs/op
Benchmark_RedisKey_SHA1/0064-4               309           3872273 ns/op            1720 B/op         35 allocs/op
Benchmark_RedisKey_SHA1/0128-4               310           3715572 ns/op            2072 B/op         35 allocs/op
Benchmark_RedisKey_SHA1/0256-4               331           3553156 ns/op            2776 B/op         35 allocs/op
Benchmark_RedisKey_SHA1/0512-4               370           3290478 ns/op            4184 B/op         35 allocs/op
Benchmark_RedisKey_SHA1/1024-4               288           3608456 ns/op            7000 B/op         35 allocs/op
Benchmark_RedisKey_SHA1/2048-4               348           4881821 ns/op           12632 B/op         35 allocs/op
Benchmark_RedisKey_SHA1/4096-4               324           3932689 ns/op           23896 B/op         35 allocs/op
Benchmark_RedisKey_SHA1/8192-4                99          12260017 ns/op           46425 B/op         35 allocs/op
2021/06/08 18:55:24 generated: 68b338cf
Benchmark_RedisKey_CRC32/0032-4              356           4136960 ns/op             952 B/op         35 allocs/op
Benchmark_RedisKey_CRC32/0064-4              285           3800490 ns/op            1128 B/op         35 allocs/op
Benchmark_RedisKey_CRC32/0128-4              283           3799783 ns/op            1480 B/op         35 allocs/op
Benchmark_RedisKey_CRC32/0256-4              308           4030056 ns/op            2184 B/op         35 allocs/op
Benchmark_RedisKey_CRC32/0512-4              320           4404797 ns/op            3592 B/op         35 allocs/op
Benchmark_RedisKey_CRC32/1024-4              314           3623127 ns/op            6408 B/op         35 allocs/op
Benchmark_RedisKey_CRC32/2048-4              290           3733290 ns/op           12040 B/op         35 allocs/op
Benchmark_RedisKey_CRC32/4096-4              320           3720955 ns/op           23304 B/op         35 allocs/op
Benchmark_RedisKey_CRC32/8192-4              292           3830472 ns/op           45832 B/op         35 allocs/op
PASS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment