Skip to content

Commit eced64a

Browse files
committed
test: create more benchmarks for serde
Signed-off-by: Chris Gianelloni <wolf31o2@blinklabs.io>
1 parent fd01888 commit eced64a

File tree

11 files changed

+425
-1
lines changed

11 files changed

+425
-1
lines changed

cbor/decode_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,3 +271,52 @@ func TestDecodeById(t *testing.T) {
271271
}
272272
}
273273
}
274+
275+
func TestDecodeMaryTransactionCbor(t *testing.T) {
276+
// Mary transaction CBOR hex from real chain data
277+
maryTxHex := "83a50081825820377732953cbd7eb824e58291dd08599cfcfe6eedb49f590633610674fc3c33c50001818258390187acac5a3d0b41cd1c5e8c03af5be782f261f21beed70970ddee0873ae34f9d442c7f1a01de9f2dd520791122d6fbf3968c5f8328e9091331a597dbcf1021a0002fd99031a025c094a04828a03581c27b1b4470c84db78ce1ffbfff77bb068abb4e47d43cb6009caaa352358204a7537ce9eeaba1c650261f167827b4e12dd403c4bf13c56b2cba06288f7e9ab1a59682f001a1443fd00d81e82011864581de1ae34f9d442c7f1a01de9f2dd520791122d6fbf3968c5f8328e90913381581cae34f9d442c7f1a01de9f2dd520791122d6fbf3968c5f8328e9091338183011917706e33342e3139382e3234312e323336827468747470733a2f2f6769742e696f2f4a7543786e5820fa77d30bb41e2998233245d269ff5763ecf4371388214943ecef277cae45492783028200581cae34f9d442c7f1a01de9f2dd520791122d6fbf3968c5f8328e909133581c27b1b4470c84db78ce1ffbfff77bb068abb4e47d43cb6009caaa3523a10083825820922a22d07c0ca148105760cb767ece603574ea465d6697c87da8207c8936ebea58405594a100197379c0de715de0b5304e0546e661dae2f36b12173cc150a42215356a5600bf0c02954f02ce3620cfb7f12c23a19328fd00dd1194b4f363675ef407825820727c1891d01cf29ccd1146528221827dcf00a093498509404af77a8b15d77c925840f52e0e1403167212b11fe5d87b7cfdb2f39e5384979ac3625917127ad46763d864a7fcb7147c7b85322ada7ba8fe91c0b5152c74ef4ff0c8132b125e681af50382582073c16f2b67ff85307c4c5935bad1389b9ead473419dbad20f5d5e6436982992b58400572eed773b9a199fd486ebe61b480f05803d107ea97ff649f28b8874d3117f890f80657cbb6eea0d833c21e4e8bc7f1a27cddb9e24fc1ed79b04ddbdcd11d0ff6"
278+
279+
cborData, err := hex.DecodeString(maryTxHex)
280+
if err != nil {
281+
t.Fatalf("failed to decode Mary transaction hex: %s", err)
282+
}
283+
284+
var decoded any
285+
bytesRead, err := cbor.Decode(cborData, &decoded)
286+
if err != nil {
287+
t.Fatalf("failed to decode Mary transaction CBOR: %s", err)
288+
}
289+
290+
if bytesRead != len(cborData) {
291+
t.Fatalf(
292+
"did not read all bytes: read %d, expected %d",
293+
bytesRead,
294+
len(cborData),
295+
)
296+
}
297+
298+
// Verify it's an array (transactions are arrays)
299+
decodedSlice, ok := decoded.([]any)
300+
if !ok {
301+
t.Fatal("decoded Mary transaction is not an array")
302+
}
303+
304+
if len(decodedSlice) != 3 {
305+
t.Fatalf(
306+
"Mary transaction array length mismatch: got %d, expected 3",
307+
len(decodedSlice),
308+
)
309+
}
310+
}
311+
312+
func BenchmarkDecode(b *testing.B) {
313+
cborData, _ := hex.DecodeString("83010203")
314+
var dest any
315+
b.ResetTimer()
316+
for i := 0; i < b.N; i++ {
317+
_, err := cbor.Decode(cborData, &dest)
318+
if err != nil {
319+
b.Fatal(err)
320+
}
321+
}
322+
}

cbor/encode_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,14 @@ func TestEncodeIndefLengthByteString(t *testing.T) {
9090
)
9191
}
9292
}
93+
94+
func BenchmarkEncode(b *testing.B) {
95+
obj := []any{1, 2, 3}
96+
b.ResetTimer()
97+
for i := 0; i < b.N; i++ {
98+
_, err := cbor.Encode(obj)
99+
if err != nil {
100+
b.Fatal(err)
101+
}
102+
}
103+
}

ledger/allegra/block_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,28 @@ func TestAllegraUtxorpcBlock(t *testing.T) {
171171
}
172172
})
173173
}
174+
175+
func BenchmarkAllegraBlockDeserialization(b *testing.B) {
176+
blockCbor, _ := hex.DecodeString(allegraBlockHex)
177+
b.ResetTimer()
178+
for i := 0; i < b.N; i++ {
179+
var block allegra.AllegraBlock
180+
err := block.UnmarshalCBOR(blockCbor)
181+
if err != nil {
182+
b.Fatal(err)
183+
}
184+
}
185+
}
186+
187+
func BenchmarkAllegraBlockSerialization(b *testing.B) {
188+
blockCbor, _ := hex.DecodeString(allegraBlockHex)
189+
var block allegra.AllegraBlock
190+
err := block.UnmarshalCBOR(blockCbor)
191+
if err != nil {
192+
b.Fatal(err)
193+
}
194+
b.ResetTimer()
195+
for i := 0; i < b.N; i++ {
196+
_ = block.Cbor()
197+
}
198+
}

ledger/alonzo/block_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,28 @@ func TestAlonzoBlock_Utxorpc(t *testing.T) {
153153
)
154154
})
155155
}
156+
157+
func BenchmarkAlonzoBlockDeserialization(b *testing.B) {
158+
blockCbor, _ := hex.DecodeString(alonzoBlockHex)
159+
b.ResetTimer()
160+
for i := 0; i < b.N; i++ {
161+
var block alonzo.AlonzoBlock
162+
err := block.UnmarshalCBOR(blockCbor)
163+
if err != nil {
164+
b.Fatal(err)
165+
}
166+
}
167+
}
168+
169+
func BenchmarkAlonzoBlockSerialization(b *testing.B) {
170+
blockCbor, _ := hex.DecodeString(alonzoBlockHex)
171+
var block alonzo.AlonzoBlock
172+
err := block.UnmarshalCBOR(blockCbor)
173+
if err != nil {
174+
b.Fatal(err)
175+
}
176+
b.ResetTimer()
177+
for i := 0; i < b.N; i++ {
178+
_ = block.Cbor()
179+
}
180+
}

ledger/babbage/block_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,28 @@ func TestBabbageBlock_Utxorpc(t *testing.T) {
177177
}
178178
}
179179
}
180+
181+
func BenchmarkBabbageBlockDeserialization(b *testing.B) {
182+
blockCbor, _ := hex.DecodeString(babbageBlockHex)
183+
b.ResetTimer()
184+
for i := 0; i < b.N; i++ {
185+
var block babbage.BabbageBlock
186+
err := block.UnmarshalCBOR(blockCbor)
187+
if err != nil {
188+
b.Fatal(err)
189+
}
190+
}
191+
}
192+
193+
func BenchmarkBabbageBlockSerialization(b *testing.B) {
194+
blockCbor, _ := hex.DecodeString(babbageBlockHex)
195+
var block babbage.BabbageBlock
196+
err := block.UnmarshalCBOR(blockCbor)
197+
if err != nil {
198+
b.Fatal(err)
199+
}
200+
b.ResetTimer()
201+
for i := 0; i < b.N; i++ {
202+
_ = block.Cbor()
203+
}
204+
}

ledger/byron/block_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,28 @@ func TestByronTransaction_Utxorpc(t *testing.T) {
169169
}
170170
}
171171
}
172+
173+
func BenchmarkByronBlockDeserialization(b *testing.B) {
174+
blockCbor, _ := hex.DecodeString(byronBlockHex)
175+
b.ResetTimer()
176+
for i := 0; i < b.N; i++ {
177+
var block byron.ByronMainBlock
178+
err := block.UnmarshalCBOR(blockCbor)
179+
if err != nil {
180+
b.Fatal(err)
181+
}
182+
}
183+
}
184+
185+
func BenchmarkByronBlockSerialization(b *testing.B) {
186+
blockCbor, _ := hex.DecodeString(byronBlockHex)
187+
var block byron.ByronMainBlock
188+
err := block.UnmarshalCBOR(blockCbor)
189+
if err != nil {
190+
b.Fatal(err)
191+
}
192+
b.ResetTimer()
193+
for i := 0; i < b.N; i++ {
194+
_ = block.Cbor()
195+
}
196+
}

ledger/common/address_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package common
1616

1717
import (
18+
"encoding/hex"
1819
"reflect"
1920
"testing"
2021

@@ -383,3 +384,30 @@ func TestAddressToPlutusData(t *testing.T) {
383384
}
384385
}
385386
}
387+
388+
func BenchmarkAddressFromBytes(b *testing.B) {
389+
addrBytes, err := hex.DecodeString(
390+
"11e1317b152faac13426e6a83e06ff88a4d62cce3c1634ab0a5ec1330952563c5410bff6a0d43ccebb7c37e1f69f5eb260552521adff33b9c2",
391+
)
392+
if err != nil {
393+
b.Fatal(err)
394+
}
395+
b.ResetTimer()
396+
for i := 0; i < b.N; i++ {
397+
_, err := NewAddressFromBytes(addrBytes)
398+
if err != nil {
399+
b.Fatal(err)
400+
}
401+
}
402+
}
403+
404+
func BenchmarkAddressFromString(b *testing.B) {
405+
addrStr := "addr1z8snz7c4974vzdpxu65ruphl3zjdvtxw8strf2c2tmqnxz2j2c79gy9l76sdg0xwhd7r0c0kna0tycz4y5s6mlenh8pq0xmsha"
406+
b.ResetTimer()
407+
for i := 0; i < b.N; i++ {
408+
_, err := NewAddress(addrStr)
409+
if err != nil {
410+
b.Fatal(err)
411+
}
412+
}
413+
}

ledger/conway/block_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,28 @@ func compareByteSlices(a, b []byte) bool {
189189
}
190190
return true
191191
}
192+
193+
func BenchmarkConwayBlockDeserialization(b *testing.B) {
194+
blockCbor, _ := hex.DecodeString(conwayBlockHex)
195+
b.ResetTimer()
196+
for i := 0; i < b.N; i++ {
197+
var block conway.ConwayBlock
198+
err := block.UnmarshalCBOR(blockCbor)
199+
if err != nil {
200+
b.Fatal(err)
201+
}
202+
}
203+
}
204+
205+
func BenchmarkConwayBlockSerialization(b *testing.B) {
206+
blockCbor, _ := hex.DecodeString(conwayBlockHex)
207+
var block conway.ConwayBlock
208+
err := block.UnmarshalCBOR(blockCbor)
209+
if err != nil {
210+
b.Fatal(err)
211+
}
212+
b.ResetTimer()
213+
for i := 0; i < b.N; i++ {
214+
_ = block.Cbor()
215+
}
216+
}

ledger/mary/block_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,28 @@ func compareByteSlices(a, b []byte) bool {
148148
}
149149
return true
150150
}
151+
152+
func BenchmarkMaryBlockDeserialization(b *testing.B) {
153+
blockCbor, _ := hex.DecodeString(maryBlockHex)
154+
b.ResetTimer()
155+
for i := 0; i < b.N; i++ {
156+
var block mary.MaryBlock
157+
err := block.UnmarshalCBOR(blockCbor)
158+
if err != nil {
159+
b.Fatal(err)
160+
}
161+
}
162+
}
163+
164+
func BenchmarkMaryBlockSerialization(b *testing.B) {
165+
blockCbor, _ := hex.DecodeString(maryBlockHex)
166+
var block mary.MaryBlock
167+
err := block.UnmarshalCBOR(blockCbor)
168+
if err != nil {
169+
b.Fatal(err)
170+
}
171+
b.ResetTimer()
172+
for i := 0; i < b.N; i++ {
173+
_ = block.Cbor()
174+
}
175+
}

ledger/shelley/block_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,28 @@ func TestShelleyBlockUtxorpc(t *testing.T) {
139139
)
140140
}
141141
}
142+
143+
func BenchmarkShelleyBlockDeserialization(b *testing.B) {
144+
blockCbor, _ := hex.DecodeString(shelleyBlockHex)
145+
b.ResetTimer()
146+
for i := 0; i < b.N; i++ {
147+
var block shelley.ShelleyBlock
148+
err := block.UnmarshalCBOR(blockCbor)
149+
if err != nil {
150+
b.Fatal(err)
151+
}
152+
}
153+
}
154+
155+
func BenchmarkShelleyBlockSerialization(b *testing.B) {
156+
blockCbor, _ := hex.DecodeString(shelleyBlockHex)
157+
var block shelley.ShelleyBlock
158+
err := block.UnmarshalCBOR(blockCbor)
159+
if err != nil {
160+
b.Fatal(err)
161+
}
162+
b.ResetTimer()
163+
for i := 0; i < b.N; i++ {
164+
_ = block.Cbor()
165+
}
166+
}

0 commit comments

Comments
 (0)