How to run performance tests with Rust.

Alejandro Aldana
6 min readMar 10, 2022
  • Introduction

We currently have various programming languages that provide us with more efficient and faster platforms when it comes to response times, this is the clear example of Rust, a low-level language, where its main virtue is to be extremely efficient in every way. The latter is a great help, especially when it comes to heavy tasks, and precisely one of those tasks is the one we are here to discuss today.

We can find a high-demand task in many places, currently, most activities require others with many processes or simply the activity is the one that demands this increase in power, things like Machine Learning, the creation of video games or the construction and development of operating systems are activities that are best carried out with a low-level language and high efficiency, these being just a few examples. That is why today we will see in more detail how to measure efficiency and how to take advantage of available resources even more.

  • Prerequisites

To build this benchmarking, it is important to be able to have 2 things or requirements, these are, to have our environment configured with the appropriate bench, which in our case will be “ nightly “ and also to know the different variables of this language and how they are applied in an array.

To configure “nightly” we must make a couple of changes to how rust works with 3 commands in our terminal, this with rust already installed. The commands are:

1. rustup default nightly2. rustup toolchain install nightly3. rustup run nightly cargo bench -> Benchs exec with this

With this, we can now run “nightly” as benchmarking. Regarding the types of variables, we must consider the load that these mean for the compiler, that is why we will avoid using 64-bit variables, such as i64, u64, etc. Instead, we’ll use f32 variables, which are 32-bit float types, as the basis for declarations of different types of arrays, such as the following:

[1f 32; 3] --> [ 1.0, 1.0, 1.0 ][1 i 32; 3] --> [ 1, 1, 1 ]
  • Benchmarking

The first thing will be to define a base workspace for these benchmarking, this workspace must have a defined structure, since this is where we can write the performance tests that we require. The basis of this would be the following, where we only have the basic imports and the main Rust mod.

Base code

Within this mod, what we can do is create variables or functions that test the performance of our program or of a particular case that we want to put into practice, the definition of these would be as follows.

Base function

Here we can see how it is that we are making imports that are only used within the mod and we are also declaring a function that is linked to the same bench, said function what it does is an iteration, the same ones that are responsible for counting various parameters, such as time or efficiency. Within this function, only what we do is a sum, and, in the end, the result of this benching will be the time and performance that it took to perform said sum.

The result of said sum in the benchmarking should be as follows:

Basic result

Where we can see the time that the function called “bench” took and the total execution time at the bottom, as well as the flags of the results, where if we had more functions here, we could see the different results, whether they are successful, failed, ignored, measured, etc.

In our case, since the function is very simple, we don’t measure too much time, what’s more, we don’t measure anything because the operation is extremely simple, so we will make more complex functions and examples.

In this second case, we will use 2 types of variables, an int and a float, but in addition we will force or simulate a bad performance using a for loop in an exaggerated way and a list of the same type of variable, as we can see below:

Code example

This last operation leads us directly to the following result, where we can see that the operations with the floats they clearly weigh twice as much as int, and this is considering that we are using the same number of bytes (32 in each case) in each of the cases.

Result example

In other words, we can evaluate variable by variable if it is necessary to use one or the other for our development and as we saw here, this can have a clear weight in the result.

Explained in another way, being able to count on these tools is a key help since their possibilities are not only limited to the fact of measuring times in variables, with this we are able to take advantage of it in several key scenarios during the development life cycle of any software. For example:

Performance: As mentioned above, this is the most representative point of this article, being able to know the performance of a given solution is what makes benchmarks so useful. This gives us a base to know how we can improve or allows us to measure if the solution is performing well.

Time: The time of our solution or, better said, the time it takes to generate the result is essential, thanks to this, we can determine shorter paths and in general make the response to the client faster and therefore, we can handle more.

Functionality: The functionality can be understood in this case as the combination of the 2 previous points, we know what our solution is like, but we can measure if it is the best in different scenarios and involving variables to see the margin for improvement.

  • Conclusions

Benchmarking are undoubtedly powerful tools that we can use to get even more benefit from the performance of any language, especially low-level ones, which are usually the fastest and most robust when it comes to taking advantage of system resources, added to the fact that we can do performance tests and find where to make substantial improvements, we have as a result a complete method of maximum tuning for the performance of our developments.

With this, we can also evaluate the weight that each variable has within our program, as was the case in this example, so we already know that, if it is necessary or we can reduce the size or type of variables, we can do so to obtain an improvement. in the result in performance.

  • More information and thanks

For your attention… thank you so much.

--

--