forked from acronis/perfkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark_test.go
112 lines (93 loc) · 2.44 KB
/
benchmark_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
package benchmark
import (
"os"
"testing"
"time"
)
func TestNewBenchmark(t *testing.T) {
b := New()
if b == nil {
t.Errorf("New() error, benchmark not created")
}
}
func TestRunOnce(t *testing.T) {
b := New()
b.CommonOpts.Workers = 1
b.CommonOpts.Loops = 1
b.Worker = func(id int) (loops int) { //nolint:revive
return 1
}
b.RunOnce(false)
if b.Score.Workers != 1 {
t.Errorf("RunOnce() error, workers = %v, want %v", b.Score.Workers, 1)
}
}
func TestFormatRateWithZeroRate(t *testing.T) {
score := Score{Rate: 0.0}
result := score.FormatRate(4)
if result != "0" {
t.Errorf("FormatRate() error, expected '0', got '%s'", result)
}
}
func TestFormatRateWithNonZeroRate(t *testing.T) {
score := Score{Rate: 1234.5678}
result := score.FormatRate(4)
if result != "1235" {
t.Errorf("FormatRate() error, expected '1234.5678', got '%s'", result)
}
}
func TestFormatRateWithLargeRate(t *testing.T) {
score := Score{Rate: 12345678.12345678}
result := score.FormatRate(4)
if result != "12345678" {
t.Errorf("FormatRate() error, expected '12345678.1235', got '%s'", result)
}
}
func TestInitOpts(t *testing.T) {
b := New()
os.Args = []string{"test", "--duration=1", "--loops=1", "-c=1"}
b.InitOpts()
if !b.OptsInitialized {
t.Errorf("InitOpts() error, expected OptsInitialized to be true, got false")
}
if b.Logger == nil {
t.Errorf("InitOpts() error, expected Logger to be initialized, got nil")
}
if b.Cli.parser == nil {
t.Errorf("InitOpts() error, expected Cli.parser to be initialized, got nil")
}
if b.CommonOpts.Workers != 1 {
t.Errorf("InitOpts() error, expected CommonOpts.Workers to be 1, got %d", b.CommonOpts.Workers)
}
}
func TestGeomean(t *testing.T) {
b := New()
scores := []Score{
{Rate: 2.0},
{Rate: 8.0},
}
result := b.Geomean(scores)
if result != 4.0 {
t.Errorf("Geomean() error, expected 4.0, got %f", result)
}
}
func TestRun(t *testing.T) {
os.Args = []string{"test", "--duration=1", "--loops=1", "-c=1"}
b := New()
b.Worker = func(id int) (loops int) { //nolint:revive
return 1
}
b.Run()
if b.Score.Workers != 1 {
t.Errorf("Run() error, workers = %v, want %v", b.Score.Workers, 1)
}
if b.Score.Loops != 1 {
t.Errorf("Run() error, loops = %v, want %v", b.Score.Loops, 1)
}
if b.Score.Rate == 0 {
t.Errorf("Run() error, rate should not be 0")
}
if b.Score.Seconds > float64(time.Second) {
t.Errorf("Run() error, seconds = %v, want less than or equal to %v", b.Score.Seconds, 1)
}
}