Measure Go Code Segment Performance and Detect Bottlenecks

StackImpact automatically covers many aspects of application performance measurement, including hot spots in CPU usage, memory allocations, I/O, channels and lock contention. It also provides a bottleneck view for HTTP handlers and calls as well as multiple runtime metrics.

It is sometimes necessary to measure application specific code performance and trace bottlenecks. For these cases, the StackImpact agent provides a very simple API:

segment := agent.MeasureSegment("Segment1")
defer segment.Stop()

The agent will calculate the 95th percentile of the segment execution times during a measurement interval, typically 60 seconds, and report it to the Dashboard.

The following example demonstrates the basic usage.

package main

import (
	"net/http"
	"time"

	"github.com/stackimpact/stackimpact-go"
)

var agent *stackimpact.Agent

func someAPICall() {
	segment := agent.MeasureSegment("SomeAPICall")
	defer segment.Stop()

	res, err := http.Get("https://jsonplaceholder.typicode.com/posts")
	if err == nil {
		defer res.Body.Close()
	}
}

func main() {
	// Initialize StackImpact agent
	agent = stackimpact.Start(stackimpact.Options{
		AgentKey: "agent key here",
		AppName: "Example App",
	})

    for {
      someAPICall()
      time.Sleep(5 * time.Second)
    }
}

After running the example application for some time, here is what we see in the Dashboard.

Follow us on Twitter @stackimpact for information on Golang performance profiling and monitoring.