-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathqt_bench_test.go
More file actions
88 lines (84 loc) · 2.2 KB
/
qt_bench_test.go
File metadata and controls
88 lines (84 loc) · 2.2 KB
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
package quadtree
import (
"math/rand"
"runtime"
"testing"
"time"
)
const LowCapacity = 4
const HighCapacity = 1000
func newLockfree(capacity int) Quadtree {
return NewLockFree(&BoundingBox{
Center: Point{100.0, 100.0},
HalfDimension: Point{50.0, 50.0},
}, capacity)
}
func newLockbased(capacity int) Quadtree {
return NewLockFree(&BoundingBox{
Center: Point{100.0, 100.0},
HalfDimension: Point{50.0, 50.0},
}, capacity)
}
func benchInsert(b *testing.B, qt Quadtree) Quadtree {
b.StopTimer()
runtime.GOMAXPROCS(runtime.NumCPU())
rand.Seed(time.Now().UnixNano())
pointsToInsert := b.N
threads := runtime.NumCPU()
tpoints := pointsToInsert / threads
done := make(chan bool)
insertPoint := func() {
for i := 0; i != tpoints; i++ {
p := &Point{rand.Float64()*100.0 + 50.0, rand.Float64()*100.0 + 50.0}
qt.Insert(p)
}
done <- true
}
b.StartTimer()
b.ResetTimer()
for i := 0; i != threads; i++ {
go insertPoint()
}
for i := 0; i != threads; i++ {
<-done
}
return qt
}
func benchQuery(b *testing.B, qt Quadtree) {
b.StopTimer()
queries := b.N
box := &BoundingBox{
Center: Point{100.0, 100.0},
HalfDimension: Point{5.0, 5.0},
}
b.StartTimer()
b.ResetTimer()
for i := 0; i != queries; i++ {
box.Center = Point{rand.Float64()*100.0 + 50.0, rand.Float64()*100.0 + 50.0}
qt.Query(box)
}
}
func Benchmark_Insert_LowCapacity_LockFree(b *testing.B) {
benchInsert(b,newLockfree(LowCapacity))
}
func Benchmark_Insert_LowCapacity_LockBased(b *testing.B) {
benchInsert(b,newLockbased(LowCapacity))
}
func Benchmark_Insert_HighCapacity_LockFree(b *testing.B) {
benchInsert(b,newLockfree(HighCapacity))
}
func Benchmark_Insert_HighCapacity_LockBased(b *testing.B) {
benchInsert(b,newLockbased(HighCapacity))
}
func Benchmark_Query_LowCapacity_LockFree(b *testing.B) {
benchQuery(b, benchInsert(b, newLockfree(LowCapacity)))
}
func Benchmark_Query_LowCapacity_LockBased(b *testing.B) {
benchQuery(b, benchInsert(b, newLockbased(LowCapacity)))
}
func Benchmark_Query_HighCapacity_LockFree(b *testing.B) {
benchQuery(b, benchInsert(b, newLockfree(HighCapacity)))
}
func Benchmark_Query_HighCapacity_LockBased(b *testing.B) {
benchQuery(b, benchInsert(b, newLockbased(HighCapacity)))
}