Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add benchmark test #62

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ BUILD_VERSIONS_DEBUG = $(shell jq -r '.versions|map("build-\(.)-debug")[]' ${TAR
STORE_MOD_VERSIONS = $(shell jq -r '.versions|map("store-mod-\(.)")[]' ${TARGETS})
TEST_VERSIONS = $(shell jq -r '.versions|map("test-\(.)")[]' ${TARGETS})
COVERAGE_VERSIONS = $(shell jq -r '.versions|map("coverage-\(.)")[]' ${TARGETS})
BENCHMARK_VERSIONS = $(shell jq -r '.versions|map("benchmark-\(.)")[]' ${TARGETS})
BRANCH := $(shell git rev-parse --abbrev-ref HEAD)
COMMIT := $(shell git log -1 --format='%H')

Expand Down Expand Up @@ -56,6 +57,11 @@ $(TEST_VERSIONS):
go test -v -failfast -race -count=1 \
-tags $(shell echo $@ | sed -e 's/test-/sdk_/g' -e 's/-/_/g'),muslc \
./...

$(BENCHMARK_VERSIONS):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be useful to enable CPU and memory profiling while running benchmarks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I am not wrong, -benchmem flag will give you the same right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have to supply flags like those as well to create the profiling files:

-cpuprofile=cpu.out -memprofile=mem.out -trace=trace.out

go test -v -failfast -bench=. -run=^# -benchmem -count=1 -cpuprofile=cpu.out -memprofile=mem.out -trace=trace.out \
-tags $(shell echo $@ | sed -e 's/benchmark-/sdk_/g' -e 's/-/_/g'),muslc \
./tracelistener

$(COVERAGE_VERSIONS):
go test -v -failfast -coverprofile=coverage.out -covermode=atomic -count=1\
Expand Down
52 changes: 52 additions & 0 deletions tracelistener/benchmark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package tracelistener_test

import (
"fmt"
"os"
"testing"

"github.com/allinbits/tracelistener/tracelistener"
"go.uber.org/zap"
)

func BenchmarkTraceListener(b *testing.B) {
f, err := os.CreateTemp("", "test_data")
if err != nil {
panic(err)
}

defer os.Remove(f.Name())

dataChan := make(chan tracelistener.TraceOperation)
errChan := make(chan error)
l, _ := zap.NewDevelopment()
tw := tracelistener.TraceWatcher{
DataSourcePath: f.Name(),
WatchedOps: []tracelistener.Operation{
tracelistener.WriteOp,
tracelistener.DeleteOp,
},
DataChan: dataChan,
ErrorChan: errChan,
Logger: l.Sugar(),
}

go func() {
tw.Watch()
}()

for i := 0; i < b.N; i++ {
loadTest(b, i, f)
}

err = f.Close()
if err != nil {
panic(err)
}
}

func loadTest(b *testing.B, height int, file *os.File) {
b.Helper()
op := fmt.Sprintf(`{"operation":"write","key":"aGVsbG8K","value":"aGVsbG8K","metadata":{"blockHeight":%d,"txHash":"A5CF62609D62ADDE56816681B6191F5F0252D2800FC2C312EB91D962AB7A97CB"}}`, height)
fmt.Fprintf(file, "%s\n", op)
}