github.com/amazechain/amc@v0.1.3/common/block/block_test.go (about) 1 // Copyright 2022 The AmazeChain Authors 2 // This file is part of the AmazeChain library. 3 // 4 // The AmazeChain library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The AmazeChain library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the AmazeChain library. If not, see <http://www.gnu.org/licenses/>. 16 17 package block 18 19 import ( 20 "encoding/hex" 21 "encoding/json" 22 "github.com/amazechain/amc/api/protocol/types_pb" 23 "github.com/amazechain/amc/internal/avm/rlp" 24 "google.golang.org/protobuf/proto" 25 "testing" 26 ) 27 28 func getBlock(tb testing.TB) *Block { 29 //sBlock := "0af5010a200c138c091cfdedd52b8a6f35f664243525de3dba58260788a8e0c1806f3fd0911214f61c62199512e8e85d7744d513b1b74bbe98aea11a200000000000000000000000000000000000000000000000000000000000000000222000000000000000000000000000000000000000000000000000000000000000002a20000000000000000000000000000000000000000000000000000000000000000032033078313a0330783850dfe882950662033078306a40e6bf01023645789ba3cb7e8200eb54289bccbc292b4f4796d2268fee49a7a9a993c539ef50815018014438436b5e5a10c27a4ef2ea4dda9efe5ed5b30ab6fa061200" 30 sBlock := "0af5010a2009876b2d89375c505dd4128e2ea6bc588d8e9beb5ec11af94fc2f9d3e9eae5081214f61c62199512e8e85d7744d513b1b74bbe98aea11a20de7f0cbe30d14da2d6950c0643fe164b9e6ea421550263a02eaaaff257c2f6cc22208ac5c253af5118c7a98eecb957a37bf72a1f5556b8dfd1932322de5e10e32f312a2015647dcb388cef8e815a09408301361aed626048764adb507a06dc08ba593ae332033078323a0330783150858283950662033078306a4080e6d9a605d8be11339047b2541d7f59f3a193a0dad1e27360d016fba82c5449310b485172b8b2a705c8a8111c960149bccdd5c0ffea924c3fe6343aafd258021200" 31 bBlock, err := hex.DecodeString(sBlock) 32 if err != nil { 33 tb.Fatal(err) 34 } 35 36 var block types_pb.Block 37 err = proto.Unmarshal(bBlock, &block) 38 if err != nil { 39 tb.Fatal(err) 40 } 41 42 //tb.Logf("block number: %v", block.Header.Number.String()) 43 44 var b Block 45 err = b.FromProtoMessage(&block) 46 if err != nil { 47 tb.Fatal(err) 48 } 49 50 return &b 51 } 52 53 type exBlock struct { 54 H *Header 55 B *Body 56 } 57 58 //func TestSize(t *testing.T) { 59 // block := getBlock(t) 60 // pb := block.ToProtoMessage() 61 // 62 // buf1, err := proto.Marshal(pb) 63 // if err != nil { 64 // t.Fatal(err) 65 // } 66 // 67 // exB := exBlock{ 68 // H: block.header, 69 // B: block.body, 70 // } 71 // 72 // buf2, err := rlp.EncodeToBytes(&exB) 73 // if err != nil { 74 // t.Fatal(err) 75 // } 76 // 77 // t.Logf("proto size: %d, rlp size: %d", len(buf1), len(buf2)) 78 //} 79 80 func BenchmarkProtobuf(b *testing.B) { 81 block := getBlock(b) 82 pb := block.ToProtoMessage() 83 b.ResetTimer() 84 for i := 0; i < b.N; i++ { 85 _, _ = proto.Marshal(pb) 86 } 87 } 88 89 func BenchmarkRlp(b *testing.B) { 90 block := getBlock(b) 91 exB := exBlock{ 92 H: block.header, 93 B: block.body, 94 } 95 b.ResetTimer() 96 for i := 0; i < b.N; i++ { 97 _, _ = rlp.EncodeToBytes(&exB) 98 } 99 } 100 101 func BenchmarkJson(b *testing.B) { 102 block := getBlock(b) 103 exB := exBlock{ 104 H: block.header, 105 B: block.body, 106 } 107 b.ResetTimer() 108 for i := 0; i < b.N; i++ { 109 _, _ = json.Marshal(exB) 110 } 111 } 112 113 func BenchmarkProtobufUnmarshal(b *testing.B) { 114 //sBlock := "0af5010a200c138c091cfdedd52b8a6f35f664243525de3dba58260788a8e0c1806f3fd0911214f61c62199512e8e85d7744d513b1b74bbe98aea11a200000000000000000000000000000000000000000000000000000000000000000222000000000000000000000000000000000000000000000000000000000000000002a20000000000000000000000000000000000000000000000000000000000000000032033078313a0330783850dfe882950662033078306a40e6bf01023645789ba3cb7e8200eb54289bccbc292b4f4796d2268fee49a7a9a993c539ef50815018014438436b5e5a10c27a4ef2ea4dda9efe5ed5b30ab6fa061200" 115 sBlock := "0af5010a2009876b2d89375c505dd4128e2ea6bc588d8e9beb5ec11af94fc2f9d3e9eae5081214f61c62199512e8e85d7744d513b1b74bbe98aea11a20de7f0cbe30d14da2d6950c0643fe164b9e6ea421550263a02eaaaff257c2f6cc22208ac5c253af5118c7a98eecb957a37bf72a1f5556b8dfd1932322de5e10e32f312a2015647dcb388cef8e815a09408301361aed626048764adb507a06dc08ba593ae332033078323a0330783150858283950662033078306a4080e6d9a605d8be11339047b2541d7f59f3a193a0dad1e27360d016fba82c5449310b485172b8b2a705c8a8111c960149bccdd5c0ffea924c3fe6343aafd258021200" 116 bBlock, err := hex.DecodeString(sBlock) 117 if err != nil { 118 b.Fatal(err) 119 } 120 b.ResetTimer() 121 for i := 0; i < b.N; i++ { 122 var block types_pb.Block 123 _ = proto.Unmarshal(bBlock, &block) 124 } 125 } 126 127 func BenchmarkRlpDecode(b *testing.B) { 128 block := getBlock(b) 129 exB := exBlock{ 130 H: block.header, 131 B: block.body, 132 } 133 134 buf, err := rlp.EncodeToBytes(&exB) 135 if err != nil { 136 b.Fatal(err) 137 } 138 139 b.ResetTimer() 140 141 for i := 0; i < b.N; i++ { 142 var exB exBlock 143 _ = rlp.DecodeBytes(buf, &exB) 144 } 145 } 146 147 func BenchmarkJsonDecode(b *testing.B) { 148 block := getBlock(b) 149 exB := exBlock{ 150 H: block.header, 151 B: block.body, 152 } 153 bytes, err := json.Marshal(exB) 154 if err != nil { 155 b.Fatal(err) 156 } 157 b.ResetTimer() 158 for i := 0; i < b.N; i++ { 159 var exB exBlock 160 _ = json.Unmarshal(bytes, &exB) 161 } 162 }