github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/common/sink/zero_copy_sink_test.go (about) 1 /* 2 * Copyright (C) 2018 The ontology Authors 3 * This file is part of The ontology library. 4 * 5 * The ontology is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU Lesser General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * 10 * The ontology is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public License 16 * along with The ontology. If not, see <http://www.gnu.org/licenses/>. 17 */ 18 package sink 19 20 import ( 21 "testing" 22 23 "bytes" 24 25 "math/big" 26 27 "github.com/stretchr/testify/assert" 28 "github.com/sixexorg/magnetic-ring/common" 29 ) 30 31 func TestSourceSink(t *testing.T) { 32 addressFs1, _ := common.ToAddress("ct1qK96vAkK6E8S7JgYUY3YY28Qhj6cmfdy") 33 addressFs2, _ := common.ToAddress("ct1qK96vAkK6E8S7JgYUY3YY28Qhj6cmfdz") 34 bint, _ := big.NewInt(0).SetString("1232222222222222222222222222222222222222222222", 10) 35 36 txamounts := &common.TxOuts{ 37 Tos: []*common.TxOut{{ 38 Address: addressFs1, 39 Amount: bint, 40 }, 41 { 42 Address: addressFs2, 43 Amount: big.NewInt(300), 44 }, 45 }, 46 } 47 bigInt := big.NewInt(1000) 48 49 a3 := uint8(100) 50 a4 := uint16(65535) 51 a5 := uint32(4294967295) 52 a6 := uint64(18446744073709551615) 53 a7 := uint64(18446744073709551615) 54 a8 := []byte{10, 11, 12} 55 a9 := "hello onchain." 56 a10, _ := TxOutsToComplex(txamounts) 57 a11, _ := BigIntToComplex(bigInt) 58 59 sink := NewZeroCopySink(nil) 60 sink.WriteByte(a3) 61 sink.WriteUint16(a4) 62 sink.WriteUint32(a5) 63 sink.WriteUint64(a6) 64 sink.WriteVarUint(a7) 65 sink.WriteVarBytes(a8) 66 sink.WriteString(a9) 67 sink.WriteComplex(a10) 68 sink.WriteComplex(a11) 69 70 source := NewZeroCopySource(sink.Bytes()) 71 b3, _ := source.NextByte() 72 assert.Equal(t, a3, b3) 73 b4, _ := source.NextUint16() 74 assert.Equal(t, a4, b4) 75 b5, _ := source.NextUint32() 76 assert.Equal(t, a5, b5) 77 b6, _ := source.NextUint64() 78 assert.Equal(t, a6, b6) 79 b7, _, _, _ := source.NextVarUint() 80 assert.Equal(t, a7, b7) 81 b8, _, _, _ := source.NextVarBytes() 82 assert.Equal(t, a8, b8) 83 b9, _, _, _ := source.NextString() 84 assert.Equal(t, a9, b9) 85 b10, _ := source.NextComplex() 86 assert.Equal(t, a10, b10) 87 b11, _ := source.NextComplex() 88 assert.Equal(t, a11, b11) 89 } 90 91 func BenchmarkNewZeroCopySink_Serialize(b *testing.B) { 92 N := 1000 93 a3 := uint8(100) 94 a4 := uint16(65535) 95 a5 := uint32(4294967295) 96 a6 := uint64(18446744073709551615) 97 a7 := uint64(18446744073709551615) 98 a8 := []byte{10, 11, 12} 99 a9 := "hello onchain." 100 buf := new(bytes.Buffer) 101 for i := 0; i < b.N; i++ { 102 buf.Reset() 103 for j := 0; j < N; j++ { 104 WriteVarUint(buf, uint64(a3)) 105 WriteVarUint(buf, uint64(a4)) 106 WriteVarUint(buf, uint64(a5)) 107 WriteVarUint(buf, uint64(a6)) 108 WriteVarUint(buf, uint64(a7)) 109 WriteVarBytes(buf, a8) 110 WriteString(buf, a9) 111 112 buf.WriteByte(20) 113 buf.WriteByte(21) 114 buf.WriteByte(22) 115 } 116 } 117 } 118 119 func BenchmarkZeroCopySink(ben *testing.B) { 120 N := 1000 121 a3 := uint8(100) 122 a4 := uint16(65535) 123 a5 := uint32(4294967295) 124 a6 := uint64(18446744073709551615) 125 a7 := uint64(18446744073709551615) 126 a8 := []byte{10, 11, 12} 127 a9 := "hello onchain." 128 sink := NewZeroCopySink(nil) 129 for i := 0; i < ben.N; i++ { 130 sink.Reset() 131 for j := 0; j < N; j++ { 132 sink.WriteVarUint(uint64(a3)) 133 sink.WriteVarUint(uint64(a4)) 134 sink.WriteVarUint(uint64(a5)) 135 sink.WriteVarUint(uint64(a6)) 136 sink.WriteVarUint(uint64(a7)) 137 sink.WriteVarBytes(a8) 138 sink.WriteString(a9) 139 sink.WriteByte(20) 140 sink.WriteByte(21) 141 sink.WriteByte(22) 142 } 143 } 144 145 }