forked from erikdubbelboer/ringqueue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_test.go
More file actions
49 lines (37 loc) · 817 Bytes
/
Copy pathmemory_test.go
File metadata and controls
49 lines (37 loc) · 817 Bytes
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
package main
import (
"math"
"runtime"
"strconv"
"testing"
"time"
)
func benchmarkMemory(b *testing.B, q intqueue) {
b.SkipNow()
b.ReportAllocs()
b.N = 30000000
for i := 0; i < b.N; i++ {
q.add(i)
}
for i := 0; i < b.N; i++ {
q.remove()
}
b.Logf(memory())
}
func BenchmarkSliceMemory(b *testing.B) {
benchmarkMemory(b, newslicequeue())
}
func BenchmarkRingMemory(b *testing.B) {
benchmarkMemory(b, newringqueue())
}
func memory() string {
runtime.GC()
time.Sleep(time.Second)
var m runtime.MemStats
runtime.ReadMemStats(&m)
units := []string{
"B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB",
}
base := int(math.Floor(math.Log(float64(m.Alloc)) / math.Log(1024)))
return strconv.FormatFloat(float64(m.Alloc)/math.Pow(1024, float64(base)), 'f', 2, 64) + " " + units[base]
}