Skip to content

Commit

Permalink
docs: upload bench image
Browse files Browse the repository at this point in the history
  • Loading branch information
xgzlucario committed Aug 3, 2024
1 parent 4de4c40 commit 5483c91
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 32 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
coverage.*
*.aof

redis
rotom
30 changes: 21 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,37 @@ Rotom has made several optimizations in data structures:

Notably, `zipmap` and `zipset` are space-efficient data structures based on `listpack`, which is a new compressed list proposed by Redis to replace `ziplist`, supporting both forward and reverse traversal and solving the cascading update issue in `ziplist`.

## Roadmap

- Support for LRU cache and memory eviction.
- Support for gradual rehashing in dict.
- Support for RDB and AOF Rewrite.
- Compatibility with more commonly used commands.

## Benchmark

![img](bench.jpg)

The test will run rotom on the same machine with `appendonly` disabled, and use `redis-benchmark` tool to test the latency of different commands.

```
goos: linux
goarch: amd64
pkg: github.com/xgzlucario/rotom
cpu: Intel(R) Core(TM) i7-10700 CPU @ 2.90GHz
cpu: 13th Gen Intel(R) Core(TM) i5-13600KF
```

![img](bench.jpg)
```
redis rotom redis_P10 rotom_P10 redis_P50 rotom_P50
SET 268817 268817 2222222 2173913 3448276 5263158
GET 265957 259740 2702702 1818181 4347826 4545454
INCR 271739 261780 2500000 2439024 4347826 7692307
LPUSH 289017 282485 2083333 2272727 2941176 4347826
RPUSH 283286 271739 2272727 2439024 3333333 7692307
SADD 273972 269541 2439024 2631579 4000000 7142857
HSET 282485 277777 2000000 2127659 3030303 3703703
ZADD 273224 272479 1960784 2702702 2941176 6249999
```

## Roadmap

- Support for LRU cache and memory eviction.
- Support for gradual rehashing in dict.
- Support for RDB and AOF Rewrite.
- Compatibility with more commonly used commands.

## Usage

Expand Down
30 changes: 21 additions & 9 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,37 @@ rotom 在数据结构上做了许多优化:

值得一提的是,`zipmap``zipset` 是空间紧凑的数据结构,它们都基于 `listpack`, 这是 Redis 提出的替代 `ziplist` 的新型压缩列表,支持正序及逆序遍历,解决了 `ziplist` 存在级联更新的问题。

## 计划

- LRU 缓存及内存淘汰支持
- dict 渐进式哈希支持
- RDB 及 AOF Rewrite 支持
- 兼容更多常用命令

## 性能

![img](bench.jpg)

测试将在同一台机器上,关闭 `appendonly`,并使用 `redis-benchmark` 工具测试不同命令的 qps。

```
goos: linux
goarch: amd64
pkg: github.com/xgzlucario/rotom
cpu: Intel(R) Core(TM) i7-10700 CPU @ 2.90GHz
cpu: 13th Gen Intel(R) Core(TM) i5-13600KF
```

![img](bench.jpg)
```
redis rotom redis_P10 rotom_P10 redis_P50 rotom_P50
SET 268817 268817 2222222 2173913 3448276 5263158
GET 265957 259740 2702702 1818181 4347826 4545454
INCR 271739 261780 2500000 2439024 4347826 7692307
LPUSH 289017 282485 2083333 2272727 2941176 4347826
RPUSH 283286 271739 2272727 2439024 3333333 7692307
SADD 273972 269541 2439024 2631579 4000000 7142857
HSET 282485 277777 2000000 2127659 3030303 3703703
ZADD 273224 272479 1960784 2702702 2941176 6249999
```

## 计划

- LRU 缓存及内存淘汰支持
- dict 渐进式哈希支持
- RDB 及 AOF Rewrite 支持
- 兼容更多常用命令

## 使用

Expand Down
Binary file modified bench.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions bench.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash

TEST_NAME=$1

IMAGE_NAME=$TEST_NAME

OUTPUT_FILE="output/$TEST_NAME"

COMMANDS="set,get,incr,lpush,rpush,hset,sadd,zadd"

PIPELINES=(1 10 50)

mkdir -p output

# clear output file
> $OUTPUT_FILE

docker run --rm -d --name bench-test $IMAGE_NAME
sleep 3

# run bench
for pipeline in "${PIPELINES[@]}"; do
echo "Testing with pipeline: $pipeline" | tee -a $OUTPUT_FILE
docker exec bench-test redis-benchmark --csv -t $COMMANDS -P $pipeline | tee -a $OUTPUT_FILE
echo "" | tee -a $OUTPUT_FILE
done

docker stop bench-test

echo "Benchmarking completed. Results are saved in $OUTPUT_FILE."
15 changes: 7 additions & 8 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func delCommand(writer *RESPWriter, args []RESP) {
}

func hsetCommand(writer *RESPWriter, args []RESP) {
hash := args[0].ToString()
hash := args[0].ToStringUnsafe()
args = args[1:]

if len(args)%2 == 1 {
Expand Down Expand Up @@ -228,13 +228,11 @@ func hdelCommand(writer *RESPWriter, args []RESP) {

func hgetallCommand(writer *RESPWriter, args []RESP) {
hash := args[0].ToStringUnsafe()

hmap, err := fetchMap(hash)
if err != nil {
writer.WriteError(err)
return
}

writer.WriteArrayHead(hmap.Len() * 2)
hmap.Scan(func(key string, value []byte) {
writer.WriteBulkString(key)
Expand All @@ -243,7 +241,7 @@ func hgetallCommand(writer *RESPWriter, args []RESP) {
}

func lpushCommand(writer *RESPWriter, args []RESP) {
key := args[0].ToString()
key := args[0].ToStringUnsafe()
ls, err := fetchList(key, true)
if err != nil {
writer.WriteError(err)
Expand All @@ -256,7 +254,7 @@ func lpushCommand(writer *RESPWriter, args []RESP) {
}

func rpushCommand(writer *RESPWriter, args []RESP) {
key := args[0].ToString()
key := args[0].ToStringUnsafe()
ls, err := fetchList(key, true)
if err != nil {
writer.WriteError(err)
Expand Down Expand Up @@ -328,7 +326,7 @@ func lrangeCommand(writer *RESPWriter, args []RESP) {
}

func saddCommand(writer *RESPWriter, args []RESP) {
key := args[0].ToString()
key := args[0].ToStringUnsafe()
args = args[1:]

set, err := fetchSet(key, true)
Expand Down Expand Up @@ -382,7 +380,7 @@ func spopCommand(writer *RESPWriter, args []RESP) {
}

func zaddCommand(writer *RESPWriter, args []RESP) {
key := args[0].ToString()
key := args[0].ToStringUnsafe()
args = args[1:]

zset, err := fetchZSet(key, true)
Expand Down Expand Up @@ -564,7 +562,8 @@ func fetch[T any](key string, new func() T, setnx ...bool) (T, error) {

v := new()
if len(setnx) > 0 && setnx[0] {
db.dict.Set(key, v)
// make sure `key` is copy
db.dict.Set(strings.Clone(key), v)
}

return v, nil
Expand Down
12 changes: 7 additions & 5 deletions internal/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import "math"
// QuickList is double linked listpack, implement redis quicklist data structure,
// based on listpack rather than ziplist to optimize cascade update.
type QuickList struct {
size int
head, tail *Node
}

Expand All @@ -36,6 +37,7 @@ func (ls *QuickList) LPush(key string) {
ls.head.prev = n
ls.head = n
}
ls.size++
ls.head.LPush(key)
}

Expand All @@ -47,13 +49,15 @@ func (ls *QuickList) RPush(key string) {
n.prev = ls.tail
ls.tail = n
}
ls.size++
ls.tail.RPush(key)
}

// LPop
func (ls *QuickList) LPop() (key string, ok bool) {
for lp := ls.head; lp != nil; lp = lp.next {
if lp.size > 0 {
ls.size--
return lp.LPop()
}
ls.free(lp)
Expand All @@ -65,6 +69,7 @@ func (ls *QuickList) LPop() (key string, ok bool) {
func (ls *QuickList) RPop() (key string, ok bool) {
for lp := ls.tail; lp != nil; lp = lp.prev {
if lp.size > 0 {
ls.size--
return lp.RPop()
}
ls.free(lp)
Expand All @@ -82,11 +87,8 @@ func (ls *QuickList) free(n *Node) {
}
}

func (ls *QuickList) Size() (n int) {
for lp := ls.head; lp != nil; lp = lp.next {
n += lp.Size()
}
return
func (ls *QuickList) Size() int {
return ls.size
}

func (ls *QuickList) Range(start, end int, f func(data []byte)) {
Expand Down
2 changes: 1 addition & 1 deletion internal/list/listpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

const (
maxListPackSize = 8 * 1024
maxListPackSize = 16 * 1024
)

var (
Expand Down
9 changes: 9 additions & 0 deletions output/bench.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
redis rotom redis_P10 rotom_P10 redis_P50 rotom_P50
SET 268817 268817 2222222 2173913 3448276 5263158
GET 265957 259740 2702702 1818181 4347826 4545454
INCR 271739 261780 2500000 2439024 4347826 7692307
LPUSH 289017 282485 2083333 2272727 2941176 4347826
RPUSH 283286 271739 2272727 2439024 3333333 7692307
SADD 273972 269541 2439024 2631579 4000000 7142857
HSET 282485 277777 2000000 2127659 3030303 3703703
ZADD 273224 272479 1960784 2702702 2941176 6249999

0 comments on commit 5483c91

Please sign in to comment.