What is Performance Tuning?
The primary goal of performance tuning is to find bad parts /bottlenecks of code and to improve a bad parts/bottlenecks of codes. How is it possible to find bottlenecks of code and to overcome bottlenecks of a code? Consider the following example to understand a concept in a more explicit manner which is as explained below:
There is a problem or trouble. There are two ways or two algorithms by following them problem solved. To know that whether an algorithm A performs faster than Algorithm B. Then, it is not possible by looking at the algorithm which will give us a better result. In that case, problem can be solved by following both algorithms. While an execution of code which is written by following the first algorithm, though file contains a benchmarking and profiling result for this code. However, In the same way, write code by following the second algorithm. Execute a code and store a result in a second file.As the second file also contains a profiling and benchmark result.
Now result of two files can be compared to check that which algorithm is optimum.GoLang comes with two features profiling and benchmark. Both of these help to analyze which code is doing well. But a most important thing here is that a result to be stored in a file so that will help to make comparisons for results of code and choose an optimum result. The code is optimum in a sense that which is taking less memory, less resource usage, execution time.
From all of above, there is one important thing is that every time while executing a code, its benchmarking and profiling result required stored in a file. Otherwise, it is not possible to make comparisons of results.
How Does Performance Tuning Work?
- Write a Golang code according to requirements in form of functions.
- Write a BenchMark for written methods of Golang in a file with extension as “filename_test.go”.
- Test all the BenchMarks for Golang code present in a file in which all the BenchMark specified along with this store a result in a BenchMark file “go test -run = xxx -bench =. -cpuprofile =cpu.out | tee bench3”.
- Implement whatever you want at the code, to optimize code performance.
- Test all the BenchMarks for Golang code present in a file in which all BenchMark specified along with this store a result in BenchMark file “go test -run = xxx -bench =. -cpuprofile =cpu.out | tee bench3”.
- Compare the result of both files using Benchcmp.
- Using following command compare BenchMarking result of two files “benchcmp bench1 bench2”.
- After the execution of command output will be visible.
Best Practices of Performance Tuning
- Content Delivery Network (CDN)
- Shrink Libraries
- Consider entire data flow
- Performance Management
- Check queries and shrink images
- Use Top-Down Tuning Approach
- Revise System Architecture and Configuration files
- Proper Data Type and Constraints
- Indexing and Optimized Queries
- Stored Procedures and Functions
- Response-Time Analysis
Benefits of Enabling Performance Tuning
The main focus of Performance Tuning is to find a bad part of the code and convert a lousy part of the code to good part. For this, every line of code which is present in our file converted into as an object in memory. There is an excellent relation between code quality and performance.
Let us consider that lousy code is optimized at the machine and now a good code 16% better than faulty code. Is it possible to achieve more than that? Yes, there is an excellent relation between code quality and performance.
While writing code more simply, the code is refactored and decoupled from each other. This step increases a code simplicity, but performance cannot request. Because when simplicity in our code increases, then in that case execution time increases. Thus, while achieving one factor other is overwhelmed.
Here a most important thing to improve performance is to identify bottlenecks and then develop these by one. These bottlenecks and loopholes can identify by analyzing our code through profiling and benchmarking.
Profiling helps to identify each line of the method that at which of code is a problem. This decision better changed to code made. Then further current code’s result is compared with the previous code result. Tracing identify each core of CPU. Here mean to say that at which core of processor our function is executing.
Why Performance Tuning Matters?
It is mandatory in the case while finding a difference between benchmarking results of two codes –
It is used with both tests and benchmarks to check that if there is synchronization. However, this can be more clear by looking at the following example –
go test -race
However this above is used in the same way both with tests and benchmarks. They make a call to
function "ParseAdexpMessage" to check any synchronization issue.
How to Adopt Performance Tuning?
Benchmark is required in a condition, whenever there is a requirement to compare the result for two benchmarks. To use a Benchcmp, it is necessary files should be available at which this benchcmp command executed. As looking a below screenshot, it will be more clear that benchcmp applied on two files named as bench1 and bench2. Despite having two files, using a "benchmp "command will be useless.
The syntax for "benchcmp" command as shown below –
Benchcmp "firstfile_contians_benchmark_Restult" "secondfile_having_benchamrk_result"
Commands required to understand
Now let us look following to understand that how a result stored in a file required to compare the result using "benchcmp."If there is no file present, then how the result of two files compared using a benchcmp? It will be more clear by looking at following commands explained along with an example –
To Store result in a benchmark file –
While storing the result in a file, following benchmarking command used –
go test -run=xxx -bench=. -cpuprofile=cpu.out | tee bench3
Similarly, the result for memory profiling stored in a file as a reference to an above command. In another hand, both a memory and CPU profiling results stored in a single file.
All other things are same as doing profiling and benchmark on go code using a benchmark file. However, the only difference is that result stored in a file, so to make comparisons later on that will be helpful for optimization of code. Now, to get a result or compare two files following command required used.
Syntax: benchcmp “filename_contianing_old_result” “filename-containing_new_result”
Performance Tuning Tools
For a Performance Tuning, though no need for any specific tool. Usually, the Testing package is required to do a benchmark and profiling on the code.