Debian Codegraph @

src/uu/sort/BENCHMARKING.md

raw
1 # Benchmarking sort
2
3 <!-- spell-checker:ignore (words) kbytes -->
4
5 Most of the time when sorting is spent comparing lines. The comparison functions however differ based
6 on which arguments are passed to `sort`, therefore it is important to always benchmark multiple scenarios.
7 This is an overview over what was benchmarked, and if you make changes to `sort`, you are encouraged to check
8 how performance was affected for the workloads listed below. Feel free to add other workloads to the
9 list that we should improve / make sure not to regress.
10
11 Run `cargo build --release` before benchmarking after you make a change!
12
13 ## Sorting a wordlist
14
15 - Get a wordlist, for example with [words](<https://en.wikipedia.org/wiki/Words_(Unix)>) on Linux. The exact wordlist
16 doesn't matter for performance comparisons. In this example I'm using `/usr/share/dict/american-english` as the wordlist.
17 - Shuffle the wordlist by running `sort -R /usr/share/dict/american-english > shuffled_wordlist.txt`.
18 - Benchmark sorting the wordlist with hyperfine: `hyperfine "target/release/coreutils sort shuffled_wordlist.txt -o output.txt"`.
19
20 ## Sorting a wordlist with ignore_case
21
22 - Same wordlist as above
23 - Benchmark sorting the wordlist ignoring the case with hyperfine: `hyperfine "target/release/coreutils sort shuffled_wordlist.txt -f -o output.txt"`.
24
25 ## Sorting numbers
26
27 - Generate a list of numbers:
28 ```
29 shuf -i 1-1000000 -n 1000000 > shuffled_numbers.txt
30 # or
31 seq 1 1000000 | sort -R > shuffled_numbers.txt
32 ```
33 - Benchmark numeric sorting with hyperfine
34 ```
35 hyperfine --warmup 3 \
36 '/tmp/gnu-sort -n /tmp/shuffled_numbers.txt'
37 '/tmp/uu_before sort -n /tmp/shuffled_numbers.txt'
38 '/tmp/uu_after sort -n /tmp/shuffled_numbers.txt'
39 ```
40
41 ## Sorting numbers with -g
42
43 - Same list of numbers as above.
44 - Benchmark numeric sorting with hyperfine: `hyperfine "target/release/coreutils sort shuffled_numbers.txt -g -o output.txt"`.
45
46 ## Sorting numbers with SI prefixes
47
48 - Generate a list of numbers:
49
50 ## Cargo.toml
51
52 ```toml
53 [dependencies]
54 rand = "0.10.0"
55 ```
56
57 ## main.rs
58
59 ```rust
60 use rand::prelude::*;
61 fn main() {
62 let suffixes = ['k', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y', 'R', 'Q'];
63 let mut rng = rand::rng();
64 for _ in 0..100000 {
65 println!(
66 "{}{}",
67 rng.random_range(0..1000000),
68 suffixes[rng.random_range(..suffixes.len())],
69 )
70 }
71 }
72 ```
73
74 ## running
75
76 `cargo run > shuffled_numbers_si.txt`
77
78 - Benchmark numeric sorting with hyperfine: `hyperfine "target/release/coreutils sort shuffled_numbers_si.txt -h -o output.txt"`.
79
80 ## External sorting
81
82 Try running commands with the `-S` option set to an amount of memory to be used, such as `1M`. Additionally, you could try sorting
83 huge files (ideally multiple Gigabytes) with `-S` (or without `-S` to benchmark with our default value).
84 Creating such a large file can be achieved by running `cat shuffled_wordlist.txt | sort -R >> shuffled_wordlist.txt`
85 multiple times (this will add the contents of `shuffled_wordlist.txt` to itself).
86 Example: Run `hyperfine './target/release/coreutils sort shuffled_wordlist.txt -S 1M' 'sort shuffled_wordlist.txt -S 1M'`
87
88 ## Merging
89
90 "Merge" sort merges already sorted files. It is a sub-step of external sorting, so benchmarking it separately may be helpful.
91
92 - Splitting `shuffled_wordlist.txt` can be achieved by running `split shuffled_wordlist.txt shuffled_wordlist_slice_ --additional-suffix=.txt`
93 - Sort each part by running `for f in shuffled_wordlist_slice_*; do sort $f -o $f; done`
94 - Benchmark merging by running `hyperfine "target/release/coreutils sort -m shuffled_wordlist_slice_*"`
95
96 The merge comparator compares lines lazily, so the locale matters a lot here: in a
97 UTF-8 locale (locale-aware collation) merging must avoid computing a full collation sort
98 key for every line, since a k-way merge only performs O(n log k) comparisons (and none at
99 all when merging a single already-sorted file). Make sure to benchmark both the C locale
100 and a UTF-8 locale, and the single-file case in particular:
101
102 ```
103 # Single already-sorted file (worst case for eager sort-key computation)
104 LC_ALL=en_US.UTF-8 hyperfine --warmup 3 \
105 'target/release/coreutils sort -m /usr/share/dict/words' \
106 'sort -m /usr/share/dict/words'
107
108 # Several pre-sorted slices
109 LC_ALL=en_US.UTF-8 hyperfine --warmup 3 \
110 'target/release/coreutils sort -m shuffled_wordlist_slice_*' \
111 'sort -m shuffled_wordlist_slice_*'
112 ```
113
114 The same scenarios are covered by the `merge_single_file_utf8_locale` and
115 `merge_pre_sorted_files_utf8_locale` cases in `benches/sort_locale_utf8_bench.rs`.
116
117 ## Check
118
119 When invoked with -c, we simply check if the input is already ordered. The input for benchmarking should be an already sorted file.
120
121 - Benchmark checking by running `hyperfine "target/release/coreutils sort -c sorted_wordlist.txt"`
122
123 ## Stdout and stdin performance
124
125 Try to run the above benchmarks by piping the input through stdin (standard input) and redirect the
126 output through stdout (standard output):
127
128 - Remove the input file from the arguments and add ```cat [input_file] |``` at the beginning.
129 - Remove `-o output.txt` and add `> output.txt` at the end.
130
131 Example: `hyperfine "target/release/coreutils sort shuffled_numbers.txt -n -o output.txt"` becomes
132 `hyperfine "cat shuffled_numbers.txt | target/release/coreutils sort -n > output.txt`
133
134 - Check that performance is similar to the original benchmark.
135
136 ## Comparing with GNU sort
137
138 Hyperfine accepts multiple commands to run and will compare them. To compare performance with GNU sort
139 duplicate the string you passed to hyperfine but remove the `target/release/coreutils` bit from it.
140
141 Example: `hyperfine "target/release/coreutils sort shuffled_numbers_si.txt -h -o output.txt"` becomes
142 `hyperfine "target/release/coreutils sort shuffled_numbers_si.txt -h -o output.txt" "sort shuffled_numbers_si.txt -h -o output.txt"`
143 (This assumes GNU sort is installed as `sort`)
144
145 ## Memory and CPU usage
146
147 The above benchmarks use hyperfine to measure the speed of sorting. There are however other useful metrics to determine overall
148 resource usage. One way to measure them is the `time` command. This is not to be confused with the `time` that is built in to the bash shell.
149 You may have to install `time` first, then you have to run it with `/bin/time -v` to give it precedence over the built in `time`.
150
151 ```plain
152 Command being timed: "target/release/coreutils sort shuffled_numbers.txt"
153 User time (seconds): 0.10
154 System time (seconds): 0.00
155 Percent of CPU this job got: 365%
156 Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.02
157 Average shared text size (kbytes): 0
158 Average unshared data size (kbytes): 0
159 Average stack size (kbytes): 0
160 Average total size (kbytes): 0
161 Maximum resident set size (kbytes): 25360
162 Average resident set size (kbytes): 0
163 Major (requiring I/O) page faults: 0
164 Minor (reclaiming a frame) page faults: 5802
165 Voluntary context switches: 462
166 Involuntary context switches: 73
167 Swaps: 0
168 File system inputs: 1184
169 File system outputs: 0
170 Socket messages sent: 0
171 Socket messages received: 0
172 Signals delivered: 0
173 Page size (bytes): 4096
174 Exit status: 0
175 ```
176
177 Useful metrics to look at could be:
178
179 - User time
180 - Percent of CPU this job got
181 - Maximum resident set size