-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprollytree_test.go
137 lines (109 loc) · 3.99 KB
/
prollytree_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package prollytree
import (
"fmt"
"math"
"math/rand"
"strconv"
"testing"
"time"
)
const (
iterations = 5
msg_size_creation = 100_000
msg_size_insertion = 200_000
)
func BenchmarkProllyTreeCreationRandom(b *testing.B) {
var durations []time.Duration
// Reset the timer and run the benchmarks sequentially in the same loop
b.ResetTimer()
for i := 0; i < iterations; i++ {
duration := treeBenchmarkCreation()
durations = append(durations, duration)
}
min, max, avg, std := calculateStats(durations)
fmt.Println("| Operation | Iterations | Min Time (ms) | Max Time (ms) | Avg Time (ms) | Std Dev (ms) | Ops/s |")
fmt.Println("|---------------------------------|------------|---------------|---------------|---------------|--------------|------------|")
fmt.Printf("| %-30s | %10d | %10.4f | %10.4f | %10.4f | %10.4f | %10d |\n",
"Create random 100k entries", iterations, min.Seconds()*1000, max.Seconds()*1000, avg.Seconds()*1000, std.Seconds()*1000, int64(float64(1)/avg.Seconds()))
}
func BenchmarkProllyTreeInsertionRandom(b *testing.B) {
var durations []time.Duration
// Reset the timer and run the benchmarks sequentially in the same loop
b.ResetTimer()
for i := 0; i < iterations; i++ {
duration := treeBenchmarkInsertion()
durations = append(durations, duration)
}
min, max, avg, std := calculateStats(durations)
fmt.Println("| Operation | Iterations | Min Time (ms) | Max Time (ms) | Avg Time (ms) | Std Dev (ms) | Ops/s |")
fmt.Println("|---------------------------------|------------|---------------|---------------|---------------|--------------|------------|")
fmt.Printf("| %-30s | %10d | %10.4f | %10.4f | %10.4f | %10.4f | %10d |\n",
"Insert random 100k entries", iterations, min.Seconds()*1000, max.Seconds()*1000, avg.Seconds()*1000, std.Seconds()*1000, int64(float64(1)/avg.Seconds()))
}
func treeBenchmarkCreation() time.Duration {
startTime := time.Now()
messages := make([]*Message, msg_size_creation)
for i := 0; i < msg_size_creation; i++ {
messages[i] = NewMessage(strconv.Itoa(i), i)
}
// Shuffle messages to simulate the random order
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(messages), func(i, j int) {
messages[i], messages[j] = messages[j], messages[i]
})
// Tree creation benchmark
_ = NewProllyTree(messages)
duration := time.Since(startTime)
return duration
}
func treeBenchmarkInsertion() time.Duration {
// Prepare messages before benchmarking to exclude the preparation time
messages := make([]*Message, msg_size_insertion)
for i := 0; i < msg_size_insertion; i++ {
messages[i] = NewMessage(strconv.Itoa(i), i)
}
// Shuffle messages to simulate the random order
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(messages), func(i, j int) {
messages[i], messages[j] = messages[j], messages[i]
})
halfSize := msg_size_insertion / 2
firstHalf := messages[:halfSize]
secondHalf := messages[halfSize:]
// Tree creation benchmark
tree := NewProllyTree(firstHalf)
// Tree insertion benchmark
startTime := time.Now()
for _, message := range secondHalf {
tree.Insert(message)
}
duration := time.Since(startTime)
return duration
}
// calculateStats computes the statistical metrics for the benchmark results
func calculateStats(durations []time.Duration) (min, max, avg, std time.Duration) {
sum := 0.0
min = time.Duration(1<<63 - 1)
max = time.Duration(0)
for _, d := range durations {
durationSec := d.Seconds() * 1000 // Convert duration to milliseconds
sum += durationSec
if d < min {
min = d
}
if d > max {
max = d
}
}
avg = time.Duration(sum/float64(len(durations))) * time.Millisecond
// Calculate standard deviation
var varianceSum float64
for _, d := range durations {
durationSec := d.Seconds() * 1000 // Convert duration to milliseconds
variance := durationSec - avg.Seconds()*1000
varianceSum += variance * variance
}
variance := varianceSum / float64(len(durations))
std = time.Duration(math.Sqrt(variance)) * time.Millisecond
return
}