You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/learning-paths/servers-and-cloud-computing/golang-on-azure/benchmarking.md
+44-54Lines changed: 44 additions & 54 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,42 +1,39 @@
1
1
---
2
-
title: Benchmarking via go test -bench
2
+
title: Benchmarking using go test -bench
3
3
weight: 6
4
4
5
5
### FIXED, DO NOT MODIFY
6
6
layout: learningpathall
7
7
---
8
8
9
-
## Run Performance Tests Using go test -bench
9
+
## Run performance tests using go test -bench
10
10
11
-
`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.
11
+
`go test -bench` is Go’s built-in benchmark runner. It repeatedly executes benchmark functions and reports latency (ns/op). With the `-benchmem` flag, it also reports memory usage (B/op) and allocations (allocs/op). It’s simple, reliable, and requires only writing benchmark functions in the standard Golang testing package.
12
12
13
-
1. Create a Project Folder
14
-
15
-
In your terminal, create a directory for your benchmark project and navigate into it:
13
+
## Create a project folder
16
14
15
+
Create a directory for your benchmark project and navigate to it:
17
16
```console
18
17
mkdir gosort-bench
19
18
cd gosort-bench
20
19
```
21
20
22
-
2. Initialize a Go Module
23
-
24
-
Inside your project directory, initialize a new Go module by running:
21
+
## Initialize a Go module
25
22
23
+
Initialize a new module:
26
24
```console
27
25
go mod init gosort-bench
28
26
```
29
27
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.
30
28
31
-
3. Add Sorting Functions
32
-
33
-
Create a file called `sorting.go`:
29
+
## Add sorting functions
34
30
31
+
Create a file named `sorting.go`:
35
32
```console
36
33
nano sorting.go
37
34
```
38
-
Paste the following code in `sorting.go`:
39
35
36
+
Paste the following implementation into `sorting.go`:
40
37
```go
41
38
package sorting
42
39
funcBubbleSort(arr []int) {
@@ -75,9 +72,10 @@ func partition(arr []int, low, high int) int {
75
72
return i + 1
76
73
}
77
74
```
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.
75
+
The code contains two sorting methods, Bubble Sort and Quick Sort, which arrange numbers in order from smallest to largest:
76
+
77
+
- 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.
78
+
- 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
79
82
80
To summarize, Bubble Sort is simple but slow, while Quick Sort is more efficient and usually much faster for big lists of numbers.
83
81
@@ -90,16 +88,14 @@ mkdir sorting
90
88
mv sorting.go sorting/
91
89
```
92
90
93
-
4. Add Benchmark Tests
94
-
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):
91
+
### Add benchmark tests
96
92
93
+
Create a benchmark file named `sorting_benchmark_test.go` in the project root:
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.
132
+
The code implements a benchmark that measures the performance of Bubble Sort and Quick Sort in Go by generating a new list of 10,000 random numbers before each run to keep the test fair and consistent. The BenchmarkBubbleSort() function evaluates the slower Bubble Sort algorithm, while the BenchmarkQuickSort() function evaluates the faster Quick Sort algorithm, allowing you to compare their relative speeds and efficiency.
140
133
141
134
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.
142
135
@@ -146,11 +139,9 @@ Execute the benchmark suite using the following command:
146
139
```console
147
140
go test -bench=. -benchmem
148
141
```
149
-
-bench=. runs every function whose name starts with Benchmark.
150
-
-benchmem adds memory metrics (B/op, allocs/op) to the report.
151
-
152
-
You should see output similar to:
142
+
`-bench=.` runs every function whose name starts with `Benchmark`. `-benchmem` adds memory metrics (B/op, allocs/op) to the report.
* 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
-
155
+
### Metrics explained
156
+
157
+
- ns/op: nanoseconds per operation (lower is better)
158
+
- B/op: bytes allocated per operation (lower is better)
159
+
- allocs/op: number of heap allocations per operation (lower is better)
160
+
169
161
### Benchmark summary on Arm64
170
-
Here is a summary of benchmark results collected on an Arm64 D4ps_v6 Ubuntu Pro 24.04 LTS virtual machine.
171
162
172
-
| Benchmark | Value on Virtual Machine |
173
-
|-------------------|--------------------------|
174
-
| BubbleSort (ns/op) | 36,616,759 |
175
-
| QuickSort (ns/op) | 340,873 |
176
-
| BubbleSort runs | 32 |
177
-
| QuickSort runs | 3,506 |
178
-
| Allocations/op | 0 |
179
-
| Bytes/op | 0 |
180
-
| Total time (s) | 2.905 |
163
+
Results collected on an Arm64 **D4ps_v6** Ubuntu Pro 24.04 LTS virtual machine:
181
164
182
-
### Benchmark summary on x86_64
183
-
Here is a summary of the benchmark results collected on x86_64 D4s_v6 Ubuntu Pro 24.04 LTS virtual machine.
165
+
| Benchmark | Value |
166
+
|----------------------|-------|
167
+
| BubbleSort (ns/op) | 36,616,759 |
168
+
| QuickSort (ns/op) | 340,873 |
169
+
| BubbleSort runs | 32 |
170
+
| QuickSort runs | 3,506 |
171
+
| Allocations/op | 0 |
172
+
| Bytes/op | 0 |
173
+
| Total time (s) | 2.905 |
174
+
175
+
## Benchmark summary on x86-64
176
+
177
+
Results collected on an x86-64 **D4s_v6** Ubuntu Pro 24.04 LTS virtual machine:
184
178
185
179
| Benchmark | Value on Virtual Machine |
186
180
|-------------------|--------------------------|
@@ -193,12 +187,8 @@ Here is a summary of the benchmark results collected on x86_64 D4s_v6 Ubuntu Pro
193
187
| Total time (s) | 2.716 |
194
188
195
189
196
-
### Benchmarking comparison summary
197
-
198
-
When you compare the benchmarking results you will notice that on the Azure Cobalt 100:
199
-
200
-
Azure Cobalt 100 (Arm64) outperforms in both BubbleSort and QuickSort benchmarks, with the advantage more pronounced for QuickSort. The performance delta (~15–33%) shows how Arm Neoverse cores deliver strong results in CPU-bound, integer-heavy workloads common in Go applications.
190
+
## Benchmarking comparison summary
201
191
202
-
For real-world Go applications that rely on sorting, JSON processing, and other recursive or data-processing workloads, running on Azure Cobalt 100 Arm64 VMs can deliver better throughput and reduced execution time compared to similarly sized x86_64 VMs.
192
+
On Azure Cobalt 100 (Arm64), both BubbleSort and QuickSort run faster, with a larger advantage for QuickSort. The observed performance delta (~15–33%) highlights how Arm Neoverse cores excel at CPU-bound, integer-heavy workloads common in Go services.
203
193
204
-
These results validate the benefits of running Go workloads on Azure Cobalt 100 Arm64 instances, and establish a baseline for extending benchmarks to real-world workloads beyond sorting.
194
+
For real-world Go applications, such as sorting, JSON processing, and other recursive or data-processing tasks, Azure Cobalt 100 Arm64 VMs can provide higher throughput and lower execution time than similarly sized x86-64 VMs. These results validate the benefits of running Go on Cobalt 100 and establish a baseline for extending benchmarks beyond simple sorting.
0 commit comments