mirror of
https://github.com/luxfi/consensus.git
synced 2026-09-24 00:26:58 +00:00
228 lines
5.2 KiB
Go
228 lines
5.2 KiB
Go
// Copyright (C) 2019-2025, Lux Industries Inc All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package consensus
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// BenchmarkSimpleConsensus measures baseline timestamp overhead for calibration.
|
|
func BenchmarkSimpleConsensus(b *testing.B) {
|
|
for i := 0; i < b.N; i++ {
|
|
_ = time.Now().Unix()
|
|
}
|
|
}
|
|
|
|
// BenchmarkConsensusCreation benchmarks consensus object creation
|
|
func BenchmarkConsensusCreation(b *testing.B) {
|
|
b.Run("SimpleCreation", func(b *testing.B) {
|
|
for i := 0; i < b.N; i++ {
|
|
// Simulate consensus creation
|
|
config := make(map[string]interface{})
|
|
config["k"] = 20
|
|
config["alpha_preference"] = 15
|
|
config["alpha_confidence"] = 15
|
|
config["beta"] = 20
|
|
_ = config
|
|
}
|
|
})
|
|
}
|
|
|
|
// BenchmarkBlockOperations benchmarks block operations
|
|
func BenchmarkBlockOperations(b *testing.B) {
|
|
b.Run("SingleBlock", func(b *testing.B) {
|
|
blocks := make([][]byte, 0, b.N)
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
block := make([]byte, 32)
|
|
block[0] = byte(i >> 8)
|
|
block[1] = byte(i & 0xFF)
|
|
blocks = append(blocks, block)
|
|
}
|
|
_ = blocks
|
|
})
|
|
|
|
b.Run("BatchBlocks_100", func(b *testing.B) {
|
|
b.ResetTimer()
|
|
for n := 0; n < b.N; n++ {
|
|
blocks := make([][]byte, 0, 100)
|
|
for i := 0; i < 100; i++ {
|
|
block := make([]byte, 32)
|
|
block[0] = byte(i >> 8)
|
|
block[1] = byte(i & 0xFF)
|
|
blocks = append(blocks, block)
|
|
}
|
|
_ = blocks
|
|
}
|
|
})
|
|
|
|
b.Run("BatchBlocks_1000", func(b *testing.B) {
|
|
b.ResetTimer()
|
|
for n := 0; n < b.N; n++ {
|
|
blocks := make([][]byte, 0, 1000)
|
|
for i := 0; i < 1000; i++ {
|
|
block := make([]byte, 32)
|
|
block[0] = byte(i >> 8)
|
|
block[1] = byte(i & 0xFF)
|
|
blocks = append(blocks, block)
|
|
}
|
|
_ = blocks
|
|
}
|
|
})
|
|
}
|
|
|
|
// BenchmarkVoteProcessing benchmarks vote processing
|
|
func BenchmarkVoteProcessing(b *testing.B) {
|
|
b.Run("SingleVote", func(b *testing.B) {
|
|
votes := make([][]byte, 0, b.N)
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
vote := make([]byte, 65) // voter_id(32) + block_id(32) + is_preference(1)
|
|
vote[0] = byte(i >> 8)
|
|
vote[1] = byte(i & 0xFF)
|
|
votes = append(votes, vote)
|
|
}
|
|
_ = votes
|
|
})
|
|
|
|
b.Run("BatchVotes_1000", func(b *testing.B) {
|
|
b.ResetTimer()
|
|
for n := 0; n < b.N; n++ {
|
|
votes := make([][]byte, 0, 1000)
|
|
for i := 0; i < 1000; i++ {
|
|
vote := make([]byte, 65)
|
|
vote[0] = byte(i >> 8)
|
|
vote[1] = byte(i & 0xFF)
|
|
votes = append(votes, vote)
|
|
}
|
|
_ = votes
|
|
}
|
|
})
|
|
|
|
b.Run("BatchVotes_10000", func(b *testing.B) {
|
|
b.ResetTimer()
|
|
for n := 0; n < b.N; n++ {
|
|
votes := make([][]byte, 0, 10000)
|
|
for i := 0; i < 10000; i++ {
|
|
vote := make([]byte, 65)
|
|
vote[0] = byte(i >> 16)
|
|
vote[1] = byte(i >> 8)
|
|
vote[2] = byte(i & 0xFF)
|
|
votes = append(votes, vote)
|
|
}
|
|
_ = votes
|
|
}
|
|
})
|
|
}
|
|
|
|
// BenchmarkQueryOperations benchmarks query operations
|
|
func BenchmarkQueryOperations(b *testing.B) {
|
|
// Prepare data
|
|
blockIDs := make([][]byte, 1000)
|
|
for i := 0; i < 1000; i++ {
|
|
blockID := make([]byte, 32)
|
|
blockID[0] = byte(i >> 8)
|
|
blockID[1] = byte(i & 0xFF)
|
|
blockIDs[i] = blockID
|
|
}
|
|
|
|
b.Run("IsAccepted", func(b *testing.B) {
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
blockID := blockIDs[i%1000]
|
|
// Simulate checking if block is accepted
|
|
_ = len(blockID) == 32
|
|
}
|
|
})
|
|
|
|
b.Run("GetPreference", func(b *testing.B) {
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
// Simulate getting preference
|
|
pref := make([]byte, 32)
|
|
_ = pref
|
|
}
|
|
})
|
|
|
|
b.Run("GetStats", func(b *testing.B) {
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
// Simulate getting stats
|
|
stats := struct {
|
|
BlocksAccepted uint64
|
|
BlocksRejected uint64
|
|
VotesProcessed uint64
|
|
PollsCompleted uint64
|
|
}{
|
|
BlocksAccepted: 100,
|
|
BlocksRejected: 10,
|
|
VotesProcessed: 1000,
|
|
PollsCompleted: 50,
|
|
}
|
|
_ = stats
|
|
}
|
|
})
|
|
}
|
|
|
|
// BenchmarkConcurrentOperations benchmarks concurrent operations
|
|
func BenchmarkConcurrentOperations(b *testing.B) {
|
|
for _, numGoroutines := range []int{1, 2, 4, 8} {
|
|
b.Run(fmt.Sprintf("Goroutines_%d", numGoroutines), func(b *testing.B) {
|
|
b.ResetTimer()
|
|
|
|
for n := 0; n < b.N; n++ {
|
|
done := make(chan bool, numGoroutines)
|
|
|
|
for g := 0; g < numGoroutines; g++ {
|
|
go func(id int) {
|
|
// Simulate concurrent operations
|
|
blocks := make([][]byte, 100)
|
|
for i := 0; i < 100; i++ {
|
|
block := make([]byte, 32)
|
|
block[0] = byte(id)
|
|
block[1] = byte(i)
|
|
blocks[i] = block
|
|
}
|
|
done <- true
|
|
}(g)
|
|
}
|
|
|
|
// Wait for all goroutines
|
|
for g := 0; g < numGoroutines; g++ {
|
|
<-done
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// BenchmarkMemoryUsage benchmarks memory usage
|
|
func BenchmarkMemoryUsage(b *testing.B) {
|
|
for _, size := range []int{1000, 10000} {
|
|
b.Run(fmt.Sprintf("Blocks_%d", size), func(b *testing.B) {
|
|
b.ResetTimer()
|
|
|
|
for n := 0; n < b.N; n++ {
|
|
blocks := make([][]byte, 0, size)
|
|
for i := 0; i < size; i++ {
|
|
block := make([]byte, 32)
|
|
block[0] = byte(i >> 24)
|
|
block[1] = byte(i >> 16)
|
|
block[2] = byte(i >> 8)
|
|
block[3] = byte(i & 0xFF)
|
|
// Add some data
|
|
data := fmt.Sprintf("Block data %d", i)
|
|
blockWithData := append(block, []byte(data)...)
|
|
blocks = append(blocks, blockWithData)
|
|
}
|
|
_ = blocks
|
|
}
|
|
})
|
|
}
|
|
}
|