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())
}
...@@ -5,8 +5,6 @@ import ( ...@@ -5,8 +5,6 @@ import (
"context" "context"
"sync" "sync"
//"encoding/hex"
"testing" "testing"
"time" "time"
...@@ -42,20 +40,24 @@ func BenchmarkQueueEthTx(b *testing.B) { ...@@ -42,20 +40,24 @@ func BenchmarkQueueEthTx(b *testing.B) {
// once.Do(onceFunc) // once.Do(onceFunc)
onceFunc := func() { //b.StopTimer()
txsQueue, err := ProduceOriginalTxByCount(500000)
if err != nil { //onceFunc := func() {
b.Fatal(err) txsQueue, err := ProduceOriginalTxByCount(500000)
}
originalTxsQueue = txsQueue if err != nil {
b.Fatal(err)
} }
once.Do(onceFunc) originalTxsQueue = txsQueue
//}
//once.Do(onceFunc)
b.ResetTimer() b.ResetTimer()
//b.StartTimer()
b.RunParallel(func(pb *testing.PB) { b.RunParallel(func(pb *testing.PB) {
for pb.Next() { for pb.Next() {
...@@ -126,17 +128,16 @@ func BenchmarkQueueStdTx(b *testing.B) { ...@@ -126,17 +128,16 @@ func BenchmarkQueueStdTx(b *testing.B) {
// once.Do(onceFunc) // once.Do(onceFunc)
onceFunc := func() { //onceFunc := func() {
txsQueue, err := ProduceOriginalTxByCount(500000) txsQueue, err := ProduceOriginalTxByCount(500000)
if err != nil {
b.Fatal(err)
}
originalTxsQueue = txsQueue if err != nil {
b.Fatal(err)
} }
once.Do(onceFunc) originalTxsQueue = txsQueue
//}
//once.Do(onceFunc)
b.ResetTimer() b.ResetTimer()
...@@ -211,17 +212,17 @@ func BenchmarkQueueAnyTx(b *testing.B) { ...@@ -211,17 +212,17 @@ func BenchmarkQueueAnyTx(b *testing.B) {
// once.Do(onceFunc) // once.Do(onceFunc)
onceFunc := func() { //onceFunc := func() {
txsQueue, err := ProduceOriginalTxByCount(500000) txsQueue, err := ProduceOriginalTxByCount(500000)
if err != nil { if err != nil {
b.Fatal(err) b.Fatal(err)
}
originalTxsQueue = txsQueue
} }
once.Do(onceFunc) originalTxsQueue = txsQueue
//}
//once.Do(onceFunc)
b.ResetTimer() b.ResetTimer()
...@@ -302,17 +303,17 @@ func BenchmarkQueueBytesEth(b *testing.B) { ...@@ -302,17 +303,17 @@ func BenchmarkQueueBytesEth(b *testing.B) {
// once.Do(onceFunc) // once.Do(onceFunc)
onceFunc := func() { //onceFunc := func() {
txsQueue, err := ProduceOriginalTxByCount(500000) txsQueue, err := ProduceOriginalTxByCount(500000)
if err != nil { if err != nil {
b.Fatal(err) b.Fatal(err)
}
originalTxsQueue = txsQueue
} }
once.Do(onceFunc) originalTxsQueue = txsQueue
//}
//once.Do(onceFunc)
b.ResetTimer() 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