Files
node/utils/sampler/weighted_benchmark_test.go
zeekay 7cf87b230b node-unified is the node
The module already called itself github.com/luxfi/node while containing no node
in Go: cmd/luxd was twenty-one lines that printed the size of a few types, and
the Go column of the matrix measured a VM plugin. A repo that fetches the thing
it claims to be has not replaced it, so the Go sources move in — 1515 files,
the daemon and everything under it — and the build reaches outside this module
for nothing.

Rust and C++ sit beside it unchanged and are still measured against it by the
differential. That is the point of one repo: the reference implementation and
the two ports are the same checkout, so parity is a thing you can run rather
than a thing you assert.

Verified: builds, and the closure carries no lux-private — checked at build time
rather than believed. The daemon starts twelve chains on a local network
(P X C Q Z A B G K M F, plus D through its plugin) and reports healthy.
2026-09-07 16:15:25 -07:00

149 lines
3.0 KiB
Go

// Copyright (C) 2019-2025, Lux Industries Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package sampler
import (
"fmt"
"math"
"testing"
safemath "github.com/luxfi/node/utils/math"
)
func BenchmarkWeightedHeapSampling(b *testing.B) {
sampler := &weightedHeap{}
pows := []float64{
0,
1,
2,
3,
}
sizes := []int{
10,
500,
1000,
50000,
100000,
}
for _, pow := range pows {
for _, size := range sizes {
if WeightedPowBenchmarkSampler(b, sampler, pow, size) {
b.Run(fmt.Sprintf("%d elements at x^%.1f", size, pow), func(b *testing.B) {
WeightedPowBenchmarkSampler(b, sampler, pow, size)
})
}
}
}
for _, size := range sizes {
if WeightedSingletonBenchmarkSampler(b, sampler, size) {
b.Run(fmt.Sprintf(" %d singleton elements", size), func(b *testing.B) {
WeightedSingletonBenchmarkSampler(b, sampler, size)
})
}
}
}
func BenchmarkWeightedHeapInitializer(b *testing.B) {
sampler := &weightedHeap{}
pows := []float64{
0,
1,
2,
3,
}
sizes := []int{
10,
500,
1000,
50000,
100000,
}
for _, pow := range pows {
for _, size := range sizes {
if WeightedPowBenchmarkSampler(b, sampler, pow, size) {
b.Run(fmt.Sprintf("%d elements at x^%.1f", size, pow), func(b *testing.B) {
_, weights, _ := CalcWeightedPoW(pow, size)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = sampler.Initialize(weights)
}
})
}
}
}
for _, size := range sizes {
if WeightedSingletonBenchmarkSampler(b, sampler, size) {
b.Run(fmt.Sprintf("%d singleton elements", size), func(b *testing.B) {
weights := make([]uint64, size)
weights[0] = math.MaxUint64 - uint64(size-1)
for i := 1; i < len(weights); i++ {
weights[i] = 1
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = sampler.Initialize(weights)
}
})
}
}
}
func CalcWeightedPoW(exponent float64, size int) (uint64, []uint64, error) {
weights := make([]uint64, size)
totalWeight := uint64(0)
for i := range weights {
weight := uint64(math.Pow(float64(i+1), exponent))
weights[i] = weight
newWeight, err := safemath.Add(totalWeight, weight)
if err != nil {
return 0, nil, err
}
totalWeight = newWeight
}
return totalWeight, weights, nil
}
func WeightedPowBenchmarkSampler(
b *testing.B,
s Weighted,
exponent float64,
size int,
) bool {
totalWeight, weights, err := CalcWeightedPoW(exponent, size)
if err != nil {
return false
}
if err := s.Initialize(weights); err != nil {
return false
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = s.Sample(globalRNG.Uint64Inclusive(totalWeight - 1))
}
return true
}
func WeightedSingletonBenchmarkSampler(b *testing.B, s Weighted, size int) bool {
weights := make([]uint64, size)
weights[0] = math.MaxUint64 - uint64(size-1)
for i := 1; i < len(weights); i++ {
weights[i] = 1
}
err := s.Initialize(weights)
if err != nil {
return false
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = s.Sample(globalRNG.Uint64Inclusive(math.MaxUint64 - 1))
}
return true
}