Profiling live webserver application

240 views
Skip to first unread message

569...@gmail.com

unread,
Oct 25, 2016, 1:19:30 AM10/25/16
to golang-nuts
Hey there,

I have a Go webapp that serves a complicated website, including a search engine, recommendation engine, all kinds of caches for TB of content, etc.

The app is using too much memory and the CPU increases recently. I'd like to profile both memory and CPU, live, so I can figure out what is going on.

I tried "github.com/pkg/profile" but the latest version does not seem to allow both CPU & memory to be profiled at the same time, and I didn't manage to get it to work. It created the mem.pprof file but the filesize was always zero.
I then tried adding import _ "net/http/pprof" but the /debug/pprof/profile URL didn't work, the site returned its custom 404 error page.

Obviously I don't quite understand how to do the profiling... I've looked through the documentation but I still don't understand how to make it work. Could someone please explain this to me?

Thanks!
Alasdair

Shawn Milochik

unread,
Oct 25, 2016, 8:39:42 AM10/25/16
to golang-nuts
The net/pprof package works great for this. What do you get when you run one of these?


or


You may have to replace port 6060 with the port you're actually listening on. Once you're in there you can type "web" to get a graphic showing where your CPU is being used.

If that's not working, perhaps you're capturing the /debug path with one of your handlers. I recommend running a separate server on port 6060 that only listens on localhost (for security reasons). Also, you should create a separate http.Server instance for this. If you're using the default http.Server in your code, you won't want to mix them, otherwise you could run into the same collision problem. Plus, you never want your profile info to be available to the public anyway.

Maybe something like this that you run in a gorouine in your main() function:


func serveProfiler() {

    mux := http.NewServeMux()
    profServer := http.Server{
        Addr:         ":6060",
        Handler:      mux,
    }   

    log.Fatalf("failed to start profile server\n", profServer.ListenAndServe())
}


569...@gmail.com

unread,
Oct 26, 2016, 6:38:22 AM10/26/16
to golang-nuts, Sh...@milochik.com
It says:

I am not capturing the /debug path, but this is not a simple webserver.

I'm not following your logic. How does the profile package know which http.Server to use for /debug/pprof?

It's probably far easier just to have the pprof files put to file, the only problem is that they stay at zero filesize. I think they are expecting the app to close before writing. How to do the profiling on a live application?

Thanks,
Alasdair
Reply all
Reply to author
Forward
0 new messages