Commit e6fdb174 authored by Ubuntu's avatar Ubuntu

fix ethtx benchmark

parent f9f37946
...@@ -10,6 +10,8 @@ import ( ...@@ -10,6 +10,8 @@ import (
"testing" "testing"
"time" "time"
"sync"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure" "google.golang.org/grpc/credentials/insecure"
...@@ -42,69 +44,78 @@ func (*RingServer) SendRawTransaction(ctx context.Context, req *base.EthTx) (*ri ...@@ -42,69 +44,78 @@ func (*RingServer) SendRawTransaction(ctx context.Context, req *base.EthTx) (*ri
return &ring.SendRawTransactionResponse{}, nil return &ring.SendRawTransactionResponse{}, nil
} }
func BenchmarkEthTx(b *testing.B) { var once sync.Once
lis, err := net.Listen("tcp", ":9006")
if err != nil {
b.Fatal(err)
}
s := grpc.NewServer() func BenchmarkEthTx(b *testing.B) {
b.ReportAllocs()
ring.RegisterRingServiceServer(s, &RingServer{}) onceFunc := func() {
go func() { lis, err := net.Listen("tcp", ":9006")
err = s.Serve(lis)
if err != nil { if err != nil {
b.Fatal(err) b.Fatal(err)
} }
}()
b.ReportAllocs() s := grpc.NewServer()
conn, err := grpc.Dial("127.0.0.1:9006", grpc.WithTransportCredentials(insecure.NewCredentials())) ring.RegisterRingServiceServer(s, &RingServer{})
if err != nil {
b.Fatal(err) 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) b.RunParallel(func(pb *testing.PB) {
defer cancel()
local, _ := crypto.HexToECDSA("FD5CC6F5E7E2805E920AC5DC83D5AF1106F9C92F0C04F9D5E1FD4261B4B4464A") for pb.Next() {
publicKey := local.Public() conn, err := grpc.Dial("127.0.0.1:9006", grpc.WithTransportCredentials(insecure.NewCredentials()))
publicKeyECDSA, _ := publicKey.(*ecdsa.PublicKey) if err != nil {
fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA) b.Fatal(err)
b.Log(fromAddress) }
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{ local, _ := crypto.HexToECDSA("FD5CC6F5E7E2805E920AC5DC83D5AF1106F9C92F0C04F9D5E1FD4261B4B4464A")
AccountNonce: tx.Nonce(),
Price: tx.GasPrice().Bytes(),
GasLimit: tx.Gas(),
Payload: tx.Data(),
}
v, r, sigs := tx.RawSignatureValues() //publicKey := local.Public()
//publicKeyECDSA, _ := publicKey.(*ecdsa.PublicKey)
//fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
//b.Log(fromAddress)
inner.V = v.Bytes() remote, _ := crypto.GenerateKey()
inner.R = r.Bytes()
inner.S = sigs.Bytes()
inner.Amount = tx.Value().Bytes()
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. // The loop body is executed b.N times total across all goroutines.
res, err := c.SendRawTransaction(ctx, &base.EthTx{Inner: &inner}) res, err := c.SendRawTransaction(ctx, &base.EthTx{Inner: &inner})
...@@ -113,7 +124,9 @@ func BenchmarkEthTx(b *testing.B) { ...@@ -113,7 +124,9 @@ func BenchmarkEthTx(b *testing.B) {
b.Fatal(err) 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 ...@@ -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) tx, _ := types.SignTx(types.NewTransaction(nonce, to, big.NewInt(100), gaslimit, gasprice, nil), types.HomesteadSigner{}, key)
return tx 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