diff options
| author | Charlie Vieth <charlie.vieth@gmail.com> | 2024-04-13 01:58:11 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-13 14:58:11 +0900 |
| commit | 3c877c504b6102daf5dcc1083b1f1a7db88d304c (patch) | |
| tree | 7072b889071948a6bfb6d980d43367745a37490f /src/options_pprof_test.go | |
| parent | 892d1acccb705e5547be1b3b6fad8b6d480c290b (diff) | |
| download | fzf-3c877c504b6102daf5dcc1083b1f1a7db88d304c.tar.gz | |
Enable profiling options when 'pprof' tag is set (#2813)
This commit enables cpu, mem, block, and mutex profling of the FZF
executable. To support flushing the profiles at program exit it adds
util.AtExit to register "at exit" functions and mandates that util.Exit
is used instead of os.Exit to stop the program.
Co-authored-by: Junegunn Choi <junegunn.c@gmail.com>
Diffstat (limited to 'src/options_pprof_test.go')
| -rw-r--r-- | src/options_pprof_test.go | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/src/options_pprof_test.go b/src/options_pprof_test.go new file mode 100644 index 00000000..ad47d1f4 --- /dev/null +++ b/src/options_pprof_test.go @@ -0,0 +1,89 @@ +//go:build pprof +// +build pprof + +package fzf + +import ( + "bytes" + "flag" + "os" + "os/exec" + "path/filepath" + "testing" + + "github.com/junegunn/fzf/src/util" +) + +// runInitProfileTests is an internal flag used TestInitProfiling +var runInitProfileTests = flag.Bool("test-init-profile", false, "run init profile tests") + +func TestInitProfiling(t *testing.T) { + if testing.Short() { + t.Skip("short test") + } + + // Run this test in a separate process since it interferes with + // profiling and modifies the global atexit state. Without this + // running `go test -bench . -cpuprofile cpu.out` will fail. + if !*runInitProfileTests { + t.Parallel() + + // Make sure we are not the child process. + if os.Getenv("_FZF_CHILD_PROC") != "" { + t.Fatal("already running as child process!") + } + + cmd := exec.Command(os.Args[0], + "-test.timeout", "30s", + "-test.run", "^"+t.Name()+"$", + "-test-init-profile", + ) + cmd.Env = append(os.Environ(), "_FZF_CHILD_PROC=1") + + out, err := cmd.CombinedOutput() + out = bytes.TrimSpace(out) + if err != nil { + t.Fatalf("Child test process failed: %v:\n%s", err, out) + } + // Make sure the test actually ran + if bytes.Contains(out, []byte("no tests to run")) { + t.Fatalf("Failed to run test %q:\n%s", t.Name(), out) + } + return + } + + // Child process + + tempdir := t.TempDir() + t.Cleanup(util.RunAtExitFuncs) + + o := Options{ + CPUProfile: filepath.Join(tempdir, "cpu.prof"), + MEMProfile: filepath.Join(tempdir, "mem.prof"), + BlockProfile: filepath.Join(tempdir, "block.prof"), + MutexProfile: filepath.Join(tempdir, "mutex.prof"), + } + if err := o.initProfiling(); err != nil { + t.Fatal(err) + } + + profiles := []string{ + o.CPUProfile, + o.MEMProfile, + o.BlockProfile, + o.MutexProfile, + } + for _, name := range profiles { + if _, err := os.Stat(name); err != nil { + t.Errorf("Failed to create profile %s: %v", filepath.Base(name), err) + } + } + + util.RunAtExitFuncs() + + for _, name := range profiles { + if _, err := os.Stat(name); err != nil { + t.Errorf("Failed to write profile %s: %v", filepath.Base(name), err) + } + } +} |
