Pocket Gophers

How to be proactive about performance

You've read articles that tell you how counting goroutines or memory allocations fixed their performance woes, but you just end up wasting time on code that is barely used or gets deleted. Maybe you're starting a new program and want to do it right this time.

How can you be proactive about performance and make your efforts count?

The best thing you can do is set yourself up so you can improve performance where and when it is needed. The process to do this is:

  1. Instrument your code
  2. Monitor its performance
  3. Improve it where it matters most

This process should extend from development, through production, and keep going until the program isn't running anymore.

Instrument your code

To improve your program’s performance, you need to know how the resources (CPU, time, etc) used by the program are associated with the code using them. This is done by adding code to keep track of how a particular part of your code uses the resources. Adding this code is called instrumenting your code. The data produced by the instrumentation are referred to as metrics.

My survey of 10 packages to instrument your go application includes sample code and output so you can choose which package works best for you.

Monitor its performance

Your program’s performance will depend on what it’s being asked to do, also known as the load it is under. Keeping track of how your program’s metrics change over time is called monitoring. If you’ve done a good job instrumenting, you will be able to see how your program responds to load changes over time.

Some popular monitoring platforms are Prometheus and InfluxDB.

Improve it where it matters most

Monitoring the program’s metrics over time will let you see problem areas and prioritize where to spend development time on improving performance. You can also see how the load changes over time, allowing your development efforts to track usage patterns. For example, if 10% of your load is using 90% of your resources, focus on improving performance for that load.

My tutorial, Concurrency Slower?, will introduce you to the tools and process of performance improvement.

Extra Credit: Build a realistic load simulation for testing or pre-production use

Maybe your program isn’t in production yet or you need to figure out how your program will scale as you get more users. To find out what will happen, you need to generate the expected load.

This is more than just hitting an end-point at an expected rate. You also need to worry about the frequency of request types, the number of clients (concurrent, per day, etc), ordering of requests that a client makes, etc. While simulating all these aspects is difficult, this simulation will give you the performance numbers to direct your improvements during development or testing, so make it as realistic as possible.

Dig into the Fundamentals of Go

Subscribe to receive a weekly email covering a Go fundamental. Be it the language, its tooling, or its packages, you will learn what you need to know.