From f51e47bfdf439b7e4ab7f97beea3f8ddc2cf0d11 Mon Sep 17 00:00:00 2001 From: theycallmeaabie Date: Sun, 5 Apr 2026 02:26:32 +0530 Subject: [PATCH] bench: add benchmarks for Quantity operations Signed-off-by: theycallmeaabie --- token/token/quantity_test.go | 60 ++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/token/token/quantity_test.go b/token/token/quantity_test.go index ed9e1e6af8..62d3711f8a 100644 --- a/token/token/quantity_test.go +++ b/token/token/quantity_test.go @@ -405,6 +405,66 @@ func TestUInt64Quantity_Clone(t *testing.T) { assert.Equal(t, "100", clone.Decimal()) } +var benchResult token.Quantity + +func BenchmarkBigQuantity_Add(b *testing.B) { + for b.Loop() { + q := token.NewZeroQuantity(128) + one := token.NewOneQuantity(128) + benchResult, _ = q.Add(one) + } +} + +func BenchmarkBigQuantity_Sub(b *testing.B) { + for b.Loop() { + q, _ := token.ToQuantity("1000000000", 128) + one := token.NewOneQuantity(128) + benchResult, _ = q.Sub(one) + } +} + +func BenchmarkBigQuantity_Cmp(b *testing.B) { + q := token.NewZeroQuantity(128) + one := token.NewOneQuantity(128) + var r int + for b.Loop() { + r = q.Cmp(one) + } + _ = r +} + +func BenchmarkUInt64Quantity_Add(b *testing.B) { + for b.Loop() { + q := token.NewZeroQuantity(64) + one := token.NewOneQuantity(64) + benchResult, _ = q.Add(one) + } +} + +func BenchmarkUInt64Quantity_Sub(b *testing.B) { + for b.Loop() { + q, _ := token.ToQuantity("1000000000", 64) + one := token.NewOneQuantity(64) + benchResult, _ = q.Sub(one) + } +} + +func BenchmarkUInt64Quantity_Cmp(b *testing.B) { + q := token.NewZeroQuantity(64) + one := token.NewOneQuantity(64) + var r int + for b.Loop() { + r = q.Cmp(one) + } + _ = r +} + +func BenchmarkToQuantity(b *testing.B) { + for b.Loop() { + benchResult, _ = token.ToQuantity("0x1234567890abcdef", 64) + } +} + func ToHex(q uint64) string { return "0x" + strconv.FormatUint(q, 16) }