Commit 6049895b authored by Ubuntu's avatar Ubuntu

mv benchmark

parent 7cb05416
package main_test
import (
"fmt"
"testing"
)
//date && go test -v -run BenchmarkHello -bench=BenchmarkHello -benchtime=3s -benchmem && date
func BenchmarkHello(b *testing.B) {
b.Log(b.N)
//b.Logf("begin time: %s \n", time.Now())
b.ResetTimer()
for i := 0; i < b.N; i++ {
//b.Logf("idx %d time: %s \n", i, time.Now())
fmt.Sprintf("hello")
}
//b.StopTimer()
//b.Logf("end time: %s \n", time.Now())
}
......@@ -4,13 +4,9 @@ import (
"bytes"
"context"
"crypto/ecdsa"
"errors"
"fmt"
"sync"
//"encoding/hex"
"math/big"
"net"
"testing"
"time"
......@@ -24,165 +20,8 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"unsafe"
)
var countParallelAsEth int64
var countParallelAsStd int64
var countParallelAsAny int64
var countParallelAsBytes int64
type RingServer struct {
ring.UnimplementedRingServiceServer
}
func (*RingServer) SendEmptyMsg(context.Context, *ring.EmptyRequest) (*ring.EmptyResponse, error) {
return &ring.EmptyResponse{}, nil
}
func (*RingServer) SendEmpty(context.Context, *emptypb.Empty) (*emptypb.Empty, error) {
return &emptypb.Empty{}, nil
}
func (*RingServer) SendTxAsEth(ctx context.Context, in *base.TransactionEth) (*ring.SendRawTransactionResponse, error) {
addr := common.Address{}
copy(addr[:], in.Tx.Inner.Recipient.Address[:common.AddressLength])
ethTx := types.NewTx(&types.LegacyTx{
Nonce: in.Tx.Inner.AccountNonce,
To: &addr,
Value: new(big.Int).SetBytes(in.Tx.Inner.Amount),
Gas: in.Tx.Inner.GasLimit,
GasPrice: new(big.Int).SetBytes(in.Tx.Inner.Price),
Data: in.Tx.Inner.Payload,
V: new(big.Int).SetBytes(in.Tx.Inner.V),
R: new(big.Int).SetBytes(in.Tx.Inner.R),
S: new(big.Int).SetBytes(in.Tx.Inner.S),
})
// atomic.AddInt64(&countParallelAsEth, 1)
// defer fmt.Println("countParallelAsEth", countParallelAsEth)
return &ring.SendRawTransactionResponse{TxHash: ethTx.Hash().Bytes()}, nil
}
func (*RingServer) SendTxAsStd(ctx context.Context, in *base.TransactionStd) (*ring.SendRawTransactionResponse, error) {
addr := common.Address{}
copy(addr[:], in.Tx.Inner.Recipient.Address[:common.AddressLength])
ethTx := types.NewTx(&types.LegacyTx{
Nonce: in.Tx.Inner.AccountNonce,
To: &addr,
Value: new(big.Int).SetBytes(in.Tx.Inner.Amount),
Gas: in.Tx.Inner.GasLimit,
GasPrice: new(big.Int).SetBytes(in.Tx.Inner.Price),
Data: in.Tx.Inner.Payload,
V: new(big.Int).SetBytes(in.Tx.Inner.V),
R: new(big.Int).SetBytes(in.Tx.Inner.R),
S: new(big.Int).SetBytes(in.Tx.Inner.S),
})
// atomic.AddInt64(&countParallelAsStd, 1)
// defer fmt.Println("countParallelAsStd", countParallelAsStd)
return &ring.SendRawTransactionResponse{TxHash: ethTx.Hash().Bytes()}, nil
}
func (*RingServer) SendTxAsAny(ctx context.Context, in *base.Transaction) (*ring.SendRawTransactionResponse, error) {
msg, err := in.Tx.UnmarshalNew()
if err != nil {
return nil, err
}
// atomic.AddInt64(&countParallelAsAny, 1)
// defer fmt.Println("countParallelAsAny", countParallelAsAny)
switch m := msg.(type) {
case *base.EthTx:
addr := common.Address{}
copy(addr[:], m.Inner.Recipient.Address[:common.AddressLength])
ethTx := types.NewTx(&types.LegacyTx{
Nonce: m.Inner.AccountNonce,
To: &addr,
Value: new(big.Int).SetBytes(m.Inner.Amount),
Gas: m.Inner.GasLimit,
GasPrice: new(big.Int).SetBytes(m.Inner.Price),
Data: m.Inner.Payload,
V: new(big.Int).SetBytes(m.Inner.V),
R: new(big.Int).SetBytes(m.Inner.R),
S: new(big.Int).SetBytes(m.Inner.S),
})
return &ring.SendRawTransactionResponse{TxHash: ethTx.Hash().Bytes()}, nil
case *base.StdTx:
addr := common.Address{}
copy(addr[:], m.Inner.Recipient.Address[:common.AddressLength])
ethTx := types.NewTx(&types.LegacyTx{
Nonce: m.Inner.AccountNonce,
To: &addr,
Value: new(big.Int).SetBytes(m.Inner.Amount),
Gas: m.Inner.GasLimit,
GasPrice: new(big.Int).SetBytes(m.Inner.Price),
Data: m.Inner.Payload,
V: new(big.Int).SetBytes(m.Inner.V),
R: new(big.Int).SetBytes(m.Inner.R),
S: new(big.Int).SetBytes(m.Inner.S),
})
return &ring.SendRawTransactionResponse{TxHash: ethTx.Hash().Bytes()}, nil
default:
return nil, errors.New(fmt.Sprintf("server expected tx type: %T and %T, but actually is %T", base.EthTx{}, base.StdTx{}, msg))
}
}
func (*RingServer) SendTxAsBytes(ctx context.Context, in *base.TransactionBytes) (*ring.SendRawTransactionResponse, error) {
ethTx := types.Transaction{}
if err := ethTx.UnmarshalBinary(in.Tx); err != nil {
return nil, err
}
// atomic.AddInt64(&countParallelAsBytes, 1)
// defer fmt.Printf("countParallelAsBytes: %d\n", countParallelAsBytes)
return &ring.SendRawTransactionResponse{TxHash: ethTx.Hash().Bytes()}, nil
}
func TestGrpcServer(t *testing.T) {
lis, err := net.Listen("tcp", ":9006")
if err != nil {
t.Fatal(err)
}
s := grpc.NewServer()
ring.RegisterRingServiceServer(s, &RingServer{})
err = s.Serve(lis)
if err != nil {
t.Fatal(err)
}
}
func pricedTransaction(to common.Address, nonce uint64, gaslimit uint64, gasprice *big.Int, key *ecdsa.PrivateKey) (*types.Transaction, error) {
return types.SignTx(types.NewTransaction(nonce, to, big.NewInt(100), gaslimit, gasprice, nil), types.HomesteadSigner{}, key)
......@@ -235,37 +74,33 @@ func pricedTransaction(to common.Address, nonce uint64, gaslimit uint64, gaspric
//BenchmarkAny
// go test -v -run Any -bench=. -benchtime=3s
var once sync.Once
//var once sync.Once
var tx *types.Transaction
// var tx *types.Transaction
var fromAddr common.Address
// var fromAddr common.Address
// var local, remote *ecdsa.PrivateKey
func TestTxSizeOf(t *testing.T) {
fmt.Println(unsafe.Sizeof(*tx)) //88
}
// func int() {
func int() {
// local, _ := crypto.HexToECDSA("FD5CC6F5E7E2805E920AC5DC83D5AF1106F9C92F0C04F9D5E1FD4261B4B4464A")
// publicKey := local.Public()
// publicKeyECDSA, _ := publicKey.(*ecdsa.PublicKey)
// fromAddr = crypto.PubkeyToAddress(*publicKeyECDSA)
local, _ := crypto.HexToECDSA("FD5CC6F5E7E2805E920AC5DC83D5AF1106F9C92F0C04F9D5E1FD4261B4B4464A")
publicKey := local.Public()
publicKeyECDSA, _ := publicKey.(*ecdsa.PublicKey)
fromAddr = crypto.PubkeyToAddress(*publicKeyECDSA)
// remote, _ := crypto.GenerateKey()
// var err error
// tx, err = pricedTransaction(crypto.PubkeyToAddress(remote.PublicKey), 0, 100000, big.NewInt(1), local)
remote, _ := crypto.GenerateKey()
var err error
tx, err = pricedTransaction(crypto.PubkeyToAddress(remote.PublicKey), 0, 100000, big.NewInt(1), local)
// if err != nil {
// fmt.Println("pricedTransaction", err.Error())
// }
if err != nil {
fmt.Println("pricedTransaction", err.Error())
}
}
// }
var count int64
var countParallel int64
// var count int64
// var countParallel int64
var onceHash sync.Once
......@@ -331,24 +166,24 @@ func BenchmarkEthTx(b *testing.B) {
b.Logf("b.N: %d\n", b.N)
onceFunc := func() {
local, _ := crypto.HexToECDSA("FD5CC6F5E7E2805E920AC5DC83D5AF1106F9C92F0C04F9D5E1FD4261B4B4464A")
local, err := crypto.HexToECDSA("FD5CC6F5E7E2805E920AC5DC83D5AF1106F9C92F0C04F9D5E1FD4261B4B4464A")
if err != nil {
b.Fatal(err)
}
publicKey := local.Public()
publicKeyECDSA, _ := publicKey.(*ecdsa.PublicKey)
fromAddr = crypto.PubkeyToAddress(*publicKeyECDSA)
fromAddr := crypto.PubkeyToAddress(*publicKeyECDSA)
remote, _ := crypto.GenerateKey()
remote, err := crypto.GenerateKey()
if err != nil {
b.Fatal(err)
}
var err error
tx, err = pricedTransaction(crypto.PubkeyToAddress(remote.PublicKey), 0, 100000, big.NewInt(1), local)
tx, err := pricedTransaction(crypto.PubkeyToAddress(remote.PublicKey), 0, 100000, big.NewInt(1), local)
if err != nil {
b.Fatal(err)
}
}
once.Do(onceFunc)
b.ResetTimer()
......@@ -409,23 +244,22 @@ func BenchmarkStdTx(b *testing.B) {
b.Logf("b.N: %d\n", b.N)
onceFunc := func() {
// onceFunc := func() {
local, _ := crypto.HexToECDSA("FD5CC6F5E7E2805E920AC5DC83D5AF1106F9C92F0C04F9D5E1FD4261B4B4464A")
publicKey := local.Public()
publicKeyECDSA, _ := publicKey.(*ecdsa.PublicKey)
fromAddr = crypto.PubkeyToAddress(*publicKeyECDSA)
fromAddr := crypto.PubkeyToAddress(*publicKeyECDSA)
remote, _ := crypto.GenerateKey()
var err error
tx, err = pricedTransaction(crypto.PubkeyToAddress(remote.PublicKey), 0, 100000, big.NewInt(1), local)
tx, err := pricedTransaction(crypto.PubkeyToAddress(remote.PublicKey), 0, 100000, big.NewInt(1), local)
if err != nil {
b.Fatal(err)
}
}
once.Do(onceFunc)
//}
// once.Do(onceFunc)
b.ResetTimer()
......@@ -485,23 +319,22 @@ func BenchmarkStdTx(b *testing.B) {
func BenchmarkAnyTxEth(b *testing.B) {
onceFunc := func() {
// onceFunc := func() {
local, _ := crypto.HexToECDSA("FD5CC6F5E7E2805E920AC5DC83D5AF1106F9C92F0C04F9D5E1FD4261B4B4464A")
publicKey := local.Public()
publicKeyECDSA, _ := publicKey.(*ecdsa.PublicKey)
fromAddr = crypto.PubkeyToAddress(*publicKeyECDSA)
fromAddr := crypto.PubkeyToAddress(*publicKeyECDSA)
remote, _ := crypto.GenerateKey()
var err error
tx, err = pricedTransaction(crypto.PubkeyToAddress(remote.PublicKey), 0, 100000, big.NewInt(1), local)
tx, err := pricedTransaction(crypto.PubkeyToAddress(remote.PublicKey), 0, 100000, big.NewInt(1), local)
if err != nil {
b.Fatal(err)
}
}
once.Do(onceFunc)
//}
// once.Do(onceFunc)
b.ResetTimer()
......@@ -539,6 +372,7 @@ func BenchmarkAnyTxEth(b *testing.B) {
addr := base.Address{Address: tx.To().Bytes()}
inner.Recipient = &addr
inner.From = fromAddr.Bytes()
ethTx := base.EthTx{Inner: &inner}
......@@ -566,28 +400,25 @@ func BenchmarkAnyTxEth(b *testing.B) {
func BenchmarkAnyTxStd(b *testing.B) {
onceFunc := func() {
//onceFunc := func() {
local, _ := crypto.HexToECDSA("FD5CC6F5E7E2805E920AC5DC83D5AF1106F9C92F0C04F9D5E1FD4261B4B4464A")
publicKey := local.Public()
publicKeyECDSA, _ := publicKey.(*ecdsa.PublicKey)
fromAddr = crypto.PubkeyToAddress(*publicKeyECDSA)
fromAddr := crypto.PubkeyToAddress(*publicKeyECDSA)
remote, _ := crypto.GenerateKey()
var err error
tx, err = pricedTransaction(crypto.PubkeyToAddress(remote.PublicKey), 0, 100000, big.NewInt(1), local)
tx, err := pricedTransaction(crypto.PubkeyToAddress(remote.PublicKey), 0, 100000, big.NewInt(1), local)
if err != nil {
b.Fatal(err)
}
}
once.Do(onceFunc)
// }
// once.Do(onceFunc)
b.ResetTimer()
// The loop body is executed b.N times total across all goroutines.
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
......@@ -620,6 +451,7 @@ func BenchmarkAnyTxStd(b *testing.B) {
addr := base.Address{Address: tx.To().Bytes()}
inner.Recipient = &addr
inner.From = fromAddr.Bytes()
stdTx := base.StdTx{Inner: &inner}
......@@ -647,23 +479,22 @@ func BenchmarkAnyTxStd(b *testing.B) {
func BenchmarkBytesEth(b *testing.B) {
onceFunc := func() {
// onceFunc := func() {
local, _ := crypto.HexToECDSA("FD5CC6F5E7E2805E920AC5DC83D5AF1106F9C92F0C04F9D5E1FD4261B4B4464A")
publicKey := local.Public()
publicKeyECDSA, _ := publicKey.(*ecdsa.PublicKey)
fromAddr = crypto.PubkeyToAddress(*publicKeyECDSA)
fromAddr := crypto.PubkeyToAddress(*publicKeyECDSA)
remote, _ := crypto.GenerateKey()
var err error
tx, err = pricedTransaction(crypto.PubkeyToAddress(remote.PublicKey), 0, 100000, big.NewInt(1), local)
tx, err := pricedTransaction(crypto.PubkeyToAddress(remote.PublicKey), 0, 100000, big.NewInt(1), local)
if err != nil {
b.Fatal(err)
}
}
// }
once.Do(onceFunc)
// once.Do(onceFunc)
b.ResetTimer()
......@@ -683,32 +514,10 @@ func BenchmarkBytesEth(b *testing.B) {
defer cancel()
txAsBytes, err := tx.MarshalBinary()
// 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
// inner.From = fromAddr.Bytes()
// txAsRes := &base.TransactionEth{Tx: &base.EthTx{Inner: &inner}}
// txAsRes.
// if err != nil {
// b.Fatal(err)
// }
if err != nil {
b.Fatal(err)
}
res, err := c.SendTxAsBytes(ctx, &base.TransactionBytes{Tx: txAsBytes, From: fromAddr[:]})
......@@ -729,130 +538,3 @@ func BenchmarkBytesEth(b *testing.B) {
}
})
}
func TestType(t *testing.T) {
ethTx := base.EthTx{}
fmt.Println(ethTx.ProtoReflect().Descriptor().FullName())
}
func TestAnyTx(t *testing.T) {
onceFunc := func() {
local, _ := crypto.HexToECDSA("FD5CC6F5E7E2805E920AC5DC83D5AF1106F9C92F0C04F9D5E1FD4261B4B4464A")
publicKey := local.Public()
publicKeyECDSA, _ := publicKey.(*ecdsa.PublicKey)
fromAddr = crypto.PubkeyToAddress(*publicKeyECDSA)
remote, _ := crypto.GenerateKey()
var err error
tx, err = pricedTransaction(crypto.PubkeyToAddress(remote.PublicKey), 0, 100000, big.NewInt(1), local)
if err != nil {
t.Fatal(err)
}
}
once.Do(onceFunc)
conn, err := grpc.Dial("127.0.0.1:9006", grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
t.Fatal(err)
}
defer conn.Close()
c := ring.NewRingServiceClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
_, _ = c, ctx
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
ethTx := base.EthTx{Inner: &inner}
ethTxAsAny, err := anypb.New(&ethTx)
if err != nil {
t.Fatal(err)
}
//fmt.Println("ethTxAsAny.ProtoReflect().Descriptor().FullName()", ethTx.ProtoReflect().Descriptor().FullName())
res, err := c.SendTxAsAny(ctx, &base.Transaction{Tx: ethTxAsAny})
_ = res
if err != nil {
t.Fatal(err)
}
stdInner := base.StdTxData{
AccountNonce: tx.Nonce(),
Price: tx.GasPrice().Bytes(),
GasLimit: tx.Gas(),
Payload: tx.Data(),
}
// v, r, sigs := tx.RawSignatureValues()
stdInner.V = v.Bytes()
stdInner.R = r.Bytes()
stdInner.S = sigs.Bytes()
stdInner.Amount = tx.Value().Bytes()
//addr := base.Address{Address: tx.To().Bytes()}
stdInner.Recipient = &addr
stdTx := base.StdTx{Inner: &stdInner}
_ = stdTx
//stdTxAsAny, err := pbany(stdTx.ProtoReflect())
stdTxAsAny, err := anypb.New(&stdTx)
if err != nil {
t.Fatal(err)
}
_ = stdTxAsAny
res, err = c.SendTxAsAny(ctx, &base.Transaction{Tx: stdTxAsAny})
if err != nil {
t.Fatal(err)
}
_ = res
}
//go test -v -run BenchmarkHello -bench BenchmarkHello -benchtime=3s
func BenchmarkHello(b *testing.B) {
b.Log(b.N)
for i := 0; i < b.N; i++ {
fmt.Sprintf("hello")
}
}
......@@ -5,8 +5,6 @@ import (
"context"
"sync"
//"encoding/hex"
"testing"
"time"
......@@ -42,7 +40,9 @@ func BenchmarkQueueEthTx(b *testing.B) {
// once.Do(onceFunc)
onceFunc := func() {
//b.StopTimer()
//onceFunc := func() {
txsQueue, err := ProduceOriginalTxByCount(500000)
if err != nil {
......@@ -50,12 +50,14 @@ func BenchmarkQueueEthTx(b *testing.B) {
}
originalTxsQueue = txsQueue
}
//}
once.Do(onceFunc)
//once.Do(onceFunc)
b.ResetTimer()
//b.StartTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
......@@ -126,7 +128,7 @@ func BenchmarkQueueStdTx(b *testing.B) {
// once.Do(onceFunc)
onceFunc := func() {
//onceFunc := func() {
txsQueue, err := ProduceOriginalTxByCount(500000)
if err != nil {
......@@ -134,9 +136,8 @@ func BenchmarkQueueStdTx(b *testing.B) {
}
originalTxsQueue = txsQueue
}
once.Do(onceFunc)
//}
//once.Do(onceFunc)
b.ResetTimer()
......@@ -211,7 +212,7 @@ func BenchmarkQueueAnyTx(b *testing.B) {
// once.Do(onceFunc)
onceFunc := func() {
//onceFunc := func() {
txsQueue, err := ProduceOriginalTxByCount(500000)
if err != nil {
......@@ -219,9 +220,9 @@ func BenchmarkQueueAnyTx(b *testing.B) {
}
originalTxsQueue = txsQueue
}
//}
once.Do(onceFunc)
//once.Do(onceFunc)
b.ResetTimer()
......@@ -302,7 +303,7 @@ func BenchmarkQueueBytesEth(b *testing.B) {
// once.Do(onceFunc)
onceFunc := func() {
//onceFunc := func() {
txsQueue, err := ProduceOriginalTxByCount(500000)
if err != nil {
......@@ -310,9 +311,9 @@ func BenchmarkQueueBytesEth(b *testing.B) {
}
originalTxsQueue = txsQueue
}
//}
once.Do(onceFunc)
//once.Do(onceFunc)
b.ResetTimer()
......
package main
import (
"bytes"
"fmt"
"testing"
"text/template"
)
func main() {
benchmarkResult := testing.Benchmark(func(b *testing.B) {
templ := template.Must(template.New("test").Parse("Hello, {{.}}!"))
// RunParallel will create GOMAXPROCS goroutines
// and distribute work among them.
b.RunParallel(func(pb *testing.PB) {
// Each goroutine has its own bytes.Buffer.
var buf bytes.Buffer
for pb.Next() {
// The loop body is executed b.N times total across all goroutines.
buf.Reset()
templ.Execute(&buf, "World")
}
})
})
fmt.Printf("%8d\t%10d ns/op\t%10d B/op\t%10d allocs/op %s total time taken \n", benchmarkResult.N, benchmarkResult.NsPerOp(), benchmarkResult.AllocedBytesPerOp(), benchmarkResult.AllocsPerOp(), benchmarkResult.T)
//fmt.Printf("%s\t%s\n", benchmarkResult.String(), benchmarkResult.MemString())
}
// go test -v -run EthTx -bench=. -benchtime=3s
// go test -v -run BenchmarkAnyTxEth -bench BenchmarkAnyTxEth -benchtime=3s -benchmem -cover
// go test -v -run BenchmarkAnyTxStd -bench BenchmarkAnyTxStd -benchtime=3s -benchmem -cover
// go test -v -run BenchmarkEthTx -bench BenchmarkEthTx -benchtime=3s -benchmem
// go test -v -run BenchmarkStdTx -bench BenchmarkStdTx -benchtime=3s -benchmem
//// go test -v -run BenchmarkAnyTx -bench BenchmarkAnyTx -benchtime=3s -benchmem
// go test -v -run BenchmarkBytesEth -bench BenchmarkBytesEth -benchtime=3s -benchmem
// go test -v -run BenchmarkEmpty -bench BenchmarkEmpty -benchtime=3s -benchmem
// go test -v -run BenchmarkEmptyMsg -bench BenchmarkEmptyMsg -benchtime=3s -benchmem
// go test -v -run BenchmarkAnyTxEth -bench BenchmarkAnyTxEth -benchtime=20000x -benchmem
// go test -v -run BenchmarkAnyTxStd -bench BenchmarkAnyTxStd -benchtime=20000x -benchmem
// go test -v -run BenchmarkEthTx -bench BenchmarkEthTx -benchtime=20000x -benchmem
// go test -v -run BenchmarkStdTx -bench BenchmarkStdTx -benchtime=20000x -benchmem
//// go test -v -run BenchmarkAnyTx -bench BenchmarkAnyTx -benchtime=20000x -benchmem
// go test -v -run BenchmarkBytesEth -bench BenchmarkBytesEth -benchtime=20000x -benchmem
// go test -v -run BenchmarkEmpty -bench BenchmarkEmpty -benchtime=20000x -benchmem
// go test -v -run BenchmarkEmptyMsg -bench BenchmarkEmptyMsg -benchtime=20000x -benchmem
//BenchmarkEmptyMsg
// go test -v -run BenchmarkEthTx -bench BenchmarkEthTx -benchtime=1s
// go test -v -run BenchmarkStdTx -bench BenchmarkStdTx -benchtime=1s
// go test -v -run BenchmarkAnyTx -bench BenchmarkAnyTx -benchtime=1s
// go test -v -run BenchmarkBytesEth -bench BenchmarkBytesEth -benchtime=1s
// go test -v -run BenchmarkEthTx -bench BenchmarkEthTx -benchtime=1x
// go test -v -run BenchmarkStdTx -bench BenchmarkStdTx -benchtime=1x
// go test -v -run BenchmarkAnyTx -bench BenchmarkAnyTx -benchtime=1x
// go test -v -run BenchmarkBytesEth -bench BenchmarkBytesEth -benchtime=1x
// go test -v -run BenchmarkEthTx -bench BenchmarkEthTx -benchtime=100x
// go test -v -run BenchmarkStdTx -bench BenchmarkStdTx -benchtime=100x
// go test -v -run BenchmarkAnyTx -bench BenchmarkAnyTx -benchtime=100x
// go test -v -run BenchmarkBytesEth -bench BenchmarkBytesEth -benchtime=100x
// go test -v -run BenchmarkEthTx -bench BenchmarkEthTx -benchtime=100x -benchmem
// go test -v -run BenchmarkStdTx -bench BenchmarkStdTx -benchtime=100x -benchmem
// go test -v -run BenchmarkAnyTx -bench BenchmarkAnyTx -benchtime=100x -benchmem
// go test -v -run BenchmarkBytesEth -bench BenchmarkBytesEth -benchtime=100x -benchmem
// go test -v -run TestGrpcServer -timeout 0
//BenchmarkAny
// go test -v -run Any -bench=. -benchtime=3s
// go test -v -run EthTx -bench=. -benchtime=3s
// go test -v -run BenchmarkQueueAnyTx -bench BenchmarkQueueAnyTx -benchtime=3s -benchmem
// go test -v -run BenchmarkQueueEthTx -bench BenchmarkQueueEthTx -benchtime=3s -benchmem
// go test -v -run BenchmarkQueueStdTx -bench BenchmarkQueueStdTx -benchtime=3s -benchmem
// go test -v -run BenchmarkQueueBytesEth -bench BenchmarkQueueBytesEth -benchtime=3s -benchmem
package main_test
import (
"context"
"crypto/ecdsa"
"errors"
"fmt"
"time"
"unsafe"
"math/big"
"net"
"testing"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/emptypb"
base "github.com/CaduceusMetaverseProtocol/metaprotocol/gen/proto/go/base/v1"
ring "github.com/CaduceusMetaverseProtocol/metaprotocol/gen/proto/go/ring/v1"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
)
var countParallelAsEth int64
var countParallelAsStd int64
var countParallelAsAny int64
var countParallelAsBytes int64
type RingServer struct {
ring.UnimplementedRingServiceServer
}
func (*RingServer) SendEmptyMsg(context.Context, *ring.EmptyRequest) (*ring.EmptyResponse, error) {
//fmt.Printf("SendEmptyMsg: %s \n", time.Now())
return &ring.EmptyResponse{}, nil
}
func (*RingServer) SendEmpty(context.Context, *emptypb.Empty) (*emptypb.Empty, error) {
return &emptypb.Empty{}, nil
}
func (*RingServer) SendTxAsEth(ctx context.Context, in *base.TransactionEth) (*ring.SendRawTransactionResponse, error) {
addr := common.Address{}
copy(addr[:], in.Tx.Inner.Recipient.Address[:common.AddressLength])
ethTx := types.NewTx(&types.LegacyTx{
Nonce: in.Tx.Inner.AccountNonce,
To: &addr,
Value: new(big.Int).SetBytes(in.Tx.Inner.Amount),
Gas: in.Tx.Inner.GasLimit,
GasPrice: new(big.Int).SetBytes(in.Tx.Inner.Price),
Data: in.Tx.Inner.Payload,
V: new(big.Int).SetBytes(in.Tx.Inner.V),
R: new(big.Int).SetBytes(in.Tx.Inner.R),
S: new(big.Int).SetBytes(in.Tx.Inner.S),
})
// atomic.AddInt64(&countParallelAsEth, 1)
// defer fmt.Println("countParallelAsEth", countParallelAsEth)
return &ring.SendRawTransactionResponse{TxHash: ethTx.Hash().Bytes()}, nil
}
func (*RingServer) SendTxAsStd(ctx context.Context, in *base.TransactionStd) (*ring.SendRawTransactionResponse, error) {
addr := common.Address{}
copy(addr[:], in.Tx.Inner.Recipient.Address[:common.AddressLength])
ethTx := types.NewTx(&types.LegacyTx{
Nonce: in.Tx.Inner.AccountNonce,
To: &addr,
Value: new(big.Int).SetBytes(in.Tx.Inner.Amount),
Gas: in.Tx.Inner.GasLimit,
GasPrice: new(big.Int).SetBytes(in.Tx.Inner.Price),
Data: in.Tx.Inner.Payload,
V: new(big.Int).SetBytes(in.Tx.Inner.V),
R: new(big.Int).SetBytes(in.Tx.Inner.R),
S: new(big.Int).SetBytes(in.Tx.Inner.S),
})
// atomic.AddInt64(&countParallelAsStd, 1)
// defer fmt.Println("countParallelAsStd", countParallelAsStd)
return &ring.SendRawTransactionResponse{TxHash: ethTx.Hash().Bytes()}, nil
}
func (*RingServer) SendTxAsAny(ctx context.Context, in *base.Transaction) (*ring.SendRawTransactionResponse, error) {
msg, err := in.Tx.UnmarshalNew()
if err != nil {
return nil, err
}
// atomic.AddInt64(&countParallelAsAny, 1)
// defer fmt.Println("countParallelAsAny", countParallelAsAny)
switch m := msg.(type) {
case *base.EthTx:
addr := common.Address{}
copy(addr[:], m.Inner.Recipient.Address[:common.AddressLength])
ethTx := types.NewTx(&types.LegacyTx{
Nonce: m.Inner.AccountNonce,
To: &addr,
Value: new(big.Int).SetBytes(m.Inner.Amount),
Gas: m.Inner.GasLimit,
GasPrice: new(big.Int).SetBytes(m.Inner.Price),
Data: m.Inner.Payload,
V: new(big.Int).SetBytes(m.Inner.V),
R: new(big.Int).SetBytes(m.Inner.R),
S: new(big.Int).SetBytes(m.Inner.S),
})
return &ring.SendRawTransactionResponse{TxHash: ethTx.Hash().Bytes()}, nil
case *base.StdTx:
addr := common.Address{}
copy(addr[:], m.Inner.Recipient.Address[:common.AddressLength])
ethTx := types.NewTx(&types.LegacyTx{
Nonce: m.Inner.AccountNonce,
To: &addr,
Value: new(big.Int).SetBytes(m.Inner.Amount),
Gas: m.Inner.GasLimit,
GasPrice: new(big.Int).SetBytes(m.Inner.Price),
Data: m.Inner.Payload,
V: new(big.Int).SetBytes(m.Inner.V),
R: new(big.Int).SetBytes(m.Inner.R),
S: new(big.Int).SetBytes(m.Inner.S),
})
return &ring.SendRawTransactionResponse{TxHash: ethTx.Hash().Bytes()}, nil
default:
return nil, errors.New(fmt.Sprintf("server expected tx type: %T and %T, but actually is %T", base.EthTx{}, base.StdTx{}, msg))
}
}
func (*RingServer) SendTxAsBytes(ctx context.Context, in *base.TransactionBytes) (*ring.SendRawTransactionResponse, error) {
ethTx := types.Transaction{}
if err := ethTx.UnmarshalBinary(in.Tx); err != nil {
return nil, err
}
// atomic.AddInt64(&countParallelAsBytes, 1)
// defer fmt.Printf("countParallelAsBytes: %d\n", countParallelAsBytes)
return &ring.SendRawTransactionResponse{TxHash: ethTx.Hash().Bytes()}, nil
}
func TestGrpcServer(t *testing.T) {
lis, err := net.Listen("tcp", ":9006")
if err != nil {
t.Fatal(err)
}
s := grpc.NewServer()
ring.RegisterRingServiceServer(s, &RingServer{})
err = s.Serve(lis)
if err != nil {
t.Fatal(err)
}
}
func TestType(t *testing.T) {
ethTx := base.EthTx{}
fmt.Println(ethTx.ProtoReflect().Descriptor().FullName())
}
func TestAnyTx(t *testing.T) {
local, _ := crypto.HexToECDSA("FD5CC6F5E7E2805E920AC5DC83D5AF1106F9C92F0C04F9D5E1FD4261B4B4464A")
publicKey := local.Public()
publicKeyECDSA, _ := publicKey.(*ecdsa.PublicKey)
fromAddr := crypto.PubkeyToAddress(*publicKeyECDSA)
remote, _ := crypto.GenerateKey()
tx, err := pricedTransaction(crypto.PubkeyToAddress(remote.PublicKey), 0, 100000, big.NewInt(1), local)
if err != nil {
t.Fatal(err)
}
conn, err := grpc.Dial("127.0.0.1:9006", grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
t.Fatal(err)
}
defer conn.Close()
c := ring.NewRingServiceClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
_, _ = c, ctx
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
inner.From = fromAddr.Bytes()
ethTx := base.EthTx{Inner: &inner}
ethTxAsAny, err := anypb.New(&ethTx)
if err != nil {
t.Fatal(err)
}
//fmt.Println("ethTxAsAny.ProtoReflect().Descriptor().FullName()", ethTx.ProtoReflect().Descriptor().FullName())
res, err := c.SendTxAsAny(ctx, &base.Transaction{Tx: ethTxAsAny})
_ = res
if err != nil {
t.Fatal(err)
}
stdInner := base.StdTxData{
AccountNonce: tx.Nonce(),
Price: tx.GasPrice().Bytes(),
GasLimit: tx.Gas(),
Payload: tx.Data(),
}
// v, r, sigs := tx.RawSignatureValues()
stdInner.V = v.Bytes()
stdInner.R = r.Bytes()
stdInner.S = sigs.Bytes()
stdInner.Amount = tx.Value().Bytes()
//addr := base.Address{Address: tx.To().Bytes()}
stdInner.Recipient = &addr
stdTx := base.StdTx{Inner: &stdInner}
_ = stdTx
stdTxAsAny, err := anypb.New(&stdTx)
if err != nil {
t.Fatal(err)
}
_ = stdTxAsAny
res, err = c.SendTxAsAny(ctx, &base.Transaction{Tx: stdTxAsAny})
if err != nil {
t.Fatal(err)
}
_ = res
}
func TestSizeOf(t *testing.T) {
local, _ := crypto.HexToECDSA("FD5CC6F5E7E2805E920AC5DC83D5AF1106F9C92F0C04F9D5E1FD4261B4B4464A")
//publicKey := local.Public()
//publicKeyECDSA, _ := publicKey.(*ecdsa.PublicKey)
//fromAddr := crypto.PubkeyToAddress(*publicKeyECDSA)
remote, _ := crypto.GenerateKey()
tx, err := pricedTransaction(crypto.PubkeyToAddress(remote.PublicKey), 0, 100000, big.NewInt(1), local)
if err != nil {
t.Fatal(err)
}
fmt.Println(unsafe.Sizeof(*tx))
}
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