When the website is online, the increase of traffic or temporary function failure will cause the user experience is quite bad, and how to find the bottleneck of performance quickly? Usually when the CPU is rushed to 100%, it is sometimes quite difficult to replicate and find out the key problem points. This article will introduce a set of tools called pyroscope, which allows developers to quickly find the code of performance bottlenecks.

Pyroscope

pyroscope currently supports the Python, Ruby or Go environments. The author will introduce the Go environment below.

What is Pyroscope?

Pyroscope is an open source performance monitoring platform with a simple Server and Agent architecture that allows developers to easily monitor code performance. Whether you are looking for performance data within 10 seconds or minutes, you can quickly and instantly see it. The storage behind Pyroscope uses Badger, a Key-Value database, and the performance is very good. Currently, only 3 languages are supported (Python, Ruby and Go) and NodeJS is expected to be supported in the future. If you have not yet imported any performance analysis tool or platform, Pyroscope is your best choice.

Pyroscope Architecture

If you are looking for a performance analysis tool platform, Pyroscope offers three major advantages that developers can use with confidence

  1. low CPU usage, no impact on existing platforms
  2. the ability to store years of data and look at it at a granularity as small as 10 seconds
  3. compressed data storage, reducing wasted hard disk space

The architecture is only divided into Server and Agent, you can refer to the following architecture diagram. In addition to the Go language, Python and Ruby App both use the pyroscope command to start the relevant app to monitor the system performance. The following architecture diagram from the official website

Pyroscope Architecture

Start Pyroscope service

There are two ways to start Pyroscope, the first is to start it directly with the docker command

1
docker run -it -p 4040:4040 pyroscope/pyroscope:latest server

Another option is to use docker-compose to start

1
2
3
4
5
6
7
8
---
services:
  pyroscope:
    image: "pyroscope/pyroscope:latest"
    ports:
      - "4040:4040"
    command:
      - "server"

Installing agent in Go

This article uses the Go language as an example, first import package

1
import "github.com/pyroscope-io/pyroscope/pkg/agent/profiler"

Then just write the following code in main.go:

1
2
3
4
profiler.Start(profiler.Config{
    ApplicationName: "simple.golang.app",
    ServerAddress:   "http://pyroscope:4040",
})

You can replace http://pyroscope with a custom hostname, and then open the above URL to see the performance monitoring screen.

performance monitoring screen

The screen allows you to quickly find out which SQL or function is taking too long to execute.

performance monitoring screen

Summary

This set of tools is very convenient, in Go language although you can use pprof to quickly find the problem, but inevitably still need to manually some places to find out the performance bottleneck, with this set of platform, you can monitor all the App, when the user has any problems, you can quickly check through Pyroscope to see which side of the code has problems.