| |
| |
| |
| |
| @@ -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) |
| |
| |
| |
| |
| @@ -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 { |
| |
| new file mode 100644 |
| |
| |
| |
| @@ -0,0 +1,3 @@ |
| +{ |
| + "test_commands": ["go test ./profiler/ -run \"TestRunProfile/cpu|TestOptions/CPUProfileRate\" -v"] |
| +} |
| |
| new file mode 100755 |
| |
| |
| @@ -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 |
|
|