Commit e6fdb174 authored by Ubuntu's avatar Ubuntu

fix ethtx benchmark

parent f9f37946
......@@ -10,6 +10,8 @@ import (
"testing"
"time"
"sync"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
......@@ -42,69 +44,78 @@ func (*RingServer) SendRawTransaction(ctx context.Context, req *base.EthTx) (*ri
return &ring.SendRawTransactionResponse{}, nil
}
func BenchmarkEthTx(b *testing.B) {
lis, err := net.Listen("tcp", ":9006")
if err != nil {
b.Fatal(err)
}
var once sync.Once
s := grpc.NewServer()
func BenchmarkEthTx(b *testing.B) {
b.ReportAllocs()
ring.RegisterRingServiceServer(s, &RingServer{})
onceFunc := func() {
go func() {
err = s.Serve(lis)
lis, err := net.Listen("tcp", ":9006")
if err != nil {
b.Fatal(err)
}
}()
b.ReportAllocs()
s := grpc.NewServer()
conn, err := grpc.Dial("127.0.0.1:9006", grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
b.Fatal(err)
ring.RegisterRingServiceServer(s, &RingServer{})
go func() {
err = s.Serve(lis)
if err != nil {
b.Fatal(err)
}
}()
}
defer conn.Close()
c := ring.NewRingServiceClient(conn)
once.Do(onceFunc)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
b.RunParallel(func(pb *testing.PB) {
local, _ := crypto.HexToECDSA("FD5CC6F5E7E2805E920AC5DC83D5AF1106F9C92F0C04F9D5E1FD4261B4B4464A")
for pb.Next() {
publicKey := local.Public()
publicKeyECDSA, _ := publicKey.(*ecdsa.PublicKey)
fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
b.Log(fromAddress)
conn, err := grpc.Dial("127.0.0.1:9006", grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
b.Fatal(err)
}
defer conn.Close()
remote, _ := crypto.GenerateKey()
c := ring.NewRingServiceClient(conn)
tx := pricedTransaction(crypto.PubkeyToAddress(remote.PublicKey), 0, 100000, big.NewInt(1), local)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
inner := base.EthTxData{
AccountNonce: tx.Nonce(),
Price: tx.GasPrice().Bytes(),
GasLimit: tx.Gas(),
Payload: tx.Data(),
}
local, _ := crypto.HexToECDSA("FD5CC6F5E7E2805E920AC5DC83D5AF1106F9C92F0C04F9D5E1FD4261B4B4464A")
v, r, sigs := tx.RawSignatureValues()
//publicKey := local.Public()
//publicKeyECDSA, _ := publicKey.(*ecdsa.PublicKey)
//fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
//b.Log(fromAddress)
inner.V = v.Bytes()
inner.R = r.Bytes()
inner.S = sigs.Bytes()
inner.Amount = tx.Value().Bytes()
remote, _ := crypto.GenerateKey()
addr := base.Address{Address: tx.To().Bytes()}
tx := pricedTransaction(crypto.PubkeyToAddress(remote.PublicKey), 0, 100000, big.NewInt(1), local)
inner.Recipient = &addr
inner := base.EthTxData{
AccountNonce: tx.Nonce(),
Price: tx.GasPrice().Bytes(),
GasLimit: tx.Gas(),
Payload: tx.Data(),
}
v, r, sigs := tx.RawSignatureValues()
inner.V = v.Bytes()
inner.R = r.Bytes()
inner.S = sigs.Bytes()
inner.Amount = tx.Value().Bytes()
addr := base.Address{Address: tx.To().Bytes()}
inner.Recipient = &addr
// Each goroutine has its own bytes.Buffer.
b.RunParallel(func(pb *testing.PB) {
// Each goroutine has its own bytes.Buffer.
for pb.Next() {
// The loop body is executed b.N times total across all goroutines.
res, err := c.SendRawTransaction(ctx, &base.EthTx{Inner: &inner})
......@@ -113,7 +124,9 @@ func BenchmarkEthTx(b *testing.B) {
b.Fatal(err)
}
b.Logf("Greeting: %v\n", res)
_ = res
//b.Logf("Greeting: %v\n", res)
}
})
......@@ -123,3 +136,5 @@ func pricedTransaction(to common.Address, nonce uint64, gaslimit uint64, gaspric
tx, _ := types.SignTx(types.NewTransaction(nonce, to, big.NewInt(100), gaslimit, gasprice, nil), types.HomesteadSigner{}, key)
return tx
}
// go test -bench=.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment