Skip to content

Commit 035fce1

Browse files
Added unit and integration tests (#26)
* Added unit and integration tests * Fixed docker image name * Updated go.mod to 1.20 * Merging coverage data from unit and integration tests * Uploading coverage report to codecov
1 parent 99f6cbe commit 035fce1

File tree

9 files changed

+290
-729
lines changed

9 files changed

+290
-729
lines changed

.github/workflows/build.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
on:
2+
push:
3+
branches: [master]
4+
pull_request:
5+
branches: [master]
6+
7+
permissions:
8+
contents: read
9+
10+
jobs:
11+
build:
12+
name: build
13+
runs-on: ubuntu-latest
14+
strategy:
15+
fail-fast: false
16+
matrix:
17+
go-version: [1.20.x, 1.21.x]
18+
19+
services:
20+
redis:
21+
image: redis
22+
options: >-
23+
--health-cmd "redis-cli ping" --health-interval 10s --health-timeout 5s --health-retries 5
24+
ports:
25+
- 6379:6379
26+
27+
steps:
28+
- name: Set up ${{ matrix.go-version }}
29+
uses: actions/setup-go@v4
30+
with:
31+
go-version: ${{ matrix.go-version }}
32+
33+
- name: Checkout code
34+
uses: actions/checkout@v4
35+
36+
- name: Test
37+
run: make test
38+
39+
- name: Upload coverage to Codecov
40+
uses: codecov/codecov-action@v4-beta
41+

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ redis-benchmark-go
44
dist
55
*.json
66
dump.rdb
7+
.coverdata

Makefile

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ endif
2323
.PHONY: all test coverage
2424
all: test build
2525

26+
build-coverage:
27+
$(GOBUILD) -cover \
28+
-ldflags="-X 'main.GitSHA1=$(GIT_SHA)' -X 'main.GitDirty=$(GIT_DIRTY)'" .
29+
2630
build:
2731
$(GOBUILD) \
2832
-ldflags="-X 'main.GitSHA1=$(GIT_SHA)' -X 'main.GitDirty=$(GIT_DIRTY)'" .
@@ -47,13 +51,12 @@ lint:
4751
get:
4852
$(GOGET) -t -v ./...
4953

50-
test: get
51-
$(GOFMT) ./...
52-
$(GOTEST) -race -covermode=atomic ./...
53-
54-
coverage: get test
55-
$(GOTEST) -race -coverprofile=coverage.txt -covermode=atomic .
56-
54+
test: get build-coverage
55+
@rm -fr .coverdata
56+
@mkdir -p .coverdata
57+
@go test -cover -args -test.gocoverdir=".coverdata" .
58+
@go tool covdata percent -i=.coverdata
59+
@go tool covdata textfmt -i=.coverdata -o coverage.txt
5760

5861
release:
5962
$(GOGET) github.com/mitchellh/gox

cli.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ func sample(cdf []float32) int {
2828
return bucket
2929
}
3030

31-
func prepareCommandsDistribution(queries arrayStringParameters, cmds [][]string, cmdRates []float64) (int, []float32) {
32-
var totalDifferentCommands = len(cmds)
31+
func prepareCommandsDistribution(queries arrayStringParameters, cmds [][]string, cmdRates []float64) (totalDifferentCommands int, cdf []float32) {
32+
totalDifferentCommands = len(cmds)
3333
var totalRateSum = 0.0
3434
var err error
3535
for i, rawCmdString := range queries {
@@ -54,7 +54,7 @@ func prepareCommandsDistribution(queries arrayStringParameters, cmds [][]string,
5454
log.Fatalf("When specifiying -cmd-ratio parameter, you need to have the same number of -cmd and -cmd-ratio parameters. Number of time -cmd ( %d ) != Number of times -cmd-ratio ( %d )", len(benchmarkCommands), len(benchmarkCommandsRatios))
5555
}
5656
pdf := make([]float32, len(queries))
57-
cdf := make([]float32, len(queries))
57+
cdf = make([]float32, len(queries))
5858
for i := 0; i < len(cmdRates); i++ {
5959
pdf[i] = float32(cmdRates[i])
6060
cdf[i] = 0
@@ -64,5 +64,5 @@ func prepareCommandsDistribution(queries arrayStringParameters, cmds [][]string,
6464
for i := 1; i < len(cmdRates); i++ {
6565
cdf[i] = cdf[i-1] + pdf[i]
6666
}
67-
return totalDifferentCommands, cdf
67+
return
6868
}

common.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,37 @@ package main
22

33
import (
44
"context"
5+
"fmt"
6+
"github.com/HdrHistogram/hdrhistogram-go"
57
radix "github.com/mediocregopher/radix/v4"
8+
"golang.org/x/time/rate"
9+
"math"
10+
"math/rand"
11+
"strings"
612
)
713

14+
var totalCommands uint64
15+
var totalErrors uint64
16+
var latencies *hdrhistogram.Histogram
17+
var benchmarkCommands arrayStringParameters
18+
var benchmarkCommandsRatios arrayStringParameters
19+
20+
const Inf = rate.Limit(math.MaxFloat64)
21+
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
22+
23+
type datapoint struct {
24+
success bool
25+
duration_ms int64
26+
}
27+
28+
func stringWithCharset(length int, charset string) string {
29+
b := make([]byte, length)
30+
for i := range b {
31+
b[i] = charset[rand.Intn(len(charset))]
32+
}
33+
return string(b)
34+
}
35+
836
type Client interface {
937

1038
// Do performs an Action on a Conn connected to the redis instance.
@@ -14,3 +42,35 @@ type Client interface {
1442
// an error
1543
Close() error
1644
}
45+
46+
func keyBuildLogic(keyPos int, dataPos int, datasize, keyspacelen uint64, cmdS []string, charset string) (newCmdS []string, key string) {
47+
newCmdS = make([]string, len(cmdS))
48+
copy(newCmdS, cmdS)
49+
if keyPos > -1 {
50+
keyV := fmt.Sprintf("%d", rand.Int63n(int64(keyspacelen)))
51+
key = strings.Replace(newCmdS[keyPos], "__key__", keyV, -1)
52+
newCmdS[keyPos] = key
53+
}
54+
if dataPos > -1 {
55+
newCmdS[dataPos] = stringWithCharset(int(datasize), charset)
56+
}
57+
return newCmdS, key
58+
}
59+
60+
func getplaceholderpos(args []string, verbose bool) (keyPlaceOlderPos int, dataPlaceOlderPos int) {
61+
keyPlaceOlderPos = -1
62+
dataPlaceOlderPos = -1
63+
for pos, arg := range args {
64+
if arg == "__data__" {
65+
dataPlaceOlderPos = pos
66+
}
67+
68+
if strings.Contains(arg, "__key__") {
69+
if verbose {
70+
fmt.Println(fmt.Sprintf("Detected __key__ placeholder in pos %d", pos))
71+
}
72+
keyPlaceOlderPos = pos
73+
}
74+
}
75+
return
76+
}

go.mod

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
module github.com/filipecosta90/redis-benchmark-go
22

3-
go 1.14
3+
go 1.20
44

55
require (
66
github.com/HdrHistogram/hdrhistogram-go v1.1.0
77
github.com/mattn/go-shellwords v1.0.12
88
github.com/mediocregopher/radix/v4 v4.1.2
9-
github.com/mitchellh/gox v1.0.1 // indirect
10-
github.com/tcnksm/ghr v0.16.0 // indirect
11-
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6 // indirect
9+
github.com/rueian/rueidis v0.0.100
1210
golang.org/x/time v0.0.0-20191024005414-555d28b269f0
1311
)
12+
13+
require (
14+
github.com/tilinna/clock v1.0.2 // indirect
15+
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6 // indirect
16+
)

0 commit comments

Comments
 (0)