lnd/routing/probability_estimator_test.go
bitromortac d78e756406
routing: add probability benchmarks
$ go test -bench=PairProbability ./routing
goos: linux
goarch: amd64
pkg: github.com/lightningnetwork/lnd/routing
cpu: AMD Ryzen 5 3600 6-Core Processor
BenchmarkBimodalPairProbability-4   	 2050206	       599.8 ns/op
BenchmarkAprioriPairProbability-4   	 9524289	       126.7 ns/op

* math.Exp:
goos: linux
goarch: amd64
pkg: github.com/lightningnetwork/lnd/routing
cpu: AMD Ryzen 5 3600 6-Core Processor
BenchmarkExp-4   	143843622	         8.700 ns/op

* bimodal benchmark profile:
Showing nodes accounting for 2000ms, 76.05% of 2630ms total
Dropped 156 nodes (cum <= 13.15ms)
Showing top 10 nodes out of 141
      flat  flat%   sum%        cum   cum%
    1110ms 42.21% 42.21%     1110ms 42.21%  math.archExp
     230ms  8.75% 50.95%      230ms  8.75%  runtime.memclrNoHeapPointers
     180ms  6.84% 57.79%      930ms 35.36%  github.com/lightningnetwork/lnd/routing.(*BimodalEstimator).calculateProbability
     110ms  4.18% 61.98%      130ms  4.94%  runtime.scanobject
      90ms  3.42% 65.40%       90ms  3.42%  memeqbody
      60ms  2.28% 67.68%      940ms 35.74%  github.com/lightningnetwork/lnd/routing.(*BimodalEstimator).directProbability
      60ms  2.28% 69.96%      230ms  8.75%  github.com/lightningnetwork/lnd/routing.cannotSend
      60ms  2.28% 72.24%       60ms  2.28%  runtime.madvise
      50ms  1.90% 74.14%      480ms 18.25%  github.com/lightningnetwork/lnd/routing.(*BimodalEstimator).primitive
      50ms  1.90% 76.05%       60ms  2.28%  runtime.mapiternext

* apriori benchmark profile:
Showing nodes accounting for 1570ms, 77.34% of 2030ms total
Dropped 138 nodes (cum <= 10.15ms)
Showing top 10 nodes out of 151
      flat  flat%   sum%        cum   cum%
     340ms 16.75% 16.75%      340ms 16.75%  math.archExp
     290ms 14.29% 31.03%      980ms 48.28%  github.com/lightningnetwork/lnd/routing.(*AprioriEstimator).getNodeProbability
     190ms  9.36% 40.39%      260ms 12.81%  runtime.mapiternext
     190ms  9.36% 49.75%      190ms  9.36%  runtime.memclrNoHeapPointers
     160ms  7.88% 57.64%      340ms 16.75%  github.com/lightningnetwork/lnd/routing.(*AprioriEstimator).calculateProbability
     110ms  5.42% 63.05%      110ms  5.42%  aeshashbody
     110ms  5.42% 68.47%      130ms  6.40%  runtime.scanobject
      80ms  3.94% 72.41%      420ms 20.69%  github.com/lightningnetwork/lnd/routing.capacityFactor
      60ms  2.96% 75.37%       60ms  2.96%  runtime.madvise
      40ms  1.97% 77.34%       40ms  1.97%  runtime.isEmpty (inline)
2023-02-14 13:34:25 +01:00

96 lines
2.3 KiB
Go

package routing
import (
"math"
"testing"
"time"
"github.com/lightningnetwork/lnd/routing/route"
)
// Create a set of test results.
var resultTime = time.Unix(1674169200, 0) // 20.01.2023
var now = time.Unix(1674190800, 0) // 6 hours later
var results = NodeResults{
route.Vertex{byte(0)}: TimedPairResult{
FailAmt: 200_000_000,
FailTime: resultTime,
SuccessAmt: 100_000_000,
SuccessTime: resultTime,
},
route.Vertex{byte(1)}: TimedPairResult{
FailAmt: 200_000_000,
FailTime: resultTime,
SuccessAmt: 100_000_000,
SuccessTime: resultTime,
},
route.Vertex{byte(2)}: TimedPairResult{
FailAmt: 200_000_000,
FailTime: resultTime,
SuccessAmt: 100_000_000,
SuccessTime: resultTime,
},
route.Vertex{byte(3)}: TimedPairResult{
FailAmt: 200_000_000,
FailTime: resultTime,
SuccessAmt: 100_000_000,
SuccessTime: resultTime,
},
route.Vertex{byte(4)}: TimedPairResult{
FailAmt: 200_000_000,
FailTime: resultTime,
SuccessAmt: 100_000_000,
SuccessTime: resultTime,
},
}
// probability is a package level variable to prevent the compiler from
// optimizing the benchmark.
var probability float64
// BenchmarkBimodalPairProbability benchmarks the probability calculation.
func BenchmarkBimodalPairProbability(b *testing.B) {
estimator := BimodalEstimator{
BimodalConfig: BimodalConfig{
BimodalScaleMsat: scale,
BimodalNodeWeight: 0.2,
BimodalDecayTime: 48 * time.Hour,
},
}
toNode := route.Vertex{byte(0)}
var p float64
for i := 0; i < b.N; i++ {
p = estimator.PairProbability(now, results, toNode,
150_000_000, 300_000)
}
probability = p
}
// BenchmarkAprioriPairProbability benchmarks the probability calculation.
func BenchmarkAprioriPairProbability(b *testing.B) {
estimator := AprioriEstimator{
AprioriConfig: AprioriConfig{
AprioriWeight: 0.2,
PenaltyHalfLife: 48 * time.Hour,
AprioriHopProbability: 0.5,
},
}
toNode := route.Vertex{byte(0)}
var p float64
for i := 0; i < b.N; i++ {
p = estimator.PairProbability(now, results, toNode,
150_000_000, 300_000)
}
probability = p
}
// BenchmarkExp benchmarks the exponential function as provided by the math
// library.
func BenchmarkExp(b *testing.B) {
for i := 0; i < b.N; i++ {
math.Exp(0.1)
}
}