Talking about performance, profiling, testing and other stuff
Tags: performance, testing, profiling, go
Published: 2023-04-18 08:40:00

---
title: Talking about performance, profiling, testing and other stuff
published: true
description: In this article I talk about performance in applications. What is performance testing? What is load testing? Is it worth it?
tags: performance, testing, profiling, golang
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6xaiu1p8kh7ygr12a1x5.jpeg
# Use a ratio of 100:42 for best results.
published_at: 2023-04-18 08:40 +0000
---

**Performance testing** is a type of software testing that measures how well an application performs under certain conditions. It is the process of determining the speed, responsiveness, stability, and scalability of a software application under a particular workload.

It is significant for a number of reasons. First off, it aids in locating performance bottlenecks and other problems that might affect an application's performance. Second, it helps to guarantee that an application is scalable and can handle the anticipated traffic. Last but not least, it assists in ensuring that an application satisfies the performance standards established by the stakeholders.

It is typically done during the testing phase of the software development life cycle (SDLC), after the functional testing has been completed. It can also be done before the application is released to production to ensure that it meets the performance requirements.

Some of the variables that can be measured in performance testing include:

1. Response Time - the time it takes for the application to respond to a user request.
2. Throughput - the number of transactions that can be processed by the application in a given time period.
3. Concurrency - the number of users that can access the application at the same time without impacting performance.
4. CPU usage - the amount of CPU resources consumed by the application.
5. Memory usage - the amount of memory resources consumed by the application.
6. Network latency - the time it takes for data to travel between the client and server.

## Is it the same than load testing?
Some people are not able to find differences between performance testing and load testing. They are related (somehow).

Load testing is a type of software testing that measures how well an application can handle a specific load or level of demand. The goal of load testing is to determine the maximum capacity of an application and to identify any bottlenecks or issues that may impact its performance under high load. Load testing typically involves simulating a large number of users or transactions to see how the application responds. The focus is on measuring the response time, throughput, and resource utilization of the application under load.

Performance testing, on the other hand, is a broader term that encompasses several types of testing. It can include load testing, stress testing, endurance testing, and other types of testing. The goal is to identify performance issues and bottlenecks that may impact the application's performance under different conditions.

## How to Write Performance Tests in Golang
Why Write Performance Tests in Golang? Golang is a programming language that is well-suited for building high-performance applications. However, even the best-written code can suffer from performance issues when it is deployed in the real world. Performance testing helps identify these issues before they become major problems.

In Golang, you can write performance tests using the built-in testing package. The package provides the Bench function, which allows you to write benchmark tests for your code.

Here's an example of a performance test for a function that sorts an array of integers:

```go
func BenchmarkSort(b *testing.B) {
    // Create a random array of integers
    arr := make([]int, 1000000)
    for i := 0; i < len(arr); i++ {
        arr[i] = rand.Intn(1000000)
    }
    // Reset the timer
    b.ResetTimer()
    // Run the sort function b.N times
    for n := 0; n < b.N; n++ {
        sort.Ints(arr)
    }
}
```

In this example, we create a random array of integers and then reset the timer before running the `sort.Ints` function `b.N` times. The `b.N` value is an integer that represents the number of times the `sort.Ints` function should be run.

Then, you can use the `go test` command. To run the BenchmarkSort test from the previous example, you can use the following command:

```sh
go test -bench=.
```

The `-bench` flag specifies the pattern to match against benchmark test names. In this case, the . matches all benchmark tests.


## Should I implement performance tests?
As always: it depends. If the value of your application is to be a high performance communication system, it would make sense to integrate performance tests. If your application is a TODO-list, maybe is not as important. **You need to implement the tests that bring more value to you**.

Also, you have to take into account that, if your application depends on third-party services or in other web services, it can be difficult to detect bottlenecks during your performance testing. Also, some performance issues will appear only in your production environment. Why? Because they will take place only in those scenarios where the load is high and the users behavior is real and, maybe, less predictable. This is the usual scenario for cloud based applications.

Performance testing can be interesting for certain parts of your code but you can be interested in **continuous profiling**.

### Continuous profiling
Profiling involves analyzing an application's behavior, such as its CPU and memory usage, to identify performance bottlenecks and areas for optimization. It is typically done by running the application with a profiling tool that collects data about its performance, which can then be analyzed to identify areas for improvement. Performance testing, on the other hand, involves testing an application's performance under specific conditions. But those conditions could not reproduce the workflows from your users (you're not testing a real behavior or under the correct load).

Continuous profiling is a technique used in software development to analyze and monitor the performance of an application in real-time. It involves the collection of data about the application's performance, such as CPU usage, memory usage, and other metrics, continuously and automatically.

In contrast to traditional profiling, which is typically performed manually and only for short periods of time, continuous profiling is designed to provide ongoing insights into an application's performance. By monitoring the application's behavior over time, continuous profiling can identify trends and patterns that may indicate performance issues or opportunities for optimization.

**Continuous profiling** is often used in conjunction with other techniques, such as continuous integration and continuous delivery, to ensure that an application is performing optimally throughout the development process. It can also be used in production environments to monitor the performance of an application in real-time and identify issues before they become critical.

Overall, continuous profiling is a valuable tool for software developers and operations teams, as it allows them to identify and address performance issues quickly and efficiently, leading to faster and more reliable software delivery.

[You can learn more about continuous profiling in the CNCF website](https://www.cncf.io/blog/2022/05/31/what-is-continuous-profiling/).