Manually instrumenting profiling in code

If there is a particular place in your code that you'd like to profile specifically, you can implement profiling directly around that code. This can be especially useful if you only want to profile a smaller segment of your code, if you want the pprof output to be smaller and more concise, or if you don't want to add additional overhead to known expensive parts of your code by implementing profiling around them. There are separate methodologies for performing CPU and memory profiling around different segments of a code base.

Profiling CPU utilization for a specific chunk of code would look as follows:

function foo() {
pprof.StartCPUProfile()
defer pprof.StopCPUProfile()
...
code
...
}

Profiling memory utilization for a specific chunk of code would look as follows:

function bar() {
runtime.GC()
defer pprof.WriteHeapProfile()
...
code
...
}

Hopefully, we won't have to implement individual segments of code if we design effectively, iterate with impact, and implement our profiling using the idioms from the next section, but it is nice to know that this is always a potential option for profiling code and retrieving meaningful output.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.214.155