Merge pull request #54 from filecoin-project/feat/benchmarks-1

write a couple simple benchmarks
This commit is contained in:
Whyrusleeping 2019-07-18 11:29:39 -07:00 committed by GitHub
commit 27a7858055
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 135 additions and 0 deletions

View File

@ -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)))
}

41
chain/types/types_test.go Normal file
View File

@ -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)
}
}
}