Commit 30c64eea authored by Ubuntu's avatar Ubuntu

update unit test

parent 71bb6705
package main
import (
"crypto/ecdsa"
"math/big"
"sync"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
)
func buildSendTx(nonce uint64, to common.Address, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte, chainId *big.Int, privateKey *ecdsa.PrivateKey) (*types.Transaction, error) {
return buildTx(nonce, to, amount, gasLimit, gasPrice, data, chainId, privateKey)
}
func buildOriginalTx(nonce uint64, to common.Address, amount int64, chainId *big.Int, privateKey *ecdsa.PrivateKey) (*types.Transaction, error) {
return buildTx(nonce, to, big.NewInt(amount), 0, big.NewInt(0), nil, chainId, privateKey)
}
func buildTx(nonce uint64, to common.Address, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte, chainId *big.Int, privateKey *ecdsa.PrivateKey) (*types.Transaction, error) {
tx := types.NewTransaction(nonce, to, amount, gasLimit, gasPrice, data)
if privateKey != nil {
signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainId), privateKey)
if err != nil {
return nil, err
}
tx = signedTx
}
return tx, nil
}
type EthClient struct {
PrivateKey *ecdsa.PrivateKey
FromAddr common.Address
NodeUrl string
Nonce uint64
GasPrice *big.Int
ChainId *big.Int
GasLimit uint64
}
var originalTxPrivateKey string = "9e0944f587e1043d6e303644738b0c7c77ed15b176ca574ed0be40c0b9bbdc3a"
var toAddress common.Address = common.HexToAddress("0x0071B39fd266F8aeF392fb50F078A233b2218a0b")
var originalTxsQueue chan *types.Transaction //= make(chan *types.Transaction, 50000)
var originalTxParam EthClient
var FromAddr common.Address
// type TxWithFrom struct {
// From []byte
// Tx []byte
// }
func init() {
originalTxPrivatekeyAsECDSA, err := crypto.HexToECDSA(originalTxPrivateKey)
if err != nil {
panic(err)
}
originalTxPublicKey := originalTxPrivatekeyAsECDSA.Public()
originalTxPublicKeyECDSA, ok := originalTxPublicKey.(*ecdsa.PublicKey)
if !ok {
panic("publicKey.(*ecdsa.PublicKey) not ok")
}
originalTxFromAddress := crypto.PubkeyToAddress(*originalTxPublicKeyECDSA)
originalTxParam = EthClient{
PrivateKey: originalTxPrivatekeyAsECDSA,
FromAddr: originalTxFromAddress,
Nonce: 0,
}
FromAddr = originalTxFromAddress
}
var doneOnce sync.Once
func ProduceOriginalTx(firstdone chan bool) error {
if originalTxsQueue == nil {
originalTxsQueue = make(chan *types.Transaction, 50000)
}
for {
if len(originalTxsQueue) == 50000 {
doneOnce.Do(func() {
firstdone <- true
})
}
tx, err := buildOriginalTx(originalTxParam.Nonce, toAddress, 0, big.NewInt(256), nil)
if err != nil {
return err
}
originalTxParam.Nonce += 1
originalTxsQueue <- tx
}
}
func ProduceOriginalTxByCount(count int64) (chan *types.Transaction, error) {
var txsQueue chan *types.Transaction = make(chan *types.Transaction, count)
for i := 0; int64(i) < count; i++ {
tx, err := buildOriginalTx(originalTxParam.Nonce, toAddress, 0, big.NewInt(256), nil)
if err != nil {
return nil, err
}
originalTxParam.Nonce += 1
txsQueue <- tx
}
return txsQueue, nil
}
...@@ -178,10 +178,10 @@ func pricedTransaction(to common.Address, nonce uint64, gaslimit uint64, gaspric ...@@ -178,10 +178,10 @@ func pricedTransaction(to common.Address, nonce uint64, gaslimit uint64, gaspric
} }
// go test -v -run EthTx -bench=. -benchtime=3s // go test -v -run EthTx -bench=. -benchtime=3s
// go test -v -run BenchmarkEthTx -bench BenchmarkEthTx -benchtime=3s // go test -v -run BenchmarkEthTx -bench BenchmarkEthTx -benchtime=3s -benchmem
// go test -v -run BenchmarkStdTx -bench BenchmarkStdTx -benchtime=3s // go test -v -run BenchmarkStdTx -bench BenchmarkStdTx -benchtime=3s -benchmem
// go test -v -run BenchmarkAnyTx -bench BenchmarkAnyTx -benchtime=3s // go test -v -run BenchmarkAnyTx -bench BenchmarkAnyTx -benchtime=3s -benchmem
// go test -v -run BenchmarkBytesEth -bench BenchmarkBytesEth -benchtime=3s // go test -v -run BenchmarkBytesEth -bench BenchmarkBytesEth -benchtime=3s -benchmem
// go test -v -run BenchmarkEthTx -bench BenchmarkEthTx -benchtime=1s // go test -v -run BenchmarkEthTx -bench BenchmarkEthTx -benchtime=1s
// go test -v -run BenchmarkStdTx -bench BenchmarkStdTx -benchtime=1s // go test -v -run BenchmarkStdTx -bench BenchmarkStdTx -benchtime=1s
......
package main
import (
"bytes"
"context"
"sync"
//"encoding/hex"
"testing"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/protobuf/types/known/anypb"
base "github.com/CaduceusMetaverseProtocol/metaprotocol/gen/proto/go/base/v1"
ring "github.com/CaduceusMetaverseProtocol/metaprotocol/gen/proto/go/ring/v1"
)
// 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
var once sync.Once
var onceHash sync.Once
func BenchmarkQueueEthTx(b *testing.B) {
b.Logf("b.N: %d \n", b.N)
// firstDone := make(chan bool, 1)
// onceFunc := func() {
// go ProduceOriginalTx(firstDone)
// <-firstDone
// }
// once.Do(onceFunc)
onceFunc := func() {
txsQueue, err := ProduceOriginalTxByCount(500000)
if err != nil {
b.Fatal(err)
}
originalTxsQueue = txsQueue
}
once.Do(onceFunc)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
conn, err := grpc.Dial("127.0.0.1:9006", grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
b.Fatal(err)
}
defer conn.Close()
c := ring.NewRingServiceClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
tx := <-originalTxsQueue
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()
res, err := c.SendTxAsEth(ctx, &base.TransactionEth{Tx: &base.EthTx{Inner: &inner}})
if err != nil {
b.Fatal(err)
}
_ = res
if bytes.Compare(tx.Hash().Bytes(), res.TxHash) != 0 {
b.Fatal(err)
}
onceHash.Do(func() {
b.Logf("response: %x local: %x \n", res.TxHash, tx.Hash().Bytes())
})
}
})
}
func BenchmarkQueueStdTx(b *testing.B) {
b.Logf("b.N: %d\n", b.N)
// firstDone := make(chan bool, 1)
// onceFunc := func() {
// go ProduceOriginalTx(firstDone)
// <-firstDone
// }
// once.Do(onceFunc)
onceFunc := func() {
txsQueue, err := ProduceOriginalTxByCount(500000)
if err != nil {
b.Fatal(err)
}
originalTxsQueue = txsQueue
}
once.Do(onceFunc)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
conn, err := grpc.Dial("127.0.0.1:9006", grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
b.Fatal(err)
}
defer conn.Close()
c := ring.NewRingServiceClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
tx := <-originalTxsQueue
inner := base.StdTxData{
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()
res, err := c.SendTxAsStd(ctx, &base.TransactionStd{Tx: &base.StdTx{Inner: &inner}})
if err != nil {
b.Fatal(err)
}
_ = res
//fmt.Printf("%x \n", res.TxHash)
if bytes.Compare(tx.Hash().Bytes(), res.TxHash) != 0 {
b.Fatal(err)
}
onceHash.Do(func() {
b.Logf("response: %x local: %x \n", res.TxHash, tx.Hash().Bytes())
})
}
})
}
func BenchmarkQueueAnyTx(b *testing.B) {
b.Logf("b.N: %d \n", b.N)
// firstDone := make(chan bool, 1)
// onceFunc := func() {
// go ProduceOriginalTx(firstDone)
// <-firstDone
// }
// once.Do(onceFunc)
onceFunc := func() {
txsQueue, err := ProduceOriginalTxByCount(500000)
if err != nil {
b.Fatal(err)
}
originalTxsQueue = txsQueue
}
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() {
conn, err := grpc.Dial("127.0.0.1:9006", grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
b.Fatal(err)
}
defer conn.Close()
c := ring.NewRingServiceClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
tx := <-originalTxsQueue
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)
res, err := c.SendTxAsAny(ctx, &base.Transaction{Tx: ethTxAsAny})
if err != nil {
b.Fatal(err)
}
_ = res
if bytes.Compare(tx.Hash().Bytes(), res.TxHash) != 0 {
b.Fatal(err)
}
onceHash.Do(func() {
b.Logf("response: %x local: %x \n", res.TxHash, tx.Hash().Bytes())
})
}
})
}
func BenchmarkQueueBytesEth(b *testing.B) {
b.Logf("b.N: %d \n", b.N)
// firstDone := make(chan bool, 1)
// onceFunc := func() {
// go ProduceOriginalTx(firstDone)
// <-firstDone
// }
// once.Do(onceFunc)
onceFunc := func() {
txsQueue, err := ProduceOriginalTxByCount(500000)
if err != nil {
b.Fatal(err)
}
originalTxsQueue = txsQueue
}
once.Do(onceFunc)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
conn, err := grpc.Dial("127.0.0.1:9006", grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
b.Fatal(err)
}
defer conn.Close()
c := ring.NewRingServiceClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
tx := <-originalTxsQueue
txAsBytes, err := tx.MarshalBinary()
if err != nil {
b.Fatal(err)
}
res, err := c.SendTxAsBytes(ctx, &base.TransactionBytes{Tx: txAsBytes, From: FromAddr[:]})
if err != nil {
b.Fatal(err)
}
_ = res
if bytes.Compare(tx.Hash().Bytes(), res.TxHash) != 0 {
b.Fatal(err)
}
onceHash.Do(func() {
b.Logf("response: %x local: %x \n", res.TxHash, tx.Hash().Bytes())
})
}
})
}
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