From 87ce23d6791917c41533c85f9113c13512c7f555 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Tue, 5 Nov 2013 21:35:28 -0600 Subject: [PATCH] Add benchmarks for readVarInt. This commit adds a few benchmarks for the readVarInt function. --- bench_test.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/bench_test.go b/bench_test.go index 158f7a56..0150c658 100644 --- a/bench_test.go +++ b/bench_test.go @@ -5,6 +5,7 @@ package btcwire_test import ( + "bytes" "github.com/conformal/btcwire" "io/ioutil" "testing" @@ -41,3 +42,39 @@ func BenchmarkWriteVarInt9(b *testing.B) { btcwire.TstWriteVarInt(ioutil.Discard, 0, 18446744073709551615) } } + +// BenchmarkReadVarInt1 performs a benchmark on how long it takes to write +// a single byte variable length integer. +func BenchmarkReadVarInt1(b *testing.B) { + buf := []byte{0x01} + for i := 0; i < b.N; i++ { + btcwire.TstReadVarInt(bytes.NewBuffer(buf), 0) + } +} + +// BenchmarkReadVarInt3 performs a benchmark on how long it takes to write +// a three byte variable length integer. +func BenchmarkReadVarInt3(b *testing.B) { + buf := []byte{0x0fd, 0xff, 0xff} + for i := 0; i < b.N; i++ { + btcwire.TstReadVarInt(bytes.NewBuffer(buf), 0) + } +} + +// BenchmarkReadVarInt5 performs a benchmark on how long it takes to write +// a five byte variable length integer. +func BenchmarkReadVarInt5(b *testing.B) { + buf := []byte{0xfe, 0xff, 0xff, 0xff, 0xff} + for i := 0; i < b.N; i++ { + btcwire.TstReadVarInt(bytes.NewBuffer(buf), 0) + } +} + +// BenchmarkReadVarInt9 performs a benchmark on how long it takes to write +// a nine byte variable length integer. +func BenchmarkReadVarInt9(b *testing.B) { + buf := []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff} + for i := 0; i < b.N; i++ { + btcwire.TstReadVarInt(bytes.NewBuffer(buf), 0) + } +}