The normal way is to use:
go tool pprof
But my use case is this: I'm using a golang javascript interpreter like https://github.com/robertkrimen/otto or https://github.com/rogchap/v8go
I want to profile when I run some user supplied javascript to know if it's using too much CPU and I need to kill the go routine.
So a valid case of the javascript taking 30 seconds would be it makes an http connection and is just sitting there idle no cpu use until the connection finally finishes.
So I can't just kill anything that takes too long. I need to only kill javascript that's pegging the cpu or eating up tons of memory.
As Volker said it's gonna be quite involved to do so in pure go.
Perhaps you could approach the problem differently and use an other mechanism to limit the cpu/memory consumption of the js interpreter ?
You could maybe fork and make the js interpreter run in a different process. That way you could use the standard linux mechanism to control and isolate the resource consumption of a process (cgroups, nice, memory limits, docker etc...)