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
+32-31Lines changed: 32 additions & 31 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,36 +6,36 @@ weight: 6
6
6
layout: learningpathall
7
7
---
8
8
9
-
## Run the performance tests using go test -bench
9
+
## Run Performance Tests Using go test -bench
10
10
11
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.
12
12
13
13
1. Create a Project Folder
14
14
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:
16
16
17
17
```console
18
18
mkdir gosort-bench
19
19
cd gosort-bench
20
20
```
21
21
22
22
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:
25
25
26
26
```console
27
27
go mod init gosort-bench
28
28
```
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.
30
30
31
31
3. Add Sorting Functions
32
32
33
-
Create a file called **sorting.go**:
33
+
Create a file called `sorting.go`:
34
34
35
35
```console
36
36
nano sorting.go
37
37
```
38
-
Paste this code in **sorting.go** file:
38
+
Paste the following code in `sorting.go`:
39
39
40
40
```go
41
41
package sorting
@@ -75,13 +75,15 @@ func partition(arr []int, low, high int) int {
75
75
return i + 1
76
76
}
77
77
```
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.
83
85
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.
85
87
86
88
```console
87
89
mkdir sorting
@@ -90,13 +92,13 @@ mv sorting.go sorting/
90
92
91
93
4. Add Benchmark Tests
92
94
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):
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.
134
140
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.
141
142
142
143
### Run the Benchmark
143
144
144
145
Execute the benchmark suite using the following command:
145
146
```console
146
147
go test -bench=. -benchmem
147
148
```
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.
0 commit comments