From 28b5350cb6543304238661f278a7951acd706317 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Thu, 6 Jul 2017 16:51:40 +0200 Subject: [PATCH] Added simple benchmark for signature checks --- Makefile | 5 ++- modules/auth/bench_test.go | 74 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 modules/auth/bench_test.go diff --git a/Makefile b/Makefile index 4a554e0f89..143395559e 100644 --- a/Makefile +++ b/Makefile @@ -16,6 +16,9 @@ dist: @bash publish/dist.sh @bash publish/publish.sh +benchmark: + @go test -bench=. ./modules/... + test: test_unit test_cli test_tutorial test_unit: @@ -65,4 +68,4 @@ clean: fresh: clean get_vendor_deps install @if [[ `git status -s` ]]; then echo; echo "Warning: uncommited changes"; git status -s; fi -.PHONY: all build install test test_cli test_unit get_vendor_deps build-docker clean fresh +.PHONY: all build install test test_cli test_unit get_vendor_deps build-docker clean fresh benchmark diff --git a/modules/auth/bench_test.go b/modules/auth/bench_test.go new file mode 100644 index 0000000000..b79449a7dd --- /dev/null +++ b/modules/auth/bench_test.go @@ -0,0 +1,74 @@ +package auth + +import ( + "fmt" + "testing" + + crypto "github.com/tendermint/go-crypto" + cmn "github.com/tendermint/tmlibs/common" + "github.com/tendermint/tmlibs/log" + + "github.com/tendermint/basecoin" + "github.com/tendermint/basecoin/stack" + "github.com/tendermint/basecoin/state" +) + +func makeSignTx() basecoin.Tx { + key := crypto.GenPrivKeyEd25519().Wrap() + payload := cmn.RandBytes(32) + tx := NewSig(stack.NewRawTx(payload)) + Sign(tx, key) + return tx.Wrap() +} + +func makeMultiSignTx(cnt int) basecoin.Tx { + payload := cmn.RandBytes(32) + tx := NewMulti(stack.NewRawTx(payload)) + for i := 0; i < cnt; i++ { + key := crypto.GenPrivKeyEd25519().Wrap() + Sign(tx, key) + } + return tx.Wrap() +} + +func makeHandler() basecoin.Handler { + return stack.New(Signatures{}).Use(stack.OKHandler{}) +} + +func BenchmarkCheckOneSig(b *testing.B) { + tx := makeSignTx() + h := makeHandler() + store := state.NewMemKVStore() + for i := 1; i <= b.N; i++ { + ctx := stack.NewContext("foo", log.NewNopLogger()) + _, err := h.DeliverTx(ctx, store, tx) + // never should error + if err != nil { + panic(err) + } + } +} + +func BenchmarkCheckMultiSig(b *testing.B) { + sigs := []int{1, 3, 8, 20} + for _, cnt := range sigs { + label := fmt.Sprintf("%dsigs", cnt) + b.Run(label, func(sub *testing.B) { + benchmarkCheckMultiSig(sub, cnt) + }) + } +} + +func benchmarkCheckMultiSig(b *testing.B, cnt int) { + tx := makeMultiSignTx(cnt) + h := makeHandler() + store := state.NewMemKVStore() + for i := 1; i <= b.N; i++ { + ctx := stack.NewContext("foo", log.NewNopLogger()) + _, err := h.DeliverTx(ctx, store, tx) + // never should error + if err != nil { + panic(err) + } + } +}