diff --git a/docs/drivers/benchmark/benchmark.md b/docs/drivers/benchmark/benchmark.md index 3fac5a2ce2..a3fc07930e 100644 --- a/docs/drivers/benchmark/benchmark.md +++ b/docs/drivers/benchmark/benchmark.md @@ -23,4 +23,7 @@ - [Identity Service - Idemix](services/identity/idemix.md) ### Node Level Benchmarks -- [Token Validation Service Benchmark](token_validation_service_benchmark.md) \ No newline at end of file +- [Token Validation Service Benchmark](token_validation_service_benchmark.md) + +### Foundational Types +- `Quantity` operations (`token/token/quantity_test.go`) — micro-benchmarks for `BigQuantity`/`UInt64Quantity` `Add`, `Sub`, `Cmp`, and `ToQuantity` parsing. Run with `go test -run='^$' -bench=. -benchmem ./token/token/` \ No newline at end of file diff --git a/token/token/quantity_test.go b/token/token/quantity_test.go index eeb333f932..64f513ebd5 100644 --- a/token/token/quantity_test.go +++ b/token/token/quantity_test.go @@ -405,6 +405,81 @@ func TestUInt64Quantity_Clone(t *testing.T) { assert.Equal(t, "100", clone.Decimal()) } +// Package-level sinks prevent the compiler from optimizing away the +// benchmarked, side-effect-free calls (dead-code elimination). +var ( + benchResult token.Quantity + benchErr error + benchCmp int +) + +func BenchmarkBigQuantity_Add(b *testing.B) { + q := token.NewZeroQuantity(128) + one := token.NewOneQuantity(128) + b.ResetTimer() + for b.Loop() { + benchResult, benchErr = q.Add(one) + } + require.NoError(b, benchErr) +} + +func BenchmarkBigQuantity_Sub(b *testing.B) { + q, err := token.ToQuantity("1000000000", 128) + require.NoError(b, err) + one := token.NewOneQuantity(128) + b.ResetTimer() + for b.Loop() { + benchResult, benchErr = q.Sub(one) + } + require.NoError(b, benchErr) +} + +func BenchmarkBigQuantity_Cmp(b *testing.B) { + q := token.NewZeroQuantity(128) + one := token.NewOneQuantity(128) + b.ResetTimer() + for b.Loop() { + benchCmp = q.Cmp(one) + } +} + +func BenchmarkUInt64Quantity_Add(b *testing.B) { + q := token.NewZeroQuantity(64) + one := token.NewOneQuantity(64) + b.ResetTimer() + for b.Loop() { + benchResult, benchErr = q.Add(one) + } + require.NoError(b, benchErr) +} + +func BenchmarkUInt64Quantity_Sub(b *testing.B) { + q, err := token.ToQuantity("1000000000", 64) + require.NoError(b, err) + one := token.NewOneQuantity(64) + b.ResetTimer() + for b.Loop() { + benchResult, benchErr = q.Sub(one) + } + require.NoError(b, benchErr) +} + +func BenchmarkUInt64Quantity_Cmp(b *testing.B) { + q := token.NewZeroQuantity(64) + one := token.NewOneQuantity(64) + b.ResetTimer() + for b.Loop() { + benchCmp = q.Cmp(one) + } +} + +func BenchmarkToQuantity(b *testing.B) { + for b.Loop() { + benchResult, benchErr = token.ToQuantity("0x1234567890abcdef", 64) + } + require.NoError(b, benchErr) +} + func ToHex(q uint64) string { return "0x" + strconv.FormatUint(q, 16) }