File size: 2,384 Bytes
f9f6cee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
diff --git a/profiler/options_test.go b/profiler/options_test.go
index 0cc04d69e..053c13ca1 100644
--- a/profiler/options_test.go
+++ b/profiler/options_test.go
@@ -105,6 +105,18 @@ func TestOptions(t *testing.T) {
 		assert.Equal(t, 3*time.Second, cfg.cpuDuration)
 	})
 
+	t.Run("CPUProfileRate", func(t *testing.T) {
+		var cfg config
+		CPUProfileRate(123)(&cfg)
+		assert.Equal(t, 123, cfg.cpuProfileRate)
+	})
+
+	t.Run("CPUProfileRate/default", func(t *testing.T) {
+		cfg, err := defaultConfig()
+		require.NoError(t, err)
+		assert.Equal(t, 0, cfg.cpuProfileRate)
+	})
+
 	t.Run("MutexProfileFraction", func(t *testing.T) {
 		var cfg config
 		MutexProfileFraction(1)(&cfg)
diff --git a/profiler/profile_test.go b/profiler/profile_test.go
index 7c14422c9..ead96f9dc 100644
--- a/profiler/profile_test.go
+++ b/profiler/profile_test.go
@@ -11,6 +11,7 @@ import (
 	"io"
 	"io/ioutil"
 	"os"
+	"runtime"
 	"strconv"
 	"strings"
 	"testing"
@@ -192,6 +193,27 @@ main;bar 0 0 8 16
 		assert.Equal(t, []byte("my-cpu-profile"), profs[0].data)
 	})
 
+	t.Run("cpu-profile-rate", func(t *testing.T) {
+
+
+
+
+		defer runtime.SetCPUProfileRate(0)
+		rate := 500
+		p, err := unstartedProfiler(
+			CPUProfileRate(rate),
+			CPUDuration(20*time.Millisecond),
+		)
+		require.NoError(t, err)
+		profs, err := p.runProfile(CPUProfile)
+		require.NoError(t, err)
+		require.Equal(t, "cpu.pprof", profs[0].name)
+		prof, err := pprofile.ParseData(profs[0].data)
+		require.NoError(t, err)
+		assert.Equal(t, int64(1e9/rate), prof.Period,
+			"CPU profile period should reflect the configured CPUProfileRate")
+	})
+
 	t.Run("goroutine", func(t *testing.T) {
 		defer func(old func(_ string, _ io.Writer, _ int) error) { lookupProfile = old }(lookupProfile)
 		lookupProfile = func(name string, w io.Writer, _ int) error {
diff --git a/test_commands.json b/test_commands.json
new file mode 100644
index 000000000..ca7b84652
--- /dev/null
+++ b/test_commands.json
@@ -0,0 +1,3 @@
+{
+  "test_commands": ["go test ./profiler/ -run \"TestRunProfile/cpu|TestOptions/CPUProfileRate\" -v"]
+}
diff --git a/mimo_test_command.sh b/mimo_test_command.sh
new file mode 100755
--- /dev/null
+++ b/mimo_test_command.sh
@@ -0,0 +1,8 @@
+#!/usr/bin/env bash
+
+set -uo pipefail
+cd /testbed
+rc=0
+go test ./profiler/ -run "TestRunProfile/cpu|TestOptions/CPUProfileRate" -v
+rc=$(( rc | $? ))
+exit $rc