-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathtuner_test.go
98 lines (85 loc) · 2.03 KB
/
tuner_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
package gogctuner
import (
"math/rand"
"runtime"
"testing"
"time"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func RandStringRunes(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}
func GenerateMap(entries int) map[int]string {
m := map[int]string{}
for i := 0; i <= entries; i++ {
m[i] = RandStringRunes(10000)
}
return m
}
func TestInitDoesNotPanic(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Error("The code paniced", r)
}
}()
go NewTuner(false, 70)
maps := []map[int]string{}
maps = append(maps, GenerateMap(2*1000))
runtime.GC()
maps = append(maps, GenerateMap(4*1000))
runtime.GC()
maps = append(maps, GenerateMap(8*1000))
time.Sleep(1 * time.Second)
}
func TestInitDoesNotPanic2(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Error("The code paniced", r)
}
}()
go NewTuner(true, 70, SetLogger(&StdLoggerAdapter{}))
GenerateMap(5 * 1000)
time.Sleep(5 * time.Second)
}
// func getGOGC(previousGOGC int , memoryLimitInPercent, memPercent float64) int {
type GetGOGCTestCase struct {
PreviousGOGC int
MemoryLimitInPercent float64
MemPercent float64
ExpectedGOGC int
}
func TestGetGOGCBasics(t *testing.T) {
cases := []GetGOGCTestCase{
{
PreviousGOGC: 100,
MemoryLimitInPercent: 80,
MemPercent: 100,
ExpectedGOGC: 80,
},
{
PreviousGOGC: 10,
MemoryLimitInPercent: 80,
MemPercent: 10,
ExpectedGOGC: 700,
},
{
PreviousGOGC: 100,
MemoryLimitInPercent: 80,
MemPercent: 30,
ExpectedGOGC: 166,
},
}
for i, _ := range cases {
result := getGOGC(cases[i].PreviousGOGC, cases[i].MemoryLimitInPercent, cases[i].MemPercent)
if result != cases[i].ExpectedGOGC {
t.Errorf("Failed Test Case #%v - Expected: %v Found: %v", i+1, cases[i].ExpectedGOGC, result)
}
}
}