Profiling Caddy using pkg/profile

Hi,

I’m interested in profiling and benchmarking Caddy running on an ARM platform.

I’ve been running h2load benchmarks on it and getting some interesting results.

For profiling, I’ve looked at using davecheney’s GitHub - pkg/profile: Simple profiling for Go profiler, I’ve started by naively putting the profiling lines into caddy/caddy/main.go like this:

package main

import "github.com/mholt/caddy/caddy/caddymain"
import "github.com/pkg/profile"

var run = caddymain.Run // replaced for tests

func main() {
	defer profile.Start().Stop()
	run()
}

I get the message profile: cpu profiling enabled, /tmp/profile385684916/cpu.pprof now when I start Caddy before the Activating privacy features message, as expected. But from trying a few times in different ways, that pprof file has always been empty. I’ve read that it’s only written to if the program being profiled exits normally, I’ve been doing Ctrl+C to end Caddy (which I think should be OK?) after running it for a few minutes and generating load. Is there a better way to go about getting some profiling results, or might know why this file is empty?

Thanks.

From what I’m reading, profile only writes if Stop() is called. defer sets the function to be called after main() returns. I have no idea if main returns when Caddy is interrupted.

I did find this profile.NoShutdownHook function, which implies you should start profiling in main() and then call the stop function during shutdown, possibly in Caddy’s SIGINT handler.

2 Likes

Thanks a lot, this solved my problem.

The deferred Stop() wasn’t being called so I modified the SIGINT handler to call the stop function instead.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.