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