From 1e15c8f75b599f6f3032d1c10efccd78e7bea3f3 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Tue, 20 Jun 2017 17:13:10 +0200 Subject: [PATCH] Extend wordcodec benchmarks --- keys/wordcodec_test.go | 30 ---------------- keys/wordcodecbench_test.go | 68 +++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 30 deletions(-) create mode 100644 keys/wordcodecbench_test.go diff --git a/keys/wordcodec_test.go b/keys/wordcodec_test.go index 3bdec110f..1ae97d8a7 100644 --- a/keys/wordcodec_test.go +++ b/keys/wordcodec_test.go @@ -178,33 +178,3 @@ func TestCheckTypoDetection(t *testing.T) { } } } - -func warmupCodec(bank string) *WordCodec { - codec, err := LoadCodec(bank) - if err != nil { - panic(err) - } - _, err = codec.GetIndex(codec.words[123]) - if err != nil { - panic(err) - } - return codec -} - -func BenchmarkWordGeneration(b *testing.B) { - // banks := []string{"english", "spanish", "japanese", "chinese_simplified"} - bank := "english" - - codec := warmupCodec(bank) - b.ResetTimer() - - numBytes := 32 - data := cmn.RandBytes(numBytes) - - for i := 1; i <= b.N; i++ { - _, err := codec.BytesToWords(data) - if err != nil { - panic(err) - } - } -} diff --git a/keys/wordcodecbench_test.go b/keys/wordcodecbench_test.go new file mode 100644 index 000000000..e100a443a --- /dev/null +++ b/keys/wordcodecbench_test.go @@ -0,0 +1,68 @@ +package keys + +import ( + "testing" + + cmn "github.com/tendermint/tmlibs/common" +) + +func warmupCodec(bank string) *WordCodec { + codec, err := LoadCodec(bank) + if err != nil { + panic(err) + } + _, err = codec.GetIndex(codec.words[123]) + if err != nil { + panic(err) + } + return codec +} + +func BenchmarkCodec(b *testing.B) { + banks := []string{"english", "spanish", "japanese", "chinese_simplified"} + + for _, bank := range banks { + b.Run(bank, func(sub *testing.B) { + codec := warmupCodec(bank) + sub.ResetTimer() + benchSuite(sub, codec) + }) + } +} + +func benchSuite(b *testing.B, codec *WordCodec) { + b.Run("to_words", func(sub *testing.B) { + benchMakeWords(sub, codec) + }) + b.Run("to_bytes", func(sub *testing.B) { + benchParseWords(sub, codec) + }) +} + +func benchMakeWords(b *testing.B, codec *WordCodec) { + numBytes := 32 + data := cmn.RandBytes(numBytes) + for i := 1; i <= b.N; i++ { + _, err := codec.BytesToWords(data) + if err != nil { + panic(err) + } + } +} + +func benchParseWords(b *testing.B, codec *WordCodec) { + // generate a valid test string to parse + numBytes := 32 + data := cmn.RandBytes(numBytes) + words, err := codec.BytesToWords(data) + if err != nil { + panic(err) + } + + for i := 1; i <= b.N; i++ { + _, err := codec.WordsToBytes(words) + if err != nil { + panic(err) + } + } +}