Skip to content

Commit 7facf69

Browse files
authored
Update benchmarking.md
1 parent 3070b3b commit 7facf69

1 file changed

Lines changed: 32 additions & 31 deletions

File tree

  • content/learning-paths/servers-and-cloud-computing/golang-on-azure

content/learning-paths/servers-and-cloud-computing/golang-on-azure/benchmarking.md

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,36 @@ weight: 6
66
layout: learningpathall
77
---
88

9-
## Run the performance tests using go test -bench
9+
## Run Performance Tests Using go test -bench
1010

1111
`go test -bench` (the benchmarking mode of go test) is Golang’s built-in benchmarking framework that measures the performance of functions by running them repeatedly and reporting execution time (**ns/op**), memory usage, and allocations. With the `-benchmem flag`, it also shows memory usage and allocations. It’s simple, reliable, and requires only writing benchmark functions in the standard Golang testing package.
1212

1313
1. Create a Project Folder
1414

15-
Open your terminal and create a new folder for this project:
15+
In your terminal, create a directory for your benchmark project and navigate into it:
1616

1717
```console
1818
mkdir gosort-bench
1919
cd gosort-bench
2020
```
2121

2222
2. Initialize a Go Module
23-
24-
Inside the project directory, run following command:
23+
24+
Inside your project directory, initialize a new Go module by running:
2525

2626
```console
2727
go mod init gosort-bench
2828
```
29-
This creates a go.mod file, which defines the module path (gosort-bench in this case) and marks the directory as a Go project. The go.mod file also allows Go to manage dependencies (external libraries) automatically, ensuring your project remains reproducible and easy to maintain.
29+
This creates a `go.mod` file, which defines the module path (gosort-bench in this case) and marks the directory as a Go project. The `go.mod` file also allows Go to manage dependencies (external libraries) automatically, ensuring your project remains reproducible and easy to maintain.
3030

3131
3. Add Sorting Functions
3232

33-
Create a file called **sorting.go**:
33+
Create a file called `sorting.go`:
3434

3535
```console
3636
nano sorting.go
3737
```
38-
Paste this code in **sorting.go** file:
38+
Paste the following code in `sorting.go`:
3939

4040
```go
4141
package sorting
@@ -75,13 +75,15 @@ func partition(arr []int, low, high int) int {
7575
return i + 1
7676
}
7777
```
78-
- The code contains **two sorting methods**, Bubble Sort and Quick Sort, which arrange numbers in order from smallest to largest.
79-
- **Bubble Sort** works by repeatedly comparing two numbers side by side and swapping them if they are in the wrong order. It keeps doing this until the whole list is sorted.
80-
- **Quick Sor**t is faster. It picks a "pivot" number and splits the list into two groups — numbers smaller than the pivot and numbers bigger than it. Then it sorts each group separately.
81-
- The **function** partition helps Quick Sort decide where to split the list based on the pivot number.
82-
- In short, **Bubble Sort is simple but slow,** while **Quick Sort is smarter and usually much faster for big lists of numbers**.
78+
The code contains two sorting methods, Bubble Sort and Quick Sort, which arrange numbers in order from smallest to largest.
79+
* Bubble Sort works by repeatedly comparing two numbers side by side and swapping them if they are in the wrong order. It keeps doing this until the whole list is sorted.
80+
* Quick Sort is faster. It picks a pivot number and splits the list into two groups — numbers smaller than the pivot and numbers bigger than it. Then it sorts each group separately. The function partition helps Quick Sort decide where to split the list based on the pivot number.
81+
82+
To summarize, Bubble Sort is simple but slow, while Quick Sort is more efficient and usually much faster for big lists of numbers.
83+
84+
At this point, you have defined two sorting algorithms ready to be benchmarked.
8385

84-
You create the sorting folder and then move `sorting.go` into it to organize your code properly so that the Go module can reference it as `gosort-bench/sorting`.
86+
To keep your project modular and maintainable, it’s best practice to place implementation code inside its own package folder. This allows benchmarks and other Go files to import it cleanly.
8587

8688
```console
8789
mkdir sorting
@@ -90,13 +92,13 @@ mv sorting.go sorting/
9092

9193
4. Add Benchmark Tests
9294

93-
Create another file called s**orting_benchmark_test.go**:
95+
Next, create a benchmark test file named `sorting_benchmark_test.go` in your project’s root directory (not inside the sorting/ folder, so it can import the sorting package cleanly):
9496

9597
```console
9698
nano sorting_benchmark_test.go
9799
````
98100

99-
Paste the below code:
101+
Paste the following code into it:
100102

101103
```go
102104
package sorting_test
@@ -131,24 +133,23 @@ func BenchmarkQuickSort(b *testing.B) {
131133
}
132134
}
133135
```
136+
The code implements a benchmark that checks how fast Bubble Sort and Quick Sort run in Go.
137+
- It first creates a list of 10,000 random numbers each time before running a sort, so the test is fair and consistent.
138+
- The BenchmarkBubbleSort() function measures the speed of sorting using the slower Bubble Sort method.
139+
- The BenchmarkQuickSort() function measures the speed of sorting using the faster Quick Sort method.
134140

135-
- The code is a **benchmark test** that checks how fast Bubble Sort and Quick Sort run in Go.
136-
- It first creates a **list of 10,000 random numbers** each time before running a sort, so the test is fair and consistent.
137-
- **BenchmarkBubbleSort** measures the speed of sorting using the slower Bubble Sort method.
138-
- **BenchmarkQuickSort** measures the speed of sorting using the faster Quick Sort method.
139-
140-
When you run **go test -bench=. -benchmem**, Go will show you how long each sort takes and how much memory it uses, so you can compare the two sorting techniques.
141+
When you run the benchmark, Go will show you how long each sort takes and how much memory it uses, so you can compare the two sorting techniques.
141142

142143
### Run the Benchmark
143144

144145
Execute the benchmark suite using the following command:
145146
```console
146147
go test -bench=. -benchmem
147148
```
148-
- **-bench=.** - runs all functions starting with Benchmark.
149-
- **-benchmem** - also shows memory usage (allocations per operation).
149+
-bench=. runs every function whose name starts with Benchmark.
150+
-benchmem adds memory metrics (B/op, allocs/op) to the report.
150151

151-
You should see the output similar to this:
152+
You should see output similar to:
152153

153154
```output
154155
goos: linux
@@ -159,14 +160,14 @@ BenchmarkQuickSort-4 3506 340873 ns/op 0
159160
PASS
160161
ok gosort-bench 2.905s
161162
```
162-
### Matrics Explanation
163-
164-
- **ns/op** - nanoseconds per operation (lower is better).
165-
- **B/op** - bytes of memory used per operation.
166-
- **allocs/op** - how many memory allocations happened per operation.
163+
### Metrics Explained
167164

165+
* ns/op – nanoseconds per operation (lower is better). This is the primary latency metric.
166+
* B/op – bytes allocated per operation (lower is better). This is useful for spotting hidden allocations.
167+
* allocs/op – number of heap allocations per operation (lower is better). Zero here means the algorithm itself didn’t allocate.
168+
168169
### Benchmark summary on Arm64
169-
Here is a summary of benchmark results collected on an Arm64 **D4ps_v6 Ubuntu Pro 24.04 LTS virtual machine**.
170+
Here is a summary of benchmark results collected on an Arm64 D4ps_v6 Ubuntu Pro 24.04 LTS virtual machine.
170171

171172
| Benchmark | Value on Virtual Machine |
172173
|-------------------|--------------------------|
@@ -179,7 +180,7 @@ Here is a summary of benchmark results collected on an Arm64 **D4ps_v6 Ubuntu Pr
179180
| Total time (s) | 2.905 |
180181

181182
### Benchmark summary on x86_64
182-
Here is a summary of the benchmark results collected on x86_64 **D4s_v6 Ubuntu Pro 24.04 LTS virtual machine**.
183+
Here is a summary of the benchmark results collected on x86_64 D4s_v6 Ubuntu Pro 24.04 LTS virtual machine.
183184

184185
| Benchmark | Value on Virtual Machine |
185186
|-------------------|--------------------------|

0 commit comments

Comments
 (0)