From b7d7f5fb7166b7e585a04416e7dbcbccd40e673d Mon Sep 17 00:00:00 2001 From: whyrusleeping Date: Wed, 17 Jul 2019 23:21:50 -0700 Subject: [PATCH] write a couple simple benchmarks --- chain/address/bench_test.go | 94 +++++++++++++++++++++++++++++++++++++ chain/types/types_test.go | 41 ++++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 chain/address/bench_test.go create mode 100644 chain/types/types_test.go diff --git a/chain/address/bench_test.go b/chain/address/bench_test.go new file mode 100644 index 000000000..9556ae20c --- /dev/null +++ b/chain/address/bench_test.go @@ -0,0 +1,94 @@ +package address + +import ( + "fmt" + "math/rand" + + "testing" +) + +func blsaddr(n int64) Address { + buf := make([]byte, 48) + r := rand.New(rand.NewSource(n)) + r.Read(buf) + + addr, err := NewBLSAddress(buf) + if err != nil { + panic(err) + } + + return addr +} + +func makeActorAddresses(n int) [][]byte { + var addrs [][]byte + for i := 0; i < n; i++ { + a, err := NewActorAddress([]byte(fmt.Sprintf("ACTOR ADDRESS %d", i))) + if err != nil { + panic(err) + } + addrs = append(addrs, a.Bytes()) + } + + return addrs +} + +func makeBlsAddresses(n int64) [][]byte { + var addrs [][]byte + for i := int64(0); i < n; i++ { + addrs = append(addrs, blsaddr(n).Bytes()) + } + return addrs +} + +func makeSecpAddresses(n int) [][]byte { + var addrs [][]byte + for i := 0; i < n; i++ { + r := rand.New(rand.NewSource(int64(i))) + buf := make([]byte, 32) + r.Read(buf) + + a, err := NewSecp256k1Address(buf) + if err != nil { + panic(err) + } + + addrs = append(addrs, a.Bytes()) + } + return addrs +} + +func makeIDAddresses(n int) [][]byte { + var addrs [][]byte + for i := 0; i < n; i++ { + + a, err := NewIDAddress(uint64(i)) + if err != nil { + panic(err) + } + + addrs = append(addrs, a.Bytes()) + } + return addrs +} + +func BenchmarkParseActorAddress(b *testing.B) { + benchTestWithAddrs := func(a [][]byte) func(b *testing.B) { + return func(b *testing.B) { + b.ReportAllocs() + b.ResetTimer() + + for i := 0; i < b.N; i++ { + _, err := NewFromBytes(a[i%len(a)]) + if err != nil { + b.Fatal(err) + } + } + } + } + + b.Run("actor", benchTestWithAddrs(makeActorAddresses(20))) + b.Run("bls", benchTestWithAddrs(makeBlsAddresses(20))) + b.Run("secp256k1", benchTestWithAddrs(makeSecpAddresses(20))) + b.Run("id", benchTestWithAddrs(makeIDAddresses(20))) +} diff --git a/chain/types/types_test.go b/chain/types/types_test.go new file mode 100644 index 000000000..38d16c0d2 --- /dev/null +++ b/chain/types/types_test.go @@ -0,0 +1,41 @@ +package types + +import ( + "math/rand" + "testing" + + "github.com/filecoin-project/go-lotus/chain/address" +) + +func blsaddr(n int64) address.Address { + buf := make([]byte, 48) + r := rand.New(rand.NewSource(n)) + r.Read(buf) + + addr, err := address.NewBLSAddress(buf) + if err != nil { + panic(err) + } + + return addr +} + +func BenchmarkSerializeMessage(b *testing.B) { + m := &Message{ + To: blsaddr(1), + From: blsaddr(2), + Nonce: 197, + Method: 1231254, + Params: []byte("some bytes, idk. probably at least ten of them"), + GasLimit: NewInt(126723), + GasPrice: NewInt(1776234), + } + + b.ReportAllocs() + for i := 0; i < b.N; i++ { + _, err := m.Serialize() + if err != nil { + b.Fatal(err) + } + } +}