stat_test.go 6.44 KB
Newer Older
duanjinfei's avatar
duanjinfei committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
package benchmark_test

import (
	"context"
	"crypto/ecdsa"
	"fmt"

	"testing"
	"time"

	"math/big"

	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/crypto"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"

	//"google.golang.org/grpc/encoding/proto"
	//"github.com/protocolbuffers/protobuf-go/proto"
	"google.golang.org/protobuf/proto"
	"google.golang.org/protobuf/types/known/anypb"
)

var tableTx = []struct {
	input int
}{
	{input: 100},
	{input: 200},
	{input: 500},
	{input: 1000},
	{input: 2000},
	{input: 5000},
	{input: 10000},
	{input: 20000},
	// {input: 50000},
}

func RepeatedEthTx(txl int, b *testing.B) {

	//b.Logf("b.N: %d\n", b.N)

	local, err := crypto.HexToECDSA("FD5CC6F5E7E2805E920AC5DC83D5AF1106F9C92F0C04F9D5E1FD4261B4B4464A")
	if err != nil {
		b.Fatal(err)
	}
	publicKey := local.Public()
	publicKeyECDSA, _ := publicKey.(*ecdsa.PublicKey)
	fromAddr := crypto.PubkeyToAddress(*publicKeyECDSA)

	remote, err := crypto.GenerateKey()
	if err != nil {
		b.Fatal(err)
	}

	tx, err := pricedTransaction(crypto.PubkeyToAddress(remote.PublicKey), 0, 100000, big.NewInt(1), local)

	//txsQueue, err := ProduceOriginalTxByCount(500000)

	if err != nil {
		b.Fatal(err)
	}

	beginTime := time.Now()
	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()

			txs := make([]*base.TransactionEth, 0, txl)

			for i := 0; i < txl; i++ {

				//tx := <-txsQueue

				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()

				txs = append(txs, &base.TransactionEth{Tx: &base.EthTx{Inner: &inner}})
			}

			req := &base.RepeatedEthTx{Txs: txs}

			out, err := proto.Marshal(req)

			if err != nil {
				b.Fatal(err)
			}

			b.SetBytes(int64(len(out)))

			res, err := c.SendRepeatedEthTx(ctx, req)

			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())
			// })
		}
	})

	b.ReportMetric(float64(b.N)/float64(time.Since(beginTime).Seconds()), "tps")
}

func RepeatedTxEthAsAny(txl int, b *testing.B) {

	// 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 {
		b.Fatal(err)
	}
	//}
	// once.Do(onceFunc)

	beginTime := time.Now()

	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()

			txs := make([]*base.TransactionEth, 0, txl)

			for i := 0; i < txl; i++ {

				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()

				txs = append(txs, &base.TransactionEth{Tx: &base.EthTx{Inner: &inner}})
			}

			ethTxAsAny, err := anypb.New(&base.RepeatedEthTx{Txs: txs})
			if err != nil {
				b.Fatal(err)
			}

			req := &base.Transaction{Tx: ethTxAsAny}

			out, err := proto.Marshal(req)

			if err != nil {
				b.Fatal(err)
			}

			b.SetBytes(int64(len(out)))

			res, err := c.SendTxAsAny(ctx, req)

			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())
			// })

		}
	})

	b.ReportMetric(float64(b.N)/float64(time.Since(beginTime).Seconds()), "tps")

}

// go test   -v -run  BenchmarkAnyTxEth -bench BenchmarkRepeated -benchmem  -count 5 | tee old.txt
// Small p-values indicate that the two distributions are significantly different.
// If the test indicates that there was no significant change between the two benchmarks (defined as p > 0.05),
// benchstat displays a single ~ instead of the percent change.
/*

benchstat -geomean -delta-test utest -html RepeatedEthTx.txt RepeatedTxEthAsAny.txt > utest.html

benchstat -geomean -delta-test ttest -html RepeatedEthTx.txt RepeatedTxEthAsAny.txt > ttest.html

benchstat -geomean -delta-test none -html RepeatedEthTx.txt RepeatedTxEthAsAny.txt > none.html

*/
func BenchmarkRepeated(b *testing.B) {
	for _, v := range tableTx {
		b.Run(fmt.Sprintf("input_size_%d", v.input), func(b *testing.B) {
			//go test   -v -run  BenchmarkAnyTxEth -bench BenchmarkRepeated -benchmem  -count 30 | tee RepeatedEthTx.txt
			//RepeatedEthTx(v.input, b)
			//go test   -v -run  BenchmarkAnyTxEth -bench BenchmarkRepeated -benchmem  -count 30 | tee RepeatedTxEthAsAny.txt
			RepeatedTxEthAsAny(v.input, b)

		})
	}
}

// 验证和 b.Run sub-benchmark的时间一致;
func Benchmark200RepeatedEthTx(b *testing.B) {

	RepeatedEthTx(5000, b)

}

// func BenchmarkRepeatedNew(b *testing.B) {
// 	for _, v := range tableTx {
// 		b.Run(fmt.Sprintf("input_size_%d", v.input), func(b *testing.B) {
// 			RepeatedTxEthAsAny(v.input, b)

// 		})
// 	}
// }