Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- deps/benchee/.fetch +0 -0
- deps/benchee/.formatter.exs +5 -0
- deps/benchee/.hex +0 -0
- deps/benchee/CHANGELOG.md +459 -0
- deps/benchee/LICENSE.md +21 -0
- deps/benchee/README.md +1144 -0
- deps/benchee/hex_metadata.config +70 -0
- deps/benchee/lib/benchee.ex +109 -0
- deps/benchee/lib/benchee/benchmark.ex +134 -0
- deps/benchee/lib/benchee/benchmark/benchmark_config.ex +55 -0
- deps/benchee/lib/benchee/benchmark/collect.ex +21 -0
- deps/benchee/lib/benchee/benchmark/collect/memory.ex +161 -0
- deps/benchee/lib/benchee/benchmark/collect/reductions.ex +24 -0
- deps/benchee/lib/benchee/benchmark/collect/time.ex +24 -0
- deps/benchee/lib/benchee/benchmark/function_call_overhead.ex +55 -0
- deps/benchee/lib/benchee/benchmark/hooks.ex +73 -0
- deps/benchee/lib/benchee/benchmark/repeated_measurement.ex +205 -0
- deps/benchee/lib/benchee/benchmark/runner.ex +380 -0
- deps/benchee/lib/benchee/benchmark/scenario_context.ex +35 -0
- deps/benchee/lib/benchee/collection_data.ex +21 -0
- deps/benchee/lib/benchee/configuration.ex +400 -0
- deps/benchee/lib/benchee/conversion.ex +76 -0
- deps/benchee/lib/benchee/conversion/count.ex +241 -0
- deps/benchee/lib/benchee/conversion/deviation_percent.ex +46 -0
- deps/benchee/lib/benchee/conversion/duration.ex +302 -0
- deps/benchee/lib/benchee/conversion/format.ex +155 -0
- deps/benchee/lib/benchee/conversion/memory.ex +278 -0
- deps/benchee/lib/benchee/conversion/scale.ex +227 -0
- deps/benchee/lib/benchee/conversion/unit.ex +26 -0
- deps/benchee/lib/benchee/errors.ex +7 -0
- deps/benchee/lib/benchee/formatter.ex +214 -0
- deps/benchee/lib/benchee/formatters/console.ex +161 -0
- deps/benchee/lib/benchee/formatters/console/helpers.ex +101 -0
- deps/benchee/lib/benchee/formatters/console/memory.ex +321 -0
- deps/benchee/lib/benchee/formatters/console/reductions.ex +318 -0
- deps/benchee/lib/benchee/formatters/console/run_time.ex +296 -0
- deps/benchee/lib/benchee/formatters/tagged_save.ex +90 -0
- deps/benchee/lib/benchee/output/benchmark_printer.ex +140 -0
- deps/benchee/lib/benchee/output/profile_printer.ex +10 -0
- deps/benchee/lib/benchee/output/progress_printer.ex +15 -0
- deps/benchee/lib/benchee/profile.ex +133 -0
- deps/benchee/lib/benchee/relative_statistics.ex +101 -0
- deps/benchee/lib/benchee/scenario.ex +213 -0
- deps/benchee/lib/benchee/scenario_loader.ex +40 -0
- deps/benchee/lib/benchee/statistics.ex +314 -0
- deps/benchee/lib/benchee/suite.ex +158 -0
- deps/benchee/lib/benchee/system.ex +295 -0
- deps/benchee/lib/benchee/utility/deep_convert.ex +51 -0
- deps/benchee/lib/benchee/utility/erlang_version.ex +134 -0
- deps/benchee/lib/benchee/utility/file_creation.ex +155 -0
deps/benchee/.fetch
ADDED
|
File without changes
|
deps/benchee/.formatter.exs
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Used by "mix format"
|
| 2 |
+
[
|
| 3 |
+
plugins: [DoctestFormatter],
|
| 4 |
+
inputs: ["{mix,.formatter}.exs", "{config,lib,test,samples,mix}/**/*.{ex,exs}"]
|
| 5 |
+
]
|
deps/benchee/.hex
ADDED
|
Binary file (271 Bytes). View file
|
|
|
deps/benchee/CHANGELOG.md
ADDED
|
@@ -0,0 +1,459 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Changelog
|
| 2 |
+
|
| 3 |
+
All notable changes to this project will be documented in this file.
|
| 4 |
+
|
| 5 |
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
| 6 |
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
| 7 |
+
|
| 8 |
+
## 1.5.0 (2025-10-21)
|
| 9 |
+
Finally `max_sample_size` is here to alleviate a variety of woes resulting from gathering too many samples!
|
| 10 |
+
Also finally, Benchee lets you exclude outliers!
|
| 11 |
+
|
| 12 |
+
## Features (User Facing)
|
| 13 |
+
* Introduce `max_sample_size` which guides how many samples will be gathered at most for a given scenario.
|
| 14 |
+
This avoids a variety of issues when scenarios gather too many samples (memory consumption, statistics taking long to calculate, formatters hanging/not working).
|
| 15 |
+
Defaults to `1_000_000`, setting it to `nil` gathers unlimited samples again (behavior before this version).
|
| 16 |
+
* Introduce `exclude_outliers` option which when set to `true` will automatically exclude outliers from the samples gathered.
|
| 17 |
+
Especially important for run time, you can remove samples caused by garbage collection or external factors.
|
| 18 |
+
Defaults to `false`.
|
| 19 |
+
Shout out to [@NickNeck](https://github.com/NickNeck) who implemented this long wished for feature over in `Statistex`.
|
| 20 |
+
* Display `input_name` entries in Livebook/`Table.Reader` protocol. Thanks [@madlep](https://github.com/madlep)!
|
| 21 |
+
|
| 22 |
+
### Bugfixes (User Facing)
|
| 23 |
+
* fixed a bug where if times were supplied as `0` instead of `0.0` we'd sometimes gather a single measurement
|
| 24 |
+
* elixir `1.19` compilation warnings have been fixed
|
| 25 |
+
|
| 26 |
+
### Features (Plugins)
|
| 27 |
+
* The `%Benchee.Statistics{}` struct now comes with values to accompany the outlier exclusion feature:
|
| 28 |
+
* outliers - if outlier exclusion was enabled, may include any samples of outliers that were found, empty list otherwise
|
| 29 |
+
* lower_outlier_bound - value below which values are considered an outlier
|
| 30 |
+
* upper_outlier_bound - value above which values are considered an outlier
|
| 31 |
+
|
| 32 |
+
## 1.4.0 (2025-04-14)
|
| 33 |
+
|
| 34 |
+
Some nice features (`pre_check: :all_same` is cool) along with adding support for some new stuff (`tprof`) and fixing some bugs.
|
| 35 |
+
|
| 36 |
+
### Features (User Facing)
|
| 37 |
+
* `pre_check` now accepts the option `:all_same` - which raises if any job returns a value different from another for the same input. Useful when benchmarking multiple variants of the same function. Thanks to [@sabiwara](https://github.com/sabiwara)!
|
| 38 |
+
* Enable the [`tprof`](https://www.erlang.org/doc/apps/tools/tprof.html) profiler that shipped with OTP 27, requires OTP 27+ and elixir 1.17+.
|
| 39 |
+
|
| 40 |
+
## Bugfixes (User Facing)
|
| 41 |
+
* Make gathering of system data more resilient, if the commands we ran didn't exist it may have crashed previously. Most notably, occurring on Windows 11 due to te removal of the tool we use to gather processor information. Now "N/A" is returned.
|
| 42 |
+
* An incompatibility with the upcoming OTP 28 that'd emit a warning on newer elixir versions was removed. Thanks [TBK145](https://github.com/TBK145)!
|
| 43 |
+
|
| 44 |
+
## 1.3.1 (2024-06-05)
|
| 45 |
+
|
| 46 |
+
Small release to remove warnings for the upcoming elixir 1.17 as well as add proper solaris support.
|
| 47 |
+
|
| 48 |
+
### Features (User Facing)
|
| 49 |
+
* Solaris systems will now correctly be identified and have their CPU and memory parsed. Thanks to [@brianewell](https://github.com/brianewell)!
|
| 50 |
+
|
| 51 |
+
### Bugfixes (User Facing)
|
| 52 |
+
* warnings on elixir-main/1.17-rc have been removed. Thanks to [@mhanberg](https://github.com/mhanberg) & [@alco](https://github.com/alco)!
|
| 53 |
+
|
| 54 |
+
## 1.3.0 (2023-12-22)
|
| 55 |
+
A big swath of bug fixes and improvements. The highlights certainly are fixes and conveniences around saved benchmarks and loading them again via the new `Benchee.report/1`. The other big one is saving a lot of memory (and time!) when processing big inputs. Sadly the latter comes with some breaking changes for plugins, but they are well justified and shouldn't actually affect any plugin in practice.
|
| 56 |
+
|
| 57 |
+
There's also a **known issue** for elixir 1.14.0 to 1.16.0-rc.0 - [read up about it](https://github.com/bencheeorg/benchee#known-issues).
|
| 58 |
+
|
| 59 |
+
### Features (User Facing)
|
| 60 |
+
* System information now includes whether or not the JIT is enabled ([erlang docs](https://www.erlang.org/doc/apps/erts/beamasm)).
|
| 61 |
+
* Benchee now let's you know when it's calculating statistics or running the formatters. Helps when you wonder what's taking so long or blows up memory.
|
| 62 |
+
* `Benchee.report/1` introduced if you just want to load saved benchmarks and report on them (---> run formatters).
|
| 63 |
+
* Configuration times will now be displayed in a more human friendly format (1 min 12 s vs. 1.2 min). Thanks to [@drobnyd](https://github.com/drobnyd).
|
| 64 |
+
|
| 65 |
+
### Bugfixes (User Facing)
|
| 66 |
+
* Memory usage should be massively reduced when dealing with larger sets of data in inputs or benchmarking functions. They were needlessly sent to processes calculating statistics or formatters which could lead to memory blowing up.
|
| 67 |
+
* Similarly, inputs and benchmarking functions will no longer be saved when using the `:save` option, this makes it immensely faster and depending on the size of the data a lot slower (I have an example with a factor 200x for the size). The side effect of this is that you also can't use `:load` and run the benchmarks saved again from just the file, this was never an intended use case though (as loading happens after benchmarking by default). You also still should have the benchmarking script so it's also not needed.
|
| 68 |
+
* Fix a bug where relative statistics would always rely on the inputs provided in the config, which can break when you load saved benchmarks but don't specify the `inputs` again.
|
| 69 |
+
|
| 70 |
+
### Breaking Changes (Plugins)
|
| 71 |
+
Woopsie, didn't wanna do any of these in 1.x, sorry but there's good reason :( However, counting them as bugs.
|
| 72 |
+
|
| 73 |
+
* Formatters have lost access to benchmarking functions and the inputs, this is to enable huge memory and run time savings when using a lot of data. I also believe they should not be needed for formatters, please get in touch if this is a problem so we can work it out. In detail this means:
|
| 74 |
+
* Each `Benchee.Scenario` struct in a formatter will have `:function` and `:input` set to `nil`
|
| 75 |
+
* The `inputs` list in `Configuration` retains the input names, but the values will be set to `:scrubbed_see_1_3_0_changelog`. It may be completely scrubbed in the future, use the newly introduced `input_names` instead if you need easy access to all the input names at once.
|
| 76 |
+
* Technically speaking formatters haven't generally lost access, only if they are processed in parallel - so not if it's the only formatter or if it's used via a function (`formatters: [fn suite -> MyFormatter.output(suite) end]`. Still, should not be used or relied upon.
|
| 77 |
+
|
| 78 |
+
### Features (Plugins)
|
| 79 |
+
* `jit_enabled?` is exposed as part of the `suite.system` struct
|
| 80 |
+
* Yes, `Benchee.System` is now a struct so feel easier about relying on the fields
|
| 81 |
+
* `Configuration` now has an `input_names` key that holds the name of all inputs, for the reasoning, see above.
|
| 82 |
+
* The more human friendly formats are accessible via `Format.format_human/2` or `Duration.format_human/1` (& friends)
|
| 83 |
+
|
| 84 |
+
## 1.2.0 (2023-11-09)
|
| 85 |
+
|
| 86 |
+
Long time, huh? 😉 I'm not gonna repeat all that, but I'm happy benchee is in a place where it just works and doesn't need too much support. Biggest feature here is the implementation of the `Table.Reader` protocol for a better Livebook experience.
|
| 87 |
+
|
| 88 |
+
### Features (User Facing)
|
| 89 |
+
* Trying to benchmark evaluated functions will now result in a warning. Thanks [@BrooklinJazz](https://github.com/BrooklinJazz), [@czrpb](https://github.com/czrpb), [@aar2dee2](https://github.com/aar2dee2),[@ReecesPeanutButterCodes](https://github.com/ReecesPeanutButterCodes).
|
| 90 |
+
* Add support for the [`Table.Reader`](https://hexdocs.pm/table/Table.html) protocol so that benchee works out of the box in Livebook. See [#369](https://github.com/bencheeorg/benchee/pull/369), big shoutout to [@akoutmos](https://github.com/akoutmos) and sorry for keeping it unreleased for so long.
|
| 91 |
+
|
| 92 |
+
### Bugfixes (User Facing)
|
| 93 |
+
* Removed Elixir 1.16 compiler warnings around +0.0 and -0.0. Thanks [@tomciop](https://github.com/tomciopp).
|
| 94 |
+
* Building an escript and running it is fixed. See [#384](https://github.com/bencheeorg/benchee/issues/384) and thanks [@Munksgaard](https://github.com/Munksgaard)!
|
| 95 |
+
|
| 96 |
+
## 1.1.0 (2022-03-08)
|
| 97 |
+
|
| 98 |
+
Long time, huh? I'm sorry, combination of priorities, difficult to fix bugs, stress and arm problems kept me away too long.
|
| 99 |
+
This release brings major features long developed and now finally released (reduction measurements + profiler after run), along with a critical bugfix around measurement accuracy for very fast functions (nanoseconds).
|
| 100 |
+
|
| 101 |
+
### Features (User Facing)
|
| 102 |
+
* Reduction counting/measurements was implemented. Basically, it's a rather stable unit of execution implemented by the BEAM that measures in more abstract manner how much work was done. It's helpful, as it shouldn't be affected by load on the system. Check out [the docs](https://github.com/bencheeorg/benchee#measuring-reductions).
|
| 103 |
+
* You can now dive straight into profiling from your benchmarks by using the profiling feature. See [the docs](https://github.com/bencheeorg/benchee#profiling-after-a-run) - thanks [@pablocostass](https://github.com/pablocostass)
|
| 104 |
+
|
| 105 |
+
### Default Behavior Changes (User Facing)
|
| 106 |
+
* measuring function call overhead is now turned off by default, as it only helps with extreme nano benchmarks and has some potential for causing wrong results, so it should only be opt in. We now also print out the measured overhead for vsibility.
|
| 107 |
+
|
| 108 |
+
### Bugfixes (User Facing)
|
| 109 |
+
* Benchee now correctly looks for the time resolution as reported by `:erlang.system_info(:os_monotonic_time_source)` to accommodate when determining if a measurement is "precise" enough. Benche also works around an erlang bug we discovered present in erlang <= 22.2. [Issue for reference](https://github.com/bencheeorg/benchee/issues/313).
|
| 110 |
+
* The annoying stacktrace warning has been removed - thanks [@mad42](https://github.com/mad42)
|
| 111 |
+
|
| 112 |
+
### Noteworthy
|
| 113 |
+
* a new dependency `statistex` will show up - it's a part of continued efforts to extract reusable libraries from Benchee.
|
| 114 |
+
|
| 115 |
+
## 1.0.1 (2019-04-09)
|
| 116 |
+
|
| 117 |
+
### Bugfixes (User Facing)
|
| 118 |
+
* When memory measurements were actually different extended statistics was displayed although the option was not provided. Now correctly only displayed if the option is provided and values actually had variance.
|
| 119 |
+
|
| 120 |
+
## 1.0.0 (2019-03-28)
|
| 121 |
+
|
| 122 |
+
It's 0.99.0 without the deprecation warnings. Specifically:
|
| 123 |
+
|
| 124 |
+
* Old way of passing formatters (`:formatter_options`) vs. new `:formatters` with modules, tuples or functions with one arg
|
| 125 |
+
* The configuration needs to be passed as the second argument to `Benchee.run/2`
|
| 126 |
+
* `Benchee.collect/1` replaces `Benchee.measure/1`
|
| 127 |
+
* `unit_scaling` is a top level configuration option, not for the console formatter
|
| 128 |
+
* the warning for memory measurements not working on OTP <= 18 will also be dropped (we already officially dropped OTP 18 support in 0.14.0)
|
| 129 |
+
|
| 130 |
+
We're aiming to follow Semantic Versioning as we go forward. That means formatters should be safe to use `~> 1.0` (or even `>= 0.99.0 and < 2.0.0`).
|
| 131 |
+
|
| 132 |
+
## 0.99.0 (2019-03-28)
|
| 133 |
+
|
| 134 |
+
The "we're almost 1.0!" release - all the last small features, a bag of polish and deprecation warnings. If you run this release successfully without deprecation warnings you should be safe to upgrade to 1.0.0, if not - it's a bug :)
|
| 135 |
+
|
| 136 |
+
### Breaking Changes (User Facing)
|
| 137 |
+
* changed official Elixir compatibility to `~> 1.6`, 1.4+ should still work but aren't guaranteed or tested against.
|
| 138 |
+
|
| 139 |
+
### Features (User Facing)
|
| 140 |
+
* the console comparison now also displays the absolute difference in the average (like +12 ms) so that you have an idea to how much time that translates to in your applications not just that it's 100x faster
|
| 141 |
+
* Overhaul of README, documentation, update samples etc. - a whole lot of things have also been marked `@doc false` as they're considered internal
|
| 142 |
+
|
| 143 |
+
### Bugfixes (User Facing)
|
| 144 |
+
* Remove double empty line after configuration display
|
| 145 |
+
* Fix some wrong type specs
|
| 146 |
+
|
| 147 |
+
### Breaking Changes (Plugins)
|
| 148 |
+
* `Scenario` made it to the big leagues, it's no longer `Benchee.Benchmark.Scenario` but `Benchee.Scenario` - as it is arguably one of our most important data structures.
|
| 149 |
+
* The `Scenario` struct had some keys changed (last time before 2.0 I promise!) - instead of `:run_times`/`:run_time_statistics` you now have one `run_time_data` key that contains `Benchee.CollectionData` which has the keys `:samples` and `:statistics`. Same for `memory_usage`. This was done to be able to handle different kinds of measurements more uniformly as we will add more of them.
|
| 150 |
+
|
| 151 |
+
### Features (Plugins)
|
| 152 |
+
* `Benchee.Statistics` comes with 3 new values: `:relative_more`, `:relative_less`, `:absolute_difference` so that you don't have to calculate these relative values yourself :)
|
| 153 |
+
|
| 154 |
+
## 0.14.0 (2019-02-10)
|
| 155 |
+
|
| 156 |
+
Highlights of this release are a new way to specify formatter options closer to the formatters themselves as well as maximum precision measurements.
|
| 157 |
+
|
| 158 |
+
### Breaking Changes (User Facing)
|
| 159 |
+
* dropped support for Erlang 18.x
|
| 160 |
+
* Formatters no longer have an `output/1` method, instead use `Formatter.output/3` please
|
| 161 |
+
* Usage of `formatter_options` is deprecated, instead please use the new tuple way
|
| 162 |
+
|
| 163 |
+
### Features (User Facing)
|
| 164 |
+
* Benchee now uses the maximum precision available to measure which on Linux and OSX is nanoseconds instead of microseconds. Somewhat surprisingly `:timer.tc/1` always cut down to microseconds although better precision is available.
|
| 165 |
+
* The preferred way to specify formatters and their options is to specify them as a tuple `{module, options}` instead of using `formatter_options`.
|
| 166 |
+
* New `Formatter.output/1` function that takes a suite and uses all configured formatters to output their results
|
| 167 |
+
* Add the concept of a benchmarking title that formatters can pick up
|
| 168 |
+
* the displayed percentiles can now be adjusted
|
| 169 |
+
* inputs option can now be an ordered list of tuples, this way you can determine their order
|
| 170 |
+
* support FreeBSD properly (system metrics) - thanks @[kimshrier](/kimshrier)
|
| 171 |
+
|
| 172 |
+
### Bugfixes (User Facing)
|
| 173 |
+
* Remove extra double quotes in operating system report line - thanks @[kimshrier](/kimshrier)
|
| 174 |
+
|
| 175 |
+
### Breaking Changes (Plugins)
|
| 176 |
+
* all reported times are now in nanoseconds instead of microseconds
|
| 177 |
+
* formatter methods `format` and `write` now take 2 arguments each where the additional arguments is the options specified for this formatter so that you have direct access to it without peeling it from the suite
|
| 178 |
+
* You can no longer `use Benchee.Formatter` - just adopt the behaviour (no more auto generated `output/1` method, but `Formatter.output/3` takes that responsibility now)
|
| 179 |
+
|
| 180 |
+
### Features (Plugins)
|
| 181 |
+
* An optional title is now available in the suite for you to display
|
| 182 |
+
* Scenarios are now sorted already sorted (first by run time, then memory usage) - no need to sort them yourself!
|
| 183 |
+
* Add `Scenario.data_processed?/2` to check if either run time or memory data has had statistics generated
|
| 184 |
+
|
| 185 |
+
## 0.13.2 (2018-08-02)
|
| 186 |
+
|
| 187 |
+
Mostly fixing memory measurement bugs and delivering them to you asap ;)
|
| 188 |
+
|
| 189 |
+
### Bugfixes (User Facing)
|
| 190 |
+
* Remove race condition that caused us to sometimes miss garbage collection events and hence report even negative or N/A results
|
| 191 |
+
* restructure measuring code to produce less overhead (micro memory benchmarks should be much better now)
|
| 192 |
+
* make console formatter more resilient to faulty memory measurements aka don't crash
|
| 193 |
+
|
| 194 |
+
## 0.13.1 (2018-05-02)
|
| 195 |
+
|
| 196 |
+
Mostly fixing memory measurement bugs and related issues :) Enjoy a better memory measurement experience from now on!
|
| 197 |
+
|
| 198 |
+
### Bugfixes (User Facing)
|
| 199 |
+
* Memory measurements now correctly take the old generation on the heap into account. In reality that means sometimes bigger results and no missing measurements. See [#216](https://github.com/bencheeorg/benchee/pull/216) for details. Thanks to @michalmuskala for providing an interesting sample.
|
| 200 |
+
* Formatters are now more robust (aka not crashing) when dealing with partially missing memory measurements. Although it shouldn't happen anymore with the item before fixed, Benchee shouldn't crash on you so we want to be on the safe side.
|
| 201 |
+
* It's now possible to run just memory measurements (i.e. `time: 0, warmup: 0, memory_time: 1`)
|
| 202 |
+
* even when you already have scenarios tagged with `-2` etc. it still correctly produces `-3`, `-4` etc. when saving again with the same "base tagged name"
|
| 203 |
+
|
| 204 |
+
## 0.13.0 (2018-04-14)
|
| 205 |
+
|
| 206 |
+
Memory Measurements are finally here! Please report problems if you experience them.
|
| 207 |
+
|
| 208 |
+
### Features (User Facing)
|
| 209 |
+
* Memory measurements obviously ;) Memory measurement are currently limited to process your function will be run in - memory consumption of other processes will **not** be measured. More information can be found in the [README](https://github.com/bencheeorg/benchee#measuring-memory-consumption). Only usable on OTP 19+. Special thanks go to @devonestes and @michalmuskala
|
| 210 |
+
* new `pre_check` configuration option which allows users to add a dry run of all
|
| 211 |
+
benchmarks with each input before running the actual suite. This should save
|
| 212 |
+
time while actually writing the code for your benchmarks.
|
| 213 |
+
|
| 214 |
+
### Bugfixes (User Facing)
|
| 215 |
+
* Standard Deviation is now calculated correctly for being a sample of the population (divided by `n - 1` and not just `n`)
|
| 216 |
+
|
| 217 |
+
## 0.12.1 (2018-02-26)
|
| 218 |
+
|
| 219 |
+
### Bugfixes (User Facing)
|
| 220 |
+
* Formatters that use `FileCreation.each` will no longer silently fail on file
|
| 221 |
+
creation and now also sanitizes `/` and other file name characters to be `_`.
|
| 222 |
+
Thanks @gfvcastro
|
| 223 |
+
|
| 224 |
+
## 0.12.0 (2018-01-20)
|
| 225 |
+
|
| 226 |
+
Adds the ability to save benchmarking results and load them again to compare
|
| 227 |
+
against. Also fixes a bug for running benchmarks in parallel.
|
| 228 |
+
|
| 229 |
+
### Breaking Changes (User Facing)
|
| 230 |
+
* Dropped Support for elixir 1.3, new support is elixir 1.4+
|
| 231 |
+
|
| 232 |
+
### Features (User Facing)
|
| 233 |
+
* new `save` option specifying a path and a tag to save the results and tag them
|
| 234 |
+
(for instance with `"main"`) and a `load` option to load those results again
|
| 235 |
+
and compare them against your current results.
|
| 236 |
+
* runs warning free with elixir 1.6
|
| 237 |
+
|
| 238 |
+
### Bugfixes (User Facing)
|
| 239 |
+
|
| 240 |
+
* If you were running benchmarks in parallel, you would see results for each
|
| 241 |
+
parallel process you were running. So, if you were running **two** jobs, and
|
| 242 |
+
setting your configuration to `parallel: 2`, you would see **four** results in the
|
| 243 |
+
formatter. This is now correctly showing only the **two** jobs.
|
| 244 |
+
|
| 245 |
+
### Features (Plugins)
|
| 246 |
+
* `Scenario` has a new `name` field to be adopted for displaying the scenario names,
|
| 247 |
+
as it includes the tag name and potential future additions.
|
| 248 |
+
|
| 249 |
+
## 0.11.0 (2017-11-30)
|
| 250 |
+
|
| 251 |
+
A tiny little release with a bugfix and MOARE statistics for the console formatter.
|
| 252 |
+
|
| 253 |
+
### Bugfixes (User Facing)
|
| 254 |
+
|
| 255 |
+
* estimated run times should be correct again, they were too high when inputs were used
|
| 256 |
+
|
| 257 |
+
### Features (User Facing)
|
| 258 |
+
|
| 259 |
+
* the console formatter accepts a new `extended_statistics` options that shows you additional statistics such as `minimum`, `maximum`, `sample_size` and the `mode`. Thanks `@lwalter`
|
| 260 |
+
|
| 261 |
+
## 0.10.0 (2017-10-24)
|
| 262 |
+
|
| 263 |
+
This release focuses on 2 main things: the internal restructuring to use _scenarios_ and the new _hooks_ system. Other than that we also have some nice convenience features and formatters can be generated in parallel now.
|
| 264 |
+
|
| 265 |
+
### Features (User Facing)
|
| 266 |
+
* Hooks system - basically you can now do something before/after a benchmarking scenario or the benchmarking function, too much to explain it in a Changelog, check the [README](https://github.com/bencheeorg/benchee#hooks-setup-teardown-etc)
|
| 267 |
+
* Don't show more precision than we have - i.e. 234.00 microseconds (measurements are in microseconds and .00 doesn't gain you anything)
|
| 268 |
+
* Limit precision of available memory displayed, you don't need to know 7.45678932 GB. Thanks to `@elpikel`.
|
| 269 |
+
* Display the 99th percentile runtime. Thanks to `@wasnotrice`.
|
| 270 |
+
* `:unit_scaling` is now a top level configuration option that can now also be used and picked up by formatters, like the HTML formatter
|
| 271 |
+
* formatters can now be specified as a module (which should implement the `Benchee.Formatter` behaviour) - this makes specifying them nice and now at least their `format/1` functions can be executed in parallel
|
| 272 |
+
|
| 273 |
+
### Bugfixes (User Facing)
|
| 274 |
+
* Determining CPUs was too strict/too assuming of a specific pattern breaking in certain environments (like Semaphore CI). That is more relaxed now thanks to `@predrag-rakic`!
|
| 275 |
+
* Memory is now correctly converted using the binary (1024) interpretation, instead of the decimal one (1000)
|
| 276 |
+
|
| 277 |
+
|
| 278 |
+
### Features (Plugins)
|
| 279 |
+
* the statistics now also provide the mode of the samples as well as the 99th percentile
|
| 280 |
+
* There is a new `Benchee.Formatter` behaviour to adopt and enforce a uniform format for formatters, best to do `use Benchee.Formatter`
|
| 281 |
+
|
| 282 |
+
### Breaking Changes (Plugins)
|
| 283 |
+
* `:run_times`, `:statistics` and `:jobs` have been removed and folded together into `:scenarios` - a scenario holds the benchmarking function, potentially the input, the raw run times measures and the computed statistics. With this data structure, all the relevant data for one scenario is one place although it takes a lot to change, this seems to be the best way going forward. Huge thanks to `@devonestes`!
|
| 284 |
+
|
| 285 |
+
## 0.9.0 (2017-06-08)
|
| 286 |
+
|
| 287 |
+
This release focuses on adding more system specific information like CPU etc. and for better Erlang compatibility if you wanna use Benchee from Erlang. There is an [example project](https://github.com/bencheeorg/benchee_erlang_try) but calling Elixir from Erlang hasn't been as easy as I hoped :)
|
| 288 |
+
|
| 289 |
+
### Features (User Facing)
|
| 290 |
+
* Gather more system data like number of cores, Operating System, memory, cpu speed - thanks @devonestes and @OvermindDL1
|
| 291 |
+
* the names for jobs in the map of `Benchee.run/2` or in `Benchee.benchmark/3` may now be given as strings or atoms - atoms will be converted to strings internally though for consistency and avoiding name duplicates
|
| 292 |
+
* the names of inputs in the Benchee configuration may now be given as strings or atoms - atoms will be converted to strings internally though for consistency and avoiding name duplicates
|
| 293 |
+
* Benchee is now also available _"Erlang Style"_ to be called on an atom like `:benchee.run(_, _)` for better Erlang compatibility
|
| 294 |
+
|
| 295 |
+
## 0.8.0 (2017-05-07)
|
| 296 |
+
|
| 297 |
+
Another smaller release that focuses on adding type specs and structs in appropriate places along with fixing a couple of small bugs.
|
| 298 |
+
|
| 299 |
+
### Features (User Facing)
|
| 300 |
+
* Providing an unrecognized configuration option (say `runNtime` instead of `runtime`) will now raise an exception
|
| 301 |
+
* Durations in the configuration will now be scaled appropriately (minutes, microseconds etc)
|
| 302 |
+
* Major functions are type specced for your viewing pleasure in the docs and your dialyzer pleasure at type check time.
|
| 303 |
+
|
| 304 |
+
### Bugfixes (User Facing)
|
| 305 |
+
* In 0.7.0 statistics generation might time out if Millions of run times were captured so that it takes longer than 5 seconds, this is fixed by waiting infinitely - thanks @devonestes for the [report](https://github.com/bencheeorg/benchee/issues/71).
|
| 306 |
+
* Unintended line break in the fast function warning removed
|
| 307 |
+
* All necessary dependencies added to `:applications` (deep_merge was missing)
|
| 308 |
+
|
| 309 |
+
### Breaking Changes (User Facing)
|
| 310 |
+
* Dropped support for elixir 1.2, new support is elixir 1.3+
|
| 311 |
+
* `Benchee.Config` was renamed to `Benchee.Configuration` (important when you use the more verbose API or used it in a Plugin)
|
| 312 |
+
|
| 313 |
+
### Features (Plugins)
|
| 314 |
+
* Major public interfacing functions are now typespecced!
|
| 315 |
+
* A couple of major data structures are now proper structs e.g. `Benchee.Suite`, `Benchee.Configuration`, `Benchee.Statistics`
|
| 316 |
+
|
| 317 |
+
### Breaking Changes (Plugins)
|
| 318 |
+
* The `config` key is now `configuration` to go along with the Configuration name change
|
| 319 |
+
* As `Benchee.Configuration` is a proper struct now, arbitrary keys don't end up in it anymore. Custom data for plugins should be passed in through `formatter_options` or `assigns`. Existing plugin keys (`csv`, `json`, `html` and `console`) are automatically put into the `formatter_options` key space for now.
|
| 320 |
+
|
| 321 |
+
## 0.7.0 (April 23, 2017)
|
| 322 |
+
|
| 323 |
+
Smaller convenience features in here - the biggest part of work went into breaking reports in [benchee_html](https://github.com/bencheeorg/benchee_html) apart :)
|
| 324 |
+
|
| 325 |
+
### Features (User Facing)
|
| 326 |
+
* the print out of the Erlang version now is less verbose (just major/minor)
|
| 327 |
+
* the fast_warning will now also tell you how to disable it
|
| 328 |
+
* When `print: [benchmarking: false]` is set, information about which input is being benchmarked at the moment also won't be printed
|
| 329 |
+
* generation of statistics parallelized (thanks hh.ex - @nesQuick and @dszam)
|
| 330 |
+
|
| 331 |
+
### Breaking Changes (User Facing)
|
| 332 |
+
* If you use the more verbose interface (`Benchee.init` and friends, e.g. not `Benchee.run`) then you have to insert a `Benchee.system` call before `Benchee.measure` (preferably right after `Benchee.init`)
|
| 333 |
+
|
| 334 |
+
### Features (Plugins)
|
| 335 |
+
* `Benchee.Utility.FileCreation.interleave/2` now also accepts a list of inputs which are then all interleaved in the file name appropriately. See the doctests for more details.
|
| 336 |
+
|
| 337 |
+
### Breaking Changes (Plugins)
|
| 338 |
+
* `Benchee.measure/1` now also needs to have the system information generated by `Benchee.system/1` present if configuration information should be printed.
|
| 339 |
+
|
| 340 |
+
## 0.6.0 (November 30, 2016)
|
| 341 |
+
|
| 342 |
+
One of the biggest releases yet. Great stuff in here - more elixir like API for `Benchee.run/2` with the jobs as the primary argument and the optional options as the second argument and now also as the more idiomatic keyword list!
|
| 343 |
+
|
| 344 |
+
The biggest feature apart from that is the possibility to use multiple inputs - which you all should do now as quite many functions behave differently with bigger, smaller or differently shaped inputs. Apart from that a bulk of work has gone into making and supporting [benchee_html](https://github.com/bencheeorg/benchee_html)!
|
| 345 |
+
|
| 346 |
+
### Features (User Facing)
|
| 347 |
+
|
| 348 |
+
* New `:inputs` configuration key that allows you to specify a map from input name to input value so that each defined benchmarking job is then executed with this input. For this to work the benchmarking function is called with the appropriate `input` as an argument. See [`samples/multiple_inputs.exs`](https://github.com/bencheeorg/benchee/blob/main/samples/multiple_inputs.exs) for an example. [#21]( https://github.com/bencheeorg/benchee/issues/21)
|
| 349 |
+
* The highlevel `Benchee.run/2` is now more idiomatic elixir and takes the map of jobs as the first argument and a keyword list of options as the second (and last) argument. The old way of passing config as a map as the first argument and the jobs as the second argument still works, **but might be deprecated later on** [#47](https://github.com/bencheeorg/benchee/issues/47)
|
| 350 |
+
* Along with that `Benchee.init/1` now also accepts keyword lists of course
|
| 351 |
+
|
| 352 |
+
### Breaking Changes (User Facing)
|
| 353 |
+
|
| 354 |
+
* The old way of providing the jobs as a list of tuples now removed, please switch to using a map from string to functions
|
| 355 |
+
|
| 356 |
+
### Features (Plugins)
|
| 357 |
+
|
| 358 |
+
* `Benchee.Utility.FileCreation` module to help with creating files from a map of multiple inputs (or other descriptors) mapping to input and an `interleave` function that spits out the correct file names especially if the `:__no_input` marker is used
|
| 359 |
+
* `Benchee.System` is available to retrieve Elixir and Erlang versions but it's
|
| 360 |
+
also already added to the suite during `Benchee.run/2`
|
| 361 |
+
|
| 362 |
+
### Breaking Changes (Plugins)
|
| 363 |
+
|
| 364 |
+
* The structure of the output from `Benchee.Benchmark.measure/1` to `Benchee.Statistics.statistics/1` has changed to accommodate the new inputs feature there is now an additional level where in a map the input name then points to the appropriate results of the jobs. When there were no inputs the key is the value returned by `Benchee.Benchmark.no_input/0`.
|
| 365 |
+
|
| 366 |
+
### Bugfixes
|
| 367 |
+
|
| 368 |
+
* prewarming (discarding the first result due to some timer issues) during run time was removed, as it should already happen during the warmup period and would discard actual useful results especially for longer running macro benchmarks.
|
| 369 |
+
* when the execution time of the benchmarking job exceeds the given `:time` it will now execute exactly once (used to be 2) [#49](https://github.com/bencheeorg/benchee/issues/49)
|
| 370 |
+
* `run_times` are now in the order as recorded (used to be reverse) - important when wants to graph them/look at them to see if there are any anomalies during benchmarking
|
| 371 |
+
* Remove elixir 1.4.0-rc.0 warnings
|
| 372 |
+
|
| 373 |
+
## 0.5.0 (October 13, 2016)
|
| 374 |
+
|
| 375 |
+
This release focuses on scaling units to more appropriate sizes. Instead of always working with base one for counts and microseconds those values are scaled accordingly to thousands, milliseconds for better readability. This work was mostly done by new contributor @wasnotrice.
|
| 376 |
+
|
| 377 |
+
### Features (User Facing)
|
| 378 |
+
|
| 379 |
+
* Console output now scales units to be more friendly. Examples:
|
| 380 |
+
* instead of "44556677" ips, you would see "44.56 M"
|
| 381 |
+
* instead of "44556.77 μs" run time, you would see "44.56 ms"
|
| 382 |
+
* Console output for standard deviation omits the parentheses
|
| 383 |
+
* Scaling of console output can be configured with the 4 different strategies `:best`, `:largest`, `:smallest` and `:none`. Refer to the documentation for their different properties.
|
| 384 |
+
* Shortened the fast function warning and instead [linked to the wiki](https://github.com/bencheeorg/benchee/wiki/Benchee-Warnings#fast-execution-warning)
|
| 385 |
+
|
| 386 |
+
### Features (Plugins)
|
| 387 |
+
|
| 388 |
+
* The statistics module now computes the `minimum`, `maximum` and `sample_size` (not yet shown in the console formatter)
|
| 389 |
+
* you can rely on `Benchee.Conversion.Duration`, `Benchee.Conversion.Count` and `Benchee.Conversion.DeviationPercent` for help with formatting and scaling units
|
| 390 |
+
|
| 391 |
+
### Breaking Changes (Plugins)
|
| 392 |
+
|
| 393 |
+
* The `Benchee.Time`module is gone, if you relied on it for one reason or another it's succeeded by the more powerful `Benchee.Conversion.Duration`
|
| 394 |
+
|
| 395 |
+
## 0.4.0 (September 11, 2016)
|
| 396 |
+
|
| 397 |
+
Focuses on making what Benchee print out configurable to make it fit to your preferences :)
|
| 398 |
+
|
| 399 |
+
### Features (User Facing)
|
| 400 |
+
* The configuration now has a `:print` key where it is possible to configure in a map what Benchee prints out during benchmarking. All options are enabled by default (true). Options are:
|
| 401 |
+
* `:benchmarking` - print when Benchee starts benchmarking a new job (Benchmarking name ..)
|
| 402 |
+
* `:configuration` - a summary of configured benchmarking options including estimated total run time is printed before benchmarking starts
|
| 403 |
+
* `:fast_warning` - warnings are displayed if functions are executed too fast leading to inaccurate measures
|
| 404 |
+
* There is also a new configuration option for the built-in console formatter. Which is also enabled by default:
|
| 405 |
+
* `:comparison` - if the comparison of the different benchmarking jobs (x times slower than) is shown
|
| 406 |
+
* The pre-benchmarking output of the configuration now also prints the currently used Erlang and Elixir versions (similar to `elixir -v`)
|
| 407 |
+
* Add a space between the benchmarked time and the unit (microseconds)
|
| 408 |
+
|
| 409 |
+
## 0.3.0 (July 11, 2016)
|
| 410 |
+
|
| 411 |
+
This release switches internal data structures from lists of tuples to maps, allows the configuration of formatters, aggregates all values and hands them down so formatters can access the whole configuration, prints general configuration information and much more great stuff :)
|
| 412 |
+
|
| 413 |
+
### Breaking Changes (User Facing)
|
| 414 |
+
* The recommended data structure handed to `Benchee.run` was changed from a list of 2-element tuples to a map (`"Name" => benchmark_function`). However, **the old list of tuples still works but may be removed in future releases** (so it's not "breaking" _strictly_ speaking).
|
| 415 |
+
* You **can not have benchmark jobs with the same names anymore**, the last one wins here. This was the reason why previously the data structure was a list of tuples. However, having benchmarks with the same name is nonsensical as you can't discern their results in the output any way.
|
| 416 |
+
|
| 417 |
+
### Breaking Changes (Plugins)
|
| 418 |
+
* main data structure to hold benchmarks and results was changed from a list of 2-element tuples to a map (`"Name" => values`). That is for the jobs, the run times as well as the statistics. However, if you used something like `Enum.each(data, fn({name, value}) -> .. end)` you are still fine though, cause Elixir is awesome :)
|
| 419 |
+
|
| 420 |
+
### Features (User Facing)
|
| 421 |
+
* now takes a `parallel: number` configuration option and will then execute each job in parallel in as many parallel processes as specified in `number`. This way you can gather more samples in the same time and also simulate a system more under load. This is tricky, however. One of the use cases is also stress testing a system. Thanks @ldr
|
| 422 |
+
* the name column width is now determined based on the longest name. Thanks @alvinlindstam
|
| 423 |
+
* Print general configuration information at the start of the benchmark, including warmup, time, parallel and an estimated total run time
|
| 424 |
+
* New method `Benchee.Formatters.Console.output/1` that immediately prints to the console
|
| 425 |
+
* now takes a `formatters: [&My.Format.function/1, &Benchee.Formatters.console.output/1]` configuration option with which multiple formatters for the same benchmarking run can be configured when using `Benchee.run/2`. E.g. you can print results to the console and create a csv from that same run. Defaults to the builtin Console formatter.
|
| 426 |
+
|
| 427 |
+
### Features (Plugins)
|
| 428 |
+
* All previous configuration options are preserved after `Benchee.Statistics.statistics/1`, meaning there is access to raw run times as well as custom options etc. E.g. you could grab custom options like `%{csv: %{file: "my_file_name.csv"}}` to use.
|
| 429 |
+
|
| 430 |
+
### Bugfixes
|
| 431 |
+
* name columns are no longer truncated after 30 characters. Thanks @alvinlindstam
|
| 432 |
+
|
| 433 |
+
## 0.2.0 (June 11, 2016)
|
| 434 |
+
|
| 435 |
+
This release introduces warmup for benchmarks, nicer console output and the new `Benchee.measure` that runs the benchmarks previously defined instead of running them instantly.
|
| 436 |
+
|
| 437 |
+
### Breaking Changes (User Facing)
|
| 438 |
+
* `Benchee.benchmark/3` now doesn't run the benchmark anymore but simply adds it to `:jobs` in the config. The whole benchmark suite is then run via `Benchee.measure/1`. This only affects you if you used the more verbose way of defining benchmarks, `Benchee.run/2` still work as before.
|
| 439 |
+
|
| 440 |
+
### Breaking Changes (Plugins)
|
| 441 |
+
* the defined benchmarking are now preserved after running the benchmark under the `:jobs` key of the suite. Run times are added to the `:run_times` key of the suite (important for alternative statistics implementations)
|
| 442 |
+
|
| 443 |
+
### Features (User Facing)
|
| 444 |
+
|
| 445 |
+
* configuring a warmup time to run functions before measurements are taken can be configured via the `warmup` key in the config defaulting to 2 (seconds)
|
| 446 |
+
|
| 447 |
+
* statistics in console output are aligned right now for better comparisons
|
| 448 |
+
* last blank line of console output removed
|
| 449 |
+
|
| 450 |
+
### Features (Plugins)
|
| 451 |
+
* additionally supply the total standard deviation of iterations per second as `std_dev_ips` after `Benchee.Statistics.statistics`
|
| 452 |
+
|
| 453 |
+
### Bugfixes
|
| 454 |
+
|
| 455 |
+
* if no time/warmup is specified the function won't be called at all
|
| 456 |
+
|
| 457 |
+
## 0.1.0 (June 5, 2016)
|
| 458 |
+
|
| 459 |
+
Initial release
|
deps/benchee/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
MIT License
|
| 2 |
+
|
| 3 |
+
Copyright (c) 2016 Tobias Pfeiffer
|
| 4 |
+
|
| 5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 6 |
+
of this software and associated documentation files (the "Software"), to deal
|
| 7 |
+
in the Software without restriction, including without limitation the rights
|
| 8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 9 |
+
copies of the Software, and to permit persons to whom the Software is
|
| 10 |
+
furnished to do so, subject to the following conditions:
|
| 11 |
+
|
| 12 |
+
The above copyright notice and this permission notice shall be included in all
|
| 13 |
+
copies or substantial portions of the Software.
|
| 14 |
+
|
| 15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 21 |
+
SOFTWARE.
|
deps/benchee/README.md
ADDED
|
@@ -0,0 +1,1144 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Benchee [](https://hex.pm/packages/benchee) [](https://hexdocs.pm/benchee/) [](https://github.com/bencheeorg/benchee/actions?query=branch%3Amain) [](https://coveralls.io/github/bencheeorg/benchee?branch=main) [](https://hex.pm/packages/benchee) [](https://github.com/bencheeorg/benchee/blob/main/LICENSE)
|
| 2 |
+
|
| 3 |
+
Library for easy and nice (micro) benchmarking in Elixir. Benchee allows you to compare the performance of different pieces of code at a glance. It is also versatile and extensible, relying only on functions. There are also a bunch of [plugins](#plugins) to draw pretty graphs and more!
|
| 4 |
+
|
| 5 |
+
Benchee runs each of your functions for a given amount of time after an initial warmup, it then measures their run time and optionally memory consumption. It then shows different statistical values like average, standard deviation etc. See [features](#features).
|
| 6 |
+
|
| 7 |
+
Benchee has a nice and concise main interface, its behavior can be altered through lots of [configuration options](#configuration):
|
| 8 |
+
|
| 9 |
+
```elixir
|
| 10 |
+
list = Enum.to_list(1..10_000)
|
| 11 |
+
map_fun = fn i -> [i, i * i] end
|
| 12 |
+
|
| 13 |
+
Benchee.run(
|
| 14 |
+
%{
|
| 15 |
+
"flat_map" => fn -> Enum.flat_map(list, map_fun) end,
|
| 16 |
+
"map.flatten" => fn -> list |> Enum.map(map_fun) |> List.flatten() end
|
| 17 |
+
},
|
| 18 |
+
time: 10,
|
| 19 |
+
memory_time: 2
|
| 20 |
+
)
|
| 21 |
+
```
|
| 22 |
+
|
| 23 |
+
Produces the following output on the console:
|
| 24 |
+
|
| 25 |
+
```
|
| 26 |
+
Operating System: Linux
|
| 27 |
+
CPU Information: AMD Ryzen 9 5900X 12-Core Processor
|
| 28 |
+
Number of Available Cores: 24
|
| 29 |
+
Available memory: 31.26 GB
|
| 30 |
+
Elixir 1.19.0
|
| 31 |
+
Erlang 28.1
|
| 32 |
+
JIT enabled: true
|
| 33 |
+
|
| 34 |
+
Benchmark suite executing with the following configuration:
|
| 35 |
+
warmup: 2 s
|
| 36 |
+
time: 10 s
|
| 37 |
+
memory time: 2 s
|
| 38 |
+
reduction time: 0 ns
|
| 39 |
+
parallel: 1
|
| 40 |
+
inputs: none specified
|
| 41 |
+
Estimated total run time: 28 s
|
| 42 |
+
Excluding outliers: false
|
| 43 |
+
|
| 44 |
+
Benchmarking flat_map ...
|
| 45 |
+
Benchmarking map.flatten ...
|
| 46 |
+
Calculating statistics...
|
| 47 |
+
Formatting results...
|
| 48 |
+
|
| 49 |
+
Name ips average deviation median 99th %
|
| 50 |
+
flat_map 3.96 K 252.74 μs ±15.64% 247.61 μs 321.85 μs
|
| 51 |
+
map.flatten 1.84 K 543.57 μs ±44.18% 414.16 μs 1223.92 μs
|
| 52 |
+
|
| 53 |
+
Comparison:
|
| 54 |
+
flat_map 3.96 K
|
| 55 |
+
map.flatten 1.84 K - 2.15x slower +290.83 μs
|
| 56 |
+
|
| 57 |
+
Memory usage statistics:
|
| 58 |
+
|
| 59 |
+
Name Memory usage
|
| 60 |
+
flat_map 624.97 KB
|
| 61 |
+
map.flatten 781.25 KB - 1.25x memory usage +156.28 KB
|
| 62 |
+
|
| 63 |
+
**All measurements for memory usage were the same**
|
| 64 |
+
```
|
| 65 |
+
|
| 66 |
+
The aforementioned [plugins](#plugins) like [benchee_html](https://github.com/bencheeorg/benchee_html) make it possible to generate nice looking [HTML reports](http://www.pragtob.info/benchee/README/results.html), where individual graphs can also be exported as PNG images:
|
| 67 |
+
|
| 68 |
+

|
| 69 |
+
|
| 70 |
+
## Table of Contents
|
| 71 |
+
|
| 72 |
+
- [Table of Contents](#table-of-contents)
|
| 73 |
+
- [Features](#features)
|
| 74 |
+
- [Statistics](#statistics)
|
| 75 |
+
- [Installation](#installation)
|
| 76 |
+
- [Usage](#usage)
|
| 77 |
+
- [Configuration](#configuration)
|
| 78 |
+
- [Metrics to measure](#metrics-to-measure)
|
| 79 |
+
- [Measuring time](#measuring-time)
|
| 80 |
+
- [A note on time measurement accuracy](#a-note-on-time-measurement-accuracy)
|
| 81 |
+
- [Measuring Memory Consumption](#measuring-memory-consumption)
|
| 82 |
+
- [Measuring Reductions](#measuring-reductions)
|
| 83 |
+
- [Inputs](#inputs)
|
| 84 |
+
- [Formatters](#formatters)
|
| 85 |
+
- [Console Formatter options](#console-formatter-options)
|
| 86 |
+
- [Profiling after a run](#profiling-after-a-run)
|
| 87 |
+
- [Remove Outliers](#remove-outliers)
|
| 88 |
+
- [Saving, loading and comparing previous runs](#saving-loading-and-comparing-previous-runs)
|
| 89 |
+
- [Hooks (Setup, Teardown etc.)](#hooks-setup-teardown-etc)
|
| 90 |
+
- [Suite hooks](#suite-hooks)
|
| 91 |
+
- [Scenario hooks](#scenario-hooks)
|
| 92 |
+
- [What is a scenario?](#what-is-a-scenario)
|
| 93 |
+
- [before\_scenario](#before_scenario)
|
| 94 |
+
- [after\_scenario](#after_scenario)
|
| 95 |
+
- [Benchmarking function hooks](#benchmarking-function-hooks)
|
| 96 |
+
- [before\_each](#before_each)
|
| 97 |
+
- [after\_each](#after_each)
|
| 98 |
+
- [Hook arguments and return values](#hook-arguments-and-return-values)
|
| 99 |
+
- [Hook configuration: global versus local](#hook-configuration-global-versus-local)
|
| 100 |
+
- [When does a hook happen? (Complete Example)](#when-does-a-hook-happen-complete-example)
|
| 101 |
+
- [More verbose usage](#more-verbose-usage)
|
| 102 |
+
- [Usage from Erlang](#usage-from-erlang)
|
| 103 |
+
- [Known Issues](#known-issues)
|
| 104 |
+
- [Plugins](#plugins)
|
| 105 |
+
- [Contributing ](#contributing-)
|
| 106 |
+
- [Development](#development)
|
| 107 |
+
- [Copyright and License](#copyright-and-license)
|
| 108 |
+
|
| 109 |
+
## Features
|
| 110 |
+
|
| 111 |
+
* first runs the functions for a given warmup time without recording the results, to simulate a _"warm"/running_ system
|
| 112 |
+
* [measures memory usage](#measuring-memory-consumption)
|
| 113 |
+
* provides you with lots of [statistics](#statistics)
|
| 114 |
+
* plugin/extensible friendly architecture so you can use different formatters to display benchmarking results as [HTML, markdown, JSON and more](#plugins)
|
| 115 |
+
* measure not only [time](#measuring-time), but also measure [memory](#measuring-memory-consumption) and [reductions](#measuring-reductions)
|
| 116 |
+
* [profile](#profiling-after-a-run) right after benchmarking
|
| 117 |
+
* as precise as it can get, measure with up to nanosecond precision (Operating System dependent)
|
| 118 |
+
* nicely formatted console output with units scaled to appropriately (nanoseconds to minutes)
|
| 119 |
+
* (optionally) measures the overhead of function calls so that the measured/reported times really are the execution time of _your_code_ without that overhead.
|
| 120 |
+
* (optionally) [removes outliers](#remove-outliers)
|
| 121 |
+
* [hooks](#hooks-setup-teardown-etc) to execute something before/after a benchmarking invocation, without it impacting the measured time
|
| 122 |
+
* execute benchmark jobs in parallel to gather more results in the same time, or simulate a system under load
|
| 123 |
+
* well documented & well tested
|
| 124 |
+
|
| 125 |
+
### Statistics
|
| 126 |
+
|
| 127 |
+
Provides you with the following **statistical data**:
|
| 128 |
+
|
| 129 |
+
* **average** - average execution time/memory usage (the lower the better)
|
| 130 |
+
* **ips** - iterations per second, aka how often can the given function be executed within one second (the higher the better - good for graphing), only for run times
|
| 131 |
+
* **deviation** - standard deviation (how much do the results vary), given as a percentage of the average (raw absolute values also available)
|
| 132 |
+
* **median** - when all measured values are sorted, this is the middle value. More stable than the average and somewhat more likely to be a typical value you see, for the most typical value see mode. (the lower the better)
|
| 133 |
+
* **99th %** - 99th percentile, 99% of all measured values are less than this - worst case performance-ish
|
| 134 |
+
|
| 135 |
+
In addition, you can optionally output an extended set of statistics:
|
| 136 |
+
|
| 137 |
+
* **minimum** - the smallest value measured for the job (fastest/least consumption)
|
| 138 |
+
* **maximum** - the biggest run time measured for the job (slowest/most consumption)
|
| 139 |
+
* **sample size** - the number of measurements taken
|
| 140 |
+
* **mode** - the measured values that occur the most. Often one value, but can be multiple values if they occur exactly as often. If no value occurs at least twice, this value will be `nil`.
|
| 141 |
+
|
| 142 |
+
Benchee can also [remove outliers](#remove-outliers).
|
| 143 |
+
|
| 144 |
+
## Installation
|
| 145 |
+
|
| 146 |
+
Add `:benchee` to your list of dependencies in `mix.exs`:
|
| 147 |
+
|
| 148 |
+
```elixir
|
| 149 |
+
defp deps do
|
| 150 |
+
[
|
| 151 |
+
{:benchee, "~> 1.0", only: :dev}
|
| 152 |
+
]
|
| 153 |
+
end
|
| 154 |
+
```
|
| 155 |
+
|
| 156 |
+
Install via `mix deps.get` and then happy benchmarking as described in [Usage](#usage) :)
|
| 157 |
+
|
| 158 |
+
Elixir versions supported/tested against are 1.7+. It _should_ theoretically still work with 1.4, 1.5 & 1.6 but we don't actively test/support it as installing them on current CI systems is not supported out of the box.
|
| 159 |
+
|
| 160 |
+
## Usage
|
| 161 |
+
|
| 162 |
+
After installing just write a little Elixir benchmarking script:
|
| 163 |
+
|
| 164 |
+
```elixir
|
| 165 |
+
list = Enum.to_list(1..10_000)
|
| 166 |
+
map_fun = fn i -> [i, i * i] end
|
| 167 |
+
|
| 168 |
+
Benchee.run(
|
| 169 |
+
%{
|
| 170 |
+
"flat_map" => fn -> Enum.flat_map(list, map_fun) end,
|
| 171 |
+
"map.flatten" => fn -> list |> Enum.map(map_fun) |> List.flatten() end
|
| 172 |
+
}
|
| 173 |
+
)
|
| 174 |
+
```
|
| 175 |
+
|
| 176 |
+
(names can also be specified as `:atoms` if you want to)
|
| 177 |
+
|
| 178 |
+
This produces the following output:
|
| 179 |
+
|
| 180 |
+
```
|
| 181 |
+
tobi@speedy:$ mix run samples/run_defaults.exs
|
| 182 |
+
Operating System: Linux
|
| 183 |
+
CPU Information: Intel(R) Core(TM) i7-4790 CPU @ 3.60GHz
|
| 184 |
+
Number of Available Cores: 8
|
| 185 |
+
Available memory: 15.61 GB
|
| 186 |
+
Elixir 1.8.1
|
| 187 |
+
Erlang 21.2.7
|
| 188 |
+
|
| 189 |
+
Benchmark suite executing with the following configuration:
|
| 190 |
+
warmup: 2 s
|
| 191 |
+
time: 5 s
|
| 192 |
+
memory time: 0 ns
|
| 193 |
+
parallel: 1
|
| 194 |
+
inputs: none specified
|
| 195 |
+
Estimated total run time: 14 s
|
| 196 |
+
|
| 197 |
+
Benchmarking flat_map...
|
| 198 |
+
Benchmarking map.flatten...
|
| 199 |
+
|
| 200 |
+
Name ips average deviation median 99th %
|
| 201 |
+
flat_map 2.34 K 427.78 μs ±16.02% 406.29 μs 743.01 μs
|
| 202 |
+
map.flatten 1.22 K 820.87 μs ±19.29% 772.61 μs 1286.35 μs
|
| 203 |
+
|
| 204 |
+
Comparison:
|
| 205 |
+
flat_map 2.34 K
|
| 206 |
+
map.flatten 1.22 K - 1.92x slower +393.09 μs
|
| 207 |
+
```
|
| 208 |
+
|
| 209 |
+
See [Features](#features) for a description of the different statistical values and what they mean.
|
| 210 |
+
|
| 211 |
+
If you're looking to see how to make something specific work, please refer to the [samples](https://github.com/bencheeorg/benchee/tree/main/samples) directory. Also, especially when wanting to extend Benchee, check out the [hexdocs](https://hexdocs.pm/benchee/api-reference.html).
|
| 212 |
+
|
| 213 |
+
### Configuration
|
| 214 |
+
|
| 215 |
+
Benchee takes a wealth of configuration options, however those are entirely optional. Benchee ships with sensible defaults for all of these.
|
| 216 |
+
|
| 217 |
+
In the most common `Benchee.run/2` interface configuration options are passed as the second argument in the form of an keyword list:
|
| 218 |
+
|
| 219 |
+
```elixir
|
| 220 |
+
Benchee.run(%{"some function" => fn -> magic end}, print: [benchmarking: false])
|
| 221 |
+
```
|
| 222 |
+
|
| 223 |
+
The available options are the following (also documented in [hexdocs](https://hexdocs.pm/benchee/Benchee.Configuration.html#init/1)).
|
| 224 |
+
|
| 225 |
+
* `warmup` - the time in seconds for which a benchmarking job should be run without measuring anything before "real" measurements start. This simulates a _"warm"/running_ system. Defaults to 2.
|
| 226 |
+
* `time` - the time in seconds for how long each individual scenario (benchmarking job x input) should be run for measuring the execution times (run time performance). Defaults to 5.
|
| 227 |
+
* `memory_time` - the time in seconds for how long [memory measurements](#measuring-memory-consumption) should be conducted. Defaults to 0 (turned off).
|
| 228 |
+
* `reduction_time` - the time in seconds for how long [reductions are measured](#measuring-memory-reductions) should be conducted. Defaults to 0 (turned off).
|
| 229 |
+
* `inputs` - a map or list of two element tuples. If a map, the keys are descriptive input names and values are the actual input values. If a list of tuples, the first element in each tuple is the input name, and the second element in each tuple is the actual input value. Your benchmarking jobs will then be run with each of these inputs. For this to work your benchmarking function gets the current input passed in as an argument into the function. Defaults to `nil`, aka no input specified and functions are called without an argument. See [Inputs](#inputs).
|
| 230 |
+
* `formatters` - list of formatters either as a module implementing the formatter behaviour, a tuple of said module and options it should take or formatter functions. They are run when using `Benchee.run/2` or you can invoke them through `Benchee.Formatter.output/1`. Functions need to accept one argument (which is the benchmarking suite with all data) and then use that to produce output. Used for plugins & configuration. Also allows the configuration of the console formatter to print extended statistics. Defaults to the builtin console formatter `Benchee.Formatters.Console`. See [Formatters](#formatters).
|
| 231 |
+
* `measure_function_call_overhead` - Measure how long an empty function call takes and deduct this from each measured run time. This overhead should be negligible for all but the most micro benchmarks. Defaults to false.
|
| 232 |
+
* `pre_check` - whether or not to run each job with each input - including all given before or after scenario or each hooks - before the benchmarks are measured to ensure that your code executes without error. This can save time while developing your suites. Defaults to `false`. Possible values are:
|
| 233 |
+
* `false` - no pre check is run
|
| 234 |
+
* `true` - each scenario is run but the return value is ignored
|
| 235 |
+
* `:all_same` - raises if all scenarios are not returning the same value for
|
| 236 |
+
each input. This is useful when benchmarking alternative implementations of a
|
| 237 |
+
deterministic function.
|
| 238 |
+
* `parallel` - the function of each benchmarking job will be executed in `parallel` number processes. If `parallel: 4` then 4 processes will be spawned that all execute the _same_ function for the given time. When `time` seconds have passed, 4 new processes will be spawned for the next scenario (meaning a new input or another function to be benchmarked). This gives you more data in the same time, but also puts load on the system interfering with benchmark results. For more on the pros and cons of parallel benchmarking [check the wiki](https://github.com/bencheeorg/benchee/wiki/Parallel-Benchmarking). Defaults to 1 (no parallel execution).
|
| 239 |
+
* `save` - specify a `path` where to store the results of the current benchmarking suite, tagged with the specified `tag`. See [Saving & Loading](#saving-loading-and-comparing-previous-runs).
|
| 240 |
+
* `load` - load saved suite or suites to compare your current benchmarks against. Can be a string or a list of strings or patterns. See [Saving & Loading](#saving-loading-and-comparing-previous-runs).
|
| 241 |
+
* `print` - a map or keyword list from atoms to `true` or `false` to configure if the output identified by the atom will be printed during the standard Benchee benchmarking process. All options are enabled by default (true). Options are:
|
| 242 |
+
* `:benchmarking` - print when Benchee starts benchmarking a new job (`Benchmarking name ...`) as well as when statistics are being calculated or formatting begins.
|
| 243 |
+
* `:configuration` - a summary of configured benchmarking options including estimated total run time is printed before benchmarking starts
|
| 244 |
+
* `:fast_warning` - warnings are displayed if functions are executed too fast leading to inaccurate measures
|
| 245 |
+
* `:unit_scaling` - the strategy for choosing a unit for durations,
|
| 246 |
+
counts & memory measurements. May or may not be implemented by a given formatter (The console formatter implements it).
|
| 247 |
+
When scaling a value, Benchee finds the "best fit"
|
| 248 |
+
unit (the largest unit for which the result is at least 1). For example,
|
| 249 |
+
1_200_000 scales to `1.2 M`, while `800_000` scales to `800 K`. The
|
| 250 |
+
`unit_scaling` strategy determines how Benchee chooses the best fit unit for
|
| 251 |
+
an entire list of values, when the individual values in the list may have
|
| 252 |
+
different best fit units. There are four strategies, defaulting to `:best`:
|
| 253 |
+
* `:best` - the most frequent best fit unit will be used, a tie
|
| 254 |
+
will result in the larger unit being selected.
|
| 255 |
+
* `:largest` - the largest best fit unit will be used
|
| 256 |
+
* `:smallest` - the smallest best fit unit will be used
|
| 257 |
+
* `:none` - no unit scaling will occur.
|
| 258 |
+
* `:before_scenario`/`after_scenario`/`before_each`/`after_each` - read up on them in the [hooks section](#hooks-setup-teardown-etc)
|
| 259 |
+
* `profile_after` - accepts any of the following options:
|
| 260 |
+
* a boolean - `true` will enable profiling with the default profiler (`:eprof`) and `false` will disable profiling. Defaults to `false`.
|
| 261 |
+
* a profiler - either as a tuple of `{profiler, opts}` (e.g., `{:fprof, [sort: :own]}`) or just the profiler (e.g., `:fprof`),
|
| 262 |
+
which is equivalent to `{profiler, []}`. The accepted built-in profilers are:
|
| 263 |
+
[`:cprof`](https://hexdocs.pm/mix/Mix.Tasks.Profile.Cprof.html),
|
| 264 |
+
[`:eprof`](https://hexdocs.pm/mix/Mix.Tasks.Profile.Eprof.html)
|
| 265 |
+
[`:fprof`](https://hexdocs.pm/mix/Mix.Tasks.Profile.Fprof.html) and
|
| 266 |
+
[`:tprof`](https://hexdocs.pm/mix/Mix.Tasks.Profile.Tprof.html) (requires elixir >= 1.17 and erlang >= 27).
|
| 267 |
+
* `max_sample_size` - the maximum number of samples collected per scenario. Defaults to `1_000_000`.
|
| 268 |
+
This is used to limit memory consumption and unnecessary processing - 1 Million samples is plenty.
|
| 269 |
+
This limit also applies to number of iterations done during warmup.
|
| 270 |
+
You can set your own number or set it to `nil` if you don't want any limit.
|
| 271 |
+
* `exclude_outliers` - whether or not statistical outliers should be removed for the calculated statistics.
|
| 272 |
+
Defaults to `false`.
|
| 273 |
+
This means that values that are far outside the usual range (as determined by the percentiles/quantiles) will
|
| 274 |
+
be removed from the gathered samples and the calculated statistics. You might want to enable this if you
|
| 275 |
+
don't want things like the garbage collection triggering to influence your results as much.
|
| 276 |
+
|
| 277 |
+
### Metrics to measure
|
| 278 |
+
|
| 279 |
+
Benchee can't only measure [execution time](#measuring-time), but also [memory consumption](#measuring-memory-consumption) and [reductions](#measuring-reductions)!
|
| 280 |
+
|
| 281 |
+
You can measure one of these metrics, or all at the same time. The choice is up to you. Warmup will only occur once though, the time for measuring the metrics are governed by `time`, `memory_time` and `reduction_time` configuration values respectively.
|
| 282 |
+
|
| 283 |
+
By default only execution time is measured, memory and reductions need to be opted in by specifying a non 0 time amount.
|
| 284 |
+
|
| 285 |
+
#### Measuring time
|
| 286 |
+
|
| 287 |
+
This is the default, which you'll most likely want to use as you want to measure how fast your system processes something or responds to a request. Benchee does its best to measure time as accurately and as scoped to your function as possible.
|
| 288 |
+
|
| 289 |
+
```elixir
|
| 290 |
+
Benchee.run(
|
| 291 |
+
%{
|
| 292 |
+
"something_great" => fn -> cool_stuff end
|
| 293 |
+
},
|
| 294 |
+
warmup: 1,
|
| 295 |
+
time: 5,
|
| 296 |
+
memory_time: 2,
|
| 297 |
+
reduction_time: 2
|
| 298 |
+
)
|
| 299 |
+
```
|
| 300 |
+
|
| 301 |
+
##### A note on time measurement accuracy
|
| 302 |
+
|
| 303 |
+
From system to system the resolution of the clock [can vary](https://www.erlang.org/doc/apps/erts/time_correction.html).
|
| 304 |
+
|
| 305 |
+
Generally speaking we have seen accuracies down to 1 nanosecond on Linux and ~1 microsecond onb both OSX and Windows. We have also seen accuracy as low as 10ms on Windows in a CI environment.
|
| 306 |
+
These numbers are not a limitation of Benchee, but of the Operating System (or at the very least how erlang makes it available).
|
| 307 |
+
|
| 308 |
+
If your benchmark takes 100s of microseconds this likely has no/little impact, but **if you want to do extremely nano benchmarks we recommend doing them on Linux**.
|
| 309 |
+
|
| 310 |
+
So, what happens if a function executes too fast for Benchee to measure? If Benchee recognizes this (not always possible) it will automatically take the function and repeat it multiple times. This way a function that takes 2 microseconds will take ~20 microseconds and differences can be measured even with microsecond accuracy. Naturally, this comes at several disadvantages:
|
| 311 |
+
|
| 312 |
+
* The looping/repetition code is now measured alongside the function
|
| 313 |
+
* essentially every single measurement is now an average across 10 runs making lots of statistics less meaningful
|
| 314 |
+
|
| 315 |
+
Benchee will print a big warning when this happens.
|
| 316 |
+
|
| 317 |
+
#### Measuring Memory Consumption
|
| 318 |
+
|
| 319 |
+
Starting with version 0.13, users can now get measurements of how much memory their benchmarked scenarios use. The measurement is **limited to the process that Benchee executes your provided code in** - i.e. other processes (like worker pools)/the whole BEAM isn't taken into account.
|
| 320 |
+
|
| 321 |
+
This measurement is **not** the actual effect on the size of the BEAM VM size, but the total amount of memory that was allocated during the execution of a given scenario. This includes all memory that was garbage collected during the execution of that scenario.
|
| 322 |
+
|
| 323 |
+
This measurement of memory does not affect the measurement of run times.
|
| 324 |
+
|
| 325 |
+
In cases where all measurements of memory consumption are identical, which happens very frequently, the full statistics will be omitted from the standard console formatter. If your function is deterministic, this should always be the case. Only in functions with some amount of randomness will there be variation in memory usage.
|
| 326 |
+
|
| 327 |
+
Memory measurement is disabled by default, you can choose to enable it by passing `memory_time: your_seconds` option to `Benchee.run/2`:
|
| 328 |
+
|
| 329 |
+
```elixir
|
| 330 |
+
Benchee.run(
|
| 331 |
+
%{
|
| 332 |
+
"something_great" => fn -> cool_stuff end
|
| 333 |
+
},
|
| 334 |
+
memory_time: 2
|
| 335 |
+
)
|
| 336 |
+
```
|
| 337 |
+
|
| 338 |
+
Memory time can be specified separately as it will often be constant - so it might not need as much measuring time.
|
| 339 |
+
|
| 340 |
+
A full example, including an example of the console output, can be found
|
| 341 |
+
[here](samples/measure_memory.exs).
|
| 342 |
+
|
| 343 |
+
#### Measuring Reductions
|
| 344 |
+
|
| 345 |
+
Starting in versions 1.1.0 Benchee can measure reductions - but what are reductions?
|
| 346 |
+
|
| 347 |
+
In short, it's not very well defined but a "unit of work". The BEAM uses them to keep track of how long a process has run. As [the Beam Book puts it as follows](https://blog.stenmans.org/theBeamBook/#_scheduling_non_preemptive_reduction_counting):
|
| 348 |
+
|
| 349 |
+
>BEAM solves this by keeping track of how long a process has been running. This is done by counting reductions. The term originally comes from the mathematical term beta-reduction used in lambda calculus.
|
| 350 |
+
>
|
| 351 |
+
>The definition of a reduction in BEAM is not very specific, but we can see it as a small piece of work, which shouldn’t take too long. Each function call is counted as a reduction. BEAM does a test upon entry to each function to check whether the process has used up all its reductions or not. If there are reductions left the function is executed otherwise the process is suspended.
|
| 352 |
+
|
| 353 |
+
Now, why would you want to measure this? Well, apart from BIFs & NIFs, which are not accurately tracked through this, it gives an impression of how much work the BEAM is doing. And the great thing is, this is independent of the load the system is under as well as the hardware. Hence, it gives you a way to check performance that is less volatile so suitable for CI for instance.
|
| 354 |
+
|
| 355 |
+
It can slightly differ between elixir & erlang versions, though.
|
| 356 |
+
|
| 357 |
+
**Like memory measurements, this only tracks reductions directly in the function given to benchee, not processes spawned by it or other processes it uses.**
|
| 358 |
+
|
| 359 |
+
Reduction measurement is disabled by default, you can choose to enable it by passing `reduction_time: your_seconds` option to `Benchee.run/2`:
|
| 360 |
+
|
| 361 |
+
```elixir
|
| 362 |
+
Benchee.run(
|
| 363 |
+
%{
|
| 364 |
+
"something_great" => fn -> cool_stuff end
|
| 365 |
+
},
|
| 366 |
+
reduction_time: 2
|
| 367 |
+
)
|
| 368 |
+
```
|
| 369 |
+
|
| 370 |
+
Also like memory measurements, reduction measurements will often be constant unless something changes about the execution of the benchmarking function.
|
| 371 |
+
|
| 372 |
+
### Inputs
|
| 373 |
+
|
| 374 |
+
`:inputs` is a very useful configuration that allows you to run the same benchmarking jobs with different inputs. We call this combination a _"scenario"_. You specify the inputs as either a map from name (String or atom) to the actual input value or a list of tuples where the first element in each tuple is the name and the second element in the tuple is the value.
|
| 375 |
+
|
| 376 |
+
Why do this? Functions can have different performance characteristics on differently shaped inputs - be that structure or input size. One of such cases is comparing tail-recursive and body-recursive implementations of `map`. More information in the [repository with the benchmark](https://github.com/PragTob/elixir_playground/blob/main/bench/tco_blog_post_focussed_inputs.exs) and the [blog post](https://pragtob.wordpress.com/2016/06/16/tail-call-optimization-in-elixir-erlang-not-as-efficient-and-important-as-you-probably-think/).
|
| 377 |
+
|
| 378 |
+
As a little sample:
|
| 379 |
+
|
| 380 |
+
```elixir
|
| 381 |
+
map_fun = fn i -> [i, i * i] end
|
| 382 |
+
|
| 383 |
+
Benchee.run(
|
| 384 |
+
%{
|
| 385 |
+
"flat_map" => fn input -> Enum.flat_map(input, map_fun) end,
|
| 386 |
+
"map.flatten" => fn input -> input |> Enum.map(map_fun) |> List.flatten() end
|
| 387 |
+
},
|
| 388 |
+
inputs: %{
|
| 389 |
+
"Small" => Enum.to_list(1..1_000),
|
| 390 |
+
"Medium" => Enum.to_list(1..10_000),
|
| 391 |
+
"Bigger" => Enum.to_list(1..100_000)
|
| 392 |
+
}
|
| 393 |
+
)
|
| 394 |
+
```
|
| 395 |
+
|
| 396 |
+
This means each function will be benchmarked with each input that is specified in the inputs. Then you'll get the output divided by input so you can see which function is fastest for which input, like so:
|
| 397 |
+
|
| 398 |
+
```
|
| 399 |
+
tobi@speedy:~/github/benchee(readme-overhaul)$ mix run samples/multiple_inputs.exs
|
| 400 |
+
|
| 401 |
+
(... general information ...)
|
| 402 |
+
|
| 403 |
+
##### With input Bigger #####
|
| 404 |
+
Name ips average deviation median 99th %
|
| 405 |
+
flat_map 150.81 6.63 ms ±12.65% 6.57 ms 8.74 ms
|
| 406 |
+
map.flatten 114.05 8.77 ms ±16.22% 8.42 ms 12.76 ms
|
| 407 |
+
|
| 408 |
+
Comparison:
|
| 409 |
+
flat_map 150.81
|
| 410 |
+
map.flatten 114.05 - 1.32x slower +2.14 ms
|
| 411 |
+
|
| 412 |
+
##### With input Medium #####
|
| 413 |
+
Name ips average deviation median 99th %
|
| 414 |
+
flat_map 2.28 K 437.80 μs ±10.72% 425.63 μs 725.09 μs
|
| 415 |
+
map.flatten 1.78 K 561.18 μs ±5.55% 553.98 μs 675.98 μs
|
| 416 |
+
|
| 417 |
+
Comparison:
|
| 418 |
+
flat_map 2.28 K
|
| 419 |
+
map.flatten 1.78 K - 1.28x slower +123.37 μs
|
| 420 |
+
|
| 421 |
+
##### With input Small #####
|
| 422 |
+
Name ips average deviation median 99th %
|
| 423 |
+
flat_map 26.31 K 38.01 μs ±15.47% 36.69 μs 67.08 μs
|
| 424 |
+
map.flatten 18.65 K 53.61 μs ±11.32% 52.79 μs 70.17 μs
|
| 425 |
+
|
| 426 |
+
Comparison:
|
| 427 |
+
flat_map 26.31 K
|
| 428 |
+
map.flatten 18.65 K - 1.41x slower +15.61 μs
|
| 429 |
+
```
|
| 430 |
+
|
| 431 |
+
Therefore, we **highly recommend** using this feature and checking different realistically structured and sized inputs for the functions you benchmark!
|
| 432 |
+
|
| 433 |
+
### Formatters
|
| 434 |
+
|
| 435 |
+
Among all the configuration options, one that you probably want to use are the formatters. They format and print out the results of the benchmarking suite.
|
| 436 |
+
|
| 437 |
+
The `:formatters` option is specified a list of:
|
| 438 |
+
* modules implementing the `Benchee.Formatter` behaviour
|
| 439 |
+
* a tuple of a module specified above and options for it `{module, options}`
|
| 440 |
+
* functions that take one argument (the benchmarking suite with all its results) and then do whatever you want them to
|
| 441 |
+
|
| 442 |
+
So if you are using the [HTML plugin](https://github.com/bencheeorg/benchee_html) and you want to run both the console formatter and the HTML formatter this looks like this (after you installed it of course):
|
| 443 |
+
|
| 444 |
+
```elixir
|
| 445 |
+
list = Enum.to_list(1..10_000)
|
| 446 |
+
map_fun = fn i -> [i, i * i] end
|
| 447 |
+
|
| 448 |
+
Benchee.run(
|
| 449 |
+
%{
|
| 450 |
+
"flat_map" => fn -> Enum.flat_map(list, map_fun) end,
|
| 451 |
+
"map.flatten" => fn -> list |> Enum.map(map_fun) |> List.flatten end
|
| 452 |
+
},
|
| 453 |
+
formatters: [
|
| 454 |
+
{Benchee.Formatters.HTML, file: "samples_output/my.html"},
|
| 455 |
+
Benchee.Formatters.Console
|
| 456 |
+
]
|
| 457 |
+
)
|
| 458 |
+
```
|
| 459 |
+
|
| 460 |
+
#### Console Formatter options
|
| 461 |
+
|
| 462 |
+
The console formatter supports 2 configuration options:
|
| 463 |
+
|
| 464 |
+
* `:comparison` - if the comparison of the different benchmarking jobs (x times slower than) is shown. Enabled by default.
|
| 465 |
+
* `extended_statistics` - display more statistics, aka `minimum`, `maximum`, `sample_size` and `mode`. Disabled by default.
|
| 466 |
+
|
| 467 |
+
So if you want to see more statistics you simple pass `extended_statistics: true` to the console formatter:
|
| 468 |
+
|
| 469 |
+
```elixir
|
| 470 |
+
list = Enum.to_list(1..10_000)
|
| 471 |
+
map_fun = fn i -> [i, i * i] end
|
| 472 |
+
|
| 473 |
+
Benchee.run(
|
| 474 |
+
%{
|
| 475 |
+
"flat_map" => fn -> Enum.flat_map(list, map_fun) end,
|
| 476 |
+
"map.flatten" => fn -> list |> Enum.map(map_fun) |> List.flatten() end
|
| 477 |
+
},
|
| 478 |
+
time: 10,
|
| 479 |
+
formatters: [{Benchee.Formatters.Console, extended_statistics: true}]
|
| 480 |
+
)
|
| 481 |
+
```
|
| 482 |
+
|
| 483 |
+
Which produces:
|
| 484 |
+
|
| 485 |
+
```
|
| 486 |
+
(... normal output ...)
|
| 487 |
+
|
| 488 |
+
Extended statistics:
|
| 489 |
+
|
| 490 |
+
Name minimum maximum sample size mode
|
| 491 |
+
flat_map 345.43 μs 1195.89 μs 23.73 K 405.64 μs
|
| 492 |
+
map.flatten 522.99 μs 2105.56 μs 12.03 K 767.83 μs, 768.44 μs
|
| 493 |
+
```
|
| 494 |
+
|
| 495 |
+
(Btw. notice how the modes of both are much closer and for `map.flatten` much less than the average of `766.99`, see `samples/run_extended_statistics`)
|
| 496 |
+
|
| 497 |
+
### Profiling after a run
|
| 498 |
+
|
| 499 |
+
Often time when benchmarking, you want to improve performance. However, Benchee only tells you how fast something is, it doesn't tell you what part of your code is slow. This is where profiling comes in.
|
| 500 |
+
|
| 501 |
+
Benchee offers you the `profile_after` option to run a profiler of your choice after a benchmarking run to see what's slow. This will run every scenario once.
|
| 502 |
+
|
| 503 |
+
By default it will run the `:eprof` profiler, different profilers with different options can be used - see [configuration](#configuration).
|
| 504 |
+
|
| 505 |
+
```elixir
|
| 506 |
+
list = Enum.to_list(1..10_000)
|
| 507 |
+
map_fun = fn i -> [i, i * i] end
|
| 508 |
+
|
| 509 |
+
Benchee.run(
|
| 510 |
+
%{
|
| 511 |
+
"flat_map" => fn -> Enum.flat_map(list, map_fun) end,
|
| 512 |
+
"map.flatten" => fn -> list |> Enum.map(map_fun) |> List.flatten() end
|
| 513 |
+
},
|
| 514 |
+
profile_after: true
|
| 515 |
+
)
|
| 516 |
+
```
|
| 517 |
+
|
| 518 |
+
After the normal benchmarking, this results prints profiles like:
|
| 519 |
+
|
| 520 |
+
```
|
| 521 |
+
Profiling flat_map with eprof...
|
| 522 |
+
Warmup...
|
| 523 |
+
|
| 524 |
+
|
| 525 |
+
Profile results of #PID<0.1885.0>
|
| 526 |
+
# CALLS % TIME µS/CALL
|
| 527 |
+
Total 30004 100.0 5665 0.19
|
| 528 |
+
Enum.flat_map/2 1 0.00 0 0.00
|
| 529 |
+
anonymous fn/2 in :elixir_compiler_1.__FILE__/1 1 0.00 0 0.00
|
| 530 |
+
:erlang.apply/2 1 0.04 2 2.00
|
| 531 |
+
:erlang.++/2 10000 18.38 1041 0.10
|
| 532 |
+
anonymous fn/1 in :elixir_compiler_1.__FILE__/1 10000 28.12 1593 0.16
|
| 533 |
+
Enum.flat_map_list/2 10001 53.47 3029 0.30
|
| 534 |
+
|
| 535 |
+
Profile done over 6 matching functions
|
| 536 |
+
|
| 537 |
+
Profiling map.flatten with eprof...
|
| 538 |
+
Warmup...
|
| 539 |
+
|
| 540 |
+
|
| 541 |
+
Profile results of #PID<0.1887.0>
|
| 542 |
+
# CALLS % TIME µS/CALL
|
| 543 |
+
Total 60007 100.0 8649 0.14
|
| 544 |
+
Enum.map/2 1 0.00 0 0.00
|
| 545 |
+
anonymous fn/2 in :elixir_compiler_1.__FILE__/1 1 0.00 0 0.00
|
| 546 |
+
List.flatten/1 1 0.00 0 0.00
|
| 547 |
+
:lists.flatten/1 1 0.00 0 0.00
|
| 548 |
+
:erlang.apply/2 1 0.02 2 2.00
|
| 549 |
+
anonymous fn/1 in :elixir_compiler_1.__FILE__/1 10000 16.35 1414 0.14
|
| 550 |
+
Enum."-map/2-lists^map/1-0-"/2 10001 26.38 2282 0.23
|
| 551 |
+
:lists.do_flatten/2 40001 57.24 4951 0.12
|
| 552 |
+
```
|
| 553 |
+
|
| 554 |
+
**Note about after_each hooks:** `after_each` hooks currently don't work when profiling a function, as they are not passed the return value of the function after the profiling run. It's already fixed on the elixir side and is waiting for release, likely in 1.14. It should then just work.
|
| 555 |
+
|
| 556 |
+
### Remove Outliers
|
| 557 |
+
|
| 558 |
+
Benchee can remove outliers from the gathered samples while calculating statistics.
|
| 559 |
+
That is, as determined by percentiles/quantiles (we follow [this approach](https://en.wikipedia.org/wiki/Interquartile_range#Outliers)).
|
| 560 |
+
|
| 561 |
+
You might consider excluding outliers for extreme micro/nano-benchmarks where individual results can be skewed a lot by the Garbage Collection.
|
| 562 |
+
|
| 563 |
+
You can simply pass `exclude_outliers: true` to Benchee to trigger the removal of outliers.
|
| 564 |
+
|
| 565 |
+
```elixir
|
| 566 |
+
Benchee.run(jobs, exclude_outliers: true)
|
| 567 |
+
```
|
| 568 |
+
|
| 569 |
+
The outliers themselves (aka the samples that have been determined to be outliers)
|
| 570 |
+
as well as the lower/upper bound after which samples are considered outliers are accessible
|
| 571 |
+
in the `Benchee.Statistics` struct.
|
| 572 |
+
|
| 573 |
+
The samples themselves still include the outliers, they are only removed for calculating statistics.
|
| 574 |
+
|
| 575 |
+
Right now Benchee doesn't print the outliers yet, but you can inspect the resulting data structures if you're interested (or send a PR :) )
|
| 576 |
+
|
| 577 |
+
### Saving, loading and comparing previous runs
|
| 578 |
+
|
| 579 |
+
Benchee can store the results of previous runs in a file and then load them again to compare them. For example this is useful to compare what was recorded on the main branch against a branch with performance improvements. You may also use this to benchmark across different exlixir/erlang versions.
|
| 580 |
+
|
| 581 |
+
**Saving** is done through the `:save` configuration option. You can specify a `:path` where results are saved, or you can use the default option of`"benchmark.benchee"` if you don't pass a `:path`. You can also pass a `:tag` option which annotates these results (for instance with a branch name or elixir version number). The default option for the `:tag` is a timestamp of when the benchmark was run.
|
| 582 |
+
|
| 583 |
+
**Loading** is done through the `:load` option specifying a path to the file to load (for instance `"benchmark.benchee"`). You can also specify multiple files to load through a list of paths (`["my.benchee", "main_save.benchee"]`) - each one of those can also be a glob expression to match even more files glob (`"save_number*.benchee"`).
|
| 584 |
+
In the more verbose API loading is triggered via `Benchee.load/1`.
|
| 585 |
+
If all you want to do is to use `:load` without running any benchmarks then you can use `Benchee.report/1` which will take a normal configuration with a `:load` key and just format the loaded saved results with the given formatters.
|
| 586 |
+
|
| 587 |
+
```elixir
|
| 588 |
+
Benchee.run(
|
| 589 |
+
%{
|
| 590 |
+
"something_great" => fn -> cool_stuff end
|
| 591 |
+
},
|
| 592 |
+
save: [path: "save.benchee", tag: "first-try"]
|
| 593 |
+
)
|
| 594 |
+
|
| 595 |
+
Benchee.report(load: "save.benchee")
|
| 596 |
+
```
|
| 597 |
+
|
| 598 |
+
### Hooks (Setup, Teardown etc.)
|
| 599 |
+
|
| 600 |
+
Most of the time, it's best to keep your benchmarks as simple as possible: plain old immutable functions work best. But sometimes you need other things to happen. When you want to add before or after hooks to your benchmarks, we've got you covered! Before you dig into this section though remember one thing: **you usually don't need hooks!**
|
| 601 |
+
|
| 602 |
+
Benchee has three types of hooks:
|
| 603 |
+
|
| 604 |
+
* [Suite hooks](#suite-hooks)
|
| 605 |
+
* [Scenario hooks](#scenario-hooks)
|
| 606 |
+
* [Benchmarking function hooks](#benchmarking-function-hooks)
|
| 607 |
+
|
| 608 |
+
|
| 609 |
+
Of course, **hooks are not included in the measurements**. So they are there especially if you want to do something and want it to **not be included in the measurements**. Sadly there is the notable exception of _too_fast_functions_ (the ones that execute faster than we can measure in [_native_ resolution](#a-note-on-time-measurement-accuracy)). As we need to measure their repeated invocations to get halfway good measurements `before_each` and `after_each` hooks are included there. However, to the best of our knowledge this should only ever happen on Windows (because of the bad run time measurement accuracy).
|
| 610 |
+
|
| 611 |
+
#### Suite hooks
|
| 612 |
+
|
| 613 |
+
It is very easy in Benchee to do setup and teardown for the whole benchmarking suite (think: "before all" and "after all"). As Benchee is just plain old functions, just do your setup and teardown before/after you call Benchee:
|
| 614 |
+
|
| 615 |
+
```elixir
|
| 616 |
+
your_setup()
|
| 617 |
+
|
| 618 |
+
Benchee.run(%{"Awesome stuff" => fn -> magic end})
|
| 619 |
+
|
| 620 |
+
your_teardown()
|
| 621 |
+
```
|
| 622 |
+
|
| 623 |
+
_When might this be useful?_
|
| 624 |
+
|
| 625 |
+
* Seeding the database with data to be used by all benchmarking functions
|
| 626 |
+
* Starting/shutting down a server, process, other dependencies
|
| 627 |
+
|
| 628 |
+
#### Scenario hooks
|
| 629 |
+
|
| 630 |
+
For the following discussions, it's important to know what benchee considers a "benchmarking scenario".
|
| 631 |
+
|
| 632 |
+
##### What is a scenario?
|
| 633 |
+
|
| 634 |
+
A scenario is the combination of one benchmarking function and one input. So, given this benchmark:
|
| 635 |
+
|
| 636 |
+
```elixir
|
| 637 |
+
Benchee.run(
|
| 638 |
+
%{
|
| 639 |
+
"foo" => fn input -> ... end,
|
| 640 |
+
"bar" => fn input -> ... end
|
| 641 |
+
},
|
| 642 |
+
inputs: %{
|
| 643 |
+
"input 1" => 1,
|
| 644 |
+
"input 2" => 2
|
| 645 |
+
}
|
| 646 |
+
)
|
| 647 |
+
```
|
| 648 |
+
|
| 649 |
+
there are 4 scenarios:
|
| 650 |
+
|
| 651 |
+
1. foo with input 1
|
| 652 |
+
2. foo with input 2
|
| 653 |
+
3. bar with input 1
|
| 654 |
+
4. bar with input 2
|
| 655 |
+
|
| 656 |
+
A scenario includes warmup and actual run time (+ other measurements like memory).
|
| 657 |
+
|
| 658 |
+
##### before_scenario
|
| 659 |
+
|
| 660 |
+
Is executed before every [scenario](#what-is-a-scenario) that it applies to (see [hook configuration](#hook-configuration-global-versus-local)). `before_scenario` hooks take the input of the scenario as an argument.
|
| 661 |
+
|
| 662 |
+
Since the return value of a `before_scenario` becomes the input for next steps (see [hook arguments and return values](#hook-arguments-and-return-values)), there usually are 3 kinds of before scenarios:
|
| 663 |
+
|
| 664 |
+
* you just want to invoke a side effect: in that case return the given input unchanged
|
| 665 |
+
* you want to alter the given input: in that case alter the given input
|
| 666 |
+
* you want to keep the given input but add some other data: in that case return a tuple like `{original_input, new_fancy_data}`
|
| 667 |
+
|
| 668 |
+
For before scenario hooks, the _global_ hook is invoked first, then the _local_ (see [when does a hook happen?](#when-does-a-hook-happen-complete-example)).
|
| 669 |
+
|
| 670 |
+
Usage:
|
| 671 |
+
|
| 672 |
+
```elixir
|
| 673 |
+
Benchee.run(
|
| 674 |
+
%{
|
| 675 |
+
"foo" =>
|
| 676 |
+
{
|
| 677 |
+
fn {input, resource} -> foo(input, resource) end,
|
| 678 |
+
before_scenario: fn {input, resource} ->
|
| 679 |
+
resource = alter_resource(resource)
|
| 680 |
+
{input, resource}
|
| 681 |
+
end
|
| 682 |
+
},
|
| 683 |
+
"bar" =>
|
| 684 |
+
fn {input, resource} -> bar(input, resource) end
|
| 685 |
+
},
|
| 686 |
+
inputs: %{"input 1" => 1},
|
| 687 |
+
before_scenario: fn input ->
|
| 688 |
+
resource = start_expensive_resource()
|
| 689 |
+
{input, resource}
|
| 690 |
+
end
|
| 691 |
+
)
|
| 692 |
+
```
|
| 693 |
+
|
| 694 |
+
_When might this be useful?_
|
| 695 |
+
|
| 696 |
+
* Starting a process to be used in your scenario
|
| 697 |
+
* Recording the PID of `self()` for use in your benchmark (each scenario is executed in its own process, so scenario PIDs aren't available in functions running before the suite)
|
| 698 |
+
* Clearing the cache before a scenario
|
| 699 |
+
|
| 700 |
+
##### after_scenario
|
| 701 |
+
|
| 702 |
+
Is executed after a scenario has completed. After scenario hooks receive the return value of the last `before_scenario` that ran as an argument. The return value is discarded (see [hook arguments and return values](#hook-arguments-and-return-values)).
|
| 703 |
+
|
| 704 |
+
For after scenario hooks, the _local_ hook is invoked first, then the _global_ (see [when does a hook happen?](#when-does-a-hook-happen-complete-example)).
|
| 705 |
+
|
| 706 |
+
Usage:
|
| 707 |
+
|
| 708 |
+
```elixir
|
| 709 |
+
Benchee.run(
|
| 710 |
+
%{
|
| 711 |
+
"bar" => fn -> bar() end
|
| 712 |
+
},
|
| 713 |
+
after_scenario: fn _input -> bust_my_cache() end
|
| 714 |
+
)
|
| 715 |
+
```
|
| 716 |
+
|
| 717 |
+
_When might this be useful?_
|
| 718 |
+
|
| 719 |
+
* Busting caches after a scenario completed
|
| 720 |
+
* Deleting all the records in the database that the scenario just created
|
| 721 |
+
* Terminating whatever was setup by `before_scenario`
|
| 722 |
+
|
| 723 |
+
#### Benchmarking function hooks
|
| 724 |
+
|
| 725 |
+
You can also schedule hooks to run before and after each invocation of a benchmarking function.
|
| 726 |
+
|
| 727 |
+
##### before_each
|
| 728 |
+
|
| 729 |
+
Is executed before each invocation of the benchmarking function (before every measurement). Before each hooks receive the return value of their `before_scenario` hook as their argument. The return value of a before each hook becomes the input to the benchmarking function (see [hook arguments and return values](#hook-arguments-and-return-values)).
|
| 730 |
+
|
| 731 |
+
For before each hooks, the _global_ hook is invoked first, then the _local_ (see [when does a hook happen?](#when-does-a-hook-happen-complete-example)).
|
| 732 |
+
|
| 733 |
+
Usage:
|
| 734 |
+
|
| 735 |
+
```elixir
|
| 736 |
+
Benchee.run(
|
| 737 |
+
%{
|
| 738 |
+
"bar" => fn record -> bar(record) end
|
| 739 |
+
},
|
| 740 |
+
inputs: %{ "record id" => 1},
|
| 741 |
+
before_each: fn input -> get_from_db(input) end
|
| 742 |
+
)
|
| 743 |
+
```
|
| 744 |
+
|
| 745 |
+
_When might this be useful?_
|
| 746 |
+
|
| 747 |
+
* Retrieving a record from the database and passing it on to the benchmarking function to do _something(tm)_ without the retrieval from the database adding to the benchmark measurement
|
| 748 |
+
* Busting caches so that all measurements are taken in an uncached state
|
| 749 |
+
* Picking a random value from a collection and passing it to the benchmarking function for measuring performance with a wider spread of values
|
| 750 |
+
* You could also use this to benchmark with random data generated by tools like [StreamData](https://github.com/whatyouhide/stream_data), Devon shows how it's done [here](https://devonestes.com/benchmarking-with-stream-data)
|
| 751 |
+
|
| 752 |
+
##### after_each
|
| 753 |
+
|
| 754 |
+
Is executed directly after the invocation of the benchmarking function. After each hooks receive the return value of the benchmarking function as their argument. The return value is discarded (see [hook arguments and return values](#hook-arguments-and-return-values)).
|
| 755 |
+
|
| 756 |
+
For after each hooks, the _local_ hook is invoked first, then the _global_ (see [when does a hook happen?](#when-does-a-hook-happen-complete-example)).
|
| 757 |
+
|
| 758 |
+
Usage:
|
| 759 |
+
|
| 760 |
+
```elixir
|
| 761 |
+
Benchee.run(
|
| 762 |
+
%{
|
| 763 |
+
"bar" => fn input -> bar(input) end
|
| 764 |
+
},
|
| 765 |
+
inputs: %{ "input 1" => 1},
|
| 766 |
+
after_each: fn result -> assert result == 42 end
|
| 767 |
+
)
|
| 768 |
+
```
|
| 769 |
+
|
| 770 |
+
_When might this be useful?_
|
| 771 |
+
|
| 772 |
+
* Running assertions that whatever your benchmarking function returns is truly what it should be (i.e. all "contestants" work as expected)
|
| 773 |
+
* Busting caches/terminating processes setup in `before_each` or elsewhere
|
| 774 |
+
* Deleting files created by the benchmarking function
|
| 775 |
+
|
| 776 |
+
#### Hook arguments and return values
|
| 777 |
+
|
| 778 |
+
Before hooks form a chain, where the return value of the previous hook becomes the argument for the next one. The first defined `before` hook receives the scenario input as an argument, and returns a value that becomes the argument of the next in the chain. The benchmarking function receives the value of the last `before` hook as its argument (or the scenario input if there are no `before` hooks).
|
| 779 |
+
|
| 780 |
+
After hooks do not form a chain, and their return values are discarded. An `after_each` hook receives the return value of the benchmarking function as its argument. An `after_scenario` function receives the return value of the last `before_scenario` that ran (or the scenario's input if there is no `before_scenario` hook).
|
| 781 |
+
|
| 782 |
+
If you haven't defined any inputs, the hook chain is started with the special input argument returned by `Benchee.Benchmark.no_input()`.
|
| 783 |
+
|
| 784 |
+
#### Hook configuration: global versus local
|
| 785 |
+
|
| 786 |
+
Hooks can be defined either _globally_ as part of the configuration or _locally_ for a specific benchmarking function. Global hooks will be executed for every scenario in the suite. Local hooks will only be executed for scenarios involving that benchmarking function.
|
| 787 |
+
|
| 788 |
+
**Global hooks**
|
| 789 |
+
|
| 790 |
+
Global hooks are specified as part of the general benchee configuration:
|
| 791 |
+
|
| 792 |
+
```elixir
|
| 793 |
+
Benchee.run(
|
| 794 |
+
%{
|
| 795 |
+
"foo" => fn input -> ... end,
|
| 796 |
+
"bar" => fn input -> ... end
|
| 797 |
+
},
|
| 798 |
+
inputs: %{
|
| 799 |
+
"input 1" => 1,
|
| 800 |
+
"input 2" => 2,
|
| 801 |
+
},
|
| 802 |
+
before_scenario: fn input -> ... end
|
| 803 |
+
)
|
| 804 |
+
```
|
| 805 |
+
|
| 806 |
+
Here the `before_scenario` function will be executed for all 4 scenarios present in this benchmarking suite.
|
| 807 |
+
|
| 808 |
+
**Local hooks**
|
| 809 |
+
|
| 810 |
+
Local hooks are defined alongside the benchmarking function. To define a local hook, pass a tuple in the initial map, instead of just a single function. The benchmarking function comes first, followed by a keyword list specifying the hooks to run:
|
| 811 |
+
|
| 812 |
+
```elixir
|
| 813 |
+
Benchee.run(
|
| 814 |
+
%{
|
| 815 |
+
"foo" => {fn input -> ... end, before_scenario: fn input -> ... end},
|
| 816 |
+
"bar" => fn input -> ... end
|
| 817 |
+
},
|
| 818 |
+
inputs: %{
|
| 819 |
+
"input 1" => 1,
|
| 820 |
+
"input 2" => 2
|
| 821 |
+
}
|
| 822 |
+
)
|
| 823 |
+
```
|
| 824 |
+
|
| 825 |
+
Here `before_scenario` is only run for the 2 scenarios associated with `"foo"`, i.e. foo with input 1 and foo with input 2. It is _not_ run for any `"bar"` benchmarks.
|
| 826 |
+
|
| 827 |
+
|
| 828 |
+
#### When does a hook happen? (Complete Example)
|
| 829 |
+
|
| 830 |
+
Yes the whole hooks system is quite a lot to take in. Here is an overview showing the order of hook execution, along with the argument each hook receives (see [hook arguments and return values](#hook-arguments-and-return-values)). The guiding principle whether _local_ or _global_ is run first is that _local_ always executes closer to the benchmarking function.
|
| 831 |
+
|
| 832 |
+
Given the following code:
|
| 833 |
+
|
| 834 |
+
```elixir
|
| 835 |
+
|
| 836 |
+
suite_set_up()
|
| 837 |
+
|
| 838 |
+
Benchee.run(
|
| 839 |
+
%{
|
| 840 |
+
"foo" =>
|
| 841 |
+
{
|
| 842 |
+
fn input -> foo(input) end,
|
| 843 |
+
before_scenario: fn input ->
|
| 844 |
+
local_before_scenario(input)
|
| 845 |
+
input + 1
|
| 846 |
+
end,
|
| 847 |
+
before_each: fn input ->
|
| 848 |
+
local_before_each(input)
|
| 849 |
+
input + 1
|
| 850 |
+
end,
|
| 851 |
+
after_each: fn value ->
|
| 852 |
+
local_after_each(value)
|
| 853 |
+
end,
|
| 854 |
+
after_scenario: fn input ->
|
| 855 |
+
local_after_scenario(input)
|
| 856 |
+
end
|
| 857 |
+
},
|
| 858 |
+
"bar" =>
|
| 859 |
+
fn input -> bar(input) end
|
| 860 |
+
},
|
| 861 |
+
inputs: %{"input 1" => 1},
|
| 862 |
+
before_scenario: fn input ->
|
| 863 |
+
global_before_scenario(input)
|
| 864 |
+
input + 1
|
| 865 |
+
end,
|
| 866 |
+
before_each: fn input ->
|
| 867 |
+
global_before_each(input)
|
| 868 |
+
input + 1
|
| 869 |
+
end,
|
| 870 |
+
after_each: fn value ->
|
| 871 |
+
global_after_each(value)
|
| 872 |
+
end,
|
| 873 |
+
after_scenario: fn input ->
|
| 874 |
+
global_after_scenario(input)
|
| 875 |
+
end
|
| 876 |
+
)
|
| 877 |
+
|
| 878 |
+
suite_tear_down()
|
| 879 |
+
```
|
| 880 |
+
|
| 881 |
+
Keeping in mind that the order of foo and bar could be different, here is how the hooks are called:
|
| 882 |
+
|
| 883 |
+
```
|
| 884 |
+
suite_set_up
|
| 885 |
+
|
| 886 |
+
# starting with the foo scenario
|
| 887 |
+
global_before_scenario(1)
|
| 888 |
+
local_before_scenario(2) # as before_scenario incremented it
|
| 889 |
+
|
| 890 |
+
global_before_each(3)
|
| 891 |
+
local_before_each(4)
|
| 892 |
+
foo(5) # let's say foo(5) returns 6
|
| 893 |
+
local_after_each(6)
|
| 894 |
+
global_after_each(6)
|
| 895 |
+
|
| 896 |
+
global_before_each(3)
|
| 897 |
+
local_before_each(4)
|
| 898 |
+
foo(5) # let's say foo(5) returns 6
|
| 899 |
+
local_after_each(6)
|
| 900 |
+
global_after_each(6)
|
| 901 |
+
|
| 902 |
+
# ... this block is repeated as many times as benchee has time
|
| 903 |
+
|
| 904 |
+
local_after_scenario(3)
|
| 905 |
+
global_after_scenario(3)
|
| 906 |
+
|
| 907 |
+
# now it's time for the bar scenario, it has no hooks specified for itself
|
| 908 |
+
# so only the global hooks are run
|
| 909 |
+
|
| 910 |
+
global_before_scenario(1)
|
| 911 |
+
|
| 912 |
+
global_before_each(2)
|
| 913 |
+
bar(3) # let's say foo(3) returns 4
|
| 914 |
+
global_after_each(4)
|
| 915 |
+
|
| 916 |
+
global_before_each(2)
|
| 917 |
+
bar(3) # let's say foo(3) returns 4
|
| 918 |
+
global_after_each(4)
|
| 919 |
+
|
| 920 |
+
# ... this block is repeated as many times as benchee has time
|
| 921 |
+
|
| 922 |
+
global_after_scenario(2)
|
| 923 |
+
|
| 924 |
+
suite_tear_down
|
| 925 |
+
```
|
| 926 |
+
|
| 927 |
+
### More verbose usage
|
| 928 |
+
|
| 929 |
+
It is important to note that the benchmarking code shown in the beginning is the convenience interface. The same benchmark in its more verbose form looks like this:
|
| 930 |
+
|
| 931 |
+
```elixir
|
| 932 |
+
list = Enum.to_list(1..10_000)
|
| 933 |
+
map_fun = fn i -> [i, i * i] end
|
| 934 |
+
|
| 935 |
+
[time: 3]
|
| 936 |
+
|> Benchee.init()
|
| 937 |
+
|> Benchee.system()
|
| 938 |
+
|> Benchee.benchmark("flat_map", fn -> Enum.flat_map(list, map_fun) end)
|
| 939 |
+
|> Benchee.benchmark(
|
| 940 |
+
"map.flatten",
|
| 941 |
+
fn -> list |> Enum.map(map_fun) |> List.flatten() end
|
| 942 |
+
)
|
| 943 |
+
|> Benchee.collect()
|
| 944 |
+
|> Benchee.statistics()
|
| 945 |
+
|> Benchee.relative_statistics()
|
| 946 |
+
|> Benchee.Formatter.output(Benchee.Formatters.Console)
|
| 947 |
+
# Instead of the last call you could also just use Benchee.Formatter.output()
|
| 948 |
+
# to just output all configured formatters
|
| 949 |
+
```
|
| 950 |
+
|
| 951 |
+
This is a take on the _functional transformation_ of data applied to benchmarks:
|
| 952 |
+
|
| 953 |
+
1. Configure the benchmarking suite to be run
|
| 954 |
+
2. Gather System data
|
| 955 |
+
3. Define the functions to be benchmarked
|
| 956 |
+
4. Run benchmarks with the given configuration gathering raw run times per function
|
| 957 |
+
5. Calculate statistics based on the raw run times
|
| 958 |
+
6. Calculate statistics between the scenarios (faster/slower...)
|
| 959 |
+
7. Format the statistics in a suitable way and print them out
|
| 960 |
+
|
| 961 |
+
This is also part of the **official API** and allows for more **fine grained control**. (It's also what Benchee does internally when you use `Benchee.run/2`).
|
| 962 |
+
|
| 963 |
+
Do you just want to have all the raw run times? Just work with the result of `Benchee.collect/1`! Just want to have the calculated statistics and use your own formatting? Grab the result of `Benchee.statistics/1`! Or, maybe you want to write to a file or send an HTTP post to some online service? Just grab the complete suite after statistics were generated.
|
| 964 |
+
|
| 965 |
+
It also allows you to alter behaviour, normally `Benchee.load/1` is called right before the formatters so that neither the benchmarks are run again or statistics are computed again. However, you might want to run the benchmarks again or recompute the statistics. Then you can call `Benchee.load/1` right at the start.
|
| 966 |
+
|
| 967 |
+
This way Benchee should be flexible enough to suit your needs and be extended at will. Have a look at the [available plugins](#plugins).
|
| 968 |
+
|
| 969 |
+
### Usage from Erlang
|
| 970 |
+
|
| 971 |
+
Before you dig deep into this, it is inherently easier to setup a small elixir project, add a dependency to your erlang project and then run the benchmarks from elixir. The reason is easy - mix knows about rebar3 and knows how to work with it. The reverse isn't true so the road ahead is somewhat bumpy.
|
| 972 |
+
|
| 973 |
+
There is an [example project](https://github.com/bencheeorg/benchee_erlang_try) to check out.
|
| 974 |
+
|
| 975 |
+
You can use the [rebar3_elixir_compile](https://github.com/barrel-db/rebar3_elixir_compile) plugin. In your `rebar.config` you can do the following which should get you started:
|
| 976 |
+
|
| 977 |
+
```erlang
|
| 978 |
+
deps, [
|
| 979 |
+
{benchee, {elixir, "benchee", "0.9.0"}}
|
| 980 |
+
]}.
|
| 981 |
+
|
| 982 |
+
{plugins, [
|
| 983 |
+
{ rebar3_elixir_compile, ".*", {git, "https://github.com/barrel-db/rebar3_elixir_compile.git", {branch, "main"}}}
|
| 984 |
+
]}.
|
| 985 |
+
|
| 986 |
+
{provider_hooks, [
|
| 987 |
+
{pre, [{compile, {ex, compile}}]},
|
| 988 |
+
{pre, [{release, {ex, compile}}]}
|
| 989 |
+
]}.
|
| 990 |
+
|
| 991 |
+
{elixir_opts,
|
| 992 |
+
[
|
| 993 |
+
{env, dev}
|
| 994 |
+
]
|
| 995 |
+
}.
|
| 996 |
+
```
|
| 997 |
+
|
| 998 |
+
Then benchee already provides a `:benchee` interface for erlang compatibility which you can use. Sadly couldn't get it to work in an escript yet.
|
| 999 |
+
|
| 1000 |
+
You can then invoke it like this (for instance):
|
| 1001 |
+
|
| 1002 |
+
```
|
| 1003 |
+
tobi@comfy ~/github/benchee_erlang_try $ rebar3 shell
|
| 1004 |
+
===> dependencies etc.
|
| 1005 |
+
Erlang/OTP 18 [erts-7.3] [source] [64-bit] [smp:4:4] [async-threads:0] [hipe] [kernel-poll:false]
|
| 1006 |
+
|
| 1007 |
+
Eshell V7.3 (abort with ^G)
|
| 1008 |
+
1> benchee:run(#{myFunc => fun() -> lists:sort([8, 2, 3, 4, 2, 1, 3, 4, 9, 10, 11, 12, 13, 20, 1000, -4, -5]) end}, [{warmup, 0}, {time, 2}]).
|
| 1009 |
+
Operating System: Linux
|
| 1010 |
+
CPU Information: Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz
|
| 1011 |
+
Number of Available Cores: 4
|
| 1012 |
+
Available memory: 8.05 GB
|
| 1013 |
+
Elixir 1.3.4
|
| 1014 |
+
Erlang 18.3
|
| 1015 |
+
Benchmark suite executing with the following configuration:
|
| 1016 |
+
warmup: 0.0 μs
|
| 1017 |
+
time: 2 s
|
| 1018 |
+
parallel: 1
|
| 1019 |
+
inputs: none specified
|
| 1020 |
+
Estimated total run time: 2 s
|
| 1021 |
+
|
| 1022 |
+
|
| 1023 |
+
Benchmarking myFunc...
|
| 1024 |
+
|
| 1025 |
+
Name ips average deviation median
|
| 1026 |
+
myFunc 289.71 K 3.45 μs ±250.31% 3 μs
|
| 1027 |
+
```
|
| 1028 |
+
|
| 1029 |
+
This doesn't seem to be too reliable right now, so suggestions and input are very welcome :)
|
| 1030 |
+
|
| 1031 |
+
## Known Issues
|
| 1032 |
+
|
| 1033 |
+
There is a known issue affecting elixir versions from 1.14.0 to 1.16.0-rc.0: Optimizations (SSA and bool passes, [see the original change](https://github.com/elixir-lang/elixir/pull/11420)) had been disabled affecting the performance of functions defined directly in the top level (i.e. outside of any module). The issue was fixed by re-enabling the optimization in [1.16.0-rc.1](https://github.com/elixir-lang/elixir/blob/v1.16/CHANGELOG.md#v1160-rc1-2023-12-12).
|
| 1034 |
+
The issue is best show-cased by the following benchmark where we'd expect ~equal results:
|
| 1035 |
+
|
| 1036 |
+
```elixir
|
| 1037 |
+
list = Enum.to_list(1..10_000)
|
| 1038 |
+
|
| 1039 |
+
defmodule Compiled do
|
| 1040 |
+
def comprehension(list) do
|
| 1041 |
+
for x <- list, rem(x, 2) == 1, do: x + 1
|
| 1042 |
+
end
|
| 1043 |
+
end
|
| 1044 |
+
|
| 1045 |
+
Benchee.run(%{
|
| 1046 |
+
"module (optimized)" => fn -> Compiled.comprehension(list) end,
|
| 1047 |
+
"top_level (non-optimized)" => fn -> for x <- list, rem(x, 2) == 1, do: x + 1 end
|
| 1048 |
+
})
|
| 1049 |
+
```
|
| 1050 |
+
|
| 1051 |
+
Which yields ~these results on an affected elixir version:
|
| 1052 |
+
|
| 1053 |
+
```
|
| 1054 |
+
Comparison:
|
| 1055 |
+
module (optimized) 18.24 K
|
| 1056 |
+
top_level (non-optimized) 11.91 K - 1.53x slower +29.14 μs
|
| 1057 |
+
```
|
| 1058 |
+
|
| 1059 |
+
So, how do you fix it/make sure a benchmark you ran is not affected? All of these work:
|
| 1060 |
+
|
| 1061 |
+
* benchmark on an unaffected/fixed version of elixir (<= 1.13.4 or >= 1.16.0-rc.1)
|
| 1062 |
+
* put the code you want to benchmark into a module (just like it is done in `Compiled` in the example above)
|
| 1063 |
+
* you can also invoke benchee from within a module, such as:
|
| 1064 |
+
|
| 1065 |
+
```elixir
|
| 1066 |
+
defmodule Compiled do
|
| 1067 |
+
def comprehension(list) do
|
| 1068 |
+
for x <- list, rem(x, 2) == 1, do: x + 1
|
| 1069 |
+
end
|
| 1070 |
+
end
|
| 1071 |
+
|
| 1072 |
+
defmodule MyBenchmark do
|
| 1073 |
+
def run do
|
| 1074 |
+
list = Enum.to_list(1..10_000)
|
| 1075 |
+
|
| 1076 |
+
Benchee.run(%{
|
| 1077 |
+
"module (optimized)" => fn -> Compiled.comprehension(list) end,
|
| 1078 |
+
"top_level (non-optimized)" => fn -> for x <- list, rem(x, 2) == 1, do: x + 1 end
|
| 1079 |
+
})
|
| 1080 |
+
end
|
| 1081 |
+
end
|
| 1082 |
+
|
| 1083 |
+
MyBenchmark.run()
|
| 1084 |
+
```
|
| 1085 |
+
|
| 1086 |
+
Also note that even if all your examples are top level functions you should still follow these tips (on affected elixir versions), as the missing optimization might affect them differently. Further note, that even though your examples use top level functions they _may_ not be affected, as the specific disabled optimization may not impact them. Better safe than sorry though :)
|
| 1087 |
+
|
| 1088 |
+
## Plugins
|
| 1089 |
+
|
| 1090 |
+
Benchee only has small runtime dependencies that were initially extracted from it. Further functionality is provided through plugins that then pull in dependencies, such as HTML generation and CSV export. They help provide excellent visualization or interoperability.
|
| 1091 |
+
|
| 1092 |
+
* [benchee_html](//github.com/bencheeorg/benchee_html) - generate HTML including a data table and many different graphs with the possibility to export individual graphs as PNG :)
|
| 1093 |
+
* [benchee_csv](//github.com/bencheeorg/benchee_csv) - generate CSV from your benchee benchmark results so you can import them into your favorite spreadsheet tool and make fancy graphs
|
| 1094 |
+
* [benchee_json](//github.com/bencheeorg/benchee_json) - export suite results as JSON to feed anywhere or feed it to your JavaScript and make magic happen :)
|
| 1095 |
+
* [benchee_markdown](//github.com/hrzndhrn/benchee_markdown) - write markdown files containing your benchmarking results
|
| 1096 |
+
|
| 1097 |
+
With the HTML plugin for instance you can get fancy graphs like this boxplot:
|
| 1098 |
+
|
| 1099 |
+

|
| 1100 |
+
|
| 1101 |
+
Of course there also are normal bar charts including standard deviation:
|
| 1102 |
+
|
| 1103 |
+

|
| 1104 |
+
|
| 1105 |
+
## Contributing [](https://www.codetriage.com/pragtob/benchee)
|
| 1106 |
+
|
| 1107 |
+
Contributions to Benchee are **very welcome**! Bug reports, documentation, spelling corrections, whole features, feature ideas, bugfixes, new plugins, fancy graphics... all of those (and probably more) are much appreciated contributions!
|
| 1108 |
+
|
| 1109 |
+
Keep in mind that the [plugins](#plugins) live in their own repositories with their own issue tracker and they also like to get contributions :)
|
| 1110 |
+
|
| 1111 |
+
Please respect the [Code of Conduct](//github.com/bencheeorg/benchee/blob/main/CODE_OF_CONDUCT.md).
|
| 1112 |
+
|
| 1113 |
+
In addition to contributing code, you can help to triage issues. This can include reproducing bug reports, or asking for vital information such as version numbers or reproduction instructions. If you would like to start triaging issues, one easy way to get started is to [subscribe to pragtob/benchee on CodeTriage](https://www.codetriage.com/pragtob/benchee).
|
| 1114 |
+
|
| 1115 |
+
You can also look directly at the [open issues](https://github.com/bencheeorg/benchee/issues). There are `help wanted` and `good first issue` labels - those are meant as guidance, of course other issues can be tackled :)
|
| 1116 |
+
|
| 1117 |
+
A couple of (hopefully) helpful points:
|
| 1118 |
+
|
| 1119 |
+
* Feel free to ask for help and guidance on an issue/PR ("How can I implement this?", "How could I test this?", ...)
|
| 1120 |
+
* Feel free to open early/not yet complete pull requests to get some early feedback
|
| 1121 |
+
* When in doubt if something is a good idea open an issue first to discuss it
|
| 1122 |
+
* In case I don't respond feel free to bump the issue/PR or ping me in other places
|
| 1123 |
+
|
| 1124 |
+
If you're on the [elixir-lang slack](https://elixir-lang.slack.com) also feel free to drop by in `#benchee` and say hi!
|
| 1125 |
+
|
| 1126 |
+
## Development
|
| 1127 |
+
|
| 1128 |
+
Note that if the change includes adding new statistics you might need to introduce them to our statistics library [Statistex](https://github.com/bencheeorg/statistex) first!
|
| 1129 |
+
|
| 1130 |
+
For an overview of benchee's architecture, check out [architecture](./architecture/README.md)!
|
| 1131 |
+
|
| 1132 |
+
* `mix deps.get` to install dependencies
|
| 1133 |
+
* `mix test` to run tests
|
| 1134 |
+
* `mix dialyzer` to run dialyzer for type checking, might take a while on the first invocation (try building plts first with `mix dialyzer --plt`)
|
| 1135 |
+
* `mix credo` to find code style problems
|
| 1136 |
+
* or run `mix guard` to run all of them continuously on file change
|
| 1137 |
+
|
| 1138 |
+
|
| 1139 |
+
## Copyright and License
|
| 1140 |
+
|
| 1141 |
+
Copyright (c) 2016 Tobias Pfeiffer
|
| 1142 |
+
|
| 1143 |
+
This work is free. You can redistribute it and/or modify it under the
|
| 1144 |
+
terms of the MIT License. See the [LICENSE.md](./LICENSE.md) file for more details.
|
deps/benchee/hex_metadata.config
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{<<"links">>,
|
| 2 |
+
[{<<"Blog posts">>,<<"https://pragtob.wordpress.com/tag/benchee/">>},
|
| 3 |
+
{<<"Changelog">>,<<"https://hexdocs.pm/benchee/changelog.html">>},
|
| 4 |
+
{<<"GitHub">>,<<"https://github.com/bencheeorg/benchee">>}]}.
|
| 5 |
+
{<<"name">>,<<"benchee">>}.
|
| 6 |
+
{<<"version">>,<<"1.5.0">>}.
|
| 7 |
+
{<<"description">>,
|
| 8 |
+
<<"Versatile (micro) benchmarking that is extensible. Get statistics such as:\naverage, iterations per second, standard deviation and the median.">>}.
|
| 9 |
+
{<<"elixir">>,<<"~> 1.6">>}.
|
| 10 |
+
{<<"app">>,<<"benchee">>}.
|
| 11 |
+
{<<"licenses">>,[<<"MIT">>]}.
|
| 12 |
+
{<<"requirements">>,
|
| 13 |
+
[[{<<"name">>,<<"table">>},
|
| 14 |
+
{<<"app">>,<<"table">>},
|
| 15 |
+
{<<"optional">>,true},
|
| 16 |
+
{<<"requirement">>,<<"~> 0.1.0">>},
|
| 17 |
+
{<<"repository">>,<<"hexpm">>}],
|
| 18 |
+
[{<<"name">>,<<"deep_merge">>},
|
| 19 |
+
{<<"app">>,<<"deep_merge">>},
|
| 20 |
+
{<<"optional">>,false},
|
| 21 |
+
{<<"requirement">>,<<"~> 1.0">>},
|
| 22 |
+
{<<"repository">>,<<"hexpm">>}],
|
| 23 |
+
[{<<"name">>,<<"statistex">>},
|
| 24 |
+
{<<"app">>,<<"statistex">>},
|
| 25 |
+
{<<"optional">>,false},
|
| 26 |
+
{<<"requirement">>,<<"~> 1.1">>},
|
| 27 |
+
{<<"repository">>,<<"hexpm">>}]]}.
|
| 28 |
+
{<<"files">>,
|
| 29 |
+
[<<"lib">>,<<"lib/benchee">>,<<"lib/benchee/conversion.ex">>,
|
| 30 |
+
<<"lib/benchee/scenario.ex">>,<<"lib/benchee/suite.ex">>,
|
| 31 |
+
<<"lib/benchee/configuration.ex">>,<<"lib/benchee/formatter.ex">>,
|
| 32 |
+
<<"lib/benchee/system.ex">>,<<"lib/benchee/profile.ex">>,
|
| 33 |
+
<<"lib/benchee/conversion">>,<<"lib/benchee/conversion/memory.ex">>,
|
| 34 |
+
<<"lib/benchee/conversion/unit.ex">>,
|
| 35 |
+
<<"lib/benchee/conversion/duration.ex">>,
|
| 36 |
+
<<"lib/benchee/conversion/scale.ex">>,<<"lib/benchee/conversion/count.ex">>,
|
| 37 |
+
<<"lib/benchee/conversion/deviation_percent.ex">>,
|
| 38 |
+
<<"lib/benchee/conversion/format.ex">>,<<"lib/benchee/utility">>,
|
| 39 |
+
<<"lib/benchee/utility/deep_convert.ex">>,
|
| 40 |
+
<<"lib/benchee/utility/repeat_n.ex">>,<<"lib/benchee/utility/parallel.ex">>,
|
| 41 |
+
<<"lib/benchee/utility/erlang_version.ex">>,
|
| 42 |
+
<<"lib/benchee/utility/file_creation.ex">>,
|
| 43 |
+
<<"lib/benchee/scenario_loader.ex">>,<<"lib/benchee/statistics.ex">>,
|
| 44 |
+
<<"lib/benchee/collection_data.ex">>,
|
| 45 |
+
<<"lib/benchee/relative_statistics.ex">>,<<"lib/benchee/errors.ex">>,
|
| 46 |
+
<<"lib/benchee/benchmark.ex">>,<<"lib/benchee/output">>,
|
| 47 |
+
<<"lib/benchee/output/progress_printer.ex">>,
|
| 48 |
+
<<"lib/benchee/output/profile_printer.ex">>,
|
| 49 |
+
<<"lib/benchee/output/benchmark_printer.ex">>,<<"lib/benchee/formatters">>,
|
| 50 |
+
<<"lib/benchee/formatters/console">>,
|
| 51 |
+
<<"lib/benchee/formatters/console/run_time.ex">>,
|
| 52 |
+
<<"lib/benchee/formatters/console/helpers.ex">>,
|
| 53 |
+
<<"lib/benchee/formatters/console/memory.ex">>,
|
| 54 |
+
<<"lib/benchee/formatters/console/reductions.ex">>,
|
| 55 |
+
<<"lib/benchee/formatters/console.ex">>,
|
| 56 |
+
<<"lib/benchee/formatters/tagged_save.ex">>,<<"lib/benchee/benchmark">>,
|
| 57 |
+
<<"lib/benchee/benchmark/runner.ex">>,
|
| 58 |
+
<<"lib/benchee/benchmark/scenario_context.ex">>,
|
| 59 |
+
<<"lib/benchee/benchmark/collect.ex">>,
|
| 60 |
+
<<"lib/benchee/benchmark/repeated_measurement.ex">>,
|
| 61 |
+
<<"lib/benchee/benchmark/function_call_overhead.ex">>,
|
| 62 |
+
<<"lib/benchee/benchmark/collect">>,
|
| 63 |
+
<<"lib/benchee/benchmark/collect/memory.ex">>,
|
| 64 |
+
<<"lib/benchee/benchmark/collect/reductions.ex">>,
|
| 65 |
+
<<"lib/benchee/benchmark/collect/time.ex">>,
|
| 66 |
+
<<"lib/benchee/benchmark/hooks.ex">>,
|
| 67 |
+
<<"lib/benchee/benchmark/benchmark_config.ex">>,<<"lib/benchee.ex">>,
|
| 68 |
+
<<".formatter.exs">>,<<"mix.exs">>,<<"README.md">>,<<"LICENSE.md">>,
|
| 69 |
+
<<"CHANGELOG.md">>]}.
|
| 70 |
+
{<<"build_tools">>,[<<"mix">>]}.
|
deps/benchee/lib/benchee.ex
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Idea from this:
|
| 2 |
+
# credo:disable-for-next-line
|
| 3 |
+
# https://github.com/bencheeorg/benchee/commit/b3ddbc132e641cdf1eec0928b322ced1dab8553f#commitcomment-23381474
|
| 4 |
+
|
| 5 |
+
elixir_doc = """
|
| 6 |
+
Top level module providing convenience access to needed functions as well
|
| 7 |
+
as the very high level `Benchee.run` API.
|
| 8 |
+
|
| 9 |
+
Intended Elixir interface.
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
erlang_doc = """
|
| 13 |
+
High-Level interface for more convenient usage from Erlang. Same as `Benchee`.
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
+
for {module, moduledoc} <- [{Benchee, elixir_doc}, {:benchee, erlang_doc}] do
|
| 17 |
+
defmodule module do
|
| 18 |
+
@moduledoc moduledoc
|
| 19 |
+
|
| 20 |
+
alias Benchee.Formatter
|
| 21 |
+
|
| 22 |
+
@doc """
|
| 23 |
+
Run benchmark jobs defined by a map and optionally provide configuration
|
| 24 |
+
options.
|
| 25 |
+
|
| 26 |
+
Benchmarks are defined as a map where the keys are a name for the given
|
| 27 |
+
function and the values are the functions to benchmark. Users can configure
|
| 28 |
+
the run by passing a keyword list as the second argument. For more
|
| 29 |
+
information on configuration see `Benchee.Configuration.init/1`.
|
| 30 |
+
|
| 31 |
+
## Examples
|
| 32 |
+
|
| 33 |
+
Benchee.run(
|
| 34 |
+
%{
|
| 35 |
+
"My Benchmark" => fn -> 1 + 1 end,
|
| 36 |
+
"My other benchmrk" => fn -> [1] ++ [1] end
|
| 37 |
+
},
|
| 38 |
+
warmup: 2,
|
| 39 |
+
time: 3
|
| 40 |
+
)
|
| 41 |
+
"""
|
| 42 |
+
@spec run(map, keyword) :: Benchee.Suite.t()
|
| 43 |
+
def run(jobs, config \\ []) when is_list(config) do
|
| 44 |
+
config
|
| 45 |
+
|> Benchee.init()
|
| 46 |
+
|> Benchee.system()
|
| 47 |
+
|> add_benchmarking_jobs(jobs)
|
| 48 |
+
|> Benchee.collect()
|
| 49 |
+
|> Benchee.statistics()
|
| 50 |
+
|> Benchee.load()
|
| 51 |
+
|> Benchee.relative_statistics()
|
| 52 |
+
|> Formatter.output()
|
| 53 |
+
|> Benchee.profile()
|
| 54 |
+
end
|
| 55 |
+
|
| 56 |
+
defp add_benchmarking_jobs(suite, jobs) do
|
| 57 |
+
Enum.reduce(jobs, suite, fn {key, function}, suite_acc ->
|
| 58 |
+
Benchee.benchmark(suite_acc, key, function)
|
| 59 |
+
end)
|
| 60 |
+
end
|
| 61 |
+
|
| 62 |
+
@doc """
|
| 63 |
+
A convenience function designed for loading saved benchmarks and running formatters on them.
|
| 64 |
+
|
| 65 |
+
Basically takes the input of the map of jobs away from you and skips unnecessary steps with
|
| 66 |
+
that data missing (aka not running benchmarks, only running relative statistics).
|
| 67 |
+
|
| 68 |
+
You can use config options as normal, but some options related to benchmarking won't take
|
| 69 |
+
effect (such as `:time`). The `:load` option however is mandatory to use, as you need to
|
| 70 |
+
load some benchmarks to report on them.
|
| 71 |
+
|
| 72 |
+
## Usage
|
| 73 |
+
|
| 74 |
+
Benchee.report(load: ["benchmark-*.benchee"])
|
| 75 |
+
"""
|
| 76 |
+
@spec report(keyword) :: Benchee.Suite.t()
|
| 77 |
+
def report(config) do
|
| 78 |
+
if !Access.get(config, :load), do: raise_missing_load()
|
| 79 |
+
|
| 80 |
+
config
|
| 81 |
+
|> Benchee.init()
|
| 82 |
+
|> Benchee.system()
|
| 83 |
+
|> Benchee.load()
|
| 84 |
+
|> Benchee.relative_statistics()
|
| 85 |
+
|> Formatter.output()
|
| 86 |
+
end
|
| 87 |
+
|
| 88 |
+
defp raise_missing_load do
|
| 89 |
+
raise ArgumentError,
|
| 90 |
+
"You need to provide at least a :load option for report/1 to make sense"
|
| 91 |
+
end
|
| 92 |
+
|
| 93 |
+
defdelegate init(), to: Benchee.Configuration
|
| 94 |
+
defdelegate init(config), to: Benchee.Configuration
|
| 95 |
+
defdelegate system(suite), to: Benchee.System
|
| 96 |
+
defdelegate benchmark(suite, name, function), to: Benchee.Benchmark
|
| 97 |
+
@doc false
|
| 98 |
+
defdelegate benchmark(suite, name, function, printer), to: Benchee.Benchmark
|
| 99 |
+
defdelegate collect(suite), to: Benchee.Benchmark
|
| 100 |
+
@doc false
|
| 101 |
+
defdelegate collect(suite, printer), to: Benchee.Benchmark
|
| 102 |
+
defdelegate statistics(suite), to: Benchee.Statistics
|
| 103 |
+
@doc false
|
| 104 |
+
defdelegate statistics(suite, printer), to: Benchee.Statistics
|
| 105 |
+
defdelegate relative_statistics(suite), to: Benchee.RelativeStatistics
|
| 106 |
+
defdelegate load(suite), to: Benchee.ScenarioLoader
|
| 107 |
+
defdelegate profile(suite), to: Benchee.Profile
|
| 108 |
+
end
|
| 109 |
+
end
|
deps/benchee/lib/benchee/benchmark.ex
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
defmodule Benchee.Benchmark do
|
| 2 |
+
@moduledoc """
|
| 3 |
+
Functions related to building and running benchmarking scenarios.
|
| 4 |
+
Exposes `benchmark/4` and `collect/3` functions.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
alias Benchee.Benchmark.{BenchmarkConfig, Runner, ScenarioContext}
|
| 8 |
+
alias Benchee.Output.BenchmarkPrinter, as: Printer
|
| 9 |
+
alias Benchee.Scenario
|
| 10 |
+
alias Benchee.Suite
|
| 11 |
+
alias Benchee.Utility.DeepConvert
|
| 12 |
+
|
| 13 |
+
@no_input :__no_input
|
| 14 |
+
@type no_input :: :__no_input
|
| 15 |
+
|
| 16 |
+
@doc """
|
| 17 |
+
Public access for the special key representing no input for a scenario.
|
| 18 |
+
"""
|
| 19 |
+
@spec no_input() :: no_input()
|
| 20 |
+
def no_input, do: @no_input
|
| 21 |
+
|
| 22 |
+
@doc """
|
| 23 |
+
Takes the current suite and adds a new benchmarking scenario (represented by a
|
| 24 |
+
%Scenario{} struct) with the given name and function to the suite's scenarios.
|
| 25 |
+
If there are inputs in the suite's config, a scenario will be added for the given
|
| 26 |
+
function for each input.
|
| 27 |
+
"""
|
| 28 |
+
@spec benchmark(Suite.t(), Suite.key(), Scenario.to_benchmark(), module) :: Suite.t()
|
| 29 |
+
def benchmark(
|
| 30 |
+
suite = %Suite{scenarios: scenarios},
|
| 31 |
+
job_name,
|
| 32 |
+
to_be_benchmark,
|
| 33 |
+
printer \\ Printer
|
| 34 |
+
) do
|
| 35 |
+
warn_if_evaluated(to_be_benchmark, job_name, printer)
|
| 36 |
+
|
| 37 |
+
normalized_name = to_string(job_name)
|
| 38 |
+
|
| 39 |
+
if duplicate?(scenarios, normalized_name) do
|
| 40 |
+
printer.duplicate_benchmark_warning(normalized_name)
|
| 41 |
+
suite
|
| 42 |
+
else
|
| 43 |
+
add_scenario(suite, normalized_name, to_be_benchmark)
|
| 44 |
+
end
|
| 45 |
+
end
|
| 46 |
+
|
| 47 |
+
defp warn_if_evaluated(to_be_benchmark, job_name, printer) do
|
| 48 |
+
function =
|
| 49 |
+
case to_be_benchmark do
|
| 50 |
+
{function, _} -> function
|
| 51 |
+
function -> function
|
| 52 |
+
end
|
| 53 |
+
|
| 54 |
+
if :erlang.fun_info(function, :module) == {:module, :erl_eval} do
|
| 55 |
+
printer.evaluated_function_warning(job_name)
|
| 56 |
+
end
|
| 57 |
+
end
|
| 58 |
+
|
| 59 |
+
defp duplicate?(scenarios, job_name) do
|
| 60 |
+
Enum.any?(scenarios, fn scenario -> scenario.name == job_name end)
|
| 61 |
+
end
|
| 62 |
+
|
| 63 |
+
defp add_scenario(
|
| 64 |
+
suite = %Suite{scenarios: scenarios, configuration: config},
|
| 65 |
+
job_name,
|
| 66 |
+
function
|
| 67 |
+
) do
|
| 68 |
+
new_scenarios = build_scenarios_for_job(job_name, function, config)
|
| 69 |
+
%Suite{suite | scenarios: List.flatten([scenarios | new_scenarios])}
|
| 70 |
+
end
|
| 71 |
+
|
| 72 |
+
defp build_scenarios_for_job(job_name, function, %{inputs: nil}) do
|
| 73 |
+
[
|
| 74 |
+
build_scenario(%{
|
| 75 |
+
job_name: job_name,
|
| 76 |
+
function: function,
|
| 77 |
+
input: @no_input,
|
| 78 |
+
input_name: @no_input
|
| 79 |
+
})
|
| 80 |
+
]
|
| 81 |
+
end
|
| 82 |
+
|
| 83 |
+
defp build_scenarios_for_job(job_name, function, %{inputs: inputs}) do
|
| 84 |
+
Enum.map(inputs, fn {input_name, input} ->
|
| 85 |
+
build_scenario(%{
|
| 86 |
+
job_name: job_name,
|
| 87 |
+
function: function,
|
| 88 |
+
input: input,
|
| 89 |
+
input_name: input_name
|
| 90 |
+
})
|
| 91 |
+
end)
|
| 92 |
+
end
|
| 93 |
+
|
| 94 |
+
defp build_scenario(scenario_data = %{function: {function, options}}) do
|
| 95 |
+
scenario_data
|
| 96 |
+
|> Map.put(:function, function)
|
| 97 |
+
|> Map.merge(DeepConvert.to_map(options))
|
| 98 |
+
|> build_scenario
|
| 99 |
+
end
|
| 100 |
+
|
| 101 |
+
defp build_scenario(scenario_data) do
|
| 102 |
+
struct!(Scenario, add_scenario_name(scenario_data))
|
| 103 |
+
end
|
| 104 |
+
|
| 105 |
+
defp add_scenario_name(scenario_data) do
|
| 106 |
+
Map.put(scenario_data, :name, Scenario.display_name(scenario_data))
|
| 107 |
+
end
|
| 108 |
+
|
| 109 |
+
@doc """
|
| 110 |
+
Kicks off the benchmarking of all scenarios defined in the given suite.
|
| 111 |
+
|
| 112 |
+
Hence, this might take a while ;) Passes a list of scenarios and a scenario context to our
|
| 113 |
+
benchmark runner. For more information on how benchmarks are actually run, see the
|
| 114 |
+
`Benchee.Benchmark.Runner` code (API considered private).
|
| 115 |
+
"""
|
| 116 |
+
@spec collect(Suite.t(), module, module) :: Suite.t()
|
| 117 |
+
def collect(
|
| 118 |
+
suite = %Suite{scenarios: scenarios, configuration: config, system: system},
|
| 119 |
+
printer \\ Printer,
|
| 120 |
+
runner \\ Runner
|
| 121 |
+
) do
|
| 122 |
+
printer.configuration_information(suite)
|
| 123 |
+
benchmark_config = BenchmarkConfig.from(config)
|
| 124 |
+
|
| 125 |
+
scenario_context = %ScenarioContext{
|
| 126 |
+
config: benchmark_config,
|
| 127 |
+
printer: printer,
|
| 128 |
+
system: system
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
scenarios = runner.run_scenarios(scenarios, scenario_context)
|
| 132 |
+
%Suite{suite | scenarios: scenarios}
|
| 133 |
+
end
|
| 134 |
+
end
|
deps/benchee/lib/benchee/benchmark/benchmark_config.ex
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
defmodule Benchee.Benchmark.BenchmarkConfig do
|
| 2 |
+
@moduledoc """
|
| 3 |
+
Benchmark Configuration, practically a sub set of `Benchee.Configuration`
|
| 4 |
+
|
| 5 |
+
`Benchee.Configuration` holds too much data that we don't want to send into the benchmarking
|
| 6 |
+
processes - inputs being potentially huge. Hence, we take the sub set the benchmarks need and
|
| 7 |
+
put it in here. Since this is a benchmarking library, to no one's surprise these are a lot of
|
| 8 |
+
them.
|
| 9 |
+
See: https://github.com/bencheeorg/benchee/issues/412
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
alias Benchee.Benchmark.Hooks
|
| 13 |
+
|
| 14 |
+
@keys [
|
| 15 |
+
:warmup,
|
| 16 |
+
:time,
|
| 17 |
+
:memory_time,
|
| 18 |
+
:reduction_time,
|
| 19 |
+
:pre_check,
|
| 20 |
+
:measure_function_call_overhead,
|
| 21 |
+
:before_each,
|
| 22 |
+
:after_each,
|
| 23 |
+
:before_scenario,
|
| 24 |
+
:after_scenario,
|
| 25 |
+
:parallel,
|
| 26 |
+
:print,
|
| 27 |
+
:max_sample_size
|
| 28 |
+
]
|
| 29 |
+
|
| 30 |
+
defstruct @keys
|
| 31 |
+
|
| 32 |
+
@type t :: %__MODULE__{
|
| 33 |
+
time: number,
|
| 34 |
+
warmup: number,
|
| 35 |
+
memory_time: number,
|
| 36 |
+
reduction_time: number,
|
| 37 |
+
pre_check: boolean,
|
| 38 |
+
measure_function_call_overhead: boolean,
|
| 39 |
+
print: map,
|
| 40 |
+
before_each: Hooks.hook_function() | nil,
|
| 41 |
+
after_each: Hooks.hook_function() | nil,
|
| 42 |
+
before_scenario: Hooks.hook_function() | nil,
|
| 43 |
+
after_scenario: Hooks.hook_function() | nil,
|
| 44 |
+
measure_function_call_overhead: boolean,
|
| 45 |
+
parallel: pos_integer(),
|
| 46 |
+
max_sample_size: pos_integer()
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
alias Benchee.Configuration
|
| 50 |
+
|
| 51 |
+
def from(config = %Configuration{}) do
|
| 52 |
+
values = Map.take(config, @keys)
|
| 53 |
+
struct!(__MODULE__, values)
|
| 54 |
+
end
|
| 55 |
+
end
|
deps/benchee/lib/benchee/benchmark/collect.ex
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
defmodule Benchee.Benchmark.Collect do
|
| 2 |
+
@moduledoc false
|
| 3 |
+
|
| 4 |
+
# A thing that collects a data point about a function execution - like time
|
| 5 |
+
# or memory needed.
|
| 6 |
+
|
| 7 |
+
@doc """
|
| 8 |
+
Takes an anonymous 0 arity function to measure and returns the measurement
|
| 9 |
+
and the return value of the function in a tuple.
|
| 10 |
+
|
| 11 |
+
The returned measurement may be `nil` if the measurement failed for some
|
| 12 |
+
reason - it will then be ignored and not counted.
|
| 13 |
+
"""
|
| 14 |
+
@type return_value :: {non_neg_integer | nil, any}
|
| 15 |
+
@type zero_arity_function :: (-> any)
|
| 16 |
+
@type opts :: any
|
| 17 |
+
@callback collect(zero_arity_function()) :: return_value()
|
| 18 |
+
@callback collect(zero_arity_function(), opts) :: return_value()
|
| 19 |
+
|
| 20 |
+
@optional_callbacks collect: 2
|
| 21 |
+
end
|
deps/benchee/lib/benchee/benchmark/collect/memory.ex
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
defmodule Benchee.Benchmark.Collect.Memory do
|
| 2 |
+
@moduledoc false
|
| 3 |
+
|
| 4 |
+
# Measure memory consumption of a function.
|
| 5 |
+
#
|
| 6 |
+
# This is somewhat tricky and hence some resources can be recommended reading alongside
|
| 7 |
+
# this code:
|
| 8 |
+
# * description of the approach: https://devonestes.herokuapp.com/using-erlang-trace-3
|
| 9 |
+
# * devon describing the journey that this feature put us through (includes remarks
|
| 10 |
+
# on why certain parts are very important: https://www.youtube.com/watch?v=aqLujfzvUgM)
|
| 11 |
+
# * erlang docs on the info data structure we use:
|
| 12 |
+
# http://erlang.org/doc/man/erlang.html#gc_minor_start
|
| 13 |
+
#
|
| 14 |
+
# Returns `{nil, return_value}` in case the memory measurement went bad.
|
| 15 |
+
|
| 16 |
+
@behaviour Benchee.Benchmark.Collect
|
| 17 |
+
|
| 18 |
+
defmacrop compatible_stacktrace do
|
| 19 |
+
if Version.match?(Version.parse!(System.version()), "~> 1.7") do
|
| 20 |
+
quote do
|
| 21 |
+
__STACKTRACE__
|
| 22 |
+
end
|
| 23 |
+
else
|
| 24 |
+
quote do
|
| 25 |
+
System.stacktrace()
|
| 26 |
+
end
|
| 27 |
+
end
|
| 28 |
+
end
|
| 29 |
+
|
| 30 |
+
@spec collect((-> any)) :: {nil | non_neg_integer, any}
|
| 31 |
+
def collect(fun) do
|
| 32 |
+
ref = make_ref()
|
| 33 |
+
Process.flag(:trap_exit, true)
|
| 34 |
+
start_runner(fun, ref)
|
| 35 |
+
await_results(nil, ref)
|
| 36 |
+
end
|
| 37 |
+
|
| 38 |
+
defp await_results(return_value, ref) do
|
| 39 |
+
receive do
|
| 40 |
+
{^ref, memory_usage} ->
|
| 41 |
+
return_memory({memory_usage, return_value})
|
| 42 |
+
|
| 43 |
+
{^ref, :shutdown} ->
|
| 44 |
+
nil
|
| 45 |
+
|
| 46 |
+
# we need a really basic pattern here because sending anything other than
|
| 47 |
+
# just what's returned from the function that we're benchmarking will
|
| 48 |
+
# involve allocating a new term, which will skew the measurements.
|
| 49 |
+
# We need to be very careful to always send the `ref` in every other
|
| 50 |
+
# message to this process.
|
| 51 |
+
new_result ->
|
| 52 |
+
await_results(new_result, ref)
|
| 53 |
+
end
|
| 54 |
+
end
|
| 55 |
+
|
| 56 |
+
defp start_runner(fun, ref) do
|
| 57 |
+
parent = self()
|
| 58 |
+
|
| 59 |
+
spawn_link(fn ->
|
| 60 |
+
tracer = start_tracer(self())
|
| 61 |
+
|
| 62 |
+
try do
|
| 63 |
+
_ = measure_memory(fun, tracer, parent)
|
| 64 |
+
word_size = :erlang.system_info(:wordsize)
|
| 65 |
+
memory_used = get_collected_memory(tracer)
|
| 66 |
+
send(parent, {ref, memory_used * word_size})
|
| 67 |
+
catch
|
| 68 |
+
kind, reason ->
|
| 69 |
+
send(tracer, :done)
|
| 70 |
+
send(parent, {ref, :shutdown})
|
| 71 |
+
stacktrace = compatible_stacktrace()
|
| 72 |
+
IO.puts(Exception.format(kind, reason, stacktrace))
|
| 73 |
+
exit(:normal)
|
| 74 |
+
after
|
| 75 |
+
send(tracer, :done)
|
| 76 |
+
end
|
| 77 |
+
end)
|
| 78 |
+
end
|
| 79 |
+
|
| 80 |
+
defp return_memory({memory_usage, return_value}) when memory_usage < 0, do: {nil, return_value}
|
| 81 |
+
defp return_memory(memory_usage_info), do: memory_usage_info
|
| 82 |
+
|
| 83 |
+
defp measure_memory(fun, tracer, parent) do
|
| 84 |
+
:erlang.garbage_collect()
|
| 85 |
+
send(tracer, :begin_collection)
|
| 86 |
+
|
| 87 |
+
receive do
|
| 88 |
+
:ready_to_begin -> nil
|
| 89 |
+
end
|
| 90 |
+
|
| 91 |
+
return_value = fun.()
|
| 92 |
+
send(parent, return_value)
|
| 93 |
+
|
| 94 |
+
:erlang.garbage_collect()
|
| 95 |
+
send(tracer, :end_collection)
|
| 96 |
+
|
| 97 |
+
receive do
|
| 98 |
+
:ready_to_end -> nil
|
| 99 |
+
end
|
| 100 |
+
|
| 101 |
+
# We need to reference these variables after we end our collection so
|
| 102 |
+
# these don't get GC'd and counted towards the memory usage of the function
|
| 103 |
+
# we're benchmarking.
|
| 104 |
+
{parent, fun}
|
| 105 |
+
end
|
| 106 |
+
|
| 107 |
+
defp get_collected_memory(tracer) do
|
| 108 |
+
ref = Process.monitor(tracer)
|
| 109 |
+
send(tracer, {:get_collected_memory, self(), ref})
|
| 110 |
+
|
| 111 |
+
receive do
|
| 112 |
+
{:DOWN, ^ref, _, _, _} -> nil
|
| 113 |
+
{^ref, collected} -> collected
|
| 114 |
+
end
|
| 115 |
+
end
|
| 116 |
+
|
| 117 |
+
defp start_tracer(pid) do
|
| 118 |
+
spawn(fn -> tracer_loop(pid, 0) end)
|
| 119 |
+
end
|
| 120 |
+
|
| 121 |
+
defp tracer_loop(pid, acc) do
|
| 122 |
+
receive do
|
| 123 |
+
:begin_collection ->
|
| 124 |
+
:erlang.trace(pid, true, [:garbage_collection, tracer: self()])
|
| 125 |
+
send(pid, :ready_to_begin)
|
| 126 |
+
tracer_loop(pid, acc)
|
| 127 |
+
|
| 128 |
+
:end_collection ->
|
| 129 |
+
:erlang.trace(pid, false, [:garbage_collection])
|
| 130 |
+
send(pid, :ready_to_end)
|
| 131 |
+
tracer_loop(pid, acc)
|
| 132 |
+
|
| 133 |
+
{:get_collected_memory, reply_to, ref} ->
|
| 134 |
+
send(reply_to, {ref, acc})
|
| 135 |
+
|
| 136 |
+
{:trace, ^pid, :gc_minor_start, info} ->
|
| 137 |
+
listen_gc_end(pid, :gc_minor_end, acc, total_memory(info))
|
| 138 |
+
|
| 139 |
+
{:trace, ^pid, :gc_major_start, info} ->
|
| 140 |
+
listen_gc_end(pid, :gc_major_end, acc, total_memory(info))
|
| 141 |
+
|
| 142 |
+
:done ->
|
| 143 |
+
exit(:normal)
|
| 144 |
+
end
|
| 145 |
+
end
|
| 146 |
+
|
| 147 |
+
defp listen_gc_end(pid, tag, acc, mem_before) do
|
| 148 |
+
receive do
|
| 149 |
+
{:trace, ^pid, ^tag, info} ->
|
| 150 |
+
mem_after = total_memory(info)
|
| 151 |
+
tracer_loop(pid, acc + mem_before - mem_after)
|
| 152 |
+
end
|
| 153 |
+
end
|
| 154 |
+
|
| 155 |
+
defp total_memory(info) do
|
| 156 |
+
# `:heap_size` seems to only contain the memory size of the youngest
|
| 157 |
+
# generation `:old_heap_size` has the old generation. There is also
|
| 158 |
+
# `:recent_size` but that seems to already be accounted for.
|
| 159 |
+
Keyword.fetch!(info, :heap_size) + Keyword.fetch!(info, :old_heap_size)
|
| 160 |
+
end
|
| 161 |
+
end
|
deps/benchee/lib/benchee/benchmark/collect/reductions.ex
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
defmodule Benchee.Benchmark.Collect.Reductions do
|
| 2 |
+
@moduledoc false
|
| 3 |
+
|
| 4 |
+
@behaviour Benchee.Benchmark.Collect
|
| 5 |
+
|
| 6 |
+
def collect(fun) do
|
| 7 |
+
parent = self()
|
| 8 |
+
|
| 9 |
+
spawn_link(fn ->
|
| 10 |
+
start = get_reductions()
|
| 11 |
+
output = fun.()
|
| 12 |
+
send(parent, {:reductions, get_reductions() - start, output})
|
| 13 |
+
end)
|
| 14 |
+
|
| 15 |
+
receive do
|
| 16 |
+
{:reductions, reductions, output} -> {reductions, output}
|
| 17 |
+
end
|
| 18 |
+
end
|
| 19 |
+
|
| 20 |
+
defp get_reductions do
|
| 21 |
+
{:reductions, reductions} = Process.info(self(), :reductions)
|
| 22 |
+
reductions
|
| 23 |
+
end
|
| 24 |
+
end
|
deps/benchee/lib/benchee/benchmark/collect/time.ex
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
defmodule Benchee.Benchmark.Collect.Time do
|
| 2 |
+
@moduledoc false
|
| 3 |
+
|
| 4 |
+
# Measure the time elapsed while executing a given function.
|
| 5 |
+
#
|
| 6 |
+
# In contrast to `:timer.tc/1` it always returns the result in nano seconds instead of micro
|
| 7 |
+
# seconds. This helps us avoid losing precision as both Linux and MacOSX seem to be able to
|
| 8 |
+
# measure in nano seconds. `:timer.tc/n`
|
| 9 |
+
# [forfeits this precision](
|
| 10 |
+
# https://github.com/erlang/otp/blob/main/lib/stdlib/src/timer.erl#L164-L169).
|
| 11 |
+
|
| 12 |
+
@behaviour Benchee.Benchmark.Collect
|
| 13 |
+
|
| 14 |
+
@spec collect((-> any)) :: {non_neg_integer, any}
|
| 15 |
+
def collect(function) do
|
| 16 |
+
start = :erlang.monotonic_time()
|
| 17 |
+
result = function.()
|
| 18 |
+
finish = :erlang.monotonic_time()
|
| 19 |
+
|
| 20 |
+
duration_nano_seconds = :erlang.convert_time_unit(finish - start, :native, :nanosecond)
|
| 21 |
+
|
| 22 |
+
{duration_nano_seconds, result}
|
| 23 |
+
end
|
| 24 |
+
end
|
deps/benchee/lib/benchee/benchmark/function_call_overhead.ex
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
defmodule Benchee.Benchmark.FunctionCallOverhead do
|
| 2 |
+
@moduledoc false
|
| 3 |
+
|
| 4 |
+
alias Benchee.Benchmark.Collect.Time
|
| 5 |
+
alias Benchee.Conversion
|
| 6 |
+
|
| 7 |
+
@overhead_determination_time Conversion.Duration.convert_value({0.01, :second}, :nanosecond)
|
| 8 |
+
|
| 9 |
+
# Compute the function call overhead on the current system
|
| 10 |
+
#
|
| 11 |
+
# You might wonder why this isn't done simply through using our existing infrastructure
|
| 12 |
+
# and just run it with Scenario, Context etc. - in fact that's how it used to be,
|
| 13 |
+
# but it meant that we'd pass half-baked scenarios to functions causing dialyzer to complain,
|
| 14 |
+
# as well as running for a lot more code than we strictly need to like the
|
| 15 |
+
# `determine_n_times` code that we should rather not go through as it changes
|
| 16 |
+
# what this function does.
|
| 17 |
+
# This also gives us a way to make sure we definitely take at least one measurement.
|
| 18 |
+
@spec measure() :: non_neg_integer()
|
| 19 |
+
def measure do
|
| 20 |
+
# just the fastest function one can think of...
|
| 21 |
+
overhead_function = fn -> nil end
|
| 22 |
+
|
| 23 |
+
_ = warmup(overhead_function)
|
| 24 |
+
run_times = run(overhead_function)
|
| 25 |
+
|
| 26 |
+
Statistex.minimum(run_times)
|
| 27 |
+
end
|
| 28 |
+
|
| 29 |
+
defp warmup(function) do
|
| 30 |
+
run_for(function, @overhead_determination_time / 2)
|
| 31 |
+
end
|
| 32 |
+
|
| 33 |
+
defp run(function) do
|
| 34 |
+
run_for(function, @overhead_determination_time)
|
| 35 |
+
end
|
| 36 |
+
|
| 37 |
+
defp run_for(function, run_time) do
|
| 38 |
+
end_time = current_time() + run_time
|
| 39 |
+
|
| 40 |
+
do_run(function, [], end_time)
|
| 41 |
+
end
|
| 42 |
+
|
| 43 |
+
@spec do_run((-> any), [number], number) :: [number, ...]
|
| 44 |
+
defp do_run(function, durations, end_time) do
|
| 45 |
+
{duration, _} = Time.collect(function)
|
| 46 |
+
|
| 47 |
+
if current_time() < end_time do
|
| 48 |
+
do_run(function, [duration | durations], end_time)
|
| 49 |
+
else
|
| 50 |
+
durations
|
| 51 |
+
end
|
| 52 |
+
end
|
| 53 |
+
|
| 54 |
+
defp current_time, do: :erlang.system_time(:nano_seconds)
|
| 55 |
+
end
|
deps/benchee/lib/benchee/benchmark/hooks.ex
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
defmodule Benchee.Benchmark.Hooks do
|
| 2 |
+
@moduledoc """
|
| 3 |
+
Internal module to support hooks functionality.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
# Non Benchee code should not rely on this module.
|
| 7 |
+
|
| 8 |
+
alias Benchee.Benchmark.ScenarioContext
|
| 9 |
+
alias Benchee.Scenario
|
| 10 |
+
|
| 11 |
+
@type hook_function :: (any -> any)
|
| 12 |
+
|
| 13 |
+
@spec run_before_scenario(Scenario.t(), ScenarioContext.t()) :: any
|
| 14 |
+
def run_before_scenario(
|
| 15 |
+
%Scenario{
|
| 16 |
+
before_scenario: local_before_scenario,
|
| 17 |
+
input: input
|
| 18 |
+
},
|
| 19 |
+
%ScenarioContext{
|
| 20 |
+
config: %{before_scenario: global_before_scenario}
|
| 21 |
+
}
|
| 22 |
+
) do
|
| 23 |
+
input
|
| 24 |
+
|> run_before_function(global_before_scenario)
|
| 25 |
+
|> run_before_function(local_before_scenario)
|
| 26 |
+
end
|
| 27 |
+
|
| 28 |
+
defp run_before_function(input, nil), do: input
|
| 29 |
+
defp run_before_function(input, function), do: function.(input)
|
| 30 |
+
|
| 31 |
+
@spec run_before_each(Scenario.t(), ScenarioContext.t()) :: any
|
| 32 |
+
def run_before_each(
|
| 33 |
+
%{
|
| 34 |
+
before_each: local_before_each
|
| 35 |
+
},
|
| 36 |
+
%{
|
| 37 |
+
config: %{before_each: global_before_each},
|
| 38 |
+
scenario_input: input
|
| 39 |
+
}
|
| 40 |
+
) do
|
| 41 |
+
input
|
| 42 |
+
|> run_before_function(global_before_each)
|
| 43 |
+
|> run_before_function(local_before_each)
|
| 44 |
+
end
|
| 45 |
+
|
| 46 |
+
@spec run_after_each(any, Scenario.t(), ScenarioContext.t()) :: any
|
| 47 |
+
def run_after_each(
|
| 48 |
+
return_value,
|
| 49 |
+
%{
|
| 50 |
+
after_each: local_after_each
|
| 51 |
+
},
|
| 52 |
+
%{
|
| 53 |
+
config: %{after_each: global_after_each}
|
| 54 |
+
}
|
| 55 |
+
) do
|
| 56 |
+
if local_after_each, do: local_after_each.(return_value)
|
| 57 |
+
if global_after_each, do: global_after_each.(return_value)
|
| 58 |
+
end
|
| 59 |
+
|
| 60 |
+
@spec run_after_scenario(Scenario.t(), ScenarioContext.t()) :: any
|
| 61 |
+
def run_after_scenario(
|
| 62 |
+
%{
|
| 63 |
+
after_scenario: local_after_scenario
|
| 64 |
+
},
|
| 65 |
+
%{
|
| 66 |
+
config: %{after_scenario: global_after_scenario},
|
| 67 |
+
scenario_input: input
|
| 68 |
+
}
|
| 69 |
+
) do
|
| 70 |
+
if local_after_scenario, do: local_after_scenario.(input)
|
| 71 |
+
if global_after_scenario, do: global_after_scenario.(input)
|
| 72 |
+
end
|
| 73 |
+
end
|
deps/benchee/lib/benchee/benchmark/repeated_measurement.ex
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
defmodule Benchee.Benchmark.RepeatedMeasurement do
|
| 2 |
+
@moduledoc false
|
| 3 |
+
|
| 4 |
+
# This module is an internal implementation detail, and should absolutely not be relied upon
|
| 5 |
+
# from external clients.
|
| 6 |
+
#
|
| 7 |
+
# It is used when we can't conduct measurements precise enough with our measurement precision.
|
| 8 |
+
# I.e. we can measure in microseconds but we measure a function call to take 1 microsecond then
|
| 9 |
+
# most measurements will either be 1 or 2 microseconds which won't give us great results.
|
| 10 |
+
# Instead we repeat the function call n times until we measure at least ~10 (time unit) so
|
| 11 |
+
# that the difference between measurements can at least be ~10%.
|
| 12 |
+
#
|
| 13 |
+
# Today this is mostly only relevant on Windows & Mac OS as we have nanosecond precision on
|
| 14 |
+
# Linux and we've failed to produce a measurable function call that takes less than 10 nano
|
| 15 |
+
# seconds.
|
| 16 |
+
#
|
| 17 |
+
# That's also why this code lives in a separate module and not `Runner` - as it's rarely used
|
| 18 |
+
# and clutters that code + we need a way to test it even if we can't trigger it's conditions.
|
| 19 |
+
|
| 20 |
+
# If a function executes way too fast measurements are too unreliable and
|
| 21 |
+
# with too high variance. Therefore determine an n how often it should be
|
| 22 |
+
# executed in the measurement cycle.
|
| 23 |
+
|
| 24 |
+
alias Benchee.Benchmark.{Collect, Hooks, Runner, ScenarioContext}
|
| 25 |
+
alias Benchee.Scenario
|
| 26 |
+
alias Benchee.Utility.ErlangVersion
|
| 27 |
+
alias Benchee.Utility.RepeatN
|
| 28 |
+
|
| 29 |
+
@minimum_execution_time 10
|
| 30 |
+
@times_multiplier 10
|
| 31 |
+
@nanosecond_resolution Benchee.Conversion.Duration.convert_value({1, :second}, :nanosecond)
|
| 32 |
+
|
| 33 |
+
@spec determine_n_times(Scenario.t(), ScenarioContext.t(), boolean, module) ::
|
| 34 |
+
{pos_integer, number}
|
| 35 |
+
def determine_n_times(
|
| 36 |
+
scenario,
|
| 37 |
+
scenario_context = %ScenarioContext{system: system_info},
|
| 38 |
+
print_fast_warning,
|
| 39 |
+
clock_info \\ :erlang.system_info(:os_monotonic_time_source),
|
| 40 |
+
collector \\ Collect.Time
|
| 41 |
+
) do
|
| 42 |
+
resolution_adjustment = determine_resolution_adjustment(system_info, clock_info)
|
| 43 |
+
|
| 44 |
+
do_determine_n_times(
|
| 45 |
+
scenario,
|
| 46 |
+
scenario_context,
|
| 47 |
+
print_fast_warning,
|
| 48 |
+
resolution_adjustment,
|
| 49 |
+
collector
|
| 50 |
+
)
|
| 51 |
+
end
|
| 52 |
+
|
| 53 |
+
# See ERL-1067 aka which was fixed here
|
| 54 |
+
# https://erlang.org/download/otp_src_22.2.readme
|
| 55 |
+
@fixed_erlang_vesion "22.2.0"
|
| 56 |
+
# MacOS usually measures in micro seconds so that's the best default to return when not given
|
| 57 |
+
@old_macos_value 1_000
|
| 58 |
+
|
| 59 |
+
defp determine_resolution_adjustment(system_info, clock_info) do
|
| 60 |
+
if trust_clock?(system_info) do
|
| 61 |
+
# If the resolution is 1_000_000 that means microsecond, while 1_000_000_000 is nanosecond.
|
| 62 |
+
# we then need to adjust our measured time by that value. I.e. if we measured "5000" here we
|
| 63 |
+
# do not want to let it pass as it is essentially just "5" for our measurement purposes.
|
| 64 |
+
{:ok, resolution} = Access.fetch(clock_info, :resolution)
|
| 65 |
+
|
| 66 |
+
@nanosecond_resolution / resolution
|
| 67 |
+
else
|
| 68 |
+
@old_macos_value
|
| 69 |
+
end
|
| 70 |
+
end
|
| 71 |
+
|
| 72 |
+
# Can't really trust the macOS clock on OTP before mentioned version, see tickets linked above
|
| 73 |
+
defp trust_clock?(%{os: :macOS, erlang: erlang_version}) do
|
| 74 |
+
ErlangVersion.includes_fixes_from?(erlang_version, @fixed_erlang_vesion)
|
| 75 |
+
end
|
| 76 |
+
|
| 77 |
+
# If `suite.system` wasn't populated then we'll not mistrust it as well as all others
|
| 78 |
+
# (can happen if people call parts of benchee themselves without calling system first)
|
| 79 |
+
defp trust_clock?(_), do: true
|
| 80 |
+
|
| 81 |
+
defp do_determine_n_times(
|
| 82 |
+
scenario,
|
| 83 |
+
scenario_context = %ScenarioContext{
|
| 84 |
+
num_iterations: num_iterations,
|
| 85 |
+
printer: printer
|
| 86 |
+
},
|
| 87 |
+
print_fast_warning,
|
| 88 |
+
resolution_adjustment,
|
| 89 |
+
collector
|
| 90 |
+
) do
|
| 91 |
+
run_time = measure_iteration(scenario, scenario_context, collector)
|
| 92 |
+
resolution_adjusted_run_time = run_time / resolution_adjustment
|
| 93 |
+
|
| 94 |
+
if resolution_adjusted_run_time >= @minimum_execution_time do
|
| 95 |
+
{num_iterations, report_time(run_time, num_iterations)}
|
| 96 |
+
else
|
| 97 |
+
if print_fast_warning, do: printer.fast_warning()
|
| 98 |
+
|
| 99 |
+
new_context = %ScenarioContext{
|
| 100 |
+
scenario_context
|
| 101 |
+
| num_iterations: num_iterations * @times_multiplier
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
do_determine_n_times(scenario, new_context, false, resolution_adjustment, collector)
|
| 105 |
+
end
|
| 106 |
+
end
|
| 107 |
+
|
| 108 |
+
# we need to convert the time here since we measure native time to see when we have enough
|
| 109 |
+
# repetitions but the first time is used in the actual samples
|
| 110 |
+
defp report_time(measurement, num_iterations) do
|
| 111 |
+
adjust_for_iterations(measurement, num_iterations)
|
| 112 |
+
end
|
| 113 |
+
|
| 114 |
+
defp adjust_for_iterations(measurement, 1), do: measurement
|
| 115 |
+
defp adjust_for_iterations(measurement, num_iterations), do: measurement / num_iterations
|
| 116 |
+
|
| 117 |
+
@spec collect(Scenario.t(), ScenarioContext.t(), module) :: number
|
| 118 |
+
def collect(
|
| 119 |
+
scenario,
|
| 120 |
+
scenario_context = %ScenarioContext{
|
| 121 |
+
num_iterations: num_iterations
|
| 122 |
+
},
|
| 123 |
+
collector
|
| 124 |
+
) do
|
| 125 |
+
measurement = measure_iteration(scenario, scenario_context, collector)
|
| 126 |
+
|
| 127 |
+
adjust_for_iterations(measurement, num_iterations)
|
| 128 |
+
end
|
| 129 |
+
|
| 130 |
+
defp measure_iteration(
|
| 131 |
+
scenario,
|
| 132 |
+
scenario_context = %ScenarioContext{
|
| 133 |
+
num_iterations: 1
|
| 134 |
+
},
|
| 135 |
+
collector
|
| 136 |
+
) do
|
| 137 |
+
Runner.collect(scenario, scenario_context, collector)
|
| 138 |
+
end
|
| 139 |
+
|
| 140 |
+
defp measure_iteration(
|
| 141 |
+
scenario,
|
| 142 |
+
scenario_context = %ScenarioContext{
|
| 143 |
+
num_iterations: iterations
|
| 144 |
+
},
|
| 145 |
+
collector
|
| 146 |
+
)
|
| 147 |
+
when iterations > 1 do
|
| 148 |
+
# When we have more than one iteration, then the repetition and calling
|
| 149 |
+
# of hooks is already included in the function, for reference/reasoning see
|
| 150 |
+
# `build_benchmarking_function/2`
|
| 151 |
+
function = build_benchmarking_function(scenario, scenario_context)
|
| 152 |
+
|
| 153 |
+
{measurement, _return_value} = collector.collect(function)
|
| 154 |
+
|
| 155 |
+
measurement
|
| 156 |
+
end
|
| 157 |
+
|
| 158 |
+
# Builds the appropriate function to benchmark. Takes into account the
|
| 159 |
+
# combinations of the following cases:
|
| 160 |
+
#
|
| 161 |
+
# * an input is specified - creates a 0-argument function calling the original
|
| 162 |
+
# function with that input
|
| 163 |
+
# * number of iterations - when there's more than one iteration we repeat the
|
| 164 |
+
# benchmarking function during execution and measure the the total run time.
|
| 165 |
+
# We only run multiple iterations if a function is so fast that we can't
|
| 166 |
+
# accurately measure it in one go. Hence, we can't split up the function
|
| 167 |
+
# execution and hooks anymore and sadly we also measure the time of the
|
| 168 |
+
# hooks.
|
| 169 |
+
defp build_benchmarking_function(
|
| 170 |
+
%Scenario{
|
| 171 |
+
function: function,
|
| 172 |
+
before_each: nil,
|
| 173 |
+
after_each: nil
|
| 174 |
+
},
|
| 175 |
+
%ScenarioContext{
|
| 176 |
+
num_iterations: iterations,
|
| 177 |
+
scenario_input: input,
|
| 178 |
+
config: %{after_each: nil, before_each: nil}
|
| 179 |
+
}
|
| 180 |
+
)
|
| 181 |
+
when iterations > 1 do
|
| 182 |
+
main = Runner.main_function(function, input)
|
| 183 |
+
# with no before/after each we can safely omit them and don't get the hit
|
| 184 |
+
# on run time measurements (See PR discussions for this for more info #127)
|
| 185 |
+
fn -> RepeatN.repeat_n(main, iterations) end
|
| 186 |
+
end
|
| 187 |
+
|
| 188 |
+
defp build_benchmarking_function(
|
| 189 |
+
scenario = %Scenario{function: function},
|
| 190 |
+
scenario_context = %ScenarioContext{num_iterations: iterations}
|
| 191 |
+
)
|
| 192 |
+
when iterations > 1 do
|
| 193 |
+
fn ->
|
| 194 |
+
RepeatN.repeat_n(
|
| 195 |
+
fn ->
|
| 196 |
+
new_input = Hooks.run_before_each(scenario, scenario_context)
|
| 197 |
+
main = Runner.main_function(function, new_input)
|
| 198 |
+
return_value = main.()
|
| 199 |
+
Hooks.run_after_each(return_value, scenario, scenario_context)
|
| 200 |
+
end,
|
| 201 |
+
iterations
|
| 202 |
+
)
|
| 203 |
+
end
|
| 204 |
+
end
|
| 205 |
+
end
|
deps/benchee/lib/benchee/benchmark/runner.ex
ADDED
|
@@ -0,0 +1,380 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
defmodule Benchee.Benchmark.Runner do
|
| 2 |
+
@moduledoc """
|
| 3 |
+
Internal module "running" a scenario, measuring all defined measurements.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
# This module actually runs our benchmark scenarios, adding information about
|
| 7 |
+
# run time and memory usage to each scenario.
|
| 8 |
+
|
| 9 |
+
alias Benchee.Benchmark
|
| 10 |
+
alias Benchee.Benchmark.BenchmarkConfig
|
| 11 |
+
alias Benchee.Scenario
|
| 12 |
+
alias Benchee.Utility.Parallel
|
| 13 |
+
|
| 14 |
+
alias Benchmark.{
|
| 15 |
+
Collect,
|
| 16 |
+
FunctionCallOverhead,
|
| 17 |
+
Hooks,
|
| 18 |
+
RepeatedMeasurement,
|
| 19 |
+
ScenarioContext
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
@no_input Benchmark.no_input()
|
| 23 |
+
@zero_values [0, 0.0]
|
| 24 |
+
|
| 25 |
+
@doc """
|
| 26 |
+
Executes the benchmarks defined before by first running the defined functions
|
| 27 |
+
for `warmup` time without gathering results and them running them for `time`
|
| 28 |
+
gathering their run times.
|
| 29 |
+
|
| 30 |
+
This means the total run time of a single benchmarking scenario is warmup +
|
| 31 |
+
time.
|
| 32 |
+
|
| 33 |
+
Warmup is usually important for run times with JIT but it seems to have some
|
| 34 |
+
effect on the BEAM as well.
|
| 35 |
+
|
| 36 |
+
There will be `parallel` processes spawned executing the benchmark job in
|
| 37 |
+
parallel.
|
| 38 |
+
"""
|
| 39 |
+
@spec run_scenarios([Scenario.t()], ScenarioContext.t()) :: [Scenario.t()]
|
| 40 |
+
def run_scenarios(scenarios, scenario_context = %ScenarioContext{}) do
|
| 41 |
+
case scenario_context.config.pre_check do
|
| 42 |
+
true -> Enum.each(scenarios, fn scenario -> pre_check(scenario, scenario_context) end)
|
| 43 |
+
false -> :ok
|
| 44 |
+
:all_same -> pre_check_all_same(scenarios, scenario_context)
|
| 45 |
+
end
|
| 46 |
+
|
| 47 |
+
function_call_overhead =
|
| 48 |
+
if scenario_context.config.measure_function_call_overhead do
|
| 49 |
+
measure_and_report_function_call_overhead(scenario_context.printer)
|
| 50 |
+
else
|
| 51 |
+
0
|
| 52 |
+
end
|
| 53 |
+
|
| 54 |
+
scenario_context = %ScenarioContext{
|
| 55 |
+
scenario_context
|
| 56 |
+
| function_call_overhead: function_call_overhead
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
Enum.map(scenarios, fn scenario -> parallel_benchmark(scenario, scenario_context) end)
|
| 60 |
+
end
|
| 61 |
+
|
| 62 |
+
# This will run the given scenario exactly once, including the before and
|
| 63 |
+
# after hooks, to ensure the function can execute without raising an error.
|
| 64 |
+
defp pre_check(scenario, scenario_context) do
|
| 65 |
+
run_once(scenario, scenario_context)
|
| 66 |
+
end
|
| 67 |
+
|
| 68 |
+
def run_once(scenario, scenario_context = %ScenarioContext{}) do
|
| 69 |
+
scenario_input = Hooks.run_before_scenario(scenario, scenario_context)
|
| 70 |
+
scenario_context = %ScenarioContext{scenario_context | scenario_input: scenario_input}
|
| 71 |
+
return_value = collect_return_value(scenario, scenario_context)
|
| 72 |
+
_ = Hooks.run_after_scenario(scenario, scenario_context)
|
| 73 |
+
return_value
|
| 74 |
+
end
|
| 75 |
+
|
| 76 |
+
defp pre_check_all_same(scenarios, scenario_context) do
|
| 77 |
+
Enum.reduce(scenarios, %{}, fn %{input_name: input_name} = scenario, previous_runs ->
|
| 78 |
+
return_value = pre_check(scenario, scenario_context)
|
| 79 |
+
|
| 80 |
+
case previous_runs do
|
| 81 |
+
%{^input_name => {previous_job, previous_value}} when return_value !== previous_value ->
|
| 82 |
+
raise Benchee.PreCheckError,
|
| 83 |
+
message: """
|
| 84 |
+
all_same pre check failed#{pre_check_failed_input_message(input_name)}:
|
| 85 |
+
- #{previous_job} returned #{inspect(previous_value)}
|
| 86 |
+
- #{scenario.job_name} returned #{inspect(return_value)}
|
| 87 |
+
"""
|
| 88 |
+
|
| 89 |
+
_ ->
|
| 90 |
+
Map.put_new(previous_runs, input_name, {scenario.job_name, return_value})
|
| 91 |
+
end
|
| 92 |
+
end)
|
| 93 |
+
end
|
| 94 |
+
|
| 95 |
+
defp pre_check_failed_input_message(@no_input), do: ""
|
| 96 |
+
defp pre_check_failed_input_message(input_name), do: " for input #{inspect(input_name)}"
|
| 97 |
+
|
| 98 |
+
def measure_and_report_function_call_overhead(printer) do
|
| 99 |
+
overhead = FunctionCallOverhead.measure()
|
| 100 |
+
printer.function_call_overhead(overhead)
|
| 101 |
+
overhead
|
| 102 |
+
end
|
| 103 |
+
|
| 104 |
+
defp parallel_benchmark(
|
| 105 |
+
scenario = %Scenario{job_name: job_name, input_name: input_name},
|
| 106 |
+
scenario_context = %ScenarioContext{
|
| 107 |
+
printer: printer,
|
| 108 |
+
config: config
|
| 109 |
+
}
|
| 110 |
+
) do
|
| 111 |
+
printer.benchmarking(job_name, input_name, config)
|
| 112 |
+
|
| 113 |
+
config
|
| 114 |
+
|> measure_scenario_parallel(scenario, scenario_context)
|
| 115 |
+
|> add_measurements_to_scenario(scenario)
|
| 116 |
+
end
|
| 117 |
+
|
| 118 |
+
defp measure_scenario_parallel(config, scenario, scenario_context) do
|
| 119 |
+
Parallel.map(1..config.parallel, fn _ -> measure_scenario(scenario, scenario_context) end)
|
| 120 |
+
end
|
| 121 |
+
|
| 122 |
+
defp add_measurements_to_scenario(measurements, scenario) do
|
| 123 |
+
run_times = Enum.flat_map(measurements, fn {run_times, _, _} -> run_times end)
|
| 124 |
+
memory_usages = Enum.flat_map(measurements, fn {_, memory_usages, _} -> memory_usages end)
|
| 125 |
+
reductions = Enum.flat_map(measurements, fn {_, _, reductions} -> reductions end)
|
| 126 |
+
|
| 127 |
+
%{
|
| 128 |
+
scenario
|
| 129 |
+
| run_time_data: %{scenario.run_time_data | samples: run_times},
|
| 130 |
+
memory_usage_data: %{scenario.memory_usage_data | samples: memory_usages},
|
| 131 |
+
reductions_data: %{scenario.reductions_data | samples: reductions}
|
| 132 |
+
}
|
| 133 |
+
end
|
| 134 |
+
|
| 135 |
+
@spec measure_scenario(Scenario.t(), ScenarioContext.t()) :: {[number], [number], [number]}
|
| 136 |
+
defp measure_scenario(scenario, scenario_context = %ScenarioContext{}) do
|
| 137 |
+
scenario_input = Hooks.run_before_scenario(scenario, scenario_context)
|
| 138 |
+
scenario_context = %ScenarioContext{scenario_context | scenario_input: scenario_input}
|
| 139 |
+
|
| 140 |
+
_ = run_warmup(scenario, scenario_context)
|
| 141 |
+
|
| 142 |
+
run_times =
|
| 143 |
+
scenario
|
| 144 |
+
|> run_runtime_benchmark(scenario_context)
|
| 145 |
+
|> deduct_function_call_overhead(scenario_context.function_call_overhead)
|
| 146 |
+
|
| 147 |
+
memory_usages = run_memory_benchmark(scenario, scenario_context)
|
| 148 |
+
|
| 149 |
+
reductions =
|
| 150 |
+
scenario
|
| 151 |
+
|> run_reductions_benchmark(scenario_context)
|
| 152 |
+
|> deduct_reduction_overhead()
|
| 153 |
+
|
| 154 |
+
Hooks.run_after_scenario(scenario, scenario_context)
|
| 155 |
+
|
| 156 |
+
{run_times, memory_usages, reductions}
|
| 157 |
+
end
|
| 158 |
+
|
| 159 |
+
defp run_warmup(
|
| 160 |
+
scenario,
|
| 161 |
+
scenario_context = %ScenarioContext{
|
| 162 |
+
config: %BenchmarkConfig{warmup: warmup}
|
| 163 |
+
}
|
| 164 |
+
) do
|
| 165 |
+
measure_runtimes(scenario, scenario_context, warmup, false)
|
| 166 |
+
end
|
| 167 |
+
|
| 168 |
+
defp run_runtime_benchmark(
|
| 169 |
+
scenario,
|
| 170 |
+
scenario_context = %ScenarioContext{
|
| 171 |
+
config: %{
|
| 172 |
+
time: run_time,
|
| 173 |
+
print: %{fast_warning: fast_warning}
|
| 174 |
+
}
|
| 175 |
+
}
|
| 176 |
+
) do
|
| 177 |
+
measure_runtimes(scenario, scenario_context, run_time, fast_warning)
|
| 178 |
+
end
|
| 179 |
+
|
| 180 |
+
defp deduct_function_call_overhead(run_times, 0) do
|
| 181 |
+
run_times
|
| 182 |
+
end
|
| 183 |
+
|
| 184 |
+
defp deduct_function_call_overhead(run_times, overhead) do
|
| 185 |
+
Enum.map(run_times, fn time ->
|
| 186 |
+
max(time - overhead, 0)
|
| 187 |
+
end)
|
| 188 |
+
end
|
| 189 |
+
|
| 190 |
+
defp deduct_reduction_overhead([]), do: []
|
| 191 |
+
|
| 192 |
+
defp deduct_reduction_overhead(reductions) do
|
| 193 |
+
me = self()
|
| 194 |
+
ref = make_ref()
|
| 195 |
+
|
| 196 |
+
spawn(fn ->
|
| 197 |
+
{offset, _} = Collect.Reductions.collect(fn -> nil end)
|
| 198 |
+
send(me, {ref, offset})
|
| 199 |
+
end)
|
| 200 |
+
|
| 201 |
+
offset =
|
| 202 |
+
receive do
|
| 203 |
+
{^ref, offset} -> offset
|
| 204 |
+
end
|
| 205 |
+
|
| 206 |
+
Enum.map(reductions, &(&1 - offset))
|
| 207 |
+
end
|
| 208 |
+
|
| 209 |
+
defp run_reductions_benchmark(_, %ScenarioContext{config: %{reduction_time: time}})
|
| 210 |
+
when time in @zero_values do
|
| 211 |
+
[]
|
| 212 |
+
end
|
| 213 |
+
|
| 214 |
+
defp run_reductions_benchmark(
|
| 215 |
+
scenario,
|
| 216 |
+
scenario_context = %ScenarioContext{
|
| 217 |
+
config: %{
|
| 218 |
+
reduction_time: reduction_time
|
| 219 |
+
}
|
| 220 |
+
}
|
| 221 |
+
) do
|
| 222 |
+
end_time = current_time() + reduction_time
|
| 223 |
+
|
| 224 |
+
new_context = %ScenarioContext{
|
| 225 |
+
scenario_context
|
| 226 |
+
| current_time: current_time(),
|
| 227 |
+
end_time: end_time
|
| 228 |
+
}
|
| 229 |
+
|
| 230 |
+
do_benchmark(scenario, new_context, Collect.Reductions, [])
|
| 231 |
+
end
|
| 232 |
+
|
| 233 |
+
defp run_memory_benchmark(_, %ScenarioContext{config: %{memory_time: time}})
|
| 234 |
+
when time in @zero_values do
|
| 235 |
+
[]
|
| 236 |
+
end
|
| 237 |
+
|
| 238 |
+
defp run_memory_benchmark(
|
| 239 |
+
scenario,
|
| 240 |
+
scenario_context = %ScenarioContext{
|
| 241 |
+
config: %{
|
| 242 |
+
memory_time: memory_time
|
| 243 |
+
}
|
| 244 |
+
}
|
| 245 |
+
) do
|
| 246 |
+
end_time = current_time() + memory_time
|
| 247 |
+
|
| 248 |
+
new_context = %ScenarioContext{
|
| 249 |
+
scenario_context
|
| 250 |
+
| current_time: current_time(),
|
| 251 |
+
end_time: end_time
|
| 252 |
+
}
|
| 253 |
+
|
| 254 |
+
do_benchmark(scenario, new_context, Collect.Memory, [])
|
| 255 |
+
end
|
| 256 |
+
|
| 257 |
+
@spec measure_runtimes(Scenario.t(), ScenarioContext.t(), number, boolean) :: [number]
|
| 258 |
+
defp measure_runtimes(scenario, context, run_time, fast_warning)
|
| 259 |
+
defp measure_runtimes(_, _, time, _) when time in @zero_values, do: []
|
| 260 |
+
|
| 261 |
+
defp measure_runtimes(scenario, scenario_context = %ScenarioContext{}, run_time, fast_warning) do
|
| 262 |
+
end_time = current_time() + run_time
|
| 263 |
+
:erlang.garbage_collect()
|
| 264 |
+
|
| 265 |
+
{num_iterations, initial_run_time} =
|
| 266 |
+
RepeatedMeasurement.determine_n_times(scenario, scenario_context, fast_warning)
|
| 267 |
+
|
| 268 |
+
new_context = %ScenarioContext{
|
| 269 |
+
scenario_context
|
| 270 |
+
| current_time: current_time(),
|
| 271 |
+
end_time: end_time,
|
| 272 |
+
num_iterations: num_iterations,
|
| 273 |
+
# we have the initial run time
|
| 274 |
+
sample_size: 1
|
| 275 |
+
}
|
| 276 |
+
|
| 277 |
+
do_benchmark(scenario, new_context, Collect.Time, [initial_run_time])
|
| 278 |
+
end
|
| 279 |
+
|
| 280 |
+
defp current_time, do: :erlang.system_time(:nano_seconds)
|
| 281 |
+
|
| 282 |
+
# `run_times` is kept separately from the `Scenario` so that for the
|
| 283 |
+
# `parallel` execution case we can easily concatenate and flatten the results
|
| 284 |
+
# of all processes. That's why we add them to the scenario once after
|
| 285 |
+
# measuring has finished. `scenario` is still needed in general for the
|
| 286 |
+
# benchmarking function, hooks etc.
|
| 287 |
+
defp do_benchmark(
|
| 288 |
+
_scenario,
|
| 289 |
+
%ScenarioContext{
|
| 290 |
+
current_time: current_time,
|
| 291 |
+
end_time: end_time,
|
| 292 |
+
sample_size: sample_size,
|
| 293 |
+
config: %BenchmarkConfig{
|
| 294 |
+
max_sample_size: max_sample_size
|
| 295 |
+
}
|
| 296 |
+
},
|
| 297 |
+
_collector,
|
| 298 |
+
measurements
|
| 299 |
+
)
|
| 300 |
+
when (current_time > end_time or
|
| 301 |
+
(not is_nil(max_sample_size) and sample_size >= max_sample_size)) and
|
| 302 |
+
measurements != [] do
|
| 303 |
+
# restore correct order - important for graphing
|
| 304 |
+
Enum.reverse(measurements)
|
| 305 |
+
end
|
| 306 |
+
|
| 307 |
+
defp do_benchmark(scenario, scenario_context = %ScenarioContext{}, collector, measurements) do
|
| 308 |
+
measurement = collect(scenario, scenario_context, collector)
|
| 309 |
+
|
| 310 |
+
updated_context = %ScenarioContext{
|
| 311 |
+
scenario_context
|
| 312 |
+
| current_time: current_time(),
|
| 313 |
+
sample_size: scenario_context.sample_size + 1
|
| 314 |
+
}
|
| 315 |
+
|
| 316 |
+
do_benchmark(
|
| 317 |
+
scenario,
|
| 318 |
+
updated_context,
|
| 319 |
+
collector,
|
| 320 |
+
updated_measurements(measurement, measurements)
|
| 321 |
+
)
|
| 322 |
+
end
|
| 323 |
+
|
| 324 |
+
# We return `nil` if memory measurement failed so keep it empty
|
| 325 |
+
@spec updated_measurements(number | nil, [number]) :: [number]
|
| 326 |
+
defp updated_measurements(nil, measurements), do: measurements
|
| 327 |
+
defp updated_measurements(measurement, measurements), do: [measurement | measurements]
|
| 328 |
+
|
| 329 |
+
# Support functionality that just runs once via `run_once` and does not care about measurements.
|
| 330 |
+
# At the time of this writing that's pre checks and profilers.
|
| 331 |
+
defp collect_return_value(scenario, scenario_context) do
|
| 332 |
+
new_input = Hooks.run_before_each(scenario, scenario_context)
|
| 333 |
+
function = main_function(scenario.function, new_input)
|
| 334 |
+
|
| 335 |
+
return_value = function.()
|
| 336 |
+
|
| 337 |
+
Hooks.run_after_each(return_value, scenario, scenario_context)
|
| 338 |
+
return_value
|
| 339 |
+
end
|
| 340 |
+
|
| 341 |
+
@doc """
|
| 342 |
+
Takes one measure with the given collector.
|
| 343 |
+
|
| 344 |
+
Correctly dispatches based on the number of iterations to perform.
|
| 345 |
+
"""
|
| 346 |
+
def collect(
|
| 347 |
+
scenario = %Scenario{function: function},
|
| 348 |
+
scenario_context = %ScenarioContext{
|
| 349 |
+
num_iterations: 1
|
| 350 |
+
},
|
| 351 |
+
collector
|
| 352 |
+
) do
|
| 353 |
+
new_input = Hooks.run_before_each(scenario, scenario_context)
|
| 354 |
+
function = main_function(function, new_input)
|
| 355 |
+
|
| 356 |
+
{measurement, return_value} = invoke_collector(collector, function)
|
| 357 |
+
|
| 358 |
+
Hooks.run_after_each(return_value, scenario, scenario_context)
|
| 359 |
+
measurement
|
| 360 |
+
end
|
| 361 |
+
|
| 362 |
+
def collect(
|
| 363 |
+
scenario,
|
| 364 |
+
scenario_context = %ScenarioContext{
|
| 365 |
+
num_iterations: iterations
|
| 366 |
+
},
|
| 367 |
+
collector
|
| 368 |
+
)
|
| 369 |
+
when iterations > 1 do
|
| 370 |
+
RepeatedMeasurement.collect(scenario, scenario_context, collector)
|
| 371 |
+
end
|
| 372 |
+
|
| 373 |
+
def main_function(function, @no_input), do: function
|
| 374 |
+
def main_function(function, input), do: fn -> function.(input) end
|
| 375 |
+
|
| 376 |
+
defp invoke_collector({collector, collector_opts}, function),
|
| 377 |
+
do: collector.collect(function, collector_opts)
|
| 378 |
+
|
| 379 |
+
defp invoke_collector(collector, function), do: collector.collect(function)
|
| 380 |
+
end
|
deps/benchee/lib/benchee/benchmark/scenario_context.ex
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
defmodule Benchee.Benchmark.ScenarioContext do
|
| 2 |
+
@moduledoc """
|
| 3 |
+
Internal struct the runner & related modules deal with to run a scenario.
|
| 4 |
+
|
| 5 |
+
Practically bundles information the runner needs to be aware of while running
|
| 6 |
+
a scenario such as the current_time, end_time, printer, input, function call
|
| 7 |
+
overhead etc.
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
# This struct holds the context in which any scenario is run.
|
| 11 |
+
|
| 12 |
+
defstruct [
|
| 13 |
+
:config,
|
| 14 |
+
:printer,
|
| 15 |
+
:system,
|
| 16 |
+
:current_time,
|
| 17 |
+
:end_time,
|
| 18 |
+
# before_scenario can alter the original input
|
| 19 |
+
:scenario_input,
|
| 20 |
+
num_iterations: 1,
|
| 21 |
+
function_call_overhead: 0,
|
| 22 |
+
sample_size: 0
|
| 23 |
+
]
|
| 24 |
+
|
| 25 |
+
@type t :: %__MODULE__{
|
| 26 |
+
config: Benchee.Benchmark.BenchmarkConfig.t(),
|
| 27 |
+
printer: module,
|
| 28 |
+
current_time: pos_integer | nil,
|
| 29 |
+
end_time: pos_integer | nil,
|
| 30 |
+
scenario_input: any,
|
| 31 |
+
num_iterations: pos_integer,
|
| 32 |
+
function_call_overhead: non_neg_integer,
|
| 33 |
+
sample_size: non_neg_integer()
|
| 34 |
+
}
|
| 35 |
+
end
|
deps/benchee/lib/benchee/collection_data.ex
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
defmodule Benchee.CollectionData do
|
| 2 |
+
@moduledoc """
|
| 3 |
+
The unified data structure for a given collection of data.
|
| 4 |
+
|
| 5 |
+
Consists of the recorded `samples` and the statistics computed from them.
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
alias Benchee.Statistics
|
| 9 |
+
|
| 10 |
+
defstruct statistics: %Statistics{}, samples: []
|
| 11 |
+
|
| 12 |
+
@typedoc """
|
| 13 |
+
Samples and statistics.
|
| 14 |
+
|
| 15 |
+
Statistics might only come later when they are computed.
|
| 16 |
+
"""
|
| 17 |
+
@type t :: %__MODULE__{
|
| 18 |
+
samples: [float | non_neg_integer],
|
| 19 |
+
statistics: Statistics.t()
|
| 20 |
+
}
|
| 21 |
+
end
|
deps/benchee/lib/benchee/configuration.ex
ADDED
|
@@ -0,0 +1,400 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
defmodule Benchee.Configuration do
|
| 2 |
+
@moduledoc """
|
| 3 |
+
Functions to handle the configuration of Benchee, exposes `init/1` function.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
alias Benchee.{
|
| 7 |
+
Benchmark.Hooks,
|
| 8 |
+
Configuration,
|
| 9 |
+
Conversion.Duration,
|
| 10 |
+
Conversion.Scale,
|
| 11 |
+
Formatters.Console,
|
| 12 |
+
Suite,
|
| 13 |
+
Utility.DeepConvert
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
defstruct parallel: 1,
|
| 17 |
+
time: 5,
|
| 18 |
+
warmup: 2,
|
| 19 |
+
memory_time: 0.0,
|
| 20 |
+
reduction_time: 0.0,
|
| 21 |
+
pre_check: false,
|
| 22 |
+
formatters: [Console],
|
| 23 |
+
percentiles: [50, 99],
|
| 24 |
+
print: %{
|
| 25 |
+
benchmarking: true,
|
| 26 |
+
configuration: true,
|
| 27 |
+
fast_warning: true
|
| 28 |
+
},
|
| 29 |
+
inputs: nil,
|
| 30 |
+
input_names: [],
|
| 31 |
+
save: false,
|
| 32 |
+
load: false,
|
| 33 |
+
unit_scaling: :best,
|
| 34 |
+
# If you/your plugin/whatever needs it your data can go here
|
| 35 |
+
assigns: %{},
|
| 36 |
+
before_each: nil,
|
| 37 |
+
after_each: nil,
|
| 38 |
+
before_scenario: nil,
|
| 39 |
+
after_scenario: nil,
|
| 40 |
+
measure_function_call_overhead: false,
|
| 41 |
+
title: nil,
|
| 42 |
+
profile_after: false,
|
| 43 |
+
# Why a million? Well, it's a nice number :P
|
| 44 |
+
# Seriously, though: That's usually enough samples to be significant enough as a
|
| 45 |
+
# result.
|
| 46 |
+
# At the same time, at least on my machine it takes less than one second for
|
| 47 |
+
# `Statistex` to calculate the statistics for a list that large.
|
| 48 |
+
# It also generates less than 1GB in data (some of which is garbage collected/
|
| 49 |
+
# not necessarily all in RAM at the same time) - which seems reasonable enough.
|
| 50 |
+
# see `samples/statistics_performance.exs` and also maybe run it yourself.
|
| 51 |
+
max_sample_size: 1_000_000,
|
| 52 |
+
exclude_outliers: false
|
| 53 |
+
|
| 54 |
+
@typedoc """
|
| 55 |
+
The configuration supplied by the user as either a map or a keyword list
|
| 56 |
+
|
| 57 |
+
Possible options are:
|
| 58 |
+
|
| 59 |
+
Possible options:
|
| 60 |
+
|
| 61 |
+
* `warmup` - the time in seconds for which a benchmarking job should be run
|
| 62 |
+
without measuring times before "real" measurements start. This simulates a
|
| 63 |
+
_"warm"_ running system. Defaults to 2.
|
| 64 |
+
* `time` - the time in seconds for how long each individual benchmarking job
|
| 65 |
+
should be run for measuring the execution times (run time performance).
|
| 66 |
+
Defaults to 5.
|
| 67 |
+
* `memory_time` - the time in seconds for how long memory measurements
|
| 68 |
+
should be conducted. Defaults to 0 (turned off).
|
| 69 |
+
* `inputs` - a map from descriptive input names to some different input,
|
| 70 |
+
your benchmarking jobs will then be run with each of these inputs. For this
|
| 71 |
+
to work your benchmarking function gets the current input passed in as an
|
| 72 |
+
argument into the function. Defaults to `nil`, aka no input specified and
|
| 73 |
+
functions are called without an argument.
|
| 74 |
+
* `title` - this option is purely cosmetic. If you would like to add a
|
| 75 |
+
title with some meaning to a given suite, you can do so by providing
|
| 76 |
+
a single string here. This is only for use by formatters.
|
| 77 |
+
* `formatters` - list of formatters either as a module implementing the formatter
|
| 78 |
+
behaviour, a tuple of said module and options it should take or formatter
|
| 79 |
+
functions. They are run when using `Benchee.run/2` or you can invoktem them
|
| 80 |
+
through `Benchee.Formatter.output/1`. Functions need to accept one argument (which
|
| 81 |
+
is the benchmarking suite with all data) and then use that to produce output. Used
|
| 82 |
+
for plugins. Defaults to the builtin console formatter
|
| 83 |
+
`Benchee.Formatters.Console`. See [Formatters](#formatters).
|
| 84 |
+
* `pre_check` - whether or not to run each job with each input - including all
|
| 85 |
+
given before or after scenario or each hooks - before the benchmarks are
|
| 86 |
+
measured to ensure that your code executes without error. This can save time
|
| 87 |
+
while developing your suites. Defaults to `false`. Possible values are:
|
| 88 |
+
* `false` - no pre check is run
|
| 89 |
+
* `true` - each scenario is run but the return value is ignored
|
| 90 |
+
* `:all_same` - raises if all scenarios are not returning the same value for
|
| 91 |
+
each input. This is useful when benchmarking alternative implementations of a
|
| 92 |
+
deterministic function.
|
| 93 |
+
* `parallel` - each the function of each job will be executed in
|
| 94 |
+
`parallel` number processes. If `parallel` is `4` then 4 processes will be
|
| 95 |
+
spawned that all execute the _same_ function for the given time. When these
|
| 96 |
+
finish/the time is up 4 new processes will be spawned for the next
|
| 97 |
+
job/function. This gives you more data in the same time, but also puts a
|
| 98 |
+
load on the system interfering with benchmark results. For more on the pros
|
| 99 |
+
and cons of parallel benchmarking [check the
|
| 100 |
+
wiki](https://github.com/bencheeorg/benchee/wiki/Parallel-Benchmarking).
|
| 101 |
+
Defaults to 1 (no parallel execution).
|
| 102 |
+
* `save` - specify a `path` where to store the results of the current
|
| 103 |
+
benchmarking suite, tagged with the specified `tag`.
|
| 104 |
+
* `load` - load saved suit or suits to compare your current benchmarks
|
| 105 |
+
against. Can be a string or a list of strings or patterns.
|
| 106 |
+
* `print` - a map from atoms to `true` or `false` to configure if the
|
| 107 |
+
output identified by the atom will be printed. All options are enabled by
|
| 108 |
+
default (true). Options are:
|
| 109 |
+
* `:benchmarking` - print when Benchee starts benchmarking a new job
|
| 110 |
+
(Benchmarking name ..) as well as when statistics are being calculated
|
| 111 |
+
or formatting begins.
|
| 112 |
+
* `:configuration` - a summary of configured benchmarking options
|
| 113 |
+
including estimated total run time is printed before benchmarking starts
|
| 114 |
+
* `:fast_warning` - warnings are displayed if functions are executed
|
| 115 |
+
too fast leading to inaccurate measures
|
| 116 |
+
* `console` - options for the built-in console formatter:
|
| 117 |
+
* `:comparison` - if the comparison of the different benchmarking jobs
|
| 118 |
+
(x times slower than) is shown (true/false). Enabled by default.
|
| 119 |
+
* `extended_statistics` - display more statistics, aka `minimum`,
|
| 120 |
+
`maximum`, `sample_size` and `mode`. Disabled by default.
|
| 121 |
+
* `percentiles` - if you are using extended statistics and want to see the
|
| 122 |
+
results for certain percentiles of results beyond just the median.
|
| 123 |
+
Defaults to [50, 99] to calculate the 50th and 99th percentiles.
|
| 124 |
+
* `:unit_scaling` - the strategy for choosing a unit for durations and
|
| 125 |
+
counts. May or may not be implemented by a given formatter (The console
|
| 126 |
+
formatter implements it). When scaling a value, Benchee finds the "best fit"
|
| 127 |
+
unit (the largest unit for which the result is at least 1). For example,
|
| 128 |
+
1_200_000 scales to `1.2 M`, while `800_000` scales to `800 K`. The
|
| 129 |
+
`unit_scaling` strategy determines how Benchee chooses the best fit unit for
|
| 130 |
+
an entire list of values, when the individual values in the list may have
|
| 131 |
+
different best fit units. There are four strategies, defaulting to `:best`:
|
| 132 |
+
* `:best` - the most frequent best fit unit will be used, a tie
|
| 133 |
+
will result in the larger unit being selected.
|
| 134 |
+
* `:largest` - the largest best fit unit will be used (i.e. thousand
|
| 135 |
+
and seconds if values are large enough).
|
| 136 |
+
* `:smallest` - the smallest best fit unit will be used (i.e.
|
| 137 |
+
millisecond and one)
|
| 138 |
+
* `:none` - no unit scaling will occur. Durations will be displayed
|
| 139 |
+
in microseconds, and counts will be displayed in ones (this is
|
| 140 |
+
equivalent to the behaviour Benchee had pre 0.5.0)
|
| 141 |
+
* `:before_scenario`/`after_scenario`/`before_each`/`after_each` - read up on them in the hooks section in the README
|
| 142 |
+
* `:measure_function_call_overhead` - Measure how long an empty function call takes and deduct this from each measure run time. This overhead should be negligible for all but the most micro benchmarks. Defaults to false.
|
| 143 |
+
* `profile_after` - accepts any of the following options:
|
| 144 |
+
* a boolean - `true` will enable profiling with the default profiler
|
| 145 |
+
(`:eprof`) and `false` will disable profiling. Defaults to `false`.
|
| 146 |
+
* a profiler - either as a tuple of `{profiler, opts}` (e.g., `{:fprof, [sort: :own]}`)
|
| 147 |
+
or just the profiler (e.g., `:fprof`), which is equivalent to `{profiler, []}`. The accepted built-in profilers are:
|
| 148 |
+
[`:cprof`](https://hexdocs.pm/mix/Mix.Tasks.Profile.Cprof.html),
|
| 149 |
+
[`:eprof`](https://hexdocs.pm/mix/Mix.Tasks.Profile.Eprof.html)
|
| 150 |
+
[`:fprof`](https://hexdocs.pm/mix/Mix.Tasks.Profile.Fprof.html) and
|
| 151 |
+
[`:tprof`](https://hexdocs.pm/mix/Mix.Tasks.Profile.Tprof.html) (requires elixir >= 1.17 and erlang >= 27).
|
| 152 |
+
* `max_sample_size` - the maximum number of samples collected per scenario. Defaults to `1_000_000`.
|
| 153 |
+
This is used to limit memory consumption and unnecessary processing - 1 Million samples is plenty.
|
| 154 |
+
This limit also applies to number of iterations done during warmup.
|
| 155 |
+
You can set your own number or set it to `nil` if you don't want any limit.
|
| 156 |
+
* `exclude_outliers` - whether or not statistical outliers should be removed for the calculated statistics.
|
| 157 |
+
Defaults to `false`.
|
| 158 |
+
This means that values that are far outside the usual range (as determined by the percentiles/quantiles) will
|
| 159 |
+
be removed from the gathered samples and the calculated statistics. You might want to enable this if you
|
| 160 |
+
don't want things like the garbage collection triggering to influence your results as much.
|
| 161 |
+
"""
|
| 162 |
+
@type user_configuration :: map | keyword
|
| 163 |
+
|
| 164 |
+
@typedoc """
|
| 165 |
+
Generated configuration struct from the user supplied configuration options.
|
| 166 |
+
|
| 167 |
+
Filled in with a lot of defaults. Also notably every option is already converted to
|
| 168 |
+
a map or struct at this point for easier handling in Benchee.
|
| 169 |
+
"""
|
| 170 |
+
@type t :: %__MODULE__{
|
| 171 |
+
parallel: pos_integer,
|
| 172 |
+
time: number,
|
| 173 |
+
warmup: number,
|
| 174 |
+
memory_time: number,
|
| 175 |
+
reduction_time: number,
|
| 176 |
+
pre_check: boolean,
|
| 177 |
+
formatters: [(Suite.t() -> Suite.t()) | module | {module, user_configuration}],
|
| 178 |
+
print: map,
|
| 179 |
+
inputs: %{Suite.key() => any} | [{Suite.key(), any}] | nil,
|
| 180 |
+
input_names: [String.t()],
|
| 181 |
+
save: map | false,
|
| 182 |
+
load: String.t() | [String.t()] | false,
|
| 183 |
+
unit_scaling: Scale.scaling_strategy(),
|
| 184 |
+
assigns: map,
|
| 185 |
+
before_each: Hooks.hook_function() | nil,
|
| 186 |
+
after_each: Hooks.hook_function() | nil,
|
| 187 |
+
before_scenario: Hooks.hook_function() | nil,
|
| 188 |
+
after_scenario: Hooks.hook_function() | nil,
|
| 189 |
+
measure_function_call_overhead: boolean,
|
| 190 |
+
title: String.t() | nil,
|
| 191 |
+
profile_after: boolean | atom | {atom, keyword},
|
| 192 |
+
max_sample_size: pos_integer(),
|
| 193 |
+
exclude_outliers: boolean()
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
@time_keys [:time, :warmup, :memory_time, :reduction_time]
|
| 197 |
+
|
| 198 |
+
@doc """
|
| 199 |
+
Returns the initial benchmark configuration for Benchee, composed of defaults
|
| 200 |
+
and an optional custom configuration.
|
| 201 |
+
|
| 202 |
+
Configuration times are given in seconds, but are converted to microseconds
|
| 203 |
+
internally.
|
| 204 |
+
|
| 205 |
+
For a list of all possible options see `t:user_configuration/0`
|
| 206 |
+
|
| 207 |
+
## Examples
|
| 208 |
+
|
| 209 |
+
iex> init()
|
| 210 |
+
%Benchee.Suite{
|
| 211 |
+
configuration: %Benchee.Configuration{
|
| 212 |
+
parallel: 1,
|
| 213 |
+
time: 5_000_000_000.0,
|
| 214 |
+
warmup: 2_000_000_000.0,
|
| 215 |
+
max_sample_size: 1_000_000,
|
| 216 |
+
inputs: nil,
|
| 217 |
+
input_names: [],
|
| 218 |
+
save: false,
|
| 219 |
+
load: false,
|
| 220 |
+
formatters: [Benchee.Formatters.Console],
|
| 221 |
+
print: %{
|
| 222 |
+
benchmarking: true,
|
| 223 |
+
fast_warning: true,
|
| 224 |
+
configuration: true
|
| 225 |
+
},
|
| 226 |
+
percentiles: [50, 99],
|
| 227 |
+
unit_scaling: :best,
|
| 228 |
+
assigns: %{},
|
| 229 |
+
before_each: nil,
|
| 230 |
+
after_each: nil,
|
| 231 |
+
before_scenario: nil,
|
| 232 |
+
after_scenario: nil
|
| 233 |
+
},
|
| 234 |
+
system: nil,
|
| 235 |
+
scenarios: []
|
| 236 |
+
}
|
| 237 |
+
|
| 238 |
+
iex> init(time: 1, warmup: 0.2)
|
| 239 |
+
%Benchee.Suite{
|
| 240 |
+
configuration: %Benchee.Configuration{
|
| 241 |
+
parallel: 1,
|
| 242 |
+
time: 1_000_000_000.0,
|
| 243 |
+
warmup: 200_000_000.0,
|
| 244 |
+
max_sample_size: 1_000_000,
|
| 245 |
+
inputs: nil,
|
| 246 |
+
input_names: [],
|
| 247 |
+
save: false,
|
| 248 |
+
load: false,
|
| 249 |
+
formatters: [Benchee.Formatters.Console],
|
| 250 |
+
print: %{
|
| 251 |
+
benchmarking: true,
|
| 252 |
+
fast_warning: true,
|
| 253 |
+
configuration: true
|
| 254 |
+
},
|
| 255 |
+
percentiles: [50, 99],
|
| 256 |
+
unit_scaling: :best,
|
| 257 |
+
assigns: %{},
|
| 258 |
+
before_each: nil,
|
| 259 |
+
after_each: nil,
|
| 260 |
+
before_scenario: nil,
|
| 261 |
+
after_scenario: nil
|
| 262 |
+
},
|
| 263 |
+
system: nil,
|
| 264 |
+
scenarios: []
|
| 265 |
+
}
|
| 266 |
+
|
| 267 |
+
iex> init(
|
| 268 |
+
...> parallel: 2,
|
| 269 |
+
...> time: 1,
|
| 270 |
+
...> warmup: 0.2,
|
| 271 |
+
...> max_sample_size: 500,
|
| 272 |
+
...> formatters: [&IO.puts/1],
|
| 273 |
+
...> print: [fast_warning: false],
|
| 274 |
+
...> inputs: %{"Small" => 5, "Big" => 9999},
|
| 275 |
+
...> unit_scaling: :smallest
|
| 276 |
+
...> )
|
| 277 |
+
%Benchee.Suite{
|
| 278 |
+
configuration: %Benchee.Configuration{
|
| 279 |
+
parallel: 2,
|
| 280 |
+
time: 1_000_000_000.0,
|
| 281 |
+
warmup: 200_000_000.0,
|
| 282 |
+
max_sample_size: 500,
|
| 283 |
+
inputs: [{"Big", 9999}, {"Small", 5}],
|
| 284 |
+
input_names: ["Big", "Small"],
|
| 285 |
+
save: false,
|
| 286 |
+
load: false,
|
| 287 |
+
formatters: [&IO.puts/1],
|
| 288 |
+
print: %{
|
| 289 |
+
benchmarking: true,
|
| 290 |
+
fast_warning: false,
|
| 291 |
+
configuration: true
|
| 292 |
+
},
|
| 293 |
+
percentiles: [50, 99],
|
| 294 |
+
unit_scaling: :smallest,
|
| 295 |
+
assigns: %{},
|
| 296 |
+
before_each: nil,
|
| 297 |
+
after_each: nil,
|
| 298 |
+
before_scenario: nil,
|
| 299 |
+
after_scenario: nil
|
| 300 |
+
},
|
| 301 |
+
system: nil,
|
| 302 |
+
scenarios: []
|
| 303 |
+
}
|
| 304 |
+
"""
|
| 305 |
+
@spec init(user_configuration) :: Suite.t()
|
| 306 |
+
def init(config \\ %{}) do
|
| 307 |
+
:ok = :timer.start()
|
| 308 |
+
|
| 309 |
+
config =
|
| 310 |
+
config
|
| 311 |
+
|> standardized_user_configuration()
|
| 312 |
+
|> merge_with_defaults()
|
| 313 |
+
|> convert_time_to_nano_s()
|
| 314 |
+
|> save_option_conversion()
|
| 315 |
+
|> add_input_names()
|
| 316 |
+
|
| 317 |
+
%Suite{configuration: config}
|
| 318 |
+
end
|
| 319 |
+
|
| 320 |
+
defp standardized_user_configuration(config) do
|
| 321 |
+
config
|
| 322 |
+
|> DeepConvert.to_map([:formatters, :inputs])
|
| 323 |
+
|> standardize_inputs()
|
| 324 |
+
end
|
| 325 |
+
|
| 326 |
+
defp standardize_inputs(config = %{inputs: inputs}) do
|
| 327 |
+
standardized_inputs =
|
| 328 |
+
inputs
|
| 329 |
+
|> Enum.reduce([], &standardize_inputs/2)
|
| 330 |
+
|> Enum.reverse()
|
| 331 |
+
|
| 332 |
+
%{config | inputs: standardized_inputs}
|
| 333 |
+
end
|
| 334 |
+
|
| 335 |
+
defp standardize_inputs(config), do: config
|
| 336 |
+
|
| 337 |
+
defp standardize_inputs({name, value}, acc) do
|
| 338 |
+
normalized_name = to_string(name)
|
| 339 |
+
|
| 340 |
+
if List.keymember?(acc, normalized_name, 0) do
|
| 341 |
+
acc
|
| 342 |
+
else
|
| 343 |
+
[{normalized_name, value} | acc]
|
| 344 |
+
end
|
| 345 |
+
end
|
| 346 |
+
|
| 347 |
+
defp add_input_names(config) do
|
| 348 |
+
input_names =
|
| 349 |
+
case config.inputs do
|
| 350 |
+
nil -> []
|
| 351 |
+
inputs -> Enum.map(inputs, fn {name, _value} -> name end)
|
| 352 |
+
end
|
| 353 |
+
|
| 354 |
+
%{config | input_names: input_names}
|
| 355 |
+
end
|
| 356 |
+
|
| 357 |
+
defp merge_with_defaults(user_config) do
|
| 358 |
+
DeepMerge.deep_merge(%Configuration{}, user_config)
|
| 359 |
+
end
|
| 360 |
+
|
| 361 |
+
defp convert_time_to_nano_s(config) do
|
| 362 |
+
Enum.reduce(@time_keys, config, fn key, new_config ->
|
| 363 |
+
{_, new_config} =
|
| 364 |
+
Map.get_and_update!(new_config, key, fn seconds ->
|
| 365 |
+
{seconds, Duration.convert_value({seconds, :second}, :nanosecond)}
|
| 366 |
+
end)
|
| 367 |
+
|
| 368 |
+
new_config
|
| 369 |
+
end)
|
| 370 |
+
end
|
| 371 |
+
|
| 372 |
+
defp save_option_conversion(config = %{save: false}), do: config
|
| 373 |
+
|
| 374 |
+
defp save_option_conversion(config = %__MODULE__{save: save_values}) do
|
| 375 |
+
save_options = Map.merge(save_defaults(), save_values)
|
| 376 |
+
tagged_save_options = %{tag: save_options.tag, path: save_options.path}
|
| 377 |
+
formatters = config.formatters ++ [{Benchee.Formatters.TaggedSave, tagged_save_options}]
|
| 378 |
+
%__MODULE__{config | formatters: formatters}
|
| 379 |
+
end
|
| 380 |
+
|
| 381 |
+
defp save_defaults do
|
| 382 |
+
now = DateTime.utc_now()
|
| 383 |
+
|
| 384 |
+
%{
|
| 385 |
+
tag: "#{now.year}-#{now.month}-#{now.day}--#{now.hour}-#{now.minute}-#{now.second}-utc",
|
| 386 |
+
path: "benchmark.benchee"
|
| 387 |
+
}
|
| 388 |
+
end
|
| 389 |
+
end
|
| 390 |
+
|
| 391 |
+
defimpl DeepMerge.Resolver, for: Benchee.Configuration do
|
| 392 |
+
def resolve(_original, override = %{__struct__: Benchee.Configuration}, _) do
|
| 393 |
+
override
|
| 394 |
+
end
|
| 395 |
+
|
| 396 |
+
def resolve(original, override, resolver) when is_map(override) do
|
| 397 |
+
merged = Map.merge(original, override, resolver)
|
| 398 |
+
struct!(Benchee.Configuration, Map.to_list(merged))
|
| 399 |
+
end
|
| 400 |
+
end
|
deps/benchee/lib/benchee/conversion.ex
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
defmodule Benchee.Conversion do
|
| 2 |
+
@moduledoc """
|
| 3 |
+
Integration of the conversion of multiple units with Benchee.
|
| 4 |
+
|
| 5 |
+
Can be used by plugins to use Benchee unit scaling logic.
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
alias Benchee.Conversion.{Count, Duration, Memory}
|
| 9 |
+
|
| 10 |
+
@doc """
|
| 11 |
+
Takes scenarios and a given scaling_strategy, returns the best units for the
|
| 12 |
+
given scaling strategy. The return value changes based on whether you want
|
| 13 |
+
units for run time or memory usage.
|
| 14 |
+
|
| 15 |
+
The units can then be passed on to the appropriate `format` calls to format
|
| 16 |
+
the output of arbitrary values with the right unit.
|
| 17 |
+
|
| 18 |
+
## Examples
|
| 19 |
+
|
| 20 |
+
iex> statistics = %Benchee.Statistics{average: 1_000_000.0, ips: 1000.0}
|
| 21 |
+
...>
|
| 22 |
+
...> scenario = %Benchee.Scenario{
|
| 23 |
+
...> run_time_data: %Benchee.CollectionData{statistics: statistics},
|
| 24 |
+
...> memory_usage_data: %Benchee.CollectionData{statistics: statistics},
|
| 25 |
+
...> reductions_data: %Benchee.CollectionData{statistics: statistics}
|
| 26 |
+
...> }
|
| 27 |
+
...>
|
| 28 |
+
...> Benchee.Conversion.units([scenario], :best)
|
| 29 |
+
%{
|
| 30 |
+
ips: %Benchee.Conversion.Unit{
|
| 31 |
+
label: "K",
|
| 32 |
+
long: "Thousand",
|
| 33 |
+
magnitude: 1000,
|
| 34 |
+
name: :thousand
|
| 35 |
+
},
|
| 36 |
+
run_time: %Benchee.Conversion.Unit{
|
| 37 |
+
label: "ms",
|
| 38 |
+
long: "Milliseconds",
|
| 39 |
+
magnitude: 1_000_000,
|
| 40 |
+
name: :millisecond
|
| 41 |
+
},
|
| 42 |
+
memory: %Benchee.Conversion.Unit{
|
| 43 |
+
label: "KB",
|
| 44 |
+
long: "Kilobytes",
|
| 45 |
+
magnitude: 1024,
|
| 46 |
+
name: :kilobyte
|
| 47 |
+
},
|
| 48 |
+
reduction_count: %Benchee.Conversion.Unit{
|
| 49 |
+
label: "M",
|
| 50 |
+
long: "Million",
|
| 51 |
+
magnitude: 1_000_000,
|
| 52 |
+
name: :million
|
| 53 |
+
}
|
| 54 |
+
}
|
| 55 |
+
"""
|
| 56 |
+
def units(scenarios, scaling_strategy) do
|
| 57 |
+
run_time_measurements = measurements_for(scenarios, :run_time_data)
|
| 58 |
+
reductions_measurements = measurements_for(scenarios, :reductions_data)
|
| 59 |
+
memory_measurements = measurements_for(scenarios, :memory_usage_data)
|
| 60 |
+
|
| 61 |
+
%{
|
| 62 |
+
run_time: Duration.best(run_time_measurements.average, strategy: scaling_strategy),
|
| 63 |
+
ips: Count.best(run_time_measurements.ips, strategy: scaling_strategy),
|
| 64 |
+
memory: Memory.best(memory_measurements.average, strategy: scaling_strategy),
|
| 65 |
+
reduction_count: Count.best(reductions_measurements.average, strategry: scaling_strategy)
|
| 66 |
+
}
|
| 67 |
+
end
|
| 68 |
+
|
| 69 |
+
defp measurements_for(scenarios, path) do
|
| 70 |
+
paths = [Access.key(path), Access.key(:statistics)]
|
| 71 |
+
|
| 72 |
+
scenarios
|
| 73 |
+
|> Enum.flat_map(fn scenario -> scenario |> get_in(paths) |> Map.to_list() end)
|
| 74 |
+
|> Enum.group_by(fn {stat_name, _} -> stat_name end, fn {_, value} -> value end)
|
| 75 |
+
end
|
| 76 |
+
end
|
deps/benchee/lib/benchee/conversion/count.ex
ADDED
|
@@ -0,0 +1,241 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
defmodule Benchee.Conversion.Count do
|
| 2 |
+
@moduledoc """
|
| 3 |
+
Unit scaling for counts, such that 1000000 can be converted to 1 Million.
|
| 4 |
+
|
| 5 |
+
Only Benchee plugins should use this code.
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
alias Benchee.Conversion.{Format, Scale, Unit}
|
| 9 |
+
|
| 10 |
+
@behaviour Scale
|
| 11 |
+
@behaviour Format
|
| 12 |
+
|
| 13 |
+
@one_billion 1_000_000_000
|
| 14 |
+
@one_million 1_000_000
|
| 15 |
+
@one_thousand 1_000
|
| 16 |
+
|
| 17 |
+
@units_map %{
|
| 18 |
+
billion: %Unit{
|
| 19 |
+
name: :billion,
|
| 20 |
+
magnitude: @one_billion,
|
| 21 |
+
label: "B",
|
| 22 |
+
long: "Billion"
|
| 23 |
+
},
|
| 24 |
+
million: %Unit{
|
| 25 |
+
name: :million,
|
| 26 |
+
magnitude: @one_million,
|
| 27 |
+
label: "M",
|
| 28 |
+
long: "Million"
|
| 29 |
+
},
|
| 30 |
+
thousand: %Unit{
|
| 31 |
+
name: :thousand,
|
| 32 |
+
magnitude: @one_thousand,
|
| 33 |
+
label: "K",
|
| 34 |
+
long: "Thousand"
|
| 35 |
+
},
|
| 36 |
+
one: %Unit{
|
| 37 |
+
name: :one,
|
| 38 |
+
magnitude: 1,
|
| 39 |
+
label: "",
|
| 40 |
+
long: ""
|
| 41 |
+
}
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
@units Map.values(@units_map)
|
| 45 |
+
|
| 46 |
+
@type unit_atoms :: :one | :thousand | :million | :billion
|
| 47 |
+
@type units :: unit_atoms | Unit.t()
|
| 48 |
+
|
| 49 |
+
@doc """
|
| 50 |
+
Scales a value representing a count in ones into a larger unit if appropriate
|
| 51 |
+
|
| 52 |
+
## Examples
|
| 53 |
+
|
| 54 |
+
iex> {value, unit} = scale(4_321.09)
|
| 55 |
+
...> value
|
| 56 |
+
4.32109
|
| 57 |
+
iex> unit.name
|
| 58 |
+
:thousand
|
| 59 |
+
|
| 60 |
+
iex> {value, unit} = scale(0.0045)
|
| 61 |
+
...> value
|
| 62 |
+
0.0045
|
| 63 |
+
iex> unit.name
|
| 64 |
+
:one
|
| 65 |
+
|
| 66 |
+
"""
|
| 67 |
+
def scale(count) when count >= @one_billion do
|
| 68 |
+
scale_with_unit(count, :billion)
|
| 69 |
+
end
|
| 70 |
+
|
| 71 |
+
def scale(count) when count >= @one_million do
|
| 72 |
+
scale_with_unit(count, :million)
|
| 73 |
+
end
|
| 74 |
+
|
| 75 |
+
def scale(count) when count >= @one_thousand do
|
| 76 |
+
scale_with_unit(count, :thousand)
|
| 77 |
+
end
|
| 78 |
+
|
| 79 |
+
def scale(count) do
|
| 80 |
+
scale_with_unit(count, :one)
|
| 81 |
+
end
|
| 82 |
+
|
| 83 |
+
# Helper function for returning a tuple of {value, unit}
|
| 84 |
+
defp scale_with_unit(count, unit) do
|
| 85 |
+
{scale(count, unit), unit_for(unit)}
|
| 86 |
+
end
|
| 87 |
+
|
| 88 |
+
@doc """
|
| 89 |
+
Get a unit by its atom representation. If handed already a %Unit{} struct it
|
| 90 |
+
just returns it.
|
| 91 |
+
|
| 92 |
+
## Examples
|
| 93 |
+
|
| 94 |
+
iex> unit_for(:thousand)
|
| 95 |
+
%Benchee.Conversion.Unit{
|
| 96 |
+
name: :thousand,
|
| 97 |
+
magnitude: 1_000,
|
| 98 |
+
label: "K",
|
| 99 |
+
long: "Thousand"
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
iex> unit_for(%Benchee.Conversion.Unit{
|
| 103 |
+
...> name: :thousand,
|
| 104 |
+
...> magnitude: 1_000,
|
| 105 |
+
...> label: "K",
|
| 106 |
+
...> long: "Thousand"
|
| 107 |
+
...> })
|
| 108 |
+
%Benchee.Conversion.Unit{
|
| 109 |
+
name: :thousand,
|
| 110 |
+
magnitude: 1_000,
|
| 111 |
+
label: "K",
|
| 112 |
+
long: "Thousand"
|
| 113 |
+
}
|
| 114 |
+
"""
|
| 115 |
+
def unit_for(unit) do
|
| 116 |
+
Scale.unit_for(@units_map, unit)
|
| 117 |
+
end
|
| 118 |
+
|
| 119 |
+
def units do
|
| 120 |
+
@units
|
| 121 |
+
end
|
| 122 |
+
|
| 123 |
+
@doc """
|
| 124 |
+
Scales a value representing a count in ones into a specified unit
|
| 125 |
+
|
| 126 |
+
## Examples
|
| 127 |
+
|
| 128 |
+
iex> scale(12345, :one)
|
| 129 |
+
12345.0
|
| 130 |
+
|
| 131 |
+
iex> scale(12345, :thousand)
|
| 132 |
+
12.345
|
| 133 |
+
|
| 134 |
+
iex> scale(12345, :billion)
|
| 135 |
+
1.2345e-5
|
| 136 |
+
|
| 137 |
+
iex> scale(12345, :million)
|
| 138 |
+
0.012345
|
| 139 |
+
|
| 140 |
+
"""
|
| 141 |
+
def scale(count, unit) do
|
| 142 |
+
Scale.scale(count, unit, __MODULE__)
|
| 143 |
+
end
|
| 144 |
+
|
| 145 |
+
@doc """
|
| 146 |
+
Converts a value for a specified %Unit or unit atom and converts it to the equivalent of another unit of measure.
|
| 147 |
+
|
| 148 |
+
## Examples
|
| 149 |
+
|
| 150 |
+
iex> {value, unit} = convert({2500, :thousand}, :million)
|
| 151 |
+
...> value
|
| 152 |
+
2.5
|
| 153 |
+
iex> unit.name
|
| 154 |
+
:million
|
| 155 |
+
"""
|
| 156 |
+
def convert(number_and_unit, desired_unit) do
|
| 157 |
+
Scale.convert(number_and_unit, desired_unit, __MODULE__)
|
| 158 |
+
end
|
| 159 |
+
|
| 160 |
+
@doc """
|
| 161 |
+
Finds the best unit for a list of counts. By default, chooses the most common
|
| 162 |
+
unit. In case of tie, chooses the largest of the most common units.
|
| 163 |
+
|
| 164 |
+
Pass `[strategy: :smallest]` to always return the smallest unit in the list.
|
| 165 |
+
Pass `[strategy: :largest]` to always return the largest unit in the list.
|
| 166 |
+
|
| 167 |
+
## Examples
|
| 168 |
+
|
| 169 |
+
iex> best([23, 23_000, 34_000, 2_340_000]).name
|
| 170 |
+
:thousand
|
| 171 |
+
|
| 172 |
+
iex> best([23, 23_000, 34_000, 2_340_000, 3_450_000]).name
|
| 173 |
+
:million
|
| 174 |
+
|
| 175 |
+
iex> best([23, 23_000, 34_000, 2_340_000], strategy: :smallest).name
|
| 176 |
+
:one
|
| 177 |
+
|
| 178 |
+
iex> best([23, 23_000, 34_000, 2_340_000], strategy: :largest).name
|
| 179 |
+
:million
|
| 180 |
+
|
| 181 |
+
"""
|
| 182 |
+
def best(list, opts \\ [strategy: :best])
|
| 183 |
+
|
| 184 |
+
def best(list, opts) do
|
| 185 |
+
Scale.best_unit(list, __MODULE__, opts)
|
| 186 |
+
end
|
| 187 |
+
|
| 188 |
+
@doc """
|
| 189 |
+
The raw count, unscaled.
|
| 190 |
+
|
| 191 |
+
## Examples
|
| 192 |
+
|
| 193 |
+
iex> base_unit().name
|
| 194 |
+
:one
|
| 195 |
+
|
| 196 |
+
"""
|
| 197 |
+
def base_unit, do: unit_for(:one)
|
| 198 |
+
|
| 199 |
+
@doc """
|
| 200 |
+
Formats a number as a string, with a unit label.
|
| 201 |
+
|
| 202 |
+
To specify the unit, pass a tuple of `{value, unit_atom}` like `{1_234, :million}`
|
| 203 |
+
|
| 204 |
+
## Examples
|
| 205 |
+
|
| 206 |
+
iex> format(45_678.9)
|
| 207 |
+
"45.68 K"
|
| 208 |
+
|
| 209 |
+
iex> format(45.6789)
|
| 210 |
+
"45.68"
|
| 211 |
+
|
| 212 |
+
iex> format({45.6789, :thousand})
|
| 213 |
+
"45.68 K"
|
| 214 |
+
|
| 215 |
+
iex> format(
|
| 216 |
+
...> {45.6789, %Benchee.Conversion.Unit{long: "Thousand", magnitude: "1_000", label: "K"}}
|
| 217 |
+
...> )
|
| 218 |
+
"45.68 K"
|
| 219 |
+
"""
|
| 220 |
+
def format(count) do
|
| 221 |
+
Format.format(count, __MODULE__)
|
| 222 |
+
end
|
| 223 |
+
|
| 224 |
+
@doc """
|
| 225 |
+
Formats in a more "human" way separating by units.
|
| 226 |
+
|
| 227 |
+
## Examples
|
| 228 |
+
|
| 229 |
+
iex> format_human(45_678.9)
|
| 230 |
+
"45 K 678.90"
|
| 231 |
+
|
| 232 |
+
iex> format_human(1_000_000)
|
| 233 |
+
"1 M"
|
| 234 |
+
|
| 235 |
+
iex> format_human(1_001_000)
|
| 236 |
+
"1 M 1 K"
|
| 237 |
+
"""
|
| 238 |
+
def format_human(count) do
|
| 239 |
+
Format.format_human(count, __MODULE__)
|
| 240 |
+
end
|
| 241 |
+
end
|
deps/benchee/lib/benchee/conversion/deviation_percent.ex
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
defmodule Benchee.Conversion.DeviationPercent do
|
| 2 |
+
@moduledoc """
|
| 3 |
+
Helps with formatting for the standard deviation ratio converting it into the
|
| 4 |
+
more common percent form.
|
| 5 |
+
|
| 6 |
+
Only Benchee plugins should use this code.
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
alias Benchee.Conversion.Format
|
| 10 |
+
|
| 11 |
+
@behaviour Format
|
| 12 |
+
|
| 13 |
+
@doc """
|
| 14 |
+
Formats the standard deviation ratio to an equivalent percent number including special signs.
|
| 15 |
+
|
| 16 |
+
The ± is an important part of it as it shows that the deviation might be up but also might be
|
| 17 |
+
down.
|
| 18 |
+
|
| 19 |
+
## Examples
|
| 20 |
+
|
| 21 |
+
iex> format(0.12345)
|
| 22 |
+
"±12.35%"
|
| 23 |
+
|
| 24 |
+
iex> format(1)
|
| 25 |
+
"±100.00%"
|
| 26 |
+
"""
|
| 27 |
+
def format(std_dev_ratio) do
|
| 28 |
+
"~ts~.2f%"
|
| 29 |
+
|> :io_lib.format(["±", std_dev_ratio * 100.0])
|
| 30 |
+
|> to_string
|
| 31 |
+
end
|
| 32 |
+
|
| 33 |
+
@doc """
|
| 34 |
+
Formats standard deviation percent, same as `format/1`.
|
| 35 |
+
|
| 36 |
+
Implemented for consistency.
|
| 37 |
+
|
| 38 |
+
## Examples
|
| 39 |
+
|
| 40 |
+
iex> format_human(0.1)
|
| 41 |
+
"±10.00%"
|
| 42 |
+
"""
|
| 43 |
+
def format_human(std_dev_ratio) do
|
| 44 |
+
format(std_dev_ratio)
|
| 45 |
+
end
|
| 46 |
+
end
|
deps/benchee/lib/benchee/conversion/duration.ex
ADDED
|
@@ -0,0 +1,302 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
defmodule Benchee.Conversion.Duration do
|
| 2 |
+
@moduledoc """
|
| 3 |
+
Unit scaling for duration converting from microseconds to minutes and others.
|
| 4 |
+
|
| 5 |
+
Only Benchee plugins should use this code.
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
alias Benchee.Conversion.{Format, Scale, Unit}
|
| 9 |
+
|
| 10 |
+
@behaviour Scale
|
| 11 |
+
@behaviour Format
|
| 12 |
+
|
| 13 |
+
@nanoseconds_per_microsecond 1000
|
| 14 |
+
@microseconds_per_millisecond 1000
|
| 15 |
+
@milliseconds_per_second 1000
|
| 16 |
+
@seconds_per_minute 60
|
| 17 |
+
@minutes_per_hour 60
|
| 18 |
+
|
| 19 |
+
@nanoseconds_per_millisecond @nanoseconds_per_microsecond * @microseconds_per_millisecond
|
| 20 |
+
@nanoseconds_per_second @nanoseconds_per_millisecond * @milliseconds_per_second
|
| 21 |
+
@nanoseconds_per_minute @nanoseconds_per_second * @seconds_per_minute
|
| 22 |
+
@nanoseconds_per_hour @nanoseconds_per_minute * @minutes_per_hour
|
| 23 |
+
|
| 24 |
+
@units_map %{
|
| 25 |
+
hour: %Unit{
|
| 26 |
+
name: :hour,
|
| 27 |
+
magnitude: @nanoseconds_per_hour,
|
| 28 |
+
label: "h",
|
| 29 |
+
long: "Hours"
|
| 30 |
+
},
|
| 31 |
+
minute: %Unit{
|
| 32 |
+
name: :minute,
|
| 33 |
+
magnitude: @nanoseconds_per_minute,
|
| 34 |
+
label: "min",
|
| 35 |
+
long: "Minutes"
|
| 36 |
+
},
|
| 37 |
+
second: %Unit{
|
| 38 |
+
name: :second,
|
| 39 |
+
magnitude: @nanoseconds_per_second,
|
| 40 |
+
label: "s",
|
| 41 |
+
long: "Seconds"
|
| 42 |
+
},
|
| 43 |
+
millisecond: %Unit{
|
| 44 |
+
name: :millisecond,
|
| 45 |
+
magnitude: @nanoseconds_per_millisecond,
|
| 46 |
+
label: "ms",
|
| 47 |
+
long: "Milliseconds"
|
| 48 |
+
},
|
| 49 |
+
microsecond: %Unit{
|
| 50 |
+
name: :microsecond,
|
| 51 |
+
magnitude: @nanoseconds_per_microsecond,
|
| 52 |
+
label: "μs",
|
| 53 |
+
long: "Microseconds"
|
| 54 |
+
},
|
| 55 |
+
nanosecond: %Unit{
|
| 56 |
+
name: :nanosecond,
|
| 57 |
+
magnitude: 1,
|
| 58 |
+
label: "ns",
|
| 59 |
+
long: "Nanoseconds"
|
| 60 |
+
}
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
@units Map.values(@units_map)
|
| 64 |
+
|
| 65 |
+
@doc """
|
| 66 |
+
Scales a duration value in nanoseconds into a larger unit if appropriate
|
| 67 |
+
|
| 68 |
+
## Examples
|
| 69 |
+
|
| 70 |
+
iex> {value, unit} = scale(1)
|
| 71 |
+
...> value
|
| 72 |
+
1.0
|
| 73 |
+
iex> unit.name
|
| 74 |
+
:nanosecond
|
| 75 |
+
|
| 76 |
+
iex> {value, unit} = scale(1_234)
|
| 77 |
+
...> value
|
| 78 |
+
1.234
|
| 79 |
+
iex> unit.name
|
| 80 |
+
:microsecond
|
| 81 |
+
|
| 82 |
+
iex> {value, unit} = scale(11_234_567_890_123)
|
| 83 |
+
...> value
|
| 84 |
+
3.1207133028119443
|
| 85 |
+
iex> unit.name
|
| 86 |
+
:hour
|
| 87 |
+
"""
|
| 88 |
+
def scale(duration) when duration >= @nanoseconds_per_hour do
|
| 89 |
+
scale_with_unit(duration, :hour)
|
| 90 |
+
end
|
| 91 |
+
|
| 92 |
+
def scale(duration) when duration >= @nanoseconds_per_minute do
|
| 93 |
+
scale_with_unit(duration, :minute)
|
| 94 |
+
end
|
| 95 |
+
|
| 96 |
+
def scale(duration) when duration >= @nanoseconds_per_second do
|
| 97 |
+
scale_with_unit(duration, :second)
|
| 98 |
+
end
|
| 99 |
+
|
| 100 |
+
def scale(duration) when duration >= @nanoseconds_per_millisecond do
|
| 101 |
+
scale_with_unit(duration, :millisecond)
|
| 102 |
+
end
|
| 103 |
+
|
| 104 |
+
def scale(duration) when duration >= @nanoseconds_per_microsecond do
|
| 105 |
+
scale_with_unit(duration, :microsecond)
|
| 106 |
+
end
|
| 107 |
+
|
| 108 |
+
def scale(duration) do
|
| 109 |
+
scale_with_unit(duration, :nanosecond)
|
| 110 |
+
end
|
| 111 |
+
|
| 112 |
+
# Helper function for returning a tuple of {value, unit}
|
| 113 |
+
defp scale_with_unit(duration, unit) do
|
| 114 |
+
{scale(duration, unit), unit_for(unit)}
|
| 115 |
+
end
|
| 116 |
+
|
| 117 |
+
@doc """
|
| 118 |
+
Get a unit by its atom representation. If handed already a %Unit{} struct it
|
| 119 |
+
just returns it.
|
| 120 |
+
|
| 121 |
+
## Examples
|
| 122 |
+
|
| 123 |
+
iex> unit_for(:hour)
|
| 124 |
+
%Benchee.Conversion.Unit{
|
| 125 |
+
name: :hour,
|
| 126 |
+
magnitude: 3_600_000_000_000,
|
| 127 |
+
label: "h",
|
| 128 |
+
long: "Hours"
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
iex> unit_for(%Benchee.Conversion.Unit{
|
| 132 |
+
...> name: :hour,
|
| 133 |
+
...> magnitude: 3_600_000_000_000,
|
| 134 |
+
...> label: "h",
|
| 135 |
+
...> long: "Hours"
|
| 136 |
+
...> })
|
| 137 |
+
%Benchee.Conversion.Unit{
|
| 138 |
+
name: :hour,
|
| 139 |
+
magnitude: 3_600_000_000_000,
|
| 140 |
+
label: "h",
|
| 141 |
+
long: "Hours"
|
| 142 |
+
}
|
| 143 |
+
"""
|
| 144 |
+
def unit_for(unit) do
|
| 145 |
+
Scale.unit_for(@units_map, unit)
|
| 146 |
+
end
|
| 147 |
+
|
| 148 |
+
def units do
|
| 149 |
+
@units
|
| 150 |
+
end
|
| 151 |
+
|
| 152 |
+
@doc """
|
| 153 |
+
Scales a duration value in nanoseconds into a value in the specified unit
|
| 154 |
+
|
| 155 |
+
## Examples
|
| 156 |
+
|
| 157 |
+
iex> scale(12345, :nanosecond)
|
| 158 |
+
12345.0
|
| 159 |
+
|
| 160 |
+
iex> scale(12345, :microsecond)
|
| 161 |
+
12.345
|
| 162 |
+
|
| 163 |
+
iex> scale(12345, :minute)
|
| 164 |
+
2.0575e-7
|
| 165 |
+
|
| 166 |
+
"""
|
| 167 |
+
def scale(count, unit) do
|
| 168 |
+
Scale.scale(count, unit, __MODULE__)
|
| 169 |
+
end
|
| 170 |
+
|
| 171 |
+
@doc """
|
| 172 |
+
Converts a value for a specified %Unit or unit atom and converts it to the equivalent of another unit of measure.
|
| 173 |
+
|
| 174 |
+
## Examples
|
| 175 |
+
|
| 176 |
+
iex> {value, unit} = convert({90, :minute}, :hour)
|
| 177 |
+
...> value
|
| 178 |
+
1.5
|
| 179 |
+
iex> unit.name
|
| 180 |
+
:hour
|
| 181 |
+
"""
|
| 182 |
+
def convert(number_and_unit, desired_unit) do
|
| 183 |
+
Scale.convert(number_and_unit, desired_unit, __MODULE__)
|
| 184 |
+
end
|
| 185 |
+
|
| 186 |
+
@doc """
|
| 187 |
+
Converts a value of the given unit into the desired unit, returning only the value not the unit.
|
| 188 |
+
|
| 189 |
+
## Examples
|
| 190 |
+
|
| 191 |
+
iex> convert_value({1.234, :second}, :microsecond)
|
| 192 |
+
1_234_000.0
|
| 193 |
+
|
| 194 |
+
iex> convert_value({1.234, :minute}, :microsecond)
|
| 195 |
+
7.404e7
|
| 196 |
+
|
| 197 |
+
iex> microseconds = convert_value({1.234, :minute}, :microsecond)
|
| 198 |
+
...> {value, _} = convert({microseconds, :microsecond}, :minute)
|
| 199 |
+
...> value
|
| 200 |
+
1.234
|
| 201 |
+
|
| 202 |
+
"""
|
| 203 |
+
def convert_value({duration, unit}, desired_unit) do
|
| 204 |
+
{value, _} = convert({duration, unit}, desired_unit)
|
| 205 |
+
value
|
| 206 |
+
end
|
| 207 |
+
|
| 208 |
+
@doc """
|
| 209 |
+
Finds the best unit for a list of durations. By default, chooses the most common
|
| 210 |
+
unit. In case of tie, chooses the largest of the most common units.
|
| 211 |
+
|
| 212 |
+
Pass `[strategy: :smallest]` to always return the smallest unit in the list.
|
| 213 |
+
Pass `[strategy: :largest]` to always return the largest unit in the list.
|
| 214 |
+
|
| 215 |
+
## Examples
|
| 216 |
+
|
| 217 |
+
iex> best([23, 23_000, 34_000, 2_340_000]).name
|
| 218 |
+
:microsecond
|
| 219 |
+
|
| 220 |
+
iex> best([23, 23_000, 34_000_000, 2_340_000_000, 3_450_000_000]).name
|
| 221 |
+
:second
|
| 222 |
+
|
| 223 |
+
iex> best([23, 23_000, 34_000, 2_340_000], strategy: :smallest).name
|
| 224 |
+
:nanosecond
|
| 225 |
+
|
| 226 |
+
iex> best([23, 23_000, 34_000, 2_340_000_000], strategy: :largest).name
|
| 227 |
+
:second
|
| 228 |
+
"""
|
| 229 |
+
def best(list, opts \\ [strategy: :best])
|
| 230 |
+
|
| 231 |
+
def best(list, opts) do
|
| 232 |
+
Scale.best_unit(list, __MODULE__, opts)
|
| 233 |
+
end
|
| 234 |
+
|
| 235 |
+
@doc """
|
| 236 |
+
The most basic unit in which measurements occur.
|
| 237 |
+
|
| 238 |
+
## Examples
|
| 239 |
+
|
| 240 |
+
iex> base_unit().name
|
| 241 |
+
:nanosecond
|
| 242 |
+
|
| 243 |
+
"""
|
| 244 |
+
def base_unit, do: unit_for(:nanosecond)
|
| 245 |
+
|
| 246 |
+
@doc """
|
| 247 |
+
Formats a number as a string, with a unit label. To specify the unit, pass
|
| 248 |
+
a tuple of `{value, unit_atom}` like `{1_234, :second}`.
|
| 249 |
+
|
| 250 |
+
## Examples
|
| 251 |
+
|
| 252 |
+
iex> format(45_678.9)
|
| 253 |
+
"45.68 μs"
|
| 254 |
+
|
| 255 |
+
iex> format(45.6789)
|
| 256 |
+
"45.68 ns"
|
| 257 |
+
|
| 258 |
+
iex> format({45.6789, :millisecond})
|
| 259 |
+
"45.68 ms"
|
| 260 |
+
|
| 261 |
+
iex> format(
|
| 262 |
+
...> {45.6789,
|
| 263 |
+
...> %Benchee.Conversion.Unit{
|
| 264 |
+
...> long: "Milliseconds",
|
| 265 |
+
...> magnitude: 1000,
|
| 266 |
+
...> label: "ms"
|
| 267 |
+
...> }}
|
| 268 |
+
...> )
|
| 269 |
+
"45.68 ms"
|
| 270 |
+
|
| 271 |
+
"""
|
| 272 |
+
def format(duration) do
|
| 273 |
+
Format.format(duration, __MODULE__)
|
| 274 |
+
end
|
| 275 |
+
|
| 276 |
+
@doc """
|
| 277 |
+
Formats in a more "human" way - 1h 30min instead of 1.5h.
|
| 278 |
+
|
| 279 |
+
## Examples
|
| 280 |
+
|
| 281 |
+
iex> format_human(5_400_000_000_000)
|
| 282 |
+
"1 h 30 min"
|
| 283 |
+
|
| 284 |
+
iex> format_human(12.5)
|
| 285 |
+
"12.50 ns"
|
| 286 |
+
|
| 287 |
+
iex> format_human(1000.555)
|
| 288 |
+
"1 μs 0.55 ns"
|
| 289 |
+
|
| 290 |
+
iex> format_human(3_660_001_001_000)
|
| 291 |
+
"1 h 1 min 1 ms 1 μs"
|
| 292 |
+
|
| 293 |
+
iex> format_human(0)
|
| 294 |
+
"0 ns"
|
| 295 |
+
|
| 296 |
+
iex> format_human(2 * 1000 * 1000 * 1000)
|
| 297 |
+
"2 s"
|
| 298 |
+
"""
|
| 299 |
+
def format_human(duration) do
|
| 300 |
+
Format.format_human(duration, __MODULE__)
|
| 301 |
+
end
|
| 302 |
+
end
|
deps/benchee/lib/benchee/conversion/format.ex
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
defmodule Benchee.Conversion.Format do
|
| 2 |
+
@moduledoc """
|
| 3 |
+
Functions for formatting values and their unit labels. Different domains
|
| 4 |
+
handle this task differently, for example durations and counts.
|
| 5 |
+
|
| 6 |
+
See `Benchee.Conversion.Count` and `Benchee.Conversion.Duration` for examples.
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
alias Benchee.Conversion.Unit
|
| 10 |
+
|
| 11 |
+
@doc """
|
| 12 |
+
Formats a number as a string, with a unit label. See `Benchee.Conversion.Count`
|
| 13 |
+
and `Benchee.Conversion.Duration` for examples
|
| 14 |
+
"""
|
| 15 |
+
@callback format(number) :: String.t()
|
| 16 |
+
|
| 17 |
+
@doc """
|
| 18 |
+
Formats in a more "human" way, one biggest unit at a time.
|
| 19 |
+
|
| 20 |
+
So instead of 1.5h it says 1h 30min
|
| 21 |
+
"""
|
| 22 |
+
@callback format_human(number) :: String.t()
|
| 23 |
+
|
| 24 |
+
# Generic formatting functions
|
| 25 |
+
|
| 26 |
+
@doc """
|
| 27 |
+
Formats a unit value with specified label and separator
|
| 28 |
+
"""
|
| 29 |
+
def format(number, label, separator) do
|
| 30 |
+
separator = separator(label, separator)
|
| 31 |
+
"#{number_format(number)}#{separator}#{label}"
|
| 32 |
+
end
|
| 33 |
+
|
| 34 |
+
defp number_format(number) when is_float(number) do
|
| 35 |
+
number
|
| 36 |
+
|> :erlang.float_to_list(decimals: float_precision(number))
|
| 37 |
+
|> to_string
|
| 38 |
+
end
|
| 39 |
+
|
| 40 |
+
defp number_format(number) when is_integer(number) do
|
| 41 |
+
to_string(number)
|
| 42 |
+
end
|
| 43 |
+
|
| 44 |
+
@doc """
|
| 45 |
+
Formats a unit value in the domain described by `module`. The module should
|
| 46 |
+
provide a `units/0` function that returns a Map like
|
| 47 |
+
|
| 48 |
+
%{ :unit_name => %Benchee.Conversion.Unit{ ... } }
|
| 49 |
+
|
| 50 |
+
Additionally, `module` may specify a `separator/0` function, which provides a
|
| 51 |
+
custom separator string that will appear between the value and label in the
|
| 52 |
+
formatted output. If no `separator/0` function exists, the default separator
|
| 53 |
+
(a single space) will be used.
|
| 54 |
+
|
| 55 |
+
iex> format({1.0, :kilobyte}, Benchee.Conversion.Memory)
|
| 56 |
+
"1 KB"
|
| 57 |
+
|
| 58 |
+
"""
|
| 59 |
+
def format({number, unit = %Unit{}}) do
|
| 60 |
+
format(number, label(unit), separator())
|
| 61 |
+
end
|
| 62 |
+
|
| 63 |
+
def format({number, unit = %Unit{}}, _module) do
|
| 64 |
+
format({number, unit})
|
| 65 |
+
end
|
| 66 |
+
|
| 67 |
+
def format({number, unit_atom}, module) do
|
| 68 |
+
format({number, module.unit_for(unit_atom)})
|
| 69 |
+
end
|
| 70 |
+
|
| 71 |
+
def format(number, module) when is_number(number) do
|
| 72 |
+
number
|
| 73 |
+
|> module.scale()
|
| 74 |
+
|> format
|
| 75 |
+
end
|
| 76 |
+
|
| 77 |
+
@doc """
|
| 78 |
+
Human friendly duration format for time as a string.
|
| 79 |
+
|
| 80 |
+
The output is a sequence of values and unit labels separated by a space.
|
| 81 |
+
Only units whose value is non-zero are included in the output.
|
| 82 |
+
The passed number is duration in the base unit - nanoseconds.
|
| 83 |
+
"""
|
| 84 |
+
def format_human(0, module) do
|
| 85 |
+
format(0, module)
|
| 86 |
+
end
|
| 87 |
+
|
| 88 |
+
def format_human(+0.0, module) do
|
| 89 |
+
format(0, module)
|
| 90 |
+
end
|
| 91 |
+
|
| 92 |
+
def format_human(number, module) when is_number(number) do
|
| 93 |
+
number
|
| 94 |
+
|> split_into_place_values(module)
|
| 95 |
+
|> Enum.map_join(" ", &format/1)
|
| 96 |
+
end
|
| 97 |
+
|
| 98 |
+
# Returns a list of place vaules with corresponding units for the `number`.
|
| 99 |
+
# The output is sorted descending by magnitude of units and excludes tuples with place value 0.
|
| 100 |
+
# Place values are `non_neg_integer` for non-base units,
|
| 101 |
+
# however base unit may also be `float` becuase the decimals can't be split further.
|
| 102 |
+
@spec split_into_place_values(number, module) :: [{number, Unit.t()}]
|
| 103 |
+
defp split_into_place_values(number, module) do
|
| 104 |
+
descending_units = units_descending(module)
|
| 105 |
+
|
| 106 |
+
place_values(number, descending_units)
|
| 107 |
+
end
|
| 108 |
+
|
| 109 |
+
defp units_descending(module) do
|
| 110 |
+
Enum.sort(module.units(), &(&1.magnitude >= &2.magnitude))
|
| 111 |
+
end
|
| 112 |
+
|
| 113 |
+
@spec place_values(number, [Unit.t()]) :: [{number, Unit.t()}]
|
| 114 |
+
defp place_values(0, _units), do: []
|
| 115 |
+
defp place_values(+0.0, _units), do: []
|
| 116 |
+
|
| 117 |
+
# smalles unit, carries the decimal
|
| 118 |
+
defp place_values(number, [base_unit = %Unit{magnitude: 1}]), do: [{number, base_unit}]
|
| 119 |
+
|
| 120 |
+
defp place_values(number, [unit | units]) do
|
| 121 |
+
integer_number = trunc(number)
|
| 122 |
+
decimal_carry = number - integer_number
|
| 123 |
+
int_carry = rem(integer_number, unit.magnitude)
|
| 124 |
+
carry = decimal_carry + int_carry
|
| 125 |
+
|
| 126 |
+
place_value = div(integer_number, unit.magnitude)
|
| 127 |
+
|
| 128 |
+
case place_value do
|
| 129 |
+
0 -> place_values(carry, units)
|
| 130 |
+
place_value -> [{place_value, unit} | place_values(carry, units)]
|
| 131 |
+
end
|
| 132 |
+
end
|
| 133 |
+
|
| 134 |
+
@default_separator " "
|
| 135 |
+
# should we need it again, a customer separator could be returned
|
| 136 |
+
# per module here
|
| 137 |
+
defp separator do
|
| 138 |
+
@default_separator
|
| 139 |
+
end
|
| 140 |
+
|
| 141 |
+
# Returns the separator, or an empty string if there isn't a label
|
| 142 |
+
defp separator(label, _separator) when label == "" or label == nil, do: ""
|
| 143 |
+
defp separator(_label, separator), do: separator
|
| 144 |
+
|
| 145 |
+
# Fetches the label for the given unit
|
| 146 |
+
defp label(%Unit{label: label}) do
|
| 147 |
+
label
|
| 148 |
+
end
|
| 149 |
+
|
| 150 |
+
defp float_precision(float) when trunc(float) == float, do: 0
|
| 151 |
+
defp float_precision(float) when float < 0.01, do: 5
|
| 152 |
+
defp float_precision(float) when float < 0.1, do: 4
|
| 153 |
+
defp float_precision(float) when float < 0.2, do: 3
|
| 154 |
+
defp float_precision(_float), do: 2
|
| 155 |
+
end
|
deps/benchee/lib/benchee/conversion/memory.ex
ADDED
|
@@ -0,0 +1,278 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
defmodule Benchee.Conversion.Memory do
|
| 2 |
+
@moduledoc """
|
| 3 |
+
Unit scaling for memory converting from bytes to kilobytes and others.
|
| 4 |
+
|
| 5 |
+
Only Benchee plugins should use this code.
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
alias Benchee.Conversion.{Format, Scale, Unit}
|
| 9 |
+
|
| 10 |
+
@behaviour Scale
|
| 11 |
+
@behaviour Format
|
| 12 |
+
|
| 13 |
+
@bytes_per_kilobyte 1024
|
| 14 |
+
@bytes_per_megabyte @bytes_per_kilobyte * @bytes_per_kilobyte
|
| 15 |
+
@bytes_per_gigabyte @bytes_per_megabyte * @bytes_per_kilobyte
|
| 16 |
+
@bytes_per_terabyte @bytes_per_gigabyte * @bytes_per_kilobyte
|
| 17 |
+
|
| 18 |
+
@units_map %{
|
| 19 |
+
terabyte: %Unit{
|
| 20 |
+
name: :terabyte,
|
| 21 |
+
magnitude: @bytes_per_terabyte,
|
| 22 |
+
label: "TB",
|
| 23 |
+
long: "Terabytes"
|
| 24 |
+
},
|
| 25 |
+
gigabyte: %Unit{
|
| 26 |
+
name: :gigabyte,
|
| 27 |
+
magnitude: @bytes_per_gigabyte,
|
| 28 |
+
label: "GB",
|
| 29 |
+
long: "Gigabytes"
|
| 30 |
+
},
|
| 31 |
+
megabyte: %Unit{
|
| 32 |
+
name: :megabyte,
|
| 33 |
+
magnitude: @bytes_per_megabyte,
|
| 34 |
+
label: "MB",
|
| 35 |
+
long: "Megabytes"
|
| 36 |
+
},
|
| 37 |
+
kilobyte: %Unit{
|
| 38 |
+
name: :kilobyte,
|
| 39 |
+
magnitude: @bytes_per_kilobyte,
|
| 40 |
+
label: "KB",
|
| 41 |
+
long: "Kilobytes"
|
| 42 |
+
},
|
| 43 |
+
byte: %Unit{
|
| 44 |
+
name: :byte,
|
| 45 |
+
magnitude: 1,
|
| 46 |
+
label: "B",
|
| 47 |
+
long: "Bytes"
|
| 48 |
+
}
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
@units Map.values(@units_map)
|
| 52 |
+
|
| 53 |
+
@type unit_atom :: :byte | :kilobyte | :megabyte | :gigabyte | :terabyte
|
| 54 |
+
@type any_unit :: unit_atom | Unit.t()
|
| 55 |
+
|
| 56 |
+
@doc """
|
| 57 |
+
Converts a value for a specified %Unit or unit atom and converts it to the equivalent of another unit of measure.
|
| 58 |
+
|
| 59 |
+
## Examples
|
| 60 |
+
|
| 61 |
+
iex> {value, unit} = convert({1024, :kilobyte}, :megabyte)
|
| 62 |
+
...> value
|
| 63 |
+
1.0
|
| 64 |
+
iex> unit.name
|
| 65 |
+
:megabyte
|
| 66 |
+
|
| 67 |
+
iex> current_unit = unit_for(:kilobyte)
|
| 68 |
+
...> {value, unit} = convert({1024, current_unit}, :megabyte)
|
| 69 |
+
...> value
|
| 70 |
+
1.0
|
| 71 |
+
iex> unit.name
|
| 72 |
+
:megabyte
|
| 73 |
+
"""
|
| 74 |
+
@spec convert({number, any_unit}, any_unit) :: Scale.scaled_number()
|
| 75 |
+
def convert(number_and_unit, desired_unit) do
|
| 76 |
+
Scale.convert(number_and_unit, desired_unit, __MODULE__)
|
| 77 |
+
end
|
| 78 |
+
|
| 79 |
+
# Scaling functions
|
| 80 |
+
|
| 81 |
+
@doc """
|
| 82 |
+
Scales a memory value in bytes into a larger unit if appropriate
|
| 83 |
+
|
| 84 |
+
## Examples
|
| 85 |
+
|
| 86 |
+
iex> {value, unit} = scale(1)
|
| 87 |
+
...> value
|
| 88 |
+
1.0
|
| 89 |
+
iex> unit.name
|
| 90 |
+
:byte
|
| 91 |
+
|
| 92 |
+
iex> {value, unit} = scale(1_234)
|
| 93 |
+
...> value
|
| 94 |
+
1.205078125
|
| 95 |
+
iex> unit.name
|
| 96 |
+
:kilobyte
|
| 97 |
+
|
| 98 |
+
iex> {value, unit} = scale(11_234_567_890.123)
|
| 99 |
+
...> value
|
| 100 |
+
10.463006692121736
|
| 101 |
+
iex> unit.name
|
| 102 |
+
:gigabyte
|
| 103 |
+
|
| 104 |
+
iex> {value, unit} = scale(1_111_234_567_890.123)
|
| 105 |
+
...> value
|
| 106 |
+
1.0106619519229962
|
| 107 |
+
iex> unit.name
|
| 108 |
+
:terabyte
|
| 109 |
+
"""
|
| 110 |
+
def scale(memory) when memory >= @bytes_per_terabyte do
|
| 111 |
+
scale_with_unit(memory, :terabyte)
|
| 112 |
+
end
|
| 113 |
+
|
| 114 |
+
def scale(memory) when memory >= @bytes_per_gigabyte do
|
| 115 |
+
scale_with_unit(memory, :gigabyte)
|
| 116 |
+
end
|
| 117 |
+
|
| 118 |
+
def scale(memory) when memory >= @bytes_per_megabyte do
|
| 119 |
+
scale_with_unit(memory, :megabyte)
|
| 120 |
+
end
|
| 121 |
+
|
| 122 |
+
def scale(memory) when memory >= @bytes_per_kilobyte do
|
| 123 |
+
scale_with_unit(memory, :kilobyte)
|
| 124 |
+
end
|
| 125 |
+
|
| 126 |
+
def scale(memory) do
|
| 127 |
+
scale_with_unit(memory, :byte)
|
| 128 |
+
end
|
| 129 |
+
|
| 130 |
+
# Helper function for returning a tuple of {value, unit}
|
| 131 |
+
defp scale_with_unit(nil, _) do
|
| 132 |
+
{nil, nil}
|
| 133 |
+
end
|
| 134 |
+
|
| 135 |
+
defp scale_with_unit(memory, unit) do
|
| 136 |
+
{scale(memory, unit), unit_for(unit)}
|
| 137 |
+
end
|
| 138 |
+
|
| 139 |
+
@doc """
|
| 140 |
+
Get a unit by its atom representation. If handed already a %Unit{} struct it
|
| 141 |
+
just returns it.
|
| 142 |
+
|
| 143 |
+
## Examples
|
| 144 |
+
|
| 145 |
+
iex> unit_for(:gigabyte)
|
| 146 |
+
%Benchee.Conversion.Unit{
|
| 147 |
+
name: :gigabyte,
|
| 148 |
+
magnitude: 1_073_741_824,
|
| 149 |
+
label: "GB",
|
| 150 |
+
long: "Gigabytes"
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
iex> unit_for(%Benchee.Conversion.Unit{
|
| 154 |
+
...> name: :gigabyte,
|
| 155 |
+
...> magnitude: 1_073_741_824,
|
| 156 |
+
...> label: "GB",
|
| 157 |
+
...> long: "Gigabytes"
|
| 158 |
+
...> })
|
| 159 |
+
%Benchee.Conversion.Unit{
|
| 160 |
+
name: :gigabyte,
|
| 161 |
+
magnitude: 1_073_741_824,
|
| 162 |
+
label: "GB",
|
| 163 |
+
long: "Gigabytes"
|
| 164 |
+
}
|
| 165 |
+
"""
|
| 166 |
+
def unit_for(unit) do
|
| 167 |
+
Scale.unit_for(@units_map, unit)
|
| 168 |
+
end
|
| 169 |
+
|
| 170 |
+
def units do
|
| 171 |
+
@units
|
| 172 |
+
end
|
| 173 |
+
|
| 174 |
+
@doc """
|
| 175 |
+
Scales a memory value in bytes into a value in the specified unit
|
| 176 |
+
|
| 177 |
+
## Examples
|
| 178 |
+
|
| 179 |
+
iex> scale(12345, :kilobyte)
|
| 180 |
+
12.0556640625
|
| 181 |
+
|
| 182 |
+
iex> scale(12345, :megabyte)
|
| 183 |
+
0.011773109436035156
|
| 184 |
+
|
| 185 |
+
iex> scale(123_456_789, :gigabyte)
|
| 186 |
+
0.11497809458523989
|
| 187 |
+
|
| 188 |
+
"""
|
| 189 |
+
def scale(count, unit) do
|
| 190 |
+
Scale.scale(count, unit, __MODULE__)
|
| 191 |
+
end
|
| 192 |
+
|
| 193 |
+
@doc """
|
| 194 |
+
Finds the best unit for a list of memory units. By default, chooses the most common
|
| 195 |
+
unit. In case of tie, chooses the largest of the most common units.
|
| 196 |
+
|
| 197 |
+
Pass `[strategy: :smallest]` to always return the smallest unit in the list.
|
| 198 |
+
Pass `[strategy: :largest]` to always return the largest unit in the list.
|
| 199 |
+
Pass `[strategy: :best]` to always return the most frequent unit in the list.
|
| 200 |
+
Pass `[strategy: :none]` to always return :byte.
|
| 201 |
+
|
| 202 |
+
## Examples
|
| 203 |
+
|
| 204 |
+
iex> best([23, 23_000, 34_000, 2_340_000]).name
|
| 205 |
+
:kilobyte
|
| 206 |
+
|
| 207 |
+
iex> best([23, 23_000, 34_000, 2_340_000, 3_450_000]).name
|
| 208 |
+
:megabyte
|
| 209 |
+
|
| 210 |
+
iex> best([23, 23_000, 34_000, 2_340_000], strategy: :smallest).name
|
| 211 |
+
:byte
|
| 212 |
+
|
| 213 |
+
iex> best([23, 23_000, 34_000, 2_340_000], strategy: :largest).name
|
| 214 |
+
:megabyte
|
| 215 |
+
"""
|
| 216 |
+
def best(list, opts \\ [strategy: :best])
|
| 217 |
+
|
| 218 |
+
def best(list, opts) do
|
| 219 |
+
Scale.best_unit(list, __MODULE__, opts)
|
| 220 |
+
end
|
| 221 |
+
|
| 222 |
+
@doc """
|
| 223 |
+
The most basic unit in which memory occur, byte.
|
| 224 |
+
|
| 225 |
+
## Examples
|
| 226 |
+
|
| 227 |
+
iex> base_unit().name
|
| 228 |
+
:byte
|
| 229 |
+
|
| 230 |
+
"""
|
| 231 |
+
def base_unit, do: unit_for(:byte)
|
| 232 |
+
|
| 233 |
+
@doc """
|
| 234 |
+
Formats a memory as a string, with a unit label.
|
| 235 |
+
|
| 236 |
+
To specify the unit, pass a tuple of `{value, unit_atom}` like `{1_234, :kilobyte}`.
|
| 237 |
+
|
| 238 |
+
## Examples
|
| 239 |
+
|
| 240 |
+
iex> format(45_678.9)
|
| 241 |
+
"44.61 KB"
|
| 242 |
+
|
| 243 |
+
iex> format(45.6789)
|
| 244 |
+
"45.68 B"
|
| 245 |
+
|
| 246 |
+
iex> format({45.6789, :kilobyte})
|
| 247 |
+
"45.68 KB"
|
| 248 |
+
|
| 249 |
+
iex> format(
|
| 250 |
+
...> {45.6789,
|
| 251 |
+
...> %Benchee.Conversion.Unit{
|
| 252 |
+
...> long: "Kilobytes",
|
| 253 |
+
...> magnitude: 1024,
|
| 254 |
+
...> label: "KB"
|
| 255 |
+
...> }}
|
| 256 |
+
...> )
|
| 257 |
+
"45.68 KB"
|
| 258 |
+
|
| 259 |
+
"""
|
| 260 |
+
def format(memory) do
|
| 261 |
+
Format.format(memory, __MODULE__)
|
| 262 |
+
end
|
| 263 |
+
|
| 264 |
+
@doc """
|
| 265 |
+
Formats in a more human way, where multiple units are used.
|
| 266 |
+
|
| 267 |
+
## Examples
|
| 268 |
+
|
| 269 |
+
iex> format_human(45_678.9)
|
| 270 |
+
"44 KB 622.90 B"
|
| 271 |
+
|
| 272 |
+
iex> format_human(1024 * 1024 * 1024)
|
| 273 |
+
"1 GB"
|
| 274 |
+
"""
|
| 275 |
+
def format_human(memory) do
|
| 276 |
+
Format.format_human(memory, __MODULE__)
|
| 277 |
+
end
|
| 278 |
+
end
|
deps/benchee/lib/benchee/conversion/scale.ex
ADDED
|
@@ -0,0 +1,227 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
defmodule Benchee.Conversion.Scale do
|
| 2 |
+
@moduledoc """
|
| 3 |
+
Functions for scaling values to other units. Different domains handle
|
| 4 |
+
this task differently, for example durations and counts.
|
| 5 |
+
|
| 6 |
+
See `Benchee.Conversion.Count` and `Benchee.Conversion.Duration` for examples
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
alias Benchee.Conversion.Unit
|
| 10 |
+
|
| 11 |
+
@type unit :: Unit.t()
|
| 12 |
+
@type unit_atom :: atom
|
| 13 |
+
@type any_unit :: unit | unit_atom
|
| 14 |
+
@type scaled_number :: {number, unit}
|
| 15 |
+
@type scaling_strategy :: :best | :largest | :smallest | :none
|
| 16 |
+
|
| 17 |
+
@doc """
|
| 18 |
+
Scales a number in a domain's base unit to an equivalent value in the best
|
| 19 |
+
fit unit. Results are a `{number, unit}` tuple. See `Benchee.Conversion.Count` and
|
| 20 |
+
`Benchee.Conversion.Duration` for examples.
|
| 21 |
+
"""
|
| 22 |
+
@callback scale(number) :: scaled_number
|
| 23 |
+
|
| 24 |
+
@doc """
|
| 25 |
+
Scales a number in a domain's base unit to an equivalent value in the
|
| 26 |
+
specified unit.
|
| 27 |
+
See `Benchee.Conversion.Count` and `Benchee.Conversion.Duration` for examples.
|
| 28 |
+
"""
|
| 29 |
+
@callback scale(number, any_unit) :: number
|
| 30 |
+
|
| 31 |
+
@doc """
|
| 32 |
+
Finds the best fit unit for a list of numbers in a domain's base unit.
|
| 33 |
+
"Best fit" is the most common unit, or (in case of tie) the largest of the
|
| 34 |
+
most common units.
|
| 35 |
+
"""
|
| 36 |
+
@callback best(list, keyword) :: unit
|
| 37 |
+
|
| 38 |
+
@doc """
|
| 39 |
+
Returns the base_unit in which Benchee takes its measurements, which in
|
| 40 |
+
general is the smallest supported unit.
|
| 41 |
+
"""
|
| 42 |
+
@callback base_unit :: unit
|
| 43 |
+
|
| 44 |
+
@doc """
|
| 45 |
+
Given the atom representation of a unit (`:hour`) return the appropriate
|
| 46 |
+
`Benchee.Conversion.Unit` struct.
|
| 47 |
+
"""
|
| 48 |
+
@callback unit_for(any_unit) :: unit
|
| 49 |
+
|
| 50 |
+
@doc """
|
| 51 |
+
List of all the units supported by this type.
|
| 52 |
+
"""
|
| 53 |
+
@callback units :: [unit]
|
| 54 |
+
|
| 55 |
+
@doc """
|
| 56 |
+
Takes a tuple of a number and a unit and a unit to be converted to, returning
|
| 57 |
+
the the number scaled to the new unit and the new unit.
|
| 58 |
+
"""
|
| 59 |
+
@callback convert({number, any_unit}, any_unit) :: scaled_number
|
| 60 |
+
|
| 61 |
+
# Generic scaling functions
|
| 62 |
+
|
| 63 |
+
@doc """
|
| 64 |
+
Used internally by implemented units to handle their scaling with units and
|
| 65 |
+
without.
|
| 66 |
+
|
| 67 |
+
## Examples
|
| 68 |
+
|
| 69 |
+
iex> scale(12345, :thousand, Benchee.Conversion.Count)
|
| 70 |
+
12.345
|
| 71 |
+
"""
|
| 72 |
+
def scale(value, unit = %Unit{}, _module) do
|
| 73 |
+
scale(value, unit)
|
| 74 |
+
end
|
| 75 |
+
|
| 76 |
+
def scale(value, unit_atom, module) do
|
| 77 |
+
scale(value, module.unit_for(unit_atom))
|
| 78 |
+
end
|
| 79 |
+
|
| 80 |
+
@doc """
|
| 81 |
+
Used internally for scaling but only supports scaling with actual units.
|
| 82 |
+
|
| 83 |
+
## Examples
|
| 84 |
+
|
| 85 |
+
iex> unit = %Benchee.Conversion.Unit{magnitude: 1000}
|
| 86 |
+
...> scale(12345, unit)
|
| 87 |
+
12.345
|
| 88 |
+
"""
|
| 89 |
+
def scale(value, %Unit{magnitude: magnitude}) do
|
| 90 |
+
value / magnitude
|
| 91 |
+
end
|
| 92 |
+
|
| 93 |
+
@doc """
|
| 94 |
+
Lookup a unit by its `atom` presentation for the representation of supported
|
| 95 |
+
units. Used by `Benchee.Conversion.Duration` and `Benchee.Conversion.Count`.
|
| 96 |
+
"""
|
| 97 |
+
def unit_for(_units, unit = %Unit{}), do: unit
|
| 98 |
+
def unit_for(units, unit), do: Map.fetch!(units, unit)
|
| 99 |
+
|
| 100 |
+
@doc """
|
| 101 |
+
Used internally to implement scaling in the modules without duplication.
|
| 102 |
+
"""
|
| 103 |
+
def convert({value, current_unit}, desired_unit, module) do
|
| 104 |
+
current_unit = module.unit_for(current_unit)
|
| 105 |
+
desired_unit = module.unit_for(desired_unit)
|
| 106 |
+
do_convert({value, current_unit}, desired_unit)
|
| 107 |
+
end
|
| 108 |
+
|
| 109 |
+
defp do_convert(
|
| 110 |
+
{value, %Unit{magnitude: current_magnitude}},
|
| 111 |
+
desired_unit = %Unit{magnitude: desired_magnitude}
|
| 112 |
+
) do
|
| 113 |
+
multiplier = current_magnitude / desired_magnitude
|
| 114 |
+
{value * multiplier, desired_unit}
|
| 115 |
+
end
|
| 116 |
+
|
| 117 |
+
@doc """
|
| 118 |
+
Given a `list` of number values and a `module` describing the domain of the
|
| 119 |
+
values (e.g. Duration, Count), finds the "best fit" unit for the list as a
|
| 120 |
+
whole.
|
| 121 |
+
|
| 122 |
+
The best fit unit for a given value is the smallest unit in the domain for
|
| 123 |
+
which the scaled value is at least 1. For example, the best fit unit for a
|
| 124 |
+
count of 1_000_000 would be `:million`.
|
| 125 |
+
|
| 126 |
+
The best fit unit for the list as a whole depends on the `:strategy` passed
|
| 127 |
+
in `opts`:
|
| 128 |
+
|
| 129 |
+
* `:best` - the most frequent best fit unit. In case of tie, the
|
| 130 |
+
largest of the most frequent units
|
| 131 |
+
* `:largest` - the largest best fit unit
|
| 132 |
+
* `:smallest` - the smallest best fit unit
|
| 133 |
+
* `:none` - the domain's base (unscaled) unit
|
| 134 |
+
|
| 135 |
+
## Examples
|
| 136 |
+
|
| 137 |
+
iex> list = [1, 101, 1_001, 10_001, 100_001, 1_000_001]
|
| 138 |
+
...> best_unit(list, Benchee.Conversion.Count, strategy: :best).name
|
| 139 |
+
:thousand
|
| 140 |
+
|
| 141 |
+
iex> list = [1, 101, 1_001, 10_001, 100_001, 1_000_001]
|
| 142 |
+
...> best_unit(list, Benchee.Conversion.Count, strategy: :smallest).name
|
| 143 |
+
:one
|
| 144 |
+
|
| 145 |
+
iex> list = [1, 101, 1_001, 10_001, 100_001, 1_000_001]
|
| 146 |
+
...> best_unit(list, Benchee.Conversion.Count, strategy: :largest).name
|
| 147 |
+
:million
|
| 148 |
+
|
| 149 |
+
iex> list = []
|
| 150 |
+
...> best_unit(list, Benchee.Conversion.Count, strategy: :best).name
|
| 151 |
+
:one
|
| 152 |
+
|
| 153 |
+
iex> list = [nil]
|
| 154 |
+
...> best_unit(list, Benchee.Conversion.Count, strategy: :best).name
|
| 155 |
+
:one
|
| 156 |
+
|
| 157 |
+
iex> list = [nil, nil, nil, nil]
|
| 158 |
+
...> best_unit(list, Benchee.Conversion.Count, strategy: :best).name
|
| 159 |
+
:one
|
| 160 |
+
|
| 161 |
+
iex> list = [nil, nil, nil, nil, 2_000]
|
| 162 |
+
...> best_unit(list, Benchee.Conversion.Count, strategy: :best).name
|
| 163 |
+
:thousand
|
| 164 |
+
"""
|
| 165 |
+
def best_unit(measurements, module, options) do
|
| 166 |
+
do_best_unit(Enum.reject(measurements, &is_nil/1), module, options)
|
| 167 |
+
end
|
| 168 |
+
|
| 169 |
+
defp do_best_unit([], module, _) do
|
| 170 |
+
module.base_unit()
|
| 171 |
+
end
|
| 172 |
+
|
| 173 |
+
defp do_best_unit(list, module, opts) do
|
| 174 |
+
case Keyword.get(opts, :strategy, :best) do
|
| 175 |
+
:best -> best_unit(list, module)
|
| 176 |
+
:largest -> largest_unit(list, module)
|
| 177 |
+
:smallest -> smallest_unit(list, module)
|
| 178 |
+
:none -> module.base_unit()
|
| 179 |
+
end
|
| 180 |
+
end
|
| 181 |
+
|
| 182 |
+
# Finds the most common unit in the list. In case of tie, chooses the
|
| 183 |
+
# largest of the most common
|
| 184 |
+
defp best_unit(list, module) do
|
| 185 |
+
list
|
| 186 |
+
|> Enum.map(fn n -> scale_unit(n, module) end)
|
| 187 |
+
|> Enum.group_by(fn unit -> unit end)
|
| 188 |
+
|> Enum.map(fn {unit, occurrences} -> {unit, length(occurrences)} end)
|
| 189 |
+
|> Enum.sort(fn unit, freq -> by_frequency_and_magnitude(unit, freq) end)
|
| 190 |
+
|> hd
|
| 191 |
+
|> elem(0)
|
| 192 |
+
end
|
| 193 |
+
|
| 194 |
+
# Finds the smallest unit in the list
|
| 195 |
+
defp smallest_unit(list, module) do
|
| 196 |
+
list
|
| 197 |
+
|> Enum.map(fn n -> scale_unit(n, module) end)
|
| 198 |
+
|> Enum.min_by(fn unit -> magnitude(unit) end)
|
| 199 |
+
end
|
| 200 |
+
|
| 201 |
+
# Finds the largest unit in the list
|
| 202 |
+
defp largest_unit(list, module) do
|
| 203 |
+
list
|
| 204 |
+
|> Enum.map(fn n -> scale_unit(n, module) end)
|
| 205 |
+
|> Enum.max_by(fn unit -> magnitude(unit) end)
|
| 206 |
+
end
|
| 207 |
+
|
| 208 |
+
defp scale_unit(count, module) do
|
| 209 |
+
{_, unit} = module.scale(count)
|
| 210 |
+
unit
|
| 211 |
+
end
|
| 212 |
+
|
| 213 |
+
# Fetches the magnitude for the given unit
|
| 214 |
+
defp magnitude(%Unit{magnitude: magnitude}) do
|
| 215 |
+
magnitude
|
| 216 |
+
end
|
| 217 |
+
|
| 218 |
+
# Sorts two elements first by total, then by magnitude of the unit in case
|
| 219 |
+
# of tie
|
| 220 |
+
defp by_frequency_and_magnitude({unit_a, frequency}, {unit_b, frequency}) do
|
| 221 |
+
magnitude(unit_a) > magnitude(unit_b)
|
| 222 |
+
end
|
| 223 |
+
|
| 224 |
+
defp by_frequency_and_magnitude({_, frequency_a}, {_, frequency_b}) do
|
| 225 |
+
frequency_a > frequency_b
|
| 226 |
+
end
|
| 227 |
+
end
|
deps/benchee/lib/benchee/conversion/unit.ex
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
defmodule Benchee.Conversion.Unit do
|
| 2 |
+
@moduledoc """
|
| 3 |
+
A representation of the different units used in `Benchee.Conversion.Format`
|
| 4 |
+
and `Benchee.Conversion.Scale` as well as the modules implementing these
|
| 5 |
+
behaviours.
|
| 6 |
+
|
| 7 |
+
A unit is characterized by:
|
| 8 |
+
|
| 9 |
+
* name - an atom representation of the unit for easy access (`:microseconds`,
|
| 10 |
+
`thousand`)
|
| 11 |
+
* magnitude - compared to he base unit (the smallest unit) what's the factor
|
| 12 |
+
you had to multiply it by to get back to the base unit. E.g. the thousand
|
| 13 |
+
unit has a magnitude of `1_000`.
|
| 14 |
+
* label - a string that is used as a unit label (`"K"` for a thousand f.ex.)
|
| 15 |
+
* long - a string giving the long version of the label (`"Thousand"`)
|
| 16 |
+
"""
|
| 17 |
+
|
| 18 |
+
defstruct [:name, :magnitude, :label, :long]
|
| 19 |
+
|
| 20 |
+
@type t :: %Benchee.Conversion.Unit{
|
| 21 |
+
name: atom,
|
| 22 |
+
magnitude: non_neg_integer,
|
| 23 |
+
label: String.t(),
|
| 24 |
+
long: String.t()
|
| 25 |
+
}
|
| 26 |
+
end
|
deps/benchee/lib/benchee/errors.ex
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
defmodule Benchee.UnknownProfilerError do
|
| 2 |
+
defexception message: "error"
|
| 3 |
+
end
|
| 4 |
+
|
| 5 |
+
defmodule Benchee.PreCheckError do
|
| 6 |
+
defexception message: "error"
|
| 7 |
+
end
|
deps/benchee/lib/benchee/formatter.ex
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
defmodule Benchee.Formatter do
|
| 2 |
+
@moduledoc """
|
| 3 |
+
Defines a behaviour for formatters in Benchee, and functions to work with these.
|
| 4 |
+
|
| 5 |
+
When implementing a Benchee formatter as a behaviour please adopt this
|
| 6 |
+
behaviour, as it helps with uniformity and also allows at least the `.format`
|
| 7 |
+
function of formatters to be run in parallel.
|
| 8 |
+
|
| 9 |
+
The module itself then has functions to deal with formatters defined in this way
|
| 10 |
+
allowing for parallel output through `output/1` or just output a single formatter
|
| 11 |
+
through `output/3`.
|
| 12 |
+
"""
|
| 13 |
+
|
| 14 |
+
alias Benchee.Output.ProgressPrinter
|
| 15 |
+
alias Benchee.{Scenario, Suite, Utility.Parallel}
|
| 16 |
+
alias Benchee.Utility.DeepConvert
|
| 17 |
+
|
| 18 |
+
@typedoc """
|
| 19 |
+
Options given to formatters, entirely defined by formatter authors.
|
| 20 |
+
"""
|
| 21 |
+
@type options :: any
|
| 22 |
+
|
| 23 |
+
@typedoc """
|
| 24 |
+
A suite scrubbed of heavy data.
|
| 25 |
+
|
| 26 |
+
Type to bring awareness to the fact that `format/2` doesn't have access to
|
| 27 |
+
_all_ data in `Benchee.Suite` - please read the docs for `format/2` to learn
|
| 28 |
+
more.
|
| 29 |
+
"""
|
| 30 |
+
@type scrubbed_suite :: Suite.t()
|
| 31 |
+
|
| 32 |
+
@doc """
|
| 33 |
+
Takes a suite and returns a representation `write/2` can use.
|
| 34 |
+
|
| 35 |
+
Takes the suite and returns whatever representation the formatter wants to use
|
| 36 |
+
to output that information. It is important that this function **needs to be
|
| 37 |
+
pure** (aka have no side effects) as Benchee will run `format/1` functions
|
| 38 |
+
of multiple formatters in parallel. The result will then be passed to
|
| 39 |
+
`write/2`.
|
| 40 |
+
|
| 41 |
+
**Note:** Due to memory consumption issues in benchmarks with big inputs, the suite
|
| 42 |
+
passed to the formatters **is missing anything referencing big input data** to avoid
|
| 43 |
+
huge memory consumption and run time. Namely this constitutes:
|
| 44 |
+
* `Benchee.Scenario` will have `function` and `input` set to `nil`
|
| 45 |
+
* `Benchee.Configuration` will have `inputs`, but it won't have values only the names,
|
| 46 |
+
it may be removed in the future please use `input_names` instead if needed (or
|
| 47 |
+
`input_name` of `Benchee.Scenario`)
|
| 48 |
+
|
| 49 |
+
Technically speaking this "scrubbing" of `Benchee.Suite` only occurs when formatters
|
| 50 |
+
are run in parallel, you still shouldn't rely on those values (and they should not
|
| 51 |
+
be needed). If you do need them for some reason, please get in touch/open an issue.
|
| 52 |
+
"""
|
| 53 |
+
@callback format(Suite.t(), options) :: any
|
| 54 |
+
|
| 55 |
+
@doc """
|
| 56 |
+
Takes the return value of `format/1` and then performs some I/O for the user
|
| 57 |
+
to actually see the formatted data (UI, File IO, HTTP, ...)
|
| 58 |
+
"""
|
| 59 |
+
@callback write(any, options) :: :ok | {:error, String.t()}
|
| 60 |
+
|
| 61 |
+
@doc """
|
| 62 |
+
Format and output all configured formatters and formatting functions.
|
| 63 |
+
|
| 64 |
+
Expects a suite that already has been run through all previous functions so has the aggregated
|
| 65 |
+
statistics etc. that the formatters rely on.
|
| 66 |
+
|
| 67 |
+
Works by invoking the `format/2` and `write/2` functions defined in this module. The `format/2`
|
| 68 |
+
functions are actually called in parallel (as they should be pure) - due to potential
|
| 69 |
+
interference the `write/2` functions are called serial.
|
| 70 |
+
|
| 71 |
+
Also handles pure functions that will then be called with the suite.
|
| 72 |
+
|
| 73 |
+
You can't rely on the formatters being called in pre determined order.
|
| 74 |
+
"""
|
| 75 |
+
@spec output(Suite.t()) :: Suite.t()
|
| 76 |
+
def output(suite = %{configuration: %{formatters: formatters}}) do
|
| 77 |
+
print_formatting(suite.configuration)
|
| 78 |
+
|
| 79 |
+
{parallelizable, serial} =
|
| 80 |
+
formatters
|
| 81 |
+
|> Enum.map(&normalize_module_configuration/1)
|
| 82 |
+
|> Enum.split_with(&is_tuple/1)
|
| 83 |
+
|
| 84 |
+
maybe_parallel_output(suite, parallelizable)
|
| 85 |
+
|
| 86 |
+
Enum.each(serial, fn function -> function.(suite) end)
|
| 87 |
+
|
| 88 |
+
suite
|
| 89 |
+
end
|
| 90 |
+
|
| 91 |
+
# This is a bit of a hack, but we can't DI the printer like we usually do as we made the _great_
|
| 92 |
+
# decisions to use both `output/1` and `output/2` as part of the public interface. Adding the
|
| 93 |
+
# printer to `output/1` would clash with `output/2` and its second parameter is _also_ a module.
|
| 94 |
+
# So, we're abusing the `assigns` in the config a bit.
|
| 95 |
+
defp print_formatting(config) do
|
| 96 |
+
printer = config.assigns[:test][:progress_printer] || ProgressPrinter
|
| 97 |
+
printer.formatting(config)
|
| 98 |
+
end
|
| 99 |
+
|
| 100 |
+
defp normalize_module_configuration(formatter) when is_function(formatter, 1), do: formatter
|
| 101 |
+
|
| 102 |
+
defp normalize_module_configuration({module, opts}) do
|
| 103 |
+
normalize_module_configuration(module, DeepConvert.to_map(opts))
|
| 104 |
+
end
|
| 105 |
+
|
| 106 |
+
defp normalize_module_configuration(module) when is_atom(module) do
|
| 107 |
+
normalize_module_configuration(module, %{})
|
| 108 |
+
end
|
| 109 |
+
|
| 110 |
+
defp normalize_module_configuration(module, opts) do
|
| 111 |
+
if formatter_module?(module) do
|
| 112 |
+
{module, opts}
|
| 113 |
+
else
|
| 114 |
+
raise_behaviour_not_implemented(module)
|
| 115 |
+
end
|
| 116 |
+
end
|
| 117 |
+
|
| 118 |
+
defp formatter_module?(module) do
|
| 119 |
+
:attributes
|
| 120 |
+
|> module.module_info()
|
| 121 |
+
|> Keyword.get(:behaviour, [])
|
| 122 |
+
|> Enum.member?(Benchee.Formatter)
|
| 123 |
+
end
|
| 124 |
+
|
| 125 |
+
@spec raise_behaviour_not_implemented(atom) :: no_return()
|
| 126 |
+
defp raise_behaviour_not_implemented(module) do
|
| 127 |
+
raise """
|
| 128 |
+
The module you're attempting to use as a formatter - #{module} - does
|
| 129 |
+
not implement the `Benchee.Formatter` behaviour.
|
| 130 |
+
"""
|
| 131 |
+
end
|
| 132 |
+
|
| 133 |
+
@doc """
|
| 134 |
+
Output a suite with a given formatter and options.
|
| 135 |
+
|
| 136 |
+
Replacement for the old `MyFormatter.output/1` - calls `format/2` and `write/2` one after another
|
| 137 |
+
to create the output defined by the given formatter module. For the given options please refer
|
| 138 |
+
to the documentation of the formatters you use.
|
| 139 |
+
"""
|
| 140 |
+
@spec output(Suite.t(), module, options) :: Suite.t()
|
| 141 |
+
def output(suite, formatter, options \\ %{}) do
|
| 142 |
+
:ok =
|
| 143 |
+
suite
|
| 144 |
+
|> formatter.format(options)
|
| 145 |
+
|> formatter.write(options)
|
| 146 |
+
|
| 147 |
+
suite
|
| 148 |
+
end
|
| 149 |
+
|
| 150 |
+
# Invokes `format/2` and `write/2` as defined by the `Benchee.Formatter`
|
| 151 |
+
# behaviour. The output for all formatters is generated in parallel, and then
|
| 152 |
+
# the results of that formatting are written in sequence.
|
| 153 |
+
# If there is only one parallelizable formatter (common case) we don't do it
|
| 154 |
+
# in parallel at all to avoid the cost of copying all the data to a new process
|
| 155 |
+
# (and of us scrubbing it so we don't need as much data)
|
| 156 |
+
defp maybe_parallel_output(suite, module_configurations) do
|
| 157 |
+
module_configurations
|
| 158 |
+
|> maybe_parallel_format(suite)
|
| 159 |
+
|> Enum.each(fn {module, options, output} -> module.write(output, options) end)
|
| 160 |
+
|
| 161 |
+
suite
|
| 162 |
+
end
|
| 163 |
+
|
| 164 |
+
# don't let it drop to the `Parallel` case so we don't do the scrubbing
|
| 165 |
+
# for nothing
|
| 166 |
+
defp maybe_parallel_format([], _suite), do: []
|
| 167 |
+
|
| 168 |
+
defp maybe_parallel_format(formatters = [_one_formatter], suite) do
|
| 169 |
+
Enum.map(formatters, fn {module, options} ->
|
| 170 |
+
{module, options, module.format(suite, options)}
|
| 171 |
+
end)
|
| 172 |
+
end
|
| 173 |
+
|
| 174 |
+
defp maybe_parallel_format(formatters, suite) do
|
| 175 |
+
# suite only needs scrubbing for parallel processing due to data copying
|
| 176 |
+
scrubbed_suite = scrub_suite(suite)
|
| 177 |
+
|
| 178 |
+
Parallel.map(formatters, fn {module, options} ->
|
| 179 |
+
{module, options, module.format(scrubbed_suite, options)}
|
| 180 |
+
end)
|
| 181 |
+
end
|
| 182 |
+
|
| 183 |
+
# The actual benchmarking functions and the actual inputs should not be important for
|
| 184 |
+
# formatters (famous last words, I know) and processing them in parallel means that for
|
| 185 |
+
# benchmarks with a lot of data we end up doing a lot of copying with huge impact on run
|
| 186 |
+
# time and memory consumption.
|
| 187 |
+
# As the suite isn't actually returned from the formatters removing them also doesn't impact
|
| 188 |
+
# anyone negatively downstream.
|
| 189 |
+
# Hence, we scrub them away here. If you maintain a formatter plugin and rely on these please
|
| 190 |
+
# get in touch so we can work on a solution.
|
| 191 |
+
defp scrub_suite(suite) do
|
| 192 |
+
suite =
|
| 193 |
+
update_in(suite.scenarios, fn scenarios ->
|
| 194 |
+
Enum.map(scenarios, &scrub_scenario/1)
|
| 195 |
+
end)
|
| 196 |
+
|
| 197 |
+
update_in(suite.configuration, &scrub_configuration/1)
|
| 198 |
+
end
|
| 199 |
+
|
| 200 |
+
defp scrub_scenario(scenario = %Scenario{}) do
|
| 201 |
+
%Scenario{scenario | function: nil, input: nil}
|
| 202 |
+
end
|
| 203 |
+
|
| 204 |
+
defp scrub_configuration(configuration) do
|
| 205 |
+
update_in(configuration.inputs, &scrub_inputs/1)
|
| 206 |
+
end
|
| 207 |
+
|
| 208 |
+
defp scrub_inputs(nil), do: nil
|
| 209 |
+
# Feels somewhat hacky, but while people should not be relying on the input itself, they
|
| 210 |
+
# may be relying on the input names/order
|
| 211 |
+
defp scrub_inputs(inputs) do
|
| 212 |
+
Enum.map(inputs, fn {name, _value} -> {name, :scrubbed_see_1_3_0_changelog} end)
|
| 213 |
+
end
|
| 214 |
+
end
|
deps/benchee/lib/benchee/formatters/console.ex
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
defmodule Benchee.Formatters.Console do
|
| 2 |
+
@moduledoc """
|
| 3 |
+
Formatter to print out the results of benchmarking suite to the console.
|
| 4 |
+
|
| 5 |
+
Example:
|
| 6 |
+
|
| 7 |
+
Name ips average deviation median 99th %
|
| 8 |
+
flat_map 2.40 K 417.00 μs ±9.40% 411.45 μs 715.21 μs
|
| 9 |
+
map.flatten 1.24 K 806.89 μs ±16.62% 768.02 μs 1170.67 μs
|
| 10 |
+
|
| 11 |
+
Comparison:
|
| 12 |
+
flat_map 2.40 K
|
| 13 |
+
map.flatten 1.24 K - 1.93x slower
|
| 14 |
+
|
| 15 |
+
Memory usage statistics:
|
| 16 |
+
|
| 17 |
+
Name Memory usage
|
| 18 |
+
flat_map 624.97 KB
|
| 19 |
+
map.flatten 781.25 KB - 1.25x memory usage
|
| 20 |
+
|
| 21 |
+
**All measurements for memory usage were the same**
|
| 22 |
+
|
| 23 |
+
Reduction count statistics:
|
| 24 |
+
|
| 25 |
+
Name average deviation median 99th %
|
| 26 |
+
flat_map 417.00 ±9.40 411.45 715.21
|
| 27 |
+
map.flatten 806.89 ±16.62 768.02 1170.67
|
| 28 |
+
|
| 29 |
+
Comparison:
|
| 30 |
+
flat_map 417.00
|
| 31 |
+
map.flatten 806.89 - 1.93x more reductions
|
| 32 |
+
"""
|
| 33 |
+
|
| 34 |
+
@behaviour Benchee.Formatter
|
| 35 |
+
|
| 36 |
+
alias Benchee.Formatters.Console.{Memory, Reductions, RunTime}
|
| 37 |
+
alias Benchee.Suite
|
| 38 |
+
|
| 39 |
+
@doc """
|
| 40 |
+
Formats the benchmark statistics to a report suitable for output on the CLI.
|
| 41 |
+
|
| 42 |
+
Returns a list of lists, where each list element is a group belonging to one
|
| 43 |
+
specific input. So if there only was one (or no) input given through `:inputs`
|
| 44 |
+
then there's just one list inside.
|
| 45 |
+
|
| 46 |
+
## Examples
|
| 47 |
+
|
| 48 |
+
```
|
| 49 |
+
iex> scenarios = [
|
| 50 |
+
...> %Benchee.Scenario{
|
| 51 |
+
...> name: "My Job",
|
| 52 |
+
...> input_name: "My input",
|
| 53 |
+
...> run_time_data: %Benchee.CollectionData{
|
| 54 |
+
...> statistics: %Benchee.Statistics{
|
| 55 |
+
...> average: 200.0,
|
| 56 |
+
...> ips: 5000.0,
|
| 57 |
+
...> std_dev_ratio: 0.1,
|
| 58 |
+
...> median: 190.0,
|
| 59 |
+
...> percentiles: %{99 => 300.1},
|
| 60 |
+
...> sample_size: 200
|
| 61 |
+
...> }
|
| 62 |
+
...> },
|
| 63 |
+
...> memory_usage_data: %Benchee.CollectionData{statistics: %Benchee.Statistics{}}
|
| 64 |
+
...> },
|
| 65 |
+
...> %Benchee.Scenario{
|
| 66 |
+
...> name: "Job 2",
|
| 67 |
+
...> input_name: "My input",
|
| 68 |
+
...> run_time_data: %Benchee.CollectionData{
|
| 69 |
+
...> statistics: %Benchee.Statistics{
|
| 70 |
+
...> average: 400.0,
|
| 71 |
+
...> ips: 2500.0,
|
| 72 |
+
...> std_dev_ratio: 0.2,
|
| 73 |
+
...> median: 390.0,
|
| 74 |
+
...> percentiles: %{99 => 500.1},
|
| 75 |
+
...> sample_size: 200
|
| 76 |
+
...> }
|
| 77 |
+
...> },
|
| 78 |
+
...> memory_usage_data: %Benchee.CollectionData{statistics: %Benchee.Statistics{}}
|
| 79 |
+
...> }
|
| 80 |
+
...> ]
|
| 81 |
+
...>
|
| 82 |
+
...> suite = %Benchee.Suite{
|
| 83 |
+
...> scenarios: scenarios,
|
| 84 |
+
...> configuration: %Benchee.Configuration{
|
| 85 |
+
...> unit_scaling: :best
|
| 86 |
+
...> }
|
| 87 |
+
...> }
|
| 88 |
+
...>
|
| 89 |
+
...> format(suite, %{comparison: false, extended_statistics: false})
|
| 90 |
+
[
|
| 91 |
+
[
|
| 92 |
+
"\n##### With input My input #####",
|
| 93 |
+
"\nName ips average deviation median 99th %\n",
|
| 94 |
+
"My Job 5 K 200 ns ±10.00% 190 ns 300.10 ns\n",
|
| 95 |
+
"Job 2 2.50 K 400 ns ±20.00% 390 ns 500.10 ns\n"
|
| 96 |
+
]
|
| 97 |
+
]
|
| 98 |
+
|
| 99 |
+
```
|
| 100 |
+
|
| 101 |
+
"""
|
| 102 |
+
@impl true
|
| 103 |
+
@spec format(Suite.t(), map) :: [any]
|
| 104 |
+
def format(%Suite{scenarios: scenarios, configuration: config}, options \\ %{}) do
|
| 105 |
+
config =
|
| 106 |
+
config
|
| 107 |
+
|> Map.take([:unit_scaling, :title])
|
| 108 |
+
|> Map.merge(options)
|
| 109 |
+
|
| 110 |
+
scenarios
|
| 111 |
+
|> Enum.reduce([], &update_grouped_list/2)
|
| 112 |
+
|> Enum.map(fn {input, scenarios} ->
|
| 113 |
+
generate_output(scenarios, config, input)
|
| 114 |
+
end)
|
| 115 |
+
end
|
| 116 |
+
|
| 117 |
+
# Normally one would prepend to lists and not append. In this case this lead to 2
|
| 118 |
+
# `Enum.reverse` scattered around. As these lists are usually very small (mostly less
|
| 119 |
+
# than 10 elements) I opted for `++` here.
|
| 120 |
+
defp update_grouped_list(scenario, grouped_scenarios) do
|
| 121 |
+
case List.keyfind(grouped_scenarios, scenario.input_name, 0) do
|
| 122 |
+
{_, group} ->
|
| 123 |
+
new_tuple = {scenario.input_name, group ++ [scenario]}
|
| 124 |
+
List.keyreplace(grouped_scenarios, scenario.input_name, 0, new_tuple)
|
| 125 |
+
|
| 126 |
+
_ ->
|
| 127 |
+
grouped_scenarios ++ [{scenario.input_name, [scenario]}]
|
| 128 |
+
end
|
| 129 |
+
end
|
| 130 |
+
|
| 131 |
+
@doc """
|
| 132 |
+
Takes the output of `format/1` and writes that to the console.
|
| 133 |
+
"""
|
| 134 |
+
@impl true
|
| 135 |
+
@spec write(any, map) :: :ok | {:error, String.t()}
|
| 136 |
+
def write(output, _options \\ %{}) do
|
| 137 |
+
IO.write(output)
|
| 138 |
+
rescue
|
| 139 |
+
_ -> {:error, "Unknown Error"}
|
| 140 |
+
end
|
| 141 |
+
|
| 142 |
+
defp generate_output(scenarios, config, input) do
|
| 143 |
+
[
|
| 144 |
+
suite_header(input, config)
|
| 145 |
+
| RunTime.format_scenarios(scenarios, config) ++
|
| 146 |
+
Memory.format_scenarios(scenarios, config) ++
|
| 147 |
+
Reductions.format_scenarios(scenarios, config)
|
| 148 |
+
]
|
| 149 |
+
end
|
| 150 |
+
|
| 151 |
+
defp suite_header(input, config) do
|
| 152 |
+
"#{title_header(config)}#{input_header(input)}"
|
| 153 |
+
end
|
| 154 |
+
|
| 155 |
+
defp title_header(%{title: nil}), do: ""
|
| 156 |
+
defp title_header(%{title: title}), do: "\n*** #{title} ***\n"
|
| 157 |
+
|
| 158 |
+
@no_input_marker Benchee.Benchmark.no_input()
|
| 159 |
+
defp input_header(input) when input == @no_input_marker, do: ""
|
| 160 |
+
defp input_header(input), do: "\n##### With input #{input} #####"
|
| 161 |
+
end
|
deps/benchee/lib/benchee/formatters/console/helpers.ex
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
defmodule Benchee.Formatters.Console.Helpers do
|
| 2 |
+
@moduledoc false
|
| 3 |
+
|
| 4 |
+
# These are common functions shared between the formatting of the run time and
|
| 5 |
+
# memory usage statistics.
|
| 6 |
+
|
| 7 |
+
alias Benchee.Conversion.{Count, DeviationPercent, Format, Scale, Unit}
|
| 8 |
+
alias Benchee.Scenario
|
| 9 |
+
alias Benchee.Statistics
|
| 10 |
+
|
| 11 |
+
@type unit_per_statistic :: %{atom => Unit.t()}
|
| 12 |
+
|
| 13 |
+
# Length of column header
|
| 14 |
+
@default_label_width 4
|
| 15 |
+
|
| 16 |
+
@spec mode_out(Statistics.mode(), Benchee.Conversion.Unit.t()) :: String.t()
|
| 17 |
+
def mode_out(modes, _run_time_unit) when is_nil(modes) do
|
| 18 |
+
"None"
|
| 19 |
+
end
|
| 20 |
+
|
| 21 |
+
def mode_out(modes, run_time_unit) when is_list(modes) do
|
| 22 |
+
Enum.map_join(modes, ", ", fn mode -> unit_output(mode, run_time_unit) end)
|
| 23 |
+
end
|
| 24 |
+
|
| 25 |
+
def mode_out(mode, run_time_unit) when is_number(mode) do
|
| 26 |
+
unit_output(mode, run_time_unit)
|
| 27 |
+
end
|
| 28 |
+
|
| 29 |
+
defp unit_output(value, unit) do
|
| 30 |
+
Format.format({Scale.scale(value, unit), unit})
|
| 31 |
+
end
|
| 32 |
+
|
| 33 |
+
@spec label_width([Scenario.t()]) :: number
|
| 34 |
+
def label_width(scenarios) do
|
| 35 |
+
max_label_width =
|
| 36 |
+
scenarios
|
| 37 |
+
|> Enum.map(fn scenario -> String.length(scenario.name) end)
|
| 38 |
+
|> Stream.concat([@default_label_width])
|
| 39 |
+
|> Enum.max()
|
| 40 |
+
|
| 41 |
+
max_label_width + 1
|
| 42 |
+
end
|
| 43 |
+
|
| 44 |
+
@spec count_output(number, Count.units()) :: binary
|
| 45 |
+
def count_output(count, unit) do
|
| 46 |
+
Count.format({Count.scale(count, unit), unit})
|
| 47 |
+
end
|
| 48 |
+
|
| 49 |
+
@spec deviation_output(number) :: binary
|
| 50 |
+
def deviation_output(std_dev_ratio) do
|
| 51 |
+
DeviationPercent.format(std_dev_ratio)
|
| 52 |
+
end
|
| 53 |
+
|
| 54 |
+
@spec descriptor(String.t()) :: String.t()
|
| 55 |
+
def descriptor(header_str), do: "\n#{header_str}: \n"
|
| 56 |
+
|
| 57 |
+
def format_comparison(
|
| 58 |
+
name,
|
| 59 |
+
statistics,
|
| 60 |
+
display_value,
|
| 61 |
+
comparison_name,
|
| 62 |
+
display_unit,
|
| 63 |
+
label_width,
|
| 64 |
+
column_width
|
| 65 |
+
) do
|
| 66 |
+
"~*s~*s ~ts"
|
| 67 |
+
|> :io_lib.format([
|
| 68 |
+
-label_width,
|
| 69 |
+
name,
|
| 70 |
+
column_width,
|
| 71 |
+
display_value,
|
| 72 |
+
comparison_display(statistics, comparison_name, display_unit)
|
| 73 |
+
])
|
| 74 |
+
|> to_string
|
| 75 |
+
end
|
| 76 |
+
|
| 77 |
+
defp comparison_display(%Statistics{relative_more: nil, absolute_difference: nil}, _, _), do: ""
|
| 78 |
+
|
| 79 |
+
defp comparison_display(statistics, comparison_name, unit) do
|
| 80 |
+
"- #{comparison_text(statistics, comparison_name)} #{absolute_difference_text(statistics, unit)}\n"
|
| 81 |
+
end
|
| 82 |
+
|
| 83 |
+
defp comparison_text(%Statistics{relative_more: :infinity}, name), do: "∞ x #{name}"
|
| 84 |
+
defp comparison_text(%Statistics{relative_more: nil}, _), do: "N/A"
|
| 85 |
+
|
| 86 |
+
defp comparison_text(statistics, comparison_name) do
|
| 87 |
+
"~.2fx ~s"
|
| 88 |
+
|> :io_lib.format([statistics.relative_more, comparison_name])
|
| 89 |
+
|> to_string
|
| 90 |
+
end
|
| 91 |
+
|
| 92 |
+
defp absolute_difference_text(statistics, unit) do
|
| 93 |
+
formatted_value = Format.format({Scale.scale(statistics.absolute_difference, unit), unit})
|
| 94 |
+
|
| 95 |
+
if statistics.absolute_difference >= 0 do
|
| 96 |
+
"+#{formatted_value}"
|
| 97 |
+
else
|
| 98 |
+
formatted_value
|
| 99 |
+
end
|
| 100 |
+
end
|
| 101 |
+
end
|
deps/benchee/lib/benchee/formatters/console/memory.ex
ADDED
|
@@ -0,0 +1,321 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
defmodule Benchee.Formatters.Console.Memory do
|
| 2 |
+
@moduledoc false
|
| 3 |
+
|
| 4 |
+
# This deals with just the formatting of the run time results. They are similar
|
| 5 |
+
# to the way the memory results are formatted, but different enough to where the
|
| 6 |
+
# abstractions start to break down pretty significantly, so I wanted to extract
|
| 7 |
+
# these two things into separate modules to avoid confusion.
|
| 8 |
+
|
| 9 |
+
alias Benchee.{
|
| 10 |
+
Conversion,
|
| 11 |
+
Conversion.Count,
|
| 12 |
+
Conversion.Memory,
|
| 13 |
+
Conversion.Unit,
|
| 14 |
+
Formatters.Console.Helpers,
|
| 15 |
+
Scenario,
|
| 16 |
+
Statistics
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
@type unit_per_statistic :: %{atom => Unit.t()}
|
| 20 |
+
|
| 21 |
+
# Length of column header
|
| 22 |
+
@average_width 15
|
| 23 |
+
@deviation_width 11
|
| 24 |
+
@median_width 15
|
| 25 |
+
@percentile_width 15
|
| 26 |
+
@minimum_width 15
|
| 27 |
+
@maximum_width 15
|
| 28 |
+
@sample_size_width 15
|
| 29 |
+
@mode_width 25
|
| 30 |
+
|
| 31 |
+
@doc """
|
| 32 |
+
Formats the memory statistics to a report suitable for output on the CLI. If
|
| 33 |
+
all memory measurements are the same and we have a standard deviation of 0.0
|
| 34 |
+
for each scenario, we don't show the statistics and report just on the single
|
| 35 |
+
measured memory usage.
|
| 36 |
+
"""
|
| 37 |
+
@spec format_scenarios([Scenario.t()], map) :: [String.t(), ...]
|
| 38 |
+
def format_scenarios(scenarios, config) do
|
| 39 |
+
if memory_measurements_present?(scenarios) do
|
| 40 |
+
render(scenarios, config)
|
| 41 |
+
else
|
| 42 |
+
[]
|
| 43 |
+
end
|
| 44 |
+
end
|
| 45 |
+
|
| 46 |
+
defp memory_measurements_present?(scenarios) do
|
| 47 |
+
Enum.any?(scenarios, fn scenario ->
|
| 48 |
+
scenario.memory_usage_data.statistics.sample_size > 0
|
| 49 |
+
end)
|
| 50 |
+
end
|
| 51 |
+
|
| 52 |
+
defp render(scenarios, config) do
|
| 53 |
+
scaling_strategy = config.unit_scaling
|
| 54 |
+
units = Conversion.units(scenarios, scaling_strategy)
|
| 55 |
+
label_width = Helpers.label_width(scenarios)
|
| 56 |
+
hide_statistics = all_have_deviation_of_0?(scenarios)
|
| 57 |
+
|
| 58 |
+
List.flatten([
|
| 59 |
+
"\nMemory usage statistics:\n",
|
| 60 |
+
column_descriptors(label_width, hide_statistics),
|
| 61 |
+
scenario_reports(scenarios, units, label_width, hide_statistics),
|
| 62 |
+
comparison_report(scenarios, units, label_width, config, hide_statistics),
|
| 63 |
+
extended_statistics_report(scenarios, units, label_width, config, hide_statistics)
|
| 64 |
+
])
|
| 65 |
+
end
|
| 66 |
+
|
| 67 |
+
defp all_have_deviation_of_0?(scenarios) do
|
| 68 |
+
Enum.all?(scenarios, fn scenario ->
|
| 69 |
+
scenario.memory_usage_data.statistics.std_dev == 0.0
|
| 70 |
+
end)
|
| 71 |
+
end
|
| 72 |
+
|
| 73 |
+
defp column_descriptors(label_width, hide_statistics)
|
| 74 |
+
|
| 75 |
+
defp column_descriptors(label_width, false) do
|
| 76 |
+
"\n~*s~*s~*s~*s~*s\n"
|
| 77 |
+
|> :io_lib.format([
|
| 78 |
+
-label_width,
|
| 79 |
+
"Name",
|
| 80 |
+
@average_width,
|
| 81 |
+
"average",
|
| 82 |
+
@deviation_width,
|
| 83 |
+
"deviation",
|
| 84 |
+
@median_width,
|
| 85 |
+
"median",
|
| 86 |
+
@percentile_width,
|
| 87 |
+
"99th %"
|
| 88 |
+
])
|
| 89 |
+
|> to_string
|
| 90 |
+
end
|
| 91 |
+
|
| 92 |
+
defp column_descriptors(label_width, true) do
|
| 93 |
+
"\n~*s~*s\n"
|
| 94 |
+
|> :io_lib.format([
|
| 95 |
+
-label_width,
|
| 96 |
+
"Name",
|
| 97 |
+
@average_width,
|
| 98 |
+
"Memory usage"
|
| 99 |
+
])
|
| 100 |
+
|> to_string
|
| 101 |
+
end
|
| 102 |
+
|
| 103 |
+
@spec scenario_reports([Scenario.t()], unit_per_statistic, integer, boolean) :: [String.t()]
|
| 104 |
+
defp scenario_reports(scenarios, units, label_width, hide_statistics)
|
| 105 |
+
|
| 106 |
+
defp scenario_reports([scenario | other_scenarios], units, label_width, true) do
|
| 107 |
+
[
|
| 108 |
+
reference_report(scenario, units, label_width),
|
| 109 |
+
comparisons(other_scenarios, units, label_width),
|
| 110 |
+
"\n**All measurements for memory usage were the same**\n"
|
| 111 |
+
]
|
| 112 |
+
end
|
| 113 |
+
|
| 114 |
+
defp scenario_reports(scenarios, units, label_width, hide_statistics) do
|
| 115 |
+
Enum.map(scenarios, fn scenario ->
|
| 116 |
+
format_scenario(scenario, units, label_width, hide_statistics)
|
| 117 |
+
end)
|
| 118 |
+
end
|
| 119 |
+
|
| 120 |
+
@na "N/A"
|
| 121 |
+
|
| 122 |
+
@spec format_scenario(Scenario.t(), unit_per_statistic, integer, boolean) :: String.t()
|
| 123 |
+
defp format_scenario(scenario, units, label_width, hide_statistics)
|
| 124 |
+
|
| 125 |
+
defp format_scenario(
|
| 126 |
+
scenario = %Scenario{memory_usage_data: %{statistics: %{sample_size: 0}}},
|
| 127 |
+
_,
|
| 128 |
+
label_width,
|
| 129 |
+
_
|
| 130 |
+
) do
|
| 131 |
+
warning =
|
| 132 |
+
"WARNING the scenario \"#{scenario.name}\" has no memory measurements!" <>
|
| 133 |
+
" This is probably a bug please report it!\n" <>
|
| 134 |
+
"https://github.com/bencheeorg/benchee/issues/new"
|
| 135 |
+
|
| 136 |
+
data =
|
| 137 |
+
"~*ts~*ts\n"
|
| 138 |
+
|> :io_lib.format([
|
| 139 |
+
-label_width,
|
| 140 |
+
scenario.name,
|
| 141 |
+
@average_width,
|
| 142 |
+
@na
|
| 143 |
+
])
|
| 144 |
+
|> to_string
|
| 145 |
+
|
| 146 |
+
warning <> "\n" <> data
|
| 147 |
+
end
|
| 148 |
+
|
| 149 |
+
defp format_scenario(scenario, %{memory: memory_unit}, label_width, false) do
|
| 150 |
+
%Scenario{
|
| 151 |
+
name: name,
|
| 152 |
+
memory_usage_data: %{
|
| 153 |
+
statistics: %Statistics{
|
| 154 |
+
average: average,
|
| 155 |
+
std_dev_ratio: std_dev_ratio,
|
| 156 |
+
median: median,
|
| 157 |
+
percentiles: %{99 => percentile_99}
|
| 158 |
+
}
|
| 159 |
+
}
|
| 160 |
+
} = scenario
|
| 161 |
+
|
| 162 |
+
"~*ts~*ts~*ts~*ts~*ts\n"
|
| 163 |
+
|> :io_lib.format([
|
| 164 |
+
-label_width,
|
| 165 |
+
name,
|
| 166 |
+
@average_width,
|
| 167 |
+
memory_output(average, memory_unit),
|
| 168 |
+
@deviation_width,
|
| 169 |
+
Helpers.deviation_output(std_dev_ratio),
|
| 170 |
+
@median_width,
|
| 171 |
+
memory_output(median, memory_unit),
|
| 172 |
+
@percentile_width,
|
| 173 |
+
memory_output(percentile_99, memory_unit)
|
| 174 |
+
])
|
| 175 |
+
|> to_string
|
| 176 |
+
end
|
| 177 |
+
|
| 178 |
+
defp format_scenario(scenario, %{memory: memory_unit}, label_width, true) do
|
| 179 |
+
%Scenario{
|
| 180 |
+
name: name,
|
| 181 |
+
memory_usage_data: %{
|
| 182 |
+
statistics: %Statistics{
|
| 183 |
+
average: average
|
| 184 |
+
}
|
| 185 |
+
}
|
| 186 |
+
} = scenario
|
| 187 |
+
|
| 188 |
+
"~*ts~*ts\n"
|
| 189 |
+
|> :io_lib.format([
|
| 190 |
+
-label_width,
|
| 191 |
+
name,
|
| 192 |
+
@average_width,
|
| 193 |
+
memory_output(average, memory_unit)
|
| 194 |
+
])
|
| 195 |
+
|> to_string
|
| 196 |
+
end
|
| 197 |
+
|
| 198 |
+
@spec comparison_report([Scenario.t()], unit_per_statistic, integer, map, boolean) :: [
|
| 199 |
+
String.t()
|
| 200 |
+
]
|
| 201 |
+
defp comparison_report(scenarios, units, label_width, config, hide_statistics)
|
| 202 |
+
|
| 203 |
+
# No need for a comparison when only one benchmark was run
|
| 204 |
+
defp comparison_report([_scenario], _, _, _, _), do: []
|
| 205 |
+
defp comparison_report(_, _, _, %{comparison: false}, _), do: []
|
| 206 |
+
defp comparison_report(_, _, _, _, true), do: []
|
| 207 |
+
|
| 208 |
+
defp comparison_report([scenario | other_scenarios], units, label_width, _, _) do
|
| 209 |
+
[
|
| 210 |
+
Helpers.descriptor("Comparison"),
|
| 211 |
+
reference_report(scenario, units, label_width)
|
| 212 |
+
| comparisons(other_scenarios, units, label_width)
|
| 213 |
+
]
|
| 214 |
+
end
|
| 215 |
+
|
| 216 |
+
defp reference_report(scenario, %{memory: memory_unit}, label_width) do
|
| 217 |
+
%Scenario{name: name, memory_usage_data: %{statistics: %Statistics{median: median}}} =
|
| 218 |
+
scenario
|
| 219 |
+
|
| 220 |
+
"~*s~*s\n"
|
| 221 |
+
|> :io_lib.format([
|
| 222 |
+
-label_width,
|
| 223 |
+
name,
|
| 224 |
+
@median_width,
|
| 225 |
+
memory_output(median, memory_unit)
|
| 226 |
+
])
|
| 227 |
+
|> to_string
|
| 228 |
+
end
|
| 229 |
+
|
| 230 |
+
@spec comparisons([Scenario.t()], unit_per_statistic, integer) :: [String.t()]
|
| 231 |
+
defp comparisons(scenarios_to_compare, units, label_width) do
|
| 232 |
+
Enum.map(
|
| 233 |
+
scenarios_to_compare,
|
| 234 |
+
fn scenario ->
|
| 235 |
+
statistics = scenario.memory_usage_data.statistics
|
| 236 |
+
memory_format = memory_output(statistics.average, units.memory)
|
| 237 |
+
|
| 238 |
+
Helpers.format_comparison(
|
| 239 |
+
scenario.name,
|
| 240 |
+
statistics,
|
| 241 |
+
memory_format,
|
| 242 |
+
"memory usage",
|
| 243 |
+
units.memory,
|
| 244 |
+
label_width,
|
| 245 |
+
@median_width
|
| 246 |
+
)
|
| 247 |
+
end
|
| 248 |
+
)
|
| 249 |
+
end
|
| 250 |
+
|
| 251 |
+
defp memory_output(nil, _unit), do: "N/A"
|
| 252 |
+
|
| 253 |
+
defp memory_output(memory, unit) do
|
| 254 |
+
Memory.format({Memory.scale(memory, unit), unit})
|
| 255 |
+
end
|
| 256 |
+
|
| 257 |
+
defp extended_statistics_report(scenarios, units, label_width, config, hide_statistics)
|
| 258 |
+
defp extended_statistics_report(_, _, _, _, true), do: []
|
| 259 |
+
|
| 260 |
+
defp extended_statistics_report(scenarios, units, label_width, %{extended_statistics: true}, _) do
|
| 261 |
+
[
|
| 262 |
+
Helpers.descriptor("Extended statistics"),
|
| 263 |
+
extended_column_descriptors(label_width)
|
| 264 |
+
| extended_statistics(scenarios, units, label_width)
|
| 265 |
+
]
|
| 266 |
+
end
|
| 267 |
+
|
| 268 |
+
defp extended_statistics_report(_, _, _, _, _), do: []
|
| 269 |
+
|
| 270 |
+
defp extended_column_descriptors(label_width) do
|
| 271 |
+
"\n~*s~*s~*s~*s~*s\n"
|
| 272 |
+
|> :io_lib.format([
|
| 273 |
+
-label_width,
|
| 274 |
+
"Name",
|
| 275 |
+
@minimum_width,
|
| 276 |
+
"minimum",
|
| 277 |
+
@maximum_width,
|
| 278 |
+
"maximum",
|
| 279 |
+
@sample_size_width,
|
| 280 |
+
"sample size",
|
| 281 |
+
@mode_width,
|
| 282 |
+
"mode"
|
| 283 |
+
])
|
| 284 |
+
|> to_string
|
| 285 |
+
end
|
| 286 |
+
|
| 287 |
+
defp extended_statistics(scenarios, units, label_width) do
|
| 288 |
+
Enum.map(scenarios, fn scenario ->
|
| 289 |
+
format_scenario_extended(scenario, units, label_width)
|
| 290 |
+
end)
|
| 291 |
+
end
|
| 292 |
+
|
| 293 |
+
defp format_scenario_extended(scenario, %{memory: memory_unit}, label_width) do
|
| 294 |
+
%Scenario{
|
| 295 |
+
name: name,
|
| 296 |
+
memory_usage_data: %{
|
| 297 |
+
statistics: %Statistics{
|
| 298 |
+
minimum: minimum,
|
| 299 |
+
maximum: maximum,
|
| 300 |
+
sample_size: sample_size,
|
| 301 |
+
mode: mode
|
| 302 |
+
}
|
| 303 |
+
}
|
| 304 |
+
} = scenario
|
| 305 |
+
|
| 306 |
+
"~*s~*ts~*ts~*ts~*ts\n"
|
| 307 |
+
|> :io_lib.format([
|
| 308 |
+
-label_width,
|
| 309 |
+
name,
|
| 310 |
+
@minimum_width,
|
| 311 |
+
Helpers.count_output(minimum, memory_unit),
|
| 312 |
+
@maximum_width,
|
| 313 |
+
Helpers.count_output(maximum, memory_unit),
|
| 314 |
+
@sample_size_width,
|
| 315 |
+
Count.format(sample_size),
|
| 316 |
+
@mode_width,
|
| 317 |
+
Helpers.mode_out(mode, memory_unit)
|
| 318 |
+
])
|
| 319 |
+
|> to_string
|
| 320 |
+
end
|
| 321 |
+
end
|
deps/benchee/lib/benchee/formatters/console/reductions.ex
ADDED
|
@@ -0,0 +1,318 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
defmodule Benchee.Formatters.Console.Reductions do
|
| 2 |
+
@moduledoc false
|
| 3 |
+
|
| 4 |
+
alias Benchee.{
|
| 5 |
+
Conversion,
|
| 6 |
+
Conversion.Count,
|
| 7 |
+
Conversion.Unit,
|
| 8 |
+
Formatters.Console.Helpers,
|
| 9 |
+
Scenario,
|
| 10 |
+
Statistics
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
@type unit_per_statistic :: %{atom => Unit.t()}
|
| 14 |
+
|
| 15 |
+
# Length of column header
|
| 16 |
+
@average_width 15
|
| 17 |
+
@deviation_width 11
|
| 18 |
+
@median_width 15
|
| 19 |
+
@percentile_width 15
|
| 20 |
+
@minimum_width 15
|
| 21 |
+
@maximum_width 15
|
| 22 |
+
@sample_size_width 15
|
| 23 |
+
@mode_width 25
|
| 24 |
+
|
| 25 |
+
@doc """
|
| 26 |
+
Formats the reductions statistics to a report suitable for output on the CLI.
|
| 27 |
+
"""
|
| 28 |
+
@spec format_scenarios([Scenario.t()], map) :: [String.t(), ...]
|
| 29 |
+
def format_scenarios(scenarios, config) do
|
| 30 |
+
if reductions_measurements_present?(scenarios) do
|
| 31 |
+
render(scenarios, config)
|
| 32 |
+
else
|
| 33 |
+
[]
|
| 34 |
+
end
|
| 35 |
+
end
|
| 36 |
+
|
| 37 |
+
defp reductions_measurements_present?(scenarios) do
|
| 38 |
+
Enum.any?(scenarios, fn scenario ->
|
| 39 |
+
scenario.reductions_data.statistics.sample_size > 0
|
| 40 |
+
end)
|
| 41 |
+
end
|
| 42 |
+
|
| 43 |
+
defp render(scenarios, config) do
|
| 44 |
+
scaling_strategy = config.unit_scaling
|
| 45 |
+
units = Conversion.units(scenarios, scaling_strategy)
|
| 46 |
+
label_width = Helpers.label_width(scenarios)
|
| 47 |
+
hide_statistics = all_have_deviation_of_0?(scenarios)
|
| 48 |
+
|
| 49 |
+
List.flatten([
|
| 50 |
+
"\nReduction count statistics:\n",
|
| 51 |
+
column_descriptors(label_width, hide_statistics),
|
| 52 |
+
scenario_reports(scenarios, units, label_width, hide_statistics),
|
| 53 |
+
comparison_report(scenarios, units, label_width, config, hide_statistics),
|
| 54 |
+
extended_statistics_report(scenarios, units, label_width, config, hide_statistics)
|
| 55 |
+
])
|
| 56 |
+
end
|
| 57 |
+
|
| 58 |
+
defp all_have_deviation_of_0?(scenarios) do
|
| 59 |
+
Enum.all?(scenarios, fn scenario ->
|
| 60 |
+
scenario.reductions_data.statistics.std_dev == 0.0
|
| 61 |
+
end)
|
| 62 |
+
end
|
| 63 |
+
|
| 64 |
+
defp column_descriptors(label_width, hide_statistics)
|
| 65 |
+
|
| 66 |
+
defp column_descriptors(label_width, false) do
|
| 67 |
+
"\n~*s~*s~*s~*s~*s\n"
|
| 68 |
+
|> :io_lib.format([
|
| 69 |
+
-label_width,
|
| 70 |
+
"Name",
|
| 71 |
+
@average_width,
|
| 72 |
+
"average",
|
| 73 |
+
@deviation_width,
|
| 74 |
+
"deviation",
|
| 75 |
+
@median_width,
|
| 76 |
+
"median",
|
| 77 |
+
@percentile_width,
|
| 78 |
+
"99th %"
|
| 79 |
+
])
|
| 80 |
+
|> to_string
|
| 81 |
+
end
|
| 82 |
+
|
| 83 |
+
defp column_descriptors(label_width, true) do
|
| 84 |
+
"\n~*s~*s\n"
|
| 85 |
+
|> :io_lib.format([
|
| 86 |
+
-label_width,
|
| 87 |
+
"Name",
|
| 88 |
+
@average_width,
|
| 89 |
+
"Reduction count"
|
| 90 |
+
])
|
| 91 |
+
|> to_string
|
| 92 |
+
end
|
| 93 |
+
|
| 94 |
+
@spec scenario_reports([Scenario.t()], unit_per_statistic, integer, boolean) :: [String.t()]
|
| 95 |
+
defp scenario_reports(scenarios, units, label_width, hide_statistics)
|
| 96 |
+
|
| 97 |
+
defp scenario_reports([scenario | other_scenarios], units, label_width, true) do
|
| 98 |
+
[
|
| 99 |
+
reference_report(scenario, units, label_width),
|
| 100 |
+
comparisons(other_scenarios, units, label_width),
|
| 101 |
+
"\n**All measurements for reduction count were the same**\n"
|
| 102 |
+
]
|
| 103 |
+
end
|
| 104 |
+
|
| 105 |
+
defp scenario_reports(scenarios, units, label_width, hide_statistics) do
|
| 106 |
+
Enum.map(scenarios, fn scenario ->
|
| 107 |
+
format_scenario(scenario, units, label_width, hide_statistics)
|
| 108 |
+
end)
|
| 109 |
+
end
|
| 110 |
+
|
| 111 |
+
@na "N/A"
|
| 112 |
+
|
| 113 |
+
@spec format_scenario(Scenario.t(), unit_per_statistic, integer, boolean) :: String.t()
|
| 114 |
+
defp format_scenario(scenario, units, label_width, hide_statistics)
|
| 115 |
+
|
| 116 |
+
defp format_scenario(
|
| 117 |
+
scenario = %Scenario{reductions_data: %{statistics: %{sample_size: 0}}},
|
| 118 |
+
_,
|
| 119 |
+
label_width,
|
| 120 |
+
_
|
| 121 |
+
) do
|
| 122 |
+
warning =
|
| 123 |
+
"WARNING the scenario \"#{scenario.name}\" has no reduction count measurements!" <>
|
| 124 |
+
" This is probably a bug please report it!\n" <>
|
| 125 |
+
"https://github.com/bencheeorg/benchee/issues/new"
|
| 126 |
+
|
| 127 |
+
data =
|
| 128 |
+
"~*ts~*ts\n"
|
| 129 |
+
|> :io_lib.format([
|
| 130 |
+
-label_width,
|
| 131 |
+
scenario.name,
|
| 132 |
+
@average_width,
|
| 133 |
+
@na
|
| 134 |
+
])
|
| 135 |
+
|> to_string
|
| 136 |
+
|
| 137 |
+
warning <> "\n" <> data
|
| 138 |
+
end
|
| 139 |
+
|
| 140 |
+
defp format_scenario(scenario, %{reduction_count: reductions_unit}, label_width, false) do
|
| 141 |
+
%Scenario{
|
| 142 |
+
name: name,
|
| 143 |
+
reductions_data: %{
|
| 144 |
+
statistics: %Statistics{
|
| 145 |
+
average: average,
|
| 146 |
+
std_dev_ratio: std_dev_ratio,
|
| 147 |
+
median: median,
|
| 148 |
+
percentiles: %{99 => percentile_99}
|
| 149 |
+
}
|
| 150 |
+
}
|
| 151 |
+
} = scenario
|
| 152 |
+
|
| 153 |
+
"~*ts~*ts~*ts~*ts~*ts\n"
|
| 154 |
+
|> :io_lib.format([
|
| 155 |
+
-label_width,
|
| 156 |
+
name,
|
| 157 |
+
@average_width,
|
| 158 |
+
Helpers.count_output(average, reductions_unit),
|
| 159 |
+
@deviation_width,
|
| 160 |
+
Helpers.deviation_output(std_dev_ratio),
|
| 161 |
+
@median_width,
|
| 162 |
+
Helpers.count_output(median, reductions_unit),
|
| 163 |
+
@percentile_width,
|
| 164 |
+
Helpers.count_output(percentile_99, reductions_unit)
|
| 165 |
+
])
|
| 166 |
+
|> to_string
|
| 167 |
+
end
|
| 168 |
+
|
| 169 |
+
defp format_scenario(scenario, %{reduction_count: reductions_unit}, label_width, true) do
|
| 170 |
+
%Scenario{
|
| 171 |
+
name: name,
|
| 172 |
+
reductions_data: %{
|
| 173 |
+
statistics: %Statistics{
|
| 174 |
+
average: average
|
| 175 |
+
}
|
| 176 |
+
}
|
| 177 |
+
} = scenario
|
| 178 |
+
|
| 179 |
+
"~*ts~*ts\n"
|
| 180 |
+
|> :io_lib.format([
|
| 181 |
+
-label_width,
|
| 182 |
+
name,
|
| 183 |
+
@average_width,
|
| 184 |
+
Helpers.count_output(average, reductions_unit)
|
| 185 |
+
])
|
| 186 |
+
|> to_string
|
| 187 |
+
end
|
| 188 |
+
|
| 189 |
+
@spec comparison_report([Scenario.t()], unit_per_statistic, integer, map, boolean) :: [
|
| 190 |
+
String.t()
|
| 191 |
+
]
|
| 192 |
+
defp comparison_report(scenarios, units, label_width, config, hide_statistics)
|
| 193 |
+
|
| 194 |
+
# No need for a comparison when only one benchmark was run
|
| 195 |
+
defp comparison_report([_scenario], _, _, _, _), do: []
|
| 196 |
+
defp comparison_report(_, _, _, %{comparison: false}, _), do: []
|
| 197 |
+
defp comparison_report(_, _, _, _, true), do: []
|
| 198 |
+
|
| 199 |
+
defp comparison_report([scenario | other_scenarios], units, label_width, _, _) do
|
| 200 |
+
[
|
| 201 |
+
Helpers.descriptor("Comparison"),
|
| 202 |
+
reference_report(scenario, units, label_width)
|
| 203 |
+
| comparisons(other_scenarios, units, label_width)
|
| 204 |
+
]
|
| 205 |
+
end
|
| 206 |
+
|
| 207 |
+
defp reference_report(scenario, %{reduction_count: reductions_unit}, label_width) do
|
| 208 |
+
%Scenario{name: name, reductions_data: %{statistics: %Statistics{median: median}}} = scenario
|
| 209 |
+
|
| 210 |
+
count =
|
| 211 |
+
if is_nil(median) do
|
| 212 |
+
@na
|
| 213 |
+
else
|
| 214 |
+
Helpers.count_output(median, reductions_unit)
|
| 215 |
+
end
|
| 216 |
+
|
| 217 |
+
"~*s~*s\n"
|
| 218 |
+
|> :io_lib.format([
|
| 219 |
+
-label_width,
|
| 220 |
+
name,
|
| 221 |
+
@median_width,
|
| 222 |
+
count
|
| 223 |
+
])
|
| 224 |
+
|> to_string
|
| 225 |
+
end
|
| 226 |
+
|
| 227 |
+
@spec comparisons([Scenario.t()], unit_per_statistic, integer) :: [String.t()]
|
| 228 |
+
defp comparisons(scenarios_to_compare, units, label_width) do
|
| 229 |
+
Enum.map(
|
| 230 |
+
scenarios_to_compare,
|
| 231 |
+
fn scenario ->
|
| 232 |
+
statistics = scenario.reductions_data.statistics
|
| 233 |
+
|
| 234 |
+
reductions_format =
|
| 235 |
+
if is_nil(statistics.average) do
|
| 236 |
+
@na
|
| 237 |
+
else
|
| 238 |
+
Helpers.count_output(statistics.average, units.reduction_count)
|
| 239 |
+
end
|
| 240 |
+
|
| 241 |
+
Helpers.format_comparison(
|
| 242 |
+
scenario.name,
|
| 243 |
+
statistics,
|
| 244 |
+
reductions_format,
|
| 245 |
+
"reduction count",
|
| 246 |
+
units.reduction_count,
|
| 247 |
+
label_width,
|
| 248 |
+
@median_width
|
| 249 |
+
)
|
| 250 |
+
end
|
| 251 |
+
)
|
| 252 |
+
end
|
| 253 |
+
|
| 254 |
+
defp extended_statistics_report(scenarios, units, label_width, config, hide_statistics)
|
| 255 |
+
defp extended_statistics_report(_, _, _, _, true), do: []
|
| 256 |
+
|
| 257 |
+
defp extended_statistics_report(scenarios, units, label_width, %{extended_statistics: true}, _) do
|
| 258 |
+
[
|
| 259 |
+
Helpers.descriptor("Extended statistics"),
|
| 260 |
+
extended_column_descriptors(label_width)
|
| 261 |
+
| extended_statistics(scenarios, units, label_width)
|
| 262 |
+
]
|
| 263 |
+
end
|
| 264 |
+
|
| 265 |
+
defp extended_statistics_report(_, _, _, _, _), do: []
|
| 266 |
+
|
| 267 |
+
defp extended_column_descriptors(label_width) do
|
| 268 |
+
"\n~*s~*s~*s~*s~*s\n"
|
| 269 |
+
|> :io_lib.format([
|
| 270 |
+
-label_width,
|
| 271 |
+
"Name",
|
| 272 |
+
@minimum_width,
|
| 273 |
+
"minimum",
|
| 274 |
+
@maximum_width,
|
| 275 |
+
"maximum",
|
| 276 |
+
@sample_size_width,
|
| 277 |
+
"sample size",
|
| 278 |
+
@mode_width,
|
| 279 |
+
"mode"
|
| 280 |
+
])
|
| 281 |
+
|> to_string
|
| 282 |
+
end
|
| 283 |
+
|
| 284 |
+
defp extended_statistics(scenarios, units, label_width) do
|
| 285 |
+
Enum.map(scenarios, fn scenario ->
|
| 286 |
+
format_scenario_extended(scenario, units, label_width)
|
| 287 |
+
end)
|
| 288 |
+
end
|
| 289 |
+
|
| 290 |
+
defp format_scenario_extended(scenario, %{reduction_count: reductions_unit}, label_width) do
|
| 291 |
+
%Scenario{
|
| 292 |
+
name: name,
|
| 293 |
+
reductions_data: %{
|
| 294 |
+
statistics: %Statistics{
|
| 295 |
+
minimum: minimum,
|
| 296 |
+
maximum: maximum,
|
| 297 |
+
sample_size: sample_size,
|
| 298 |
+
mode: mode
|
| 299 |
+
}
|
| 300 |
+
}
|
| 301 |
+
} = scenario
|
| 302 |
+
|
| 303 |
+
"~*s~*ts~*ts~*ts~*ts\n"
|
| 304 |
+
|> :io_lib.format([
|
| 305 |
+
-label_width,
|
| 306 |
+
name,
|
| 307 |
+
@minimum_width,
|
| 308 |
+
Helpers.count_output(minimum, reductions_unit),
|
| 309 |
+
@maximum_width,
|
| 310 |
+
Helpers.count_output(maximum, reductions_unit),
|
| 311 |
+
@sample_size_width,
|
| 312 |
+
Count.format(sample_size),
|
| 313 |
+
@mode_width,
|
| 314 |
+
Helpers.mode_out(mode, reductions_unit)
|
| 315 |
+
])
|
| 316 |
+
|> to_string
|
| 317 |
+
end
|
| 318 |
+
end
|
deps/benchee/lib/benchee/formatters/console/run_time.ex
ADDED
|
@@ -0,0 +1,296 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
defmodule Benchee.Formatters.Console.RunTime do
|
| 2 |
+
@moduledoc false
|
| 3 |
+
|
| 4 |
+
# This deals with just the formatting of the run time results. They are similar
|
| 5 |
+
# to the way the memory results are formatted, but different enough to where the
|
| 6 |
+
# abstractions start to break down pretty significantly, so I wanted to extract
|
| 7 |
+
# these two things into separate modules to avoid confusion.
|
| 8 |
+
|
| 9 |
+
alias Benchee.{
|
| 10 |
+
Conversion,
|
| 11 |
+
Conversion.Count,
|
| 12 |
+
Conversion.Duration,
|
| 13 |
+
Conversion.Unit,
|
| 14 |
+
Formatters.Console.Helpers,
|
| 15 |
+
Scenario,
|
| 16 |
+
Statistics
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
@type unit_per_statistic :: %{atom => Unit.t()}
|
| 20 |
+
|
| 21 |
+
@ips_width 13
|
| 22 |
+
@average_width 15
|
| 23 |
+
@deviation_width 11
|
| 24 |
+
@median_width 15
|
| 25 |
+
@percentile_width 15
|
| 26 |
+
@minimum_width 15
|
| 27 |
+
@maximum_width 15
|
| 28 |
+
@sample_size_width 15
|
| 29 |
+
@mode_width 25
|
| 30 |
+
|
| 31 |
+
@doc """
|
| 32 |
+
Formats the run time statistics to a report suitable for output on the CLI.
|
| 33 |
+
|
| 34 |
+
## Examples
|
| 35 |
+
|
| 36 |
+
```
|
| 37 |
+
iex> memory_statistics = %Benchee.Statistics{average: 100.0}
|
| 38 |
+
...>
|
| 39 |
+
...> scenarios = [
|
| 40 |
+
...> %Benchee.Scenario{
|
| 41 |
+
...> name: "My Job",
|
| 42 |
+
...> run_time_data: %Benchee.CollectionData{
|
| 43 |
+
...> statistics: %Benchee.Statistics{
|
| 44 |
+
...> average: 200.0,
|
| 45 |
+
...> ips: 5000.0,
|
| 46 |
+
...> std_dev_ratio: 0.1,
|
| 47 |
+
...> median: 190.0,
|
| 48 |
+
...> percentiles: %{99 => 300.1},
|
| 49 |
+
...> minimum: 100.1,
|
| 50 |
+
...> maximum: 200.2,
|
| 51 |
+
...> sample_size: 10_101,
|
| 52 |
+
...> mode: 333.2
|
| 53 |
+
...> }
|
| 54 |
+
...> },
|
| 55 |
+
...> memory_usage_data: %Benchee.CollectionData{statistics: memory_statistics}
|
| 56 |
+
...> },
|
| 57 |
+
...> %Benchee.Scenario{
|
| 58 |
+
...> name: "Job 2",
|
| 59 |
+
...> run_time_data: %Benchee.CollectionData{
|
| 60 |
+
...> statistics: %Benchee.Statistics{
|
| 61 |
+
...> average: 400.0,
|
| 62 |
+
...> ips: 2500.0,
|
| 63 |
+
...> std_dev_ratio: 0.2,
|
| 64 |
+
...> median: 390.0,
|
| 65 |
+
...> percentiles: %{99 => 500.1},
|
| 66 |
+
...> minimum: 200.2,
|
| 67 |
+
...> maximum: 400.4,
|
| 68 |
+
...> sample_size: 20_202,
|
| 69 |
+
...> mode: [612.3, 554.1]
|
| 70 |
+
...> }
|
| 71 |
+
...> },
|
| 72 |
+
...> memory_usage_data: %Benchee.CollectionData{statistics: memory_statistics}
|
| 73 |
+
...> }
|
| 74 |
+
...> ]
|
| 75 |
+
...>
|
| 76 |
+
...> configuration = %{comparison: false, unit_scaling: :best, extended_statistics: true}
|
| 77 |
+
...> format_scenarios(scenarios, configuration)
|
| 78 |
+
[
|
| 79 |
+
"\nName ips average deviation median 99th %\n",
|
| 80 |
+
"My Job 5 K 200 ns ±10.00% 190 ns 300.10 ns\n",
|
| 81 |
+
"Job 2 2.50 K 400 ns ±20.00% 390 ns 500.10 ns\n",
|
| 82 |
+
"\nExtended statistics: \n",
|
| 83 |
+
"\nName minimum maximum sample size mode\n",
|
| 84 |
+
"My Job 100.10 ns 200.20 ns 10.10 K 333.20 ns\n",
|
| 85 |
+
"Job 2 200.20 ns 400.40 ns 20.20 K 612.30 ns, 554.10 ns\n"
|
| 86 |
+
]
|
| 87 |
+
|
| 88 |
+
```
|
| 89 |
+
|
| 90 |
+
"""
|
| 91 |
+
@spec format_scenarios([Scenario.t()], map) :: [String.t(), ...]
|
| 92 |
+
def format_scenarios(scenarios, config) do
|
| 93 |
+
if run_time_measurements_present?(scenarios) do
|
| 94 |
+
render(scenarios, config)
|
| 95 |
+
else
|
| 96 |
+
[]
|
| 97 |
+
end
|
| 98 |
+
end
|
| 99 |
+
|
| 100 |
+
defp run_time_measurements_present?(scenarios) do
|
| 101 |
+
Enum.any?(scenarios, fn scenario ->
|
| 102 |
+
scenario.run_time_data.statistics.sample_size > 0
|
| 103 |
+
end)
|
| 104 |
+
end
|
| 105 |
+
|
| 106 |
+
defp render(scenarios, config) do
|
| 107 |
+
%{unit_scaling: scaling_strategy} = config
|
| 108 |
+
units = Conversion.units(scenarios, scaling_strategy)
|
| 109 |
+
label_width = Helpers.label_width(scenarios)
|
| 110 |
+
|
| 111 |
+
List.flatten([
|
| 112 |
+
column_descriptors(label_width),
|
| 113 |
+
scenario_reports(scenarios, units, label_width),
|
| 114 |
+
comparison_report(scenarios, units, label_width, config),
|
| 115 |
+
extended_statistics_report(scenarios, units, label_width, config)
|
| 116 |
+
])
|
| 117 |
+
end
|
| 118 |
+
|
| 119 |
+
@spec extended_statistics_report([Scenario.t()], unit_per_statistic, integer, map) :: [
|
| 120 |
+
String.t()
|
| 121 |
+
]
|
| 122 |
+
defp extended_statistics_report(scenarios, units, label_width, %{extended_statistics: true}) do
|
| 123 |
+
[
|
| 124 |
+
Helpers.descriptor("Extended statistics"),
|
| 125 |
+
extended_column_descriptors(label_width)
|
| 126 |
+
| extended_statistics(scenarios, units, label_width)
|
| 127 |
+
]
|
| 128 |
+
end
|
| 129 |
+
|
| 130 |
+
defp extended_statistics_report(_, _, _, _) do
|
| 131 |
+
[]
|
| 132 |
+
end
|
| 133 |
+
|
| 134 |
+
@spec extended_statistics([Scenario.t()], unit_per_statistic, integer) :: [String.t()]
|
| 135 |
+
defp extended_statistics(scenarios, units, label_width) do
|
| 136 |
+
Enum.map(scenarios, fn scenario ->
|
| 137 |
+
format_scenario_extended(scenario, units, label_width)
|
| 138 |
+
end)
|
| 139 |
+
end
|
| 140 |
+
|
| 141 |
+
@spec format_scenario_extended(Scenario.t(), unit_per_statistic, integer) :: String.t()
|
| 142 |
+
defp format_scenario_extended(scenario, %{run_time: run_time_unit}, label_width) do
|
| 143 |
+
%Scenario{
|
| 144 |
+
name: name,
|
| 145 |
+
run_time_data: %{
|
| 146 |
+
statistics: %Statistics{
|
| 147 |
+
minimum: minimum,
|
| 148 |
+
maximum: maximum,
|
| 149 |
+
sample_size: sample_size,
|
| 150 |
+
mode: mode
|
| 151 |
+
}
|
| 152 |
+
}
|
| 153 |
+
} = scenario
|
| 154 |
+
|
| 155 |
+
"~*s~*ts~*ts~*ts~*ts\n"
|
| 156 |
+
|> :io_lib.format([
|
| 157 |
+
-label_width,
|
| 158 |
+
name,
|
| 159 |
+
@minimum_width,
|
| 160 |
+
duration_output(minimum, run_time_unit),
|
| 161 |
+
@maximum_width,
|
| 162 |
+
duration_output(maximum, run_time_unit),
|
| 163 |
+
@sample_size_width,
|
| 164 |
+
Count.format(sample_size),
|
| 165 |
+
@mode_width,
|
| 166 |
+
Helpers.mode_out(mode, run_time_unit)
|
| 167 |
+
])
|
| 168 |
+
|> to_string
|
| 169 |
+
end
|
| 170 |
+
|
| 171 |
+
@spec extended_column_descriptors(integer) :: String.t()
|
| 172 |
+
defp extended_column_descriptors(label_width) do
|
| 173 |
+
"\n~*s~*s~*s~*s~*s\n"
|
| 174 |
+
|> :io_lib.format([
|
| 175 |
+
-label_width,
|
| 176 |
+
"Name",
|
| 177 |
+
@minimum_width,
|
| 178 |
+
"minimum",
|
| 179 |
+
@maximum_width,
|
| 180 |
+
"maximum",
|
| 181 |
+
@sample_size_width,
|
| 182 |
+
"sample size",
|
| 183 |
+
@mode_width,
|
| 184 |
+
"mode"
|
| 185 |
+
])
|
| 186 |
+
|> to_string
|
| 187 |
+
end
|
| 188 |
+
|
| 189 |
+
@spec column_descriptors(integer) :: String.t()
|
| 190 |
+
defp column_descriptors(label_width) do
|
| 191 |
+
"\n~*s~*s~*s~*s~*s~*s\n"
|
| 192 |
+
|> :io_lib.format([
|
| 193 |
+
-label_width,
|
| 194 |
+
"Name",
|
| 195 |
+
@ips_width,
|
| 196 |
+
"ips",
|
| 197 |
+
@average_width,
|
| 198 |
+
"average",
|
| 199 |
+
@deviation_width,
|
| 200 |
+
"deviation",
|
| 201 |
+
@median_width,
|
| 202 |
+
"median",
|
| 203 |
+
@percentile_width,
|
| 204 |
+
"99th %"
|
| 205 |
+
])
|
| 206 |
+
|> to_string
|
| 207 |
+
end
|
| 208 |
+
|
| 209 |
+
@spec scenario_reports([Scenario.t()], unit_per_statistic, integer) :: [String.t()]
|
| 210 |
+
defp scenario_reports(scenarios, units, label_width) do
|
| 211 |
+
Enum.map(scenarios, fn scenario ->
|
| 212 |
+
format_scenario(scenario, units, label_width)
|
| 213 |
+
end)
|
| 214 |
+
end
|
| 215 |
+
|
| 216 |
+
@spec format_scenario(Scenario.t(), unit_per_statistic, integer) :: String.t()
|
| 217 |
+
defp format_scenario(scenario, %{run_time: run_time_unit, ips: ips_unit}, label_width) do
|
| 218 |
+
%Scenario{
|
| 219 |
+
name: name,
|
| 220 |
+
run_time_data: %{
|
| 221 |
+
statistics: %Statistics{
|
| 222 |
+
average: average,
|
| 223 |
+
ips: ips,
|
| 224 |
+
std_dev_ratio: std_dev_ratio,
|
| 225 |
+
median: median,
|
| 226 |
+
percentiles: %{99 => percentile_99}
|
| 227 |
+
}
|
| 228 |
+
}
|
| 229 |
+
} = scenario
|
| 230 |
+
|
| 231 |
+
"~*s~*ts~*ts~*ts~*ts~*ts\n"
|
| 232 |
+
|> :io_lib.format([
|
| 233 |
+
-label_width,
|
| 234 |
+
name,
|
| 235 |
+
@ips_width,
|
| 236 |
+
Helpers.count_output(ips, ips_unit),
|
| 237 |
+
@average_width,
|
| 238 |
+
duration_output(average, run_time_unit),
|
| 239 |
+
@deviation_width,
|
| 240 |
+
Helpers.deviation_output(std_dev_ratio),
|
| 241 |
+
@median_width,
|
| 242 |
+
duration_output(median, run_time_unit),
|
| 243 |
+
@percentile_width,
|
| 244 |
+
duration_output(percentile_99, run_time_unit)
|
| 245 |
+
])
|
| 246 |
+
|> to_string
|
| 247 |
+
end
|
| 248 |
+
|
| 249 |
+
@spec comparison_report([Scenario.t()], unit_per_statistic, integer, map) :: [String.t()]
|
| 250 |
+
defp comparison_report(scenarios, units, label_width, config)
|
| 251 |
+
|
| 252 |
+
# No need for a comparison when only one benchmark was run
|
| 253 |
+
defp comparison_report([_scenario], _, _, _), do: []
|
| 254 |
+
defp comparison_report(_, _, _, %{comparison: false}), do: []
|
| 255 |
+
|
| 256 |
+
defp comparison_report([scenario | other_scenarios], units, label_width, _) do
|
| 257 |
+
[
|
| 258 |
+
Helpers.descriptor("Comparison"),
|
| 259 |
+
reference_report(scenario, units, label_width)
|
| 260 |
+
| comparisons(other_scenarios, units, label_width)
|
| 261 |
+
]
|
| 262 |
+
end
|
| 263 |
+
|
| 264 |
+
defp reference_report(scenario, %{ips: ips_unit}, label_width) do
|
| 265 |
+
%Scenario{name: name, run_time_data: %{statistics: %Statistics{ips: ips}}} = scenario
|
| 266 |
+
|
| 267 |
+
"~*s~*s\n"
|
| 268 |
+
|> :io_lib.format([-label_width, name, @ips_width, Helpers.count_output(ips, ips_unit)])
|
| 269 |
+
|> to_string
|
| 270 |
+
end
|
| 271 |
+
|
| 272 |
+
@spec comparisons([Scenario.t()], unit_per_statistic, integer) :: [String.t()]
|
| 273 |
+
defp comparisons(scenarios_to_compare, units, label_width) do
|
| 274 |
+
Enum.map(
|
| 275 |
+
scenarios_to_compare,
|
| 276 |
+
fn scenario ->
|
| 277 |
+
statistics = scenario.run_time_data.statistics
|
| 278 |
+
ips_format = Helpers.count_output(statistics.ips, units.ips)
|
| 279 |
+
|
| 280 |
+
Helpers.format_comparison(
|
| 281 |
+
scenario.name,
|
| 282 |
+
statistics,
|
| 283 |
+
ips_format,
|
| 284 |
+
"slower",
|
| 285 |
+
units.run_time,
|
| 286 |
+
label_width,
|
| 287 |
+
@ips_width
|
| 288 |
+
)
|
| 289 |
+
end
|
| 290 |
+
)
|
| 291 |
+
end
|
| 292 |
+
|
| 293 |
+
defp duration_output(duration, unit) do
|
| 294 |
+
Duration.format({Duration.scale(duration, unit), unit})
|
| 295 |
+
end
|
| 296 |
+
end
|
deps/benchee/lib/benchee/formatters/tagged_save.ex
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
defmodule Benchee.Formatters.TaggedSave do
|
| 2 |
+
@moduledoc """
|
| 3 |
+
Store the whole suite in the Erlang `ExternalTermFormat` while tagging the
|
| 4 |
+
scenarios of the current run with a specified tag - can be used for storing
|
| 5 |
+
and later loading the results of previous runs with `Benchee.ScenarioLoader`.
|
| 6 |
+
|
| 7 |
+
Automatically configured as the last formatter to run when specifying the
|
| 8 |
+
`save` option in the configuration - see `Benchee.Configuration`.
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
@behaviour Benchee.Formatter
|
| 12 |
+
|
| 13 |
+
alias Benchee.Scenario
|
| 14 |
+
alias Benchee.Suite
|
| 15 |
+
alias Benchee.Utility.FileCreation
|
| 16 |
+
|
| 17 |
+
@doc """
|
| 18 |
+
Tags all scenario with the desired tag and returns it in term_to_binary along with save path.
|
| 19 |
+
"""
|
| 20 |
+
@impl true
|
| 21 |
+
@spec format(Suite.t(), map) :: {binary, String.t()}
|
| 22 |
+
def format(suite = %Suite{scenarios: scenarios}, formatter_config) do
|
| 23 |
+
tag = determine_tag(scenarios, formatter_config)
|
| 24 |
+
tagged_scenarios = tag_scenarios(scenarios, tag)
|
| 25 |
+
tagged_suite = %Suite{suite | scenarios: tagged_scenarios}
|
| 26 |
+
|
| 27 |
+
{:erlang.term_to_binary(tagged_suite), formatter_config.path}
|
| 28 |
+
end
|
| 29 |
+
|
| 30 |
+
defp determine_tag(scenarios, %{tag: desired_tag}) do
|
| 31 |
+
scenarios
|
| 32 |
+
|> Enum.map(fn scenario -> scenario.tag end)
|
| 33 |
+
|> Enum.uniq()
|
| 34 |
+
|> Enum.filter(fn tag ->
|
| 35 |
+
tag != nil && tag =~ ~r/#{Regex.escape(desired_tag)}/
|
| 36 |
+
end)
|
| 37 |
+
|> choose_tag(desired_tag)
|
| 38 |
+
end
|
| 39 |
+
|
| 40 |
+
defp choose_tag([], desired_tag), do: desired_tag
|
| 41 |
+
|
| 42 |
+
defp choose_tag(tags, desired_tag) do
|
| 43 |
+
max = get_maximum_tag_increaser(tags, desired_tag)
|
| 44 |
+
"#{desired_tag}-#{max + 1}"
|
| 45 |
+
end
|
| 46 |
+
|
| 47 |
+
defp get_maximum_tag_increaser(tags, desired_tag) do
|
| 48 |
+
tags
|
| 49 |
+
|> Enum.map(fn tag -> String.replace(tag, ~r/#{Regex.escape(desired_tag)}-?/, "") end)
|
| 50 |
+
|> Enum.map(&tag_increaser/1)
|
| 51 |
+
|> Enum.max()
|
| 52 |
+
end
|
| 53 |
+
|
| 54 |
+
defp tag_increaser(""), do: 1
|
| 55 |
+
defp tag_increaser(string_number), do: String.to_integer(string_number)
|
| 56 |
+
|
| 57 |
+
defp tag_scenarios(scenarios, tag) do
|
| 58 |
+
Enum.map(scenarios, fn scenario ->
|
| 59 |
+
scenario
|
| 60 |
+
|> tagged_scenario(tag)
|
| 61 |
+
|> update_name
|
| 62 |
+
end)
|
| 63 |
+
end
|
| 64 |
+
|
| 65 |
+
defp tagged_scenario(scenario = %Scenario{tag: nil}, desired_tag) do
|
| 66 |
+
%Scenario{scenario | tag: desired_tag}
|
| 67 |
+
end
|
| 68 |
+
|
| 69 |
+
defp tagged_scenario(scenario, _desired_tag) do
|
| 70 |
+
scenario
|
| 71 |
+
end
|
| 72 |
+
|
| 73 |
+
defp update_name(scenario = %Scenario{}) do
|
| 74 |
+
%Scenario{scenario | name: Scenario.display_name(scenario)}
|
| 75 |
+
end
|
| 76 |
+
|
| 77 |
+
@doc """
|
| 78 |
+
Writes the binary returned by `format/2` to the indicated location, telling you where that is.
|
| 79 |
+
"""
|
| 80 |
+
@spec write({binary, String.t()}, map) :: :ok
|
| 81 |
+
@impl true
|
| 82 |
+
def write({term_binary, filename}, _) do
|
| 83 |
+
FileCreation.ensure_directory_exists(filename)
|
| 84 |
+
return_value = File.write(filename, term_binary)
|
| 85 |
+
|
| 86 |
+
IO.puts("Suite saved in external term format at #{filename}")
|
| 87 |
+
|
| 88 |
+
return_value
|
| 89 |
+
end
|
| 90 |
+
end
|
deps/benchee/lib/benchee/output/benchmark_printer.ex
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
defmodule Benchee.Output.BenchmarkPrinter do
|
| 2 |
+
@moduledoc false
|
| 3 |
+
|
| 4 |
+
alias Benchee.Benchmark
|
| 5 |
+
alias Benchee.Conversion.Duration
|
| 6 |
+
alias Benchee.System
|
| 7 |
+
|
| 8 |
+
@doc """
|
| 9 |
+
Shown when you try benchmark an evaluated function.
|
| 10 |
+
|
| 11 |
+
Compiled functions should be preferred as they are less likely to introduce additional overhead to your benchmark timing.
|
| 12 |
+
"""
|
| 13 |
+
def evaluated_function_warning(job_name) do
|
| 14 |
+
IO.puts("""
|
| 15 |
+
Warning: the benchmark #{job_name} is using an evaluated function.
|
| 16 |
+
Evaluated functions perform slower than compiled functions.
|
| 17 |
+
You can move the Benchee caller to a function in a module and invoke `Mod.fun()` instead.
|
| 18 |
+
Alternatively, you can move the benchmark into a benchmark.exs file and run mix run benchmark.exs
|
| 19 |
+
""")
|
| 20 |
+
end
|
| 21 |
+
|
| 22 |
+
@doc """
|
| 23 |
+
Shown when you try to define a benchmark with the same name twice.
|
| 24 |
+
|
| 25 |
+
How would you want to discern those anyhow?
|
| 26 |
+
"""
|
| 27 |
+
def duplicate_benchmark_warning(name) do
|
| 28 |
+
IO.puts(
|
| 29 |
+
"You already have a job defined with the name \"#{name}\", you can't add two jobs with the same name!"
|
| 30 |
+
)
|
| 31 |
+
end
|
| 32 |
+
|
| 33 |
+
@doc """
|
| 34 |
+
Prints general information such as system information and estimated
|
| 35 |
+
benchmarking time.
|
| 36 |
+
"""
|
| 37 |
+
def configuration_information(%{configuration: %{print: %{configuration: false}}}) do
|
| 38 |
+
nil
|
| 39 |
+
end
|
| 40 |
+
|
| 41 |
+
def configuration_information(%{scenarios: scenarios, system: sys, configuration: config}) do
|
| 42 |
+
system_information(sys)
|
| 43 |
+
suite_information(scenarios, config)
|
| 44 |
+
end
|
| 45 |
+
|
| 46 |
+
defp system_information(%System{
|
| 47 |
+
erlang: erlang_version,
|
| 48 |
+
elixir: elixir_version,
|
| 49 |
+
jit_enabled?: jit_enabled?,
|
| 50 |
+
os: os,
|
| 51 |
+
num_cores: num_cores,
|
| 52 |
+
cpu_speed: cpu_speed,
|
| 53 |
+
available_memory: available_memory
|
| 54 |
+
}) do
|
| 55 |
+
IO.puts("""
|
| 56 |
+
Operating System: #{os}
|
| 57 |
+
CPU Information: #{cpu_speed}
|
| 58 |
+
Number of Available Cores: #{num_cores}
|
| 59 |
+
Available memory: #{available_memory}
|
| 60 |
+
Elixir #{elixir_version}
|
| 61 |
+
Erlang #{erlang_version}
|
| 62 |
+
JIT enabled: #{jit_enabled?}
|
| 63 |
+
""")
|
| 64 |
+
end
|
| 65 |
+
|
| 66 |
+
defp suite_information(scenarios, %{
|
| 67 |
+
parallel: parallel,
|
| 68 |
+
time: time,
|
| 69 |
+
warmup: warmup,
|
| 70 |
+
inputs: inputs,
|
| 71 |
+
memory_time: memory_time,
|
| 72 |
+
reduction_time: reduction_time,
|
| 73 |
+
exclude_outliers: exclude_outliers
|
| 74 |
+
}) do
|
| 75 |
+
scenario_count = length(scenarios)
|
| 76 |
+
exec_time = warmup + time + memory_time + reduction_time
|
| 77 |
+
total_time = scenario_count * exec_time
|
| 78 |
+
|
| 79 |
+
IO.puts("""
|
| 80 |
+
Benchmark suite executing with the following configuration:
|
| 81 |
+
warmup: #{Duration.format_human(warmup)}
|
| 82 |
+
time: #{Duration.format_human(time)}
|
| 83 |
+
memory time: #{Duration.format_human(memory_time)}
|
| 84 |
+
reduction time: #{Duration.format_human(reduction_time)}
|
| 85 |
+
parallel: #{parallel}
|
| 86 |
+
inputs: #{inputs_out(inputs)}
|
| 87 |
+
Estimated total run time: #{Duration.format_human(total_time)}
|
| 88 |
+
Excluding outliers: #{exclude_outliers}
|
| 89 |
+
""")
|
| 90 |
+
end
|
| 91 |
+
|
| 92 |
+
defp inputs_out(nil), do: "none specified"
|
| 93 |
+
|
| 94 |
+
defp inputs_out(inputs) do
|
| 95 |
+
Enum.map_join(inputs, ", ", fn {name, _} -> name end)
|
| 96 |
+
end
|
| 97 |
+
|
| 98 |
+
@doc """
|
| 99 |
+
Prints a notice which job is currently being benchmarked.
|
| 100 |
+
"""
|
| 101 |
+
def benchmarking(_, _, %{print: %{benchmarking: false}}), do: nil
|
| 102 |
+
|
| 103 |
+
def benchmarking(name, input_name, config) do
|
| 104 |
+
time_configs = [config.time, config.warmup, config.memory_time, config.reduction_time]
|
| 105 |
+
|
| 106 |
+
if Enum.all?(time_configs, fn time -> time == 0 end) do
|
| 107 |
+
nil
|
| 108 |
+
else
|
| 109 |
+
IO.puts("Benchmarking #{name}#{input_information(input_name)} ...")
|
| 110 |
+
end
|
| 111 |
+
end
|
| 112 |
+
|
| 113 |
+
@no_input Benchmark.no_input()
|
| 114 |
+
defp input_information(@no_input), do: ""
|
| 115 |
+
defp input_information(input_name), do: " with input #{input_name}"
|
| 116 |
+
|
| 117 |
+
@doc """
|
| 118 |
+
Prints a warning about accuracy of benchmarks when the function is super fast.
|
| 119 |
+
"""
|
| 120 |
+
def fast_warning do
|
| 121 |
+
IO.puts("""
|
| 122 |
+
Warning: The function you are trying to benchmark is super fast, making measurements more unreliable!
|
| 123 |
+
This holds especially true for memory measurements or when running with hooks.
|
| 124 |
+
|
| 125 |
+
See: https://github.com/bencheeorg/benchee/wiki/Benchee-Warnings#fast-execution-warning
|
| 126 |
+
|
| 127 |
+
You may disable this warning by passing print: [fast_warning: false] as configuration options.
|
| 128 |
+
""")
|
| 129 |
+
end
|
| 130 |
+
|
| 131 |
+
@doc """
|
| 132 |
+
Print the measured function call overhead.
|
| 133 |
+
"""
|
| 134 |
+
@spec function_call_overhead(non_neg_integer()) :: :ok
|
| 135 |
+
def function_call_overhead(overhead) do
|
| 136 |
+
scaled_overhead = Duration.scale(overhead)
|
| 137 |
+
|
| 138 |
+
IO.puts("Measured function call overhead as: #{Duration.format(scaled_overhead)}")
|
| 139 |
+
end
|
| 140 |
+
end
|
deps/benchee/lib/benchee/output/profile_printer.ex
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
defmodule Benchee.Output.ProfilePrinter do
|
| 2 |
+
@moduledoc false
|
| 3 |
+
|
| 4 |
+
@doc """
|
| 5 |
+
Prints a notification of which job is being profiled.
|
| 6 |
+
"""
|
| 7 |
+
def profiling(name, profiler) do
|
| 8 |
+
IO.puts("\nProfiling #{name} with #{profiler}...")
|
| 9 |
+
end
|
| 10 |
+
end
|
deps/benchee/lib/benchee/output/progress_printer.ex
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
defmodule Benchee.Output.ProgressPrinter do
|
| 2 |
+
@moduledoc false
|
| 3 |
+
|
| 4 |
+
def calculating_statistics(%{print: %{benchmarking: false}}), do: nil
|
| 5 |
+
|
| 6 |
+
def calculating_statistics(_config) do
|
| 7 |
+
IO.puts("Calculating statistics...")
|
| 8 |
+
end
|
| 9 |
+
|
| 10 |
+
def formatting(%{print: %{benchmarking: false}}), do: nil
|
| 11 |
+
|
| 12 |
+
def formatting(_config) do
|
| 13 |
+
IO.puts("Formatting results...")
|
| 14 |
+
end
|
| 15 |
+
end
|
deps/benchee/lib/benchee/profile.ex
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
defmodule Benchee.Profile do
|
| 2 |
+
@moduledoc """
|
| 3 |
+
Profiles each scenario after benchmarking them if the `profile_after` option is either set to:
|
| 4 |
+
* `true`,
|
| 5 |
+
* a valid `profiler`,
|
| 6 |
+
* a tuple of a valid `profiler` and a list of options to pass to it, e.g., `{:fprof, [sort: :own]}`.
|
| 7 |
+
|
| 8 |
+
The profiler that will be used is either the one set by the `profiler_after` option or, if set to `true`,
|
| 9 |
+
the default one (`:eprof`). It accepts however the following profilers:
|
| 10 |
+
* `:cprof` will profile with [`Mix.Task.Profile.Cprof`](https://hexdocs.pm/mix/Mix.Tasks.Profile.Cprof.html).
|
| 11 |
+
It provides information related to the number of function calls.
|
| 12 |
+
* `:eprof` will profile with [`Mix.Task.Profile.Eprof`](https://hexdocs.pm/mix/Mix.Tasks.Profile.Eprof.html).
|
| 13 |
+
It provides information related to the time spent on each function in regard to the total execution time.
|
| 14 |
+
* `:fprof` will profile with [`Mix.Task.Profile.Fprof`](https://hexdocs.pm/mix/Mix.Tasks.Profile.Cprof.html).
|
| 15 |
+
It provides information related to the time spent on each function, both the *total* time spent on it and the time spent on it,
|
| 16 |
+
*excluding* the time of called functions.
|
| 17 |
+
"""
|
| 18 |
+
|
| 19 |
+
alias Benchee.Benchmark.BenchmarkConfig
|
| 20 |
+
alias Benchee.Benchmark.Runner
|
| 21 |
+
alias Benchee.Benchmark.ScenarioContext
|
| 22 |
+
alias Benchee.Output.ProfilePrinter, as: Printer
|
| 23 |
+
alias Benchee.Suite
|
| 24 |
+
|
| 25 |
+
@default_profiler :eprof
|
| 26 |
+
@builtin_profilers [:cprof, :eprof, :fprof]
|
| 27 |
+
# https://hexdocs.pm/mix/1.17.0/Mix.Tasks.Profile.Tprof.html
|
| 28 |
+
# Tprof was introduced in elixir 1.17.0 and requires OTP 27
|
| 29 |
+
# Elixir errors out fine, but our test kind of put their trust into `builtin_profilers`
|
| 30 |
+
# being runnable.
|
| 31 |
+
if Version.match?(System.version(), ">= 1.17.0") &&
|
| 32 |
+
String.to_integer(System.otp_release()) >= 27 do
|
| 33 |
+
@builtin_profilers [:tprof | @builtin_profilers]
|
| 34 |
+
end
|
| 35 |
+
|
| 36 |
+
# we run the function a bunch already, no need for further warmup
|
| 37 |
+
@default_profiler_opts [warmup: false]
|
| 38 |
+
|
| 39 |
+
@doc """
|
| 40 |
+
Returns the atom corresponding to the default profiler.
|
| 41 |
+
"""
|
| 42 |
+
@spec default_profiler() :: unquote(@default_profiler)
|
| 43 |
+
def default_profiler, do: @default_profiler
|
| 44 |
+
|
| 45 |
+
@doc """
|
| 46 |
+
List of supported builtin profilers as atoms.
|
| 47 |
+
"""
|
| 48 |
+
def builtin_profilers, do: @builtin_profilers
|
| 49 |
+
|
| 50 |
+
@doc """
|
| 51 |
+
Runs for each scenario found in the suite the `profile/2` function from the given profiler.
|
| 52 |
+
"""
|
| 53 |
+
@spec profile(Suite.t(), module) :: Suite.t()
|
| 54 |
+
def profile(suite, printer \\ Printer)
|
| 55 |
+
def profile(suite = %{configuration: %{profile_after: false}}, _printer), do: suite
|
| 56 |
+
|
| 57 |
+
def profile(
|
| 58 |
+
suite = %{
|
| 59 |
+
scenarios: scenarios,
|
| 60 |
+
configuration: config = %{profile_after: true}
|
| 61 |
+
},
|
| 62 |
+
printer
|
| 63 |
+
) do
|
| 64 |
+
do_profile(scenarios, {@default_profiler, @default_profiler_opts}, config, printer)
|
| 65 |
+
|
| 66 |
+
suite
|
| 67 |
+
end
|
| 68 |
+
|
| 69 |
+
def profile(
|
| 70 |
+
suite = %{
|
| 71 |
+
scenarios: scenarios,
|
| 72 |
+
configuration: config = %{profile_after: {profiler, profiler_opts}}
|
| 73 |
+
},
|
| 74 |
+
printer
|
| 75 |
+
) do
|
| 76 |
+
profiler_opts = Keyword.merge(@default_profiler_opts, profiler_opts)
|
| 77 |
+
do_profile(scenarios, {profiler, profiler_opts}, config, printer)
|
| 78 |
+
|
| 79 |
+
suite
|
| 80 |
+
end
|
| 81 |
+
|
| 82 |
+
def profile(
|
| 83 |
+
suite = %{
|
| 84 |
+
scenarios: scenarios,
|
| 85 |
+
configuration: config = %{profile_after: profiler}
|
| 86 |
+
},
|
| 87 |
+
printer
|
| 88 |
+
) do
|
| 89 |
+
do_profile(scenarios, {profiler, @default_profiler_opts}, config, printer)
|
| 90 |
+
|
| 91 |
+
suite
|
| 92 |
+
end
|
| 93 |
+
|
| 94 |
+
defp do_profile(scenarios, {profiler, profiler_opts}, config, printer) do
|
| 95 |
+
profiler_module = profiler_to_module(profiler)
|
| 96 |
+
|
| 97 |
+
Enum.each(scenarios, fn scenario ->
|
| 98 |
+
run(scenario, {profiler, profiler_module, profiler_opts}, config, printer)
|
| 99 |
+
end)
|
| 100 |
+
end
|
| 101 |
+
|
| 102 |
+
defp run(
|
| 103 |
+
scenario,
|
| 104 |
+
{profiler, profiler_module, profiler_opts},
|
| 105 |
+
config,
|
| 106 |
+
printer
|
| 107 |
+
) do
|
| 108 |
+
printer.profiling(scenario.name, profiler)
|
| 109 |
+
|
| 110 |
+
profiler_module.profile(
|
| 111 |
+
fn ->
|
| 112 |
+
Runner.run_once(scenario, %ScenarioContext{config: BenchmarkConfig.from(config)})
|
| 113 |
+
end,
|
| 114 |
+
profiler_opts
|
| 115 |
+
)
|
| 116 |
+
end
|
| 117 |
+
|
| 118 |
+
# If given a builtin profiler the function will return its proper module.
|
| 119 |
+
# In the case of an unknown profiler, it will raise an `UnknownProfilerError` exception.
|
| 120 |
+
defp profiler_to_module(profiler) do
|
| 121 |
+
if Enum.member?(@builtin_profilers, profiler) do
|
| 122 |
+
profiler =
|
| 123 |
+
profiler
|
| 124 |
+
|> Atom.to_string()
|
| 125 |
+
|> String.capitalize()
|
| 126 |
+
|
| 127 |
+
Module.concat(Mix.Tasks.Profile, profiler)
|
| 128 |
+
else
|
| 129 |
+
raise Benchee.UnknownProfilerError,
|
| 130 |
+
message: "Got an unknown '#{inspect(profiler)}' built-in profiler."
|
| 131 |
+
end
|
| 132 |
+
end
|
| 133 |
+
end
|
deps/benchee/lib/benchee/relative_statistics.ex
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
defmodule Benchee.RelativeStatistics do
|
| 2 |
+
@moduledoc """
|
| 3 |
+
Statistics that are relative from one scenario to another.
|
| 4 |
+
|
| 5 |
+
Such as how much slower/faster something is or what the absolute difference is in the measured
|
| 6 |
+
values.
|
| 7 |
+
Is its own step because it has to be executed after scenarios have been loaded via
|
| 8 |
+
`Benchee.ScenarioLoader` to include them in the calculation, while `Benchee.Statistics`
|
| 9 |
+
has to happen before they are loaded to avoid recalculating their statistics.
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
alias Benchee.{Scenario, Statistics, Suite}
|
| 13 |
+
|
| 14 |
+
@doc """
|
| 15 |
+
Calculate the statistics of scenarios relative to each other and sorts scenarios.
|
| 16 |
+
|
| 17 |
+
Such as `relative_more`, `relative_less` and `absolute_difference`,
|
| 18 |
+
see `t:Benchee.Statistics.t/0` for more.
|
| 19 |
+
|
| 20 |
+
The sorting of scenarios is important so that they always have the same order in
|
| 21 |
+
all formatters. Scenarios are sorted first by run time average, then by memory average.
|
| 22 |
+
"""
|
| 23 |
+
@spec relative_statistics(Suite.t()) :: Suite.t()
|
| 24 |
+
def relative_statistics(suite = %Suite{}) do
|
| 25 |
+
%Suite{suite | scenarios: calculate_relative_statistics(suite.scenarios)}
|
| 26 |
+
end
|
| 27 |
+
|
| 28 |
+
defp calculate_relative_statistics([]), do: []
|
| 29 |
+
|
| 30 |
+
defp calculate_relative_statistics(scenarios) do
|
| 31 |
+
scenarios
|
| 32 |
+
|> scenarios_by_input()
|
| 33 |
+
|> Enum.flat_map(fn scenarios_with_same_input ->
|
| 34 |
+
sorted_scenarios = sort(scenarios_with_same_input)
|
| 35 |
+
{reference, others} = split_reference_scenario(sorted_scenarios)
|
| 36 |
+
others_with_relative = statistics_relative_to(others, reference)
|
| 37 |
+
[reference | others_with_relative]
|
| 38 |
+
end)
|
| 39 |
+
end
|
| 40 |
+
|
| 41 |
+
@spec sort([Scenario.t()]) :: [Scenario.t()]
|
| 42 |
+
defp sort(scenarios) do
|
| 43 |
+
Enum.sort_by(scenarios, fn scenario ->
|
| 44 |
+
{scenario.run_time_data.statistics.average, scenario.memory_usage_data.statistics.average,
|
| 45 |
+
scenario.reductions_data.statistics.average}
|
| 46 |
+
end)
|
| 47 |
+
end
|
| 48 |
+
|
| 49 |
+
# we can't just group_by `input_name` because that'd lose the order of inputs which might
|
| 50 |
+
# be important
|
| 51 |
+
# Thanks past Tobi, I was just about to do that. - Tobi, 5 years later
|
| 52 |
+
defp scenarios_by_input(scenarios) do
|
| 53 |
+
original_input_order = scenarios |> Enum.map(& &1.input_name) |> Enum.uniq()
|
| 54 |
+
|
| 55 |
+
grouped_scenarios = Enum.group_by(scenarios, & &1.input_name)
|
| 56 |
+
|
| 57 |
+
Enum.map(original_input_order, fn input_name ->
|
| 58 |
+
Map.fetch!(grouped_scenarios, input_name)
|
| 59 |
+
end)
|
| 60 |
+
end
|
| 61 |
+
|
| 62 |
+
# right now we take the first scenario as we sorted them and it is the fastest,
|
| 63 |
+
# whenever we implement #179 though this becomes more involved
|
| 64 |
+
defp split_reference_scenario(scenarios) do
|
| 65 |
+
[reference | others] = scenarios
|
| 66 |
+
{reference, others}
|
| 67 |
+
end
|
| 68 |
+
|
| 69 |
+
defp statistics_relative_to(scenarios, reference) do
|
| 70 |
+
Enum.map(scenarios, fn scenario ->
|
| 71 |
+
scenario
|
| 72 |
+
|> update_in([Access.key!(:run_time_data), Access.key!(:statistics)], fn statistics ->
|
| 73 |
+
add_relative_statistics(statistics, reference.run_time_data.statistics)
|
| 74 |
+
end)
|
| 75 |
+
|> update_in([Access.key!(:memory_usage_data), Access.key!(:statistics)], fn statistics ->
|
| 76 |
+
add_relative_statistics(statistics, reference.memory_usage_data.statistics)
|
| 77 |
+
end)
|
| 78 |
+
|> update_in([Access.key!(:reductions_data), Access.key!(:statistics)], fn statistics ->
|
| 79 |
+
add_relative_statistics(statistics, reference.reductions_data.statistics)
|
| 80 |
+
end)
|
| 81 |
+
end)
|
| 82 |
+
end
|
| 83 |
+
|
| 84 |
+
# we might not run time/memory --> we shouldn't crash then ;)
|
| 85 |
+
defp add_relative_statistics(statistics = %{average: nil}, _reference), do: statistics
|
| 86 |
+
defp add_relative_statistics(statistics, %{average: nil}), do: statistics
|
| 87 |
+
|
| 88 |
+
defp add_relative_statistics(statistics = %Statistics{}, reference_statistics) do
|
| 89 |
+
%Statistics{
|
| 90 |
+
statistics
|
| 91 |
+
| relative_more: zero_safe_division(statistics.average, reference_statistics.average),
|
| 92 |
+
relative_less: zero_safe_division(reference_statistics.average, statistics.average),
|
| 93 |
+
absolute_difference: statistics.average - reference_statistics.average
|
| 94 |
+
}
|
| 95 |
+
end
|
| 96 |
+
|
| 97 |
+
defp zero_safe_division(+0.0, +0.0), do: 1.0
|
| 98 |
+
defp zero_safe_division(_, 0), do: :infinity
|
| 99 |
+
defp zero_safe_division(_, +0.0), do: :infinity
|
| 100 |
+
defp zero_safe_division(a, b), do: a / b
|
| 101 |
+
end
|
deps/benchee/lib/benchee/scenario.ex
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
defmodule Benchee.Scenario do
|
| 2 |
+
@moduledoc """
|
| 3 |
+
Core data structure representing one particular case (combination of function and input).
|
| 4 |
+
|
| 5 |
+
Represents the combination of a particular function to benchmark (also called "job" defined
|
| 6 |
+
by `job_name` and `function`) in combination with a specific input (`input_name` and `input`).
|
| 7 |
+
When no input is given, the combined value is representative of "no input".
|
| 8 |
+
|
| 9 |
+
A scenario then further gathers all data collected for this particular combination during
|
| 10 |
+
`Benchee.Benchmark.collect/3`, which are then used later in the process by `Benchee.Statistics`
|
| 11 |
+
to compute the relevant statistics which are then also added to the scenario.
|
| 12 |
+
It is the home of the aggregated knowledge regarding this particular case/scenario.
|
| 13 |
+
|
| 14 |
+
`name` is the name that should be used by formatters to display scenarios as
|
| 15 |
+
it potentially includes the `tag` present when loading scenarios that were
|
| 16 |
+
saved before. See `display_name/1`.
|
| 17 |
+
"""
|
| 18 |
+
|
| 19 |
+
alias Benchee.Benchmark.Hooks
|
| 20 |
+
alias Benchee.CollectionData
|
| 21 |
+
|
| 22 |
+
defstruct [
|
| 23 |
+
:name,
|
| 24 |
+
:job_name,
|
| 25 |
+
:function,
|
| 26 |
+
:input_name,
|
| 27 |
+
:input,
|
| 28 |
+
:before_each,
|
| 29 |
+
:after_each,
|
| 30 |
+
:before_scenario,
|
| 31 |
+
:after_scenario,
|
| 32 |
+
:tag,
|
| 33 |
+
run_time_data: %CollectionData{},
|
| 34 |
+
memory_usage_data: %CollectionData{},
|
| 35 |
+
reductions_data: %CollectionData{}
|
| 36 |
+
]
|
| 37 |
+
|
| 38 |
+
@typedoc """
|
| 39 |
+
The main function executed while benchmarking.
|
| 40 |
+
|
| 41 |
+
No arguments if no inputs are used, one argument if inputs are used.
|
| 42 |
+
"""
|
| 43 |
+
@type benchmarking_function :: (-> any) | (any -> any)
|
| 44 |
+
|
| 45 |
+
@typedoc """
|
| 46 |
+
What shall be benchmarked, mostly a function but can contain options.
|
| 47 |
+
|
| 48 |
+
Options are there for hooks (`after_each`, `before_each` etc.)
|
| 49 |
+
"""
|
| 50 |
+
@type to_benchmark :: benchmarking_function() | {benchmarking_function(), keyword()}
|
| 51 |
+
|
| 52 |
+
@typedoc """
|
| 53 |
+
All the data collected for a scenario (combination of function and input)
|
| 54 |
+
|
| 55 |
+
Among all the data required to execute the scenario (function, input, all the hooks aka
|
| 56 |
+
after_*/before_*), data needed to display (name, job_name, input_name, tag) and of course
|
| 57 |
+
run_time_data and memory_data with all the samples and computed statistics.
|
| 58 |
+
"""
|
| 59 |
+
@type t :: %__MODULE__{
|
| 60 |
+
name: String.t(),
|
| 61 |
+
job_name: String.t(),
|
| 62 |
+
function: benchmarking_function,
|
| 63 |
+
input_name: String.t() | Benchee.Benchmark.no_input(),
|
| 64 |
+
input: any | nil | Benchee.Benchmark.no_input(),
|
| 65 |
+
run_time_data: CollectionData.t(),
|
| 66 |
+
memory_usage_data: CollectionData.t(),
|
| 67 |
+
reductions_data: CollectionData.t(),
|
| 68 |
+
before_each: Hooks.hook_function() | nil,
|
| 69 |
+
after_each: Hooks.hook_function() | nil,
|
| 70 |
+
before_scenario: Hooks.hook_function() | nil,
|
| 71 |
+
after_scenario: Hooks.hook_function() | nil,
|
| 72 |
+
tag: String.t() | nil
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
@doc """
|
| 76 |
+
Returns the correct name to display of the given scenario data.
|
| 77 |
+
|
| 78 |
+
In the normal case this is `job_name`, however when scenarios are loaded they
|
| 79 |
+
are tagged and these tags should be shown for disambiguation.
|
| 80 |
+
|
| 81 |
+
## Examples
|
| 82 |
+
|
| 83 |
+
iex> display_name(%Benchee.Scenario{job_name: "flat_map"})
|
| 84 |
+
"flat_map"
|
| 85 |
+
|
| 86 |
+
iex> display_name(%Benchee.Scenario{job_name: "flat_map", tag: "main"})
|
| 87 |
+
"flat_map (main)"
|
| 88 |
+
|
| 89 |
+
iex> display_name(%{job_name: "flat_map"})
|
| 90 |
+
"flat_map"
|
| 91 |
+
"""
|
| 92 |
+
@spec display_name(t) :: String.t()
|
| 93 |
+
def display_name(%{job_name: job_name, tag: nil}), do: job_name
|
| 94 |
+
def display_name(%{job_name: job_name, tag: tag}), do: "#{job_name} (#{tag})"
|
| 95 |
+
def display_name(%{job_name: job_name}), do: job_name
|
| 96 |
+
|
| 97 |
+
@doc """
|
| 98 |
+
Returns the different measurement types supported.
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
## Examples
|
| 102 |
+
|
| 103 |
+
iex> measurement_types()
|
| 104 |
+
[:run_time, :memory, :reductions]
|
| 105 |
+
"""
|
| 106 |
+
@spec measurement_types :: [:memory | :reductions | :run_time, ...]
|
| 107 |
+
def measurement_types, do: [:run_time, :memory, :reductions]
|
| 108 |
+
|
| 109 |
+
@doc """
|
| 110 |
+
Given the measurement type name given by `measurement_types/0`, get the associated data.
|
| 111 |
+
|
| 112 |
+
Raises if no correct measurement type was specified.
|
| 113 |
+
|
| 114 |
+
## Examples
|
| 115 |
+
|
| 116 |
+
iex> scenario = %Benchee.Scenario{
|
| 117 |
+
...> run_time_data: %Benchee.CollectionData{statistics: %Benchee.Statistics{sample_size: 1}}
|
| 118 |
+
...> }
|
| 119 |
+
...>
|
| 120 |
+
...> measurement_data(scenario, :run_time)
|
| 121 |
+
%Benchee.CollectionData{statistics: %Benchee.Statistics{sample_size: 1}}
|
| 122 |
+
|
| 123 |
+
iex> scenario = %Benchee.Scenario{
|
| 124 |
+
...> memory_usage_data: %Benchee.CollectionData{
|
| 125 |
+
...> statistics: %Benchee.Statistics{sample_size: 2}
|
| 126 |
+
...> }
|
| 127 |
+
...> }
|
| 128 |
+
...>
|
| 129 |
+
...> measurement_data(scenario, :memory)
|
| 130 |
+
%Benchee.CollectionData{statistics: %Benchee.Statistics{sample_size: 2}}
|
| 131 |
+
|
| 132 |
+
iex> scenario = %Benchee.Scenario{
|
| 133 |
+
...> reductions_data: %Benchee.CollectionData{
|
| 134 |
+
...> statistics: %Benchee.Statistics{sample_size: 3}
|
| 135 |
+
...> }
|
| 136 |
+
...> }
|
| 137 |
+
...>
|
| 138 |
+
...> measurement_data(scenario, :reductions)
|
| 139 |
+
%Benchee.CollectionData{statistics: %Benchee.Statistics{sample_size: 3}}
|
| 140 |
+
|
| 141 |
+
iex> measurement_data(%Benchee.Scenario{}, :memory)
|
| 142 |
+
%Benchee.CollectionData{}
|
| 143 |
+
"""
|
| 144 |
+
@spec measurement_data(t, :memory | :reductions | :run_time) :: CollectionData.t()
|
| 145 |
+
# Arguably this access is OO-ish/not great. However, with the incosistency we have in naming
|
| 146 |
+
# between the scenario struct fields and the measurement types this should be good/covnenient.
|
| 147 |
+
# Technically, we could/should move to a map structure from "measurement_type" to collection
|
| 148 |
+
# data but it's probably not worth breaking compatibility for right now.
|
| 149 |
+
def measurement_data(scenario, measurement_type)
|
| 150 |
+
def measurement_data(scenario, :run_time), do: scenario.run_time_data
|
| 151 |
+
def measurement_data(scenario, :memory), do: scenario.memory_usage_data
|
| 152 |
+
def measurement_data(scenario, :reductions), do: scenario.reductions_data
|
| 153 |
+
|
| 154 |
+
@doc """
|
| 155 |
+
Returns `true` if data of the provided type has been fully procsessed, `false` otherwise.
|
| 156 |
+
|
| 157 |
+
Current available types are `run_time` and `memory`. Reasons they might not have been processed
|
| 158 |
+
yet are:
|
| 159 |
+
* Suite wasn't configured to collect them at all
|
| 160 |
+
* `Benchee.statistics/1` hasn't been called yet so that data was collected but statistics
|
| 161 |
+
aren't present yet
|
| 162 |
+
|
| 163 |
+
## Examples
|
| 164 |
+
|
| 165 |
+
iex> scenario = %Benchee.Scenario{
|
| 166 |
+
...> run_time_data: %Benchee.CollectionData{
|
| 167 |
+
...> statistics: %Benchee.Statistics{sample_size: 100}
|
| 168 |
+
...> }
|
| 169 |
+
...> }
|
| 170 |
+
...>
|
| 171 |
+
...> data_processed?(scenario, :run_time)
|
| 172 |
+
true
|
| 173 |
+
|
| 174 |
+
iex> scenario = %Benchee.Scenario{
|
| 175 |
+
...> memory_usage_data: %Benchee.CollectionData{
|
| 176 |
+
...> statistics: %Benchee.Statistics{sample_size: 1}
|
| 177 |
+
...> }
|
| 178 |
+
...> }
|
| 179 |
+
...>
|
| 180 |
+
...> data_processed?(scenario, :memory)
|
| 181 |
+
true
|
| 182 |
+
|
| 183 |
+
iex> scenario = %Benchee.Scenario{
|
| 184 |
+
...> reductions_data: %Benchee.CollectionData{
|
| 185 |
+
...> statistics: %Benchee.Statistics{sample_size: 1}
|
| 186 |
+
...> }
|
| 187 |
+
...> }
|
| 188 |
+
...>
|
| 189 |
+
...> data_processed?(scenario, :reductions)
|
| 190 |
+
true
|
| 191 |
+
|
| 192 |
+
iex> scenario = %Benchee.Scenario{
|
| 193 |
+
...> memory_usage_data: %Benchee.CollectionData{
|
| 194 |
+
...> statistics: %Benchee.Statistics{sample_size: 0}
|
| 195 |
+
...> }
|
| 196 |
+
...> }
|
| 197 |
+
...>
|
| 198 |
+
...> data_processed?(scenario, :memory)
|
| 199 |
+
false
|
| 200 |
+
"""
|
| 201 |
+
@spec data_processed?(t, :run_time | :memory) :: boolean
|
| 202 |
+
def data_processed?(scenario, :run_time) do
|
| 203 |
+
scenario.run_time_data.statistics.sample_size > 0
|
| 204 |
+
end
|
| 205 |
+
|
| 206 |
+
def data_processed?(scenario, :memory) do
|
| 207 |
+
scenario.memory_usage_data.statistics.sample_size > 0
|
| 208 |
+
end
|
| 209 |
+
|
| 210 |
+
def data_processed?(scenario, :reductions) do
|
| 211 |
+
scenario.reductions_data.statistics.sample_size > 0
|
| 212 |
+
end
|
| 213 |
+
end
|
deps/benchee/lib/benchee/scenario_loader.ex
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
defmodule Benchee.ScenarioLoader do
|
| 2 |
+
@moduledoc """
|
| 3 |
+
Load scenarios that were saved using the saved option to be included.
|
| 4 |
+
|
| 5 |
+
Usually this is done right before the formatters run (that's when it happens
|
| 6 |
+
in `Benchee.run/2`) as all measurements and statistics should be there.
|
| 7 |
+
However, if you want to recompute statistics or others you can load them at
|
| 8 |
+
any time. Just be aware that if you load them before `Benchee.collect/1` then
|
| 9 |
+
they'll be rerun and measurements overridden.
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
alias Benchee.Suite
|
| 13 |
+
|
| 14 |
+
@doc """
|
| 15 |
+
Load the file(s) specified as `load_path` and add the scenarios to the list of the
|
| 16 |
+
current scenarios in the suite.
|
| 17 |
+
"""
|
| 18 |
+
def load(suite = %Suite{configuration: %{load: load_path}, scenarios: scenarios}) do
|
| 19 |
+
loaded = load_scenarios(load_path)
|
| 20 |
+
%Suite{suite | scenarios: scenarios ++ loaded}
|
| 21 |
+
end
|
| 22 |
+
|
| 23 |
+
defp load_scenarios(false), do: []
|
| 24 |
+
defp load_scenarios(path) when is_binary(path), do: load_scenarios([path])
|
| 25 |
+
|
| 26 |
+
defp load_scenarios(paths) do
|
| 27 |
+
Enum.flat_map(paths, fn path_or_glob ->
|
| 28 |
+
Enum.flat_map(Path.wildcard(path_or_glob), &load_scenario/1)
|
| 29 |
+
end)
|
| 30 |
+
end
|
| 31 |
+
|
| 32 |
+
defp load_scenario(path) do
|
| 33 |
+
loaded_suite =
|
| 34 |
+
path
|
| 35 |
+
|> File.read!()
|
| 36 |
+
|> :erlang.binary_to_term()
|
| 37 |
+
|
| 38 |
+
loaded_suite.scenarios
|
| 39 |
+
end
|
| 40 |
+
end
|
deps/benchee/lib/benchee/statistics.ex
ADDED
|
@@ -0,0 +1,314 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
defmodule Benchee.Statistics do
|
| 2 |
+
@moduledoc """
|
| 3 |
+
Statistics related functionality that is meant to take the raw benchmark data
|
| 4 |
+
and then compute statistics like the average and the standard deviation etc.
|
| 5 |
+
|
| 6 |
+
See `statistics/1` for a breakdown of the included statistics.
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
alias Benchee.{CollectionData, Conversion.Duration, Scenario, Suite}
|
| 10 |
+
alias Benchee.Output.ProgressPrinter
|
| 11 |
+
|
| 12 |
+
require Integer
|
| 13 |
+
|
| 14 |
+
defstruct [
|
| 15 |
+
:average,
|
| 16 |
+
:ips,
|
| 17 |
+
:std_dev,
|
| 18 |
+
:std_dev_ratio,
|
| 19 |
+
:std_dev_ips,
|
| 20 |
+
:median,
|
| 21 |
+
:percentiles,
|
| 22 |
+
:mode,
|
| 23 |
+
:minimum,
|
| 24 |
+
:maximum,
|
| 25 |
+
:relative_more,
|
| 26 |
+
:relative_less,
|
| 27 |
+
:absolute_difference,
|
| 28 |
+
:outliers,
|
| 29 |
+
:lower_outlier_bound,
|
| 30 |
+
:upper_outlier_bound,
|
| 31 |
+
sample_size: 0
|
| 32 |
+
]
|
| 33 |
+
|
| 34 |
+
@typedoc """
|
| 35 |
+
Careful with the mode, might be multiple values, one value or nothing.😱
|
| 36 |
+
"""
|
| 37 |
+
@type mode :: [number] | number | nil
|
| 38 |
+
|
| 39 |
+
@typedoc """
|
| 40 |
+
All the statistics `statistics/1` computes from the samples.
|
| 41 |
+
|
| 42 |
+
This used for run times, memory and reductions. Generally with these,
|
| 43 |
+
the lower the better (less run time, memory consumption or reductions).
|
| 44 |
+
|
| 45 |
+
These values mostly correspond to their cousins in `Statistex`.
|
| 46 |
+
|
| 47 |
+
Overview of all the statistics Benchee currently provides:
|
| 48 |
+
|
| 49 |
+
* average - average of all the samples (the lower the better)
|
| 50 |
+
* ips - iterations per second, how often can the given function be
|
| 51 |
+
executed within one second, used only for run times (the higher the better)
|
| 52 |
+
* std_dev - standard deviation, how much results vary among the samples
|
| 53 |
+
(the higher the more the results vary)
|
| 54 |
+
* std_dev_ratio - standard deviation expressed as how much it is relative to
|
| 55 |
+
the average
|
| 56 |
+
* std_dev_ips - the absolute standard deviation of iterations per second
|
| 57 |
+
* median - when all measured times are sorted, this is the middle
|
| 58 |
+
value (or average of the two middle values when the number of times is
|
| 59 |
+
even). More stable than the average and somewhat more likely to be a
|
| 60 |
+
typical value you see.
|
| 61 |
+
* percentiles - a map of percentile ranks. These are the values below
|
| 62 |
+
which x% of the samples lie. For example, 99% of samples are less than
|
| 63 |
+
is a value for which 99% of the run times are less than it.
|
| 64 |
+
* mode - the samples that occur the most. Often one value, but
|
| 65 |
+
can be multiple values if they occur the same amount of times. If no value
|
| 66 |
+
occurs at least twice, this value will be `nil`.
|
| 67 |
+
* minimum - the smallest sample measured for the scenario
|
| 68 |
+
* maximum - the biggest sample measured for the scenario
|
| 69 |
+
* relative_more - relative to the reference (usually the fastest scenario) how much more
|
| 70 |
+
was the average of this scenario. E.g. for reference at 100, this scenario 200 then it
|
| 71 |
+
is 2.0.
|
| 72 |
+
* relative_less - relative to the reference (usually the fastest scenario) how much less
|
| 73 |
+
was the average of this scenario. E.g. for reference at 100, this scenario 200 then it
|
| 74 |
+
is 0.5.
|
| 75 |
+
* absolute_difference - relative to the reference (usually the fastest scenario) what is
|
| 76 |
+
the difference of the averages of the scenarios. e.g. for reference at 100, this
|
| 77 |
+
scenario 200 then it is 100.
|
| 78 |
+
* sample_size - the number of measurements/samples taken into account for calculating statistics
|
| 79 |
+
* outliers - if outlier exclusion was enabled, may include any samples of outliers that were found, empty list otherwise
|
| 80 |
+
* lower_outlier_bound - value below which values are considered an outlier
|
| 81 |
+
* upper_outlier_bound - value above which values are considered an outlier
|
| 82 |
+
"""
|
| 83 |
+
@type t :: %__MODULE__{
|
| 84 |
+
average: float,
|
| 85 |
+
ips: float | nil,
|
| 86 |
+
std_dev: float,
|
| 87 |
+
std_dev_ratio: float,
|
| 88 |
+
std_dev_ips: float | nil,
|
| 89 |
+
median: number,
|
| 90 |
+
percentiles: %{number => float},
|
| 91 |
+
mode: mode,
|
| 92 |
+
minimum: number,
|
| 93 |
+
maximum: number,
|
| 94 |
+
relative_more: float | nil | :infinity,
|
| 95 |
+
relative_less: float | nil | :infinity,
|
| 96 |
+
absolute_difference: float | nil,
|
| 97 |
+
outliers: [number],
|
| 98 |
+
lower_outlier_bound: number,
|
| 99 |
+
upper_outlier_bound: number,
|
| 100 |
+
sample_size: integer
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
@typedoc """
|
| 104 |
+
The samples a `Benchee.Collect` collected to compute statistics from.
|
| 105 |
+
"""
|
| 106 |
+
@type samples :: [number]
|
| 107 |
+
|
| 108 |
+
@doc """
|
| 109 |
+
Takes a suite with scenarios and their data samples, adds the statistics to the
|
| 110 |
+
scenarios. For an overview of what the statistics mean see `t:t/0`.
|
| 111 |
+
|
| 112 |
+
Note that this will also sort the scenarios fastest to slowest to ensure a consistent order
|
| 113 |
+
of scenarios in all used formatters.
|
| 114 |
+
|
| 115 |
+
## Examples
|
| 116 |
+
|
| 117 |
+
iex> scenarios = [
|
| 118 |
+
...> %Benchee.Scenario{
|
| 119 |
+
...> job_name: "My Job",
|
| 120 |
+
...> run_time_data: %Benchee.CollectionData{
|
| 121 |
+
...> samples: [200, 400, 400, 400, 500, 500, 500, 700, 900]
|
| 122 |
+
...> },
|
| 123 |
+
...> memory_usage_data: %Benchee.CollectionData{
|
| 124 |
+
...> samples: [200, 400, 400, 400, 500, 500, 500, 700, 900]
|
| 125 |
+
...> },
|
| 126 |
+
...> input_name: "Input",
|
| 127 |
+
...> input: "Input"
|
| 128 |
+
...> }
|
| 129 |
+
...> ]
|
| 130 |
+
...>
|
| 131 |
+
...> suite = %Benchee.Suite{scenarios: scenarios}
|
| 132 |
+
...> statistics(suite, Benchee.Test.FakeProgressPrinter)
|
| 133 |
+
%Benchee.Suite{
|
| 134 |
+
scenarios: [
|
| 135 |
+
%Benchee.Scenario{
|
| 136 |
+
job_name: "My Job",
|
| 137 |
+
input_name: "Input",
|
| 138 |
+
input: "Input",
|
| 139 |
+
run_time_data: %Benchee.CollectionData{
|
| 140 |
+
samples: [200, 400, 400, 400, 500, 500, 500, 700, 900],
|
| 141 |
+
statistics: %Benchee.Statistics{
|
| 142 |
+
average: 500.0,
|
| 143 |
+
ips: 2000_000.0,
|
| 144 |
+
std_dev: 200.0,
|
| 145 |
+
std_dev_ratio: 0.4,
|
| 146 |
+
std_dev_ips: 800_000.0,
|
| 147 |
+
median: 500.0,
|
| 148 |
+
percentiles: %{25 => 400.0, 50 => 500.0, 75 => 600.0, 99 => 900.0},
|
| 149 |
+
mode: [500, 400],
|
| 150 |
+
minimum: 200,
|
| 151 |
+
maximum: 900,
|
| 152 |
+
sample_size: 9,
|
| 153 |
+
outliers: [],
|
| 154 |
+
lower_outlier_bound: 100.0,
|
| 155 |
+
upper_outlier_bound: 900.0
|
| 156 |
+
}
|
| 157 |
+
},
|
| 158 |
+
memory_usage_data: %Benchee.CollectionData{
|
| 159 |
+
samples: [200, 400, 400, 400, 500, 500, 500, 700, 900],
|
| 160 |
+
statistics: %Benchee.Statistics{
|
| 161 |
+
average: 500.0,
|
| 162 |
+
ips: nil,
|
| 163 |
+
std_dev: 200.0,
|
| 164 |
+
std_dev_ratio: 0.4,
|
| 165 |
+
std_dev_ips: nil,
|
| 166 |
+
median: 500.0,
|
| 167 |
+
percentiles: %{25 => 400.0, 50 => 500.0, 75 => 600.0, 99 => 900.0},
|
| 168 |
+
mode: [500, 400],
|
| 169 |
+
minimum: 200,
|
| 170 |
+
maximum: 900,
|
| 171 |
+
sample_size: 9,
|
| 172 |
+
outliers: [],
|
| 173 |
+
lower_outlier_bound: 100.0,
|
| 174 |
+
upper_outlier_bound: 900.0
|
| 175 |
+
}
|
| 176 |
+
}
|
| 177 |
+
}
|
| 178 |
+
],
|
| 179 |
+
system: nil
|
| 180 |
+
}
|
| 181 |
+
|
| 182 |
+
"""
|
| 183 |
+
@spec statistics(Suite.t()) :: Suite.t()
|
| 184 |
+
def statistics(suite, printer \\ ProgressPrinter) do
|
| 185 |
+
printer.calculating_statistics(suite.configuration)
|
| 186 |
+
|
| 187 |
+
percentiles = suite.configuration.percentiles
|
| 188 |
+
exclude_outliers? = suite.configuration.exclude_outliers
|
| 189 |
+
|
| 190 |
+
update_in(suite.scenarios, fn scenarios ->
|
| 191 |
+
scenario_statistics =
|
| 192 |
+
compute_statistics_in_parallel(scenarios, percentiles, exclude_outliers?)
|
| 193 |
+
|
| 194 |
+
update_scenarios_with_statistics(scenarios, scenario_statistics)
|
| 195 |
+
end)
|
| 196 |
+
end
|
| 197 |
+
|
| 198 |
+
defp compute_statistics_in_parallel(scenarios, percentiles, exclude_outliers?) do
|
| 199 |
+
scenarios
|
| 200 |
+
|> Enum.map(fn scenario ->
|
| 201 |
+
# we filter down the data here to avoid sending the input and benchmarking function to
|
| 202 |
+
# the other processes
|
| 203 |
+
# we send over all of the collection data as in the future (tm) we might want to already
|
| 204 |
+
# provide the sample size, which this gives us a way to do that and not touch this code
|
| 205 |
+
# again
|
| 206 |
+
{scenario.run_time_data, scenario.memory_usage_data, scenario.reductions_data}
|
| 207 |
+
end)
|
| 208 |
+
# async_stream as we might run a ton of scenarios depending on the benchmark
|
| 209 |
+
|> Task.async_stream(
|
| 210 |
+
fn scenario_collection_data ->
|
| 211 |
+
calculate_scenario_statistics(scenario_collection_data, percentiles, exclude_outliers?)
|
| 212 |
+
end,
|
| 213 |
+
timeout: :infinity,
|
| 214 |
+
ordered: true
|
| 215 |
+
)
|
| 216 |
+
|> Enum.map(fn {:ok, stats} -> stats end)
|
| 217 |
+
end
|
| 218 |
+
|
| 219 |
+
defp update_scenarios_with_statistics(scenarios, scenario_statistics) do
|
| 220 |
+
# we can zip them as they retained order
|
| 221 |
+
scenarios
|
| 222 |
+
|> Enum.zip(scenario_statistics)
|
| 223 |
+
|> Enum.map(fn {%Scenario{
|
| 224 |
+
run_time_data: %CollectionData{},
|
| 225 |
+
memory_usage_data: %CollectionData{},
|
| 226 |
+
reductions_data: %CollectionData{}
|
| 227 |
+
} = scenario, {run_time_stats, memory_stats, reductions_stats}} ->
|
| 228 |
+
%Scenario{
|
| 229 |
+
scenario
|
| 230 |
+
| run_time_data: %CollectionData{
|
| 231 |
+
scenario.run_time_data
|
| 232 |
+
| statistics: run_time_stats
|
| 233 |
+
},
|
| 234 |
+
memory_usage_data: %CollectionData{
|
| 235 |
+
scenario.memory_usage_data
|
| 236 |
+
| statistics: memory_stats
|
| 237 |
+
},
|
| 238 |
+
reductions_data: %CollectionData{
|
| 239 |
+
scenario.reductions_data
|
| 240 |
+
| statistics: reductions_stats
|
| 241 |
+
}
|
| 242 |
+
}
|
| 243 |
+
end)
|
| 244 |
+
end
|
| 245 |
+
|
| 246 |
+
defp calculate_scenario_statistics(
|
| 247 |
+
{run_time_data, memory_data, reductions_data},
|
| 248 |
+
percentiles,
|
| 249 |
+
exclude_outliers?
|
| 250 |
+
) do
|
| 251 |
+
run_time_stats =
|
| 252 |
+
run_time_data.samples
|
| 253 |
+
|> calculate_statistics(percentiles, exclude_outliers?)
|
| 254 |
+
|> add_ips
|
| 255 |
+
|
| 256 |
+
memory_stats = calculate_statistics(memory_data.samples, percentiles, exclude_outliers?)
|
| 257 |
+
|
| 258 |
+
reductions_stats =
|
| 259 |
+
calculate_statistics(reductions_data.samples, percentiles, exclude_outliers?)
|
| 260 |
+
|
| 261 |
+
{run_time_stats, memory_stats, reductions_stats}
|
| 262 |
+
end
|
| 263 |
+
|
| 264 |
+
defp calculate_statistics([], _, _) do
|
| 265 |
+
%__MODULE__{
|
| 266 |
+
sample_size: 0
|
| 267 |
+
}
|
| 268 |
+
end
|
| 269 |
+
|
| 270 |
+
defp calculate_statistics(samples, percentiles, exclude_outliers?) do
|
| 271 |
+
samples
|
| 272 |
+
|> Statistex.statistics(percentiles: percentiles, exclude_outliers: exclude_outliers?)
|
| 273 |
+
|> convert_from_statistex
|
| 274 |
+
end
|
| 275 |
+
|
| 276 |
+
# It might seem silly to maintain and map statistex to our own struct,
|
| 277 |
+
# but this gives benchee more control and makes it safer to upgrade and change.
|
| 278 |
+
# Also, we don't expose changes in statistex versions automatically to plugins.
|
| 279 |
+
#
|
| 280 |
+
# As an example right now it's being discussed in statistex to add an `m2` statistic that holds
|
| 281 |
+
# no value for benchee (as it's ony used to calculate variance).
|
| 282 |
+
#
|
| 283 |
+
# We also manually add `ips` related stats (see `add_ips/1`) so differences are sufficient.
|
| 284 |
+
defp convert_from_statistex(statistex_statistics) do
|
| 285 |
+
%__MODULE__{
|
| 286 |
+
average: statistex_statistics.average,
|
| 287 |
+
std_dev: statistex_statistics.standard_deviation,
|
| 288 |
+
std_dev_ratio: statistex_statistics.standard_deviation_ratio,
|
| 289 |
+
median: statistex_statistics.median,
|
| 290 |
+
percentiles: statistex_statistics.percentiles,
|
| 291 |
+
mode: statistex_statistics.mode,
|
| 292 |
+
minimum: statistex_statistics.minimum,
|
| 293 |
+
maximum: statistex_statistics.maximum,
|
| 294 |
+
sample_size: statistex_statistics.sample_size,
|
| 295 |
+
outliers: statistex_statistics.outliers,
|
| 296 |
+
lower_outlier_bound: statistex_statistics.lower_outlier_bound,
|
| 297 |
+
upper_outlier_bound: statistex_statistics.upper_outlier_bound
|
| 298 |
+
}
|
| 299 |
+
end
|
| 300 |
+
|
| 301 |
+
defp add_ips(statistics = %__MODULE__{sample_size: 0}), do: statistics
|
| 302 |
+
defp add_ips(statistics = %__MODULE__{average: +0.0}), do: statistics
|
| 303 |
+
|
| 304 |
+
defp add_ips(statistics = %__MODULE__{}) do
|
| 305 |
+
ips = Duration.convert_value({1, :second}, :nanosecond) / statistics.average
|
| 306 |
+
standard_dev_ips = ips * statistics.std_dev_ratio
|
| 307 |
+
|
| 308 |
+
%__MODULE__{
|
| 309 |
+
statistics
|
| 310 |
+
| ips: ips,
|
| 311 |
+
std_dev_ips: standard_dev_ips
|
| 312 |
+
}
|
| 313 |
+
end
|
| 314 |
+
end
|
deps/benchee/lib/benchee/suite.ex
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
defmodule Benchee.Suite do
|
| 2 |
+
@moduledoc """
|
| 3 |
+
Main Benchee data structure that aggregates the results from every step.
|
| 4 |
+
|
| 5 |
+
Different layers of the benchmarking rely on different data being present
|
| 6 |
+
here. For instance for `Benchee.Statistics.statistics/1` to work the
|
| 7 |
+
`run_time_data` key of each scenario needs to be filled with the samples
|
| 8 |
+
collected by `Benchee.Benchmark.collect/1`.
|
| 9 |
+
|
| 10 |
+
Formatters can then use the data to display all of the results and the
|
| 11 |
+
configuration.
|
| 12 |
+
"""
|
| 13 |
+
defstruct [
|
| 14 |
+
:system,
|
| 15 |
+
configuration: %Benchee.Configuration{},
|
| 16 |
+
scenarios: []
|
| 17 |
+
]
|
| 18 |
+
|
| 19 |
+
@typedoc """
|
| 20 |
+
Valid key for either input or benchmarking job names.
|
| 21 |
+
"""
|
| 22 |
+
@type key :: String.t() | atom
|
| 23 |
+
|
| 24 |
+
@typedoc """
|
| 25 |
+
The main suite consisting of the configuration data, information about the system and most
|
| 26 |
+
importantly a list of `t:Benchee.Scenario.t/0`.
|
| 27 |
+
"""
|
| 28 |
+
@type t :: %__MODULE__{
|
| 29 |
+
configuration: Benchee.Configuration.t() | nil,
|
| 30 |
+
system: Benchee.System.t() | nil,
|
| 31 |
+
scenarios: [] | [Benchee.Scenario.t()]
|
| 32 |
+
}
|
| 33 |
+
end
|
| 34 |
+
|
| 35 |
+
defimpl DeepMerge.Resolver, for: Benchee.Suite do
|
| 36 |
+
def resolve(original, override = %Benchee.Suite{}, resolver) do
|
| 37 |
+
cleaned_override =
|
| 38 |
+
override
|
| 39 |
+
|> Map.from_struct()
|
| 40 |
+
|> Enum.reject(fn {_key, value} -> is_nil(value) end)
|
| 41 |
+
|> Map.new()
|
| 42 |
+
|
| 43 |
+
Map.merge(original, cleaned_override, resolver)
|
| 44 |
+
end
|
| 45 |
+
|
| 46 |
+
def resolve(original, override, resolver) when is_map(override) do
|
| 47 |
+
Map.merge(original, override, resolver)
|
| 48 |
+
end
|
| 49 |
+
end
|
| 50 |
+
|
| 51 |
+
if Code.ensure_loaded?(Table.Reader) do
|
| 52 |
+
defimpl Table.Reader, for: Benchee.Suite do
|
| 53 |
+
alias Benchee.Benchmark
|
| 54 |
+
alias Benchee.CollectionData
|
| 55 |
+
alias Benchee.Scenario
|
| 56 |
+
|
| 57 |
+
def init(suite) do
|
| 58 |
+
measurements_processed = map_measurements_processed(suite)
|
| 59 |
+
columns = get_columns_from_suite(suite, measurements_processed)
|
| 60 |
+
{rows, count} = extract_rows_from_suite(suite, measurements_processed)
|
| 61 |
+
|
| 62 |
+
{:rows, %{columns: columns, count: count}, rows}
|
| 63 |
+
end
|
| 64 |
+
|
| 65 |
+
defp map_measurements_processed(suite) do
|
| 66 |
+
Enum.filter(Scenario.measurement_types(), fn type ->
|
| 67 |
+
Enum.any?(suite.scenarios, fn scenario -> Scenario.data_processed?(scenario, type) end)
|
| 68 |
+
end)
|
| 69 |
+
end
|
| 70 |
+
|
| 71 |
+
@run_time_fields [
|
| 72 |
+
"samples",
|
| 73 |
+
"ips",
|
| 74 |
+
"average",
|
| 75 |
+
"std_dev",
|
| 76 |
+
"median",
|
| 77 |
+
"minimum",
|
| 78 |
+
"maximum",
|
| 79 |
+
"mode",
|
| 80 |
+
"sample_size"
|
| 81 |
+
]
|
| 82 |
+
|
| 83 |
+
@non_run_time_fields List.delete(@run_time_fields, "ips")
|
| 84 |
+
|
| 85 |
+
defp get_columns_from_suite(suite, measurements_processed) do
|
| 86 |
+
config_percentiles = suite.configuration.percentiles
|
| 87 |
+
|
| 88 |
+
percentile_labels =
|
| 89 |
+
Enum.map(config_percentiles, fn percentile ->
|
| 90 |
+
"p_#{percentile}"
|
| 91 |
+
end)
|
| 92 |
+
|
| 93 |
+
measurement_headers =
|
| 94 |
+
Enum.flat_map(measurements_processed, fn measurement_type ->
|
| 95 |
+
fields = fields_for(measurement_type) ++ percentile_labels
|
| 96 |
+
|
| 97 |
+
Enum.map(fields, fn field -> "#{measurement_type}_#{field}" end)
|
| 98 |
+
end)
|
| 99 |
+
|
| 100 |
+
["job_name", "input_name"] ++ measurement_headers
|
| 101 |
+
end
|
| 102 |
+
|
| 103 |
+
defp fields_for(:run_time), do: @run_time_fields
|
| 104 |
+
defp fields_for(_), do: @non_run_time_fields
|
| 105 |
+
|
| 106 |
+
defp extract_rows_from_suite(suite, measurements_processed) do
|
| 107 |
+
config_percentiles = suite.configuration.percentiles
|
| 108 |
+
|
| 109 |
+
Enum.map_reduce(suite.scenarios, 0, fn %Scenario{} = scenario, count ->
|
| 110 |
+
scenario_data =
|
| 111 |
+
Enum.flat_map(measurements_processed, fn measurement_type ->
|
| 112 |
+
scenario
|
| 113 |
+
|> Scenario.measurement_data(measurement_type)
|
| 114 |
+
|> get_stats_from_collection_data(measurement_type, config_percentiles)
|
| 115 |
+
end)
|
| 116 |
+
|
| 117 |
+
no_input = Benchmark.no_input()
|
| 118 |
+
|
| 119 |
+
input_name =
|
| 120 |
+
case scenario.input_name do
|
| 121 |
+
^no_input -> ""
|
| 122 |
+
name -> name
|
| 123 |
+
end
|
| 124 |
+
|
| 125 |
+
row = [scenario.name, input_name] ++ scenario_data
|
| 126 |
+
|
| 127 |
+
{row, count + 1}
|
| 128 |
+
end)
|
| 129 |
+
end
|
| 130 |
+
|
| 131 |
+
defp get_stats_from_collection_data(
|
| 132 |
+
%CollectionData{statistics: statistics, samples: samples},
|
| 133 |
+
measurement_type,
|
| 134 |
+
percentiles
|
| 135 |
+
) do
|
| 136 |
+
percentile_data =
|
| 137 |
+
Enum.map(percentiles, fn percentile -> statistics.percentiles[percentile] end)
|
| 138 |
+
|
| 139 |
+
Enum.concat([
|
| 140 |
+
[samples],
|
| 141 |
+
maybe_ips(statistics, measurement_type),
|
| 142 |
+
[
|
| 143 |
+
statistics.average,
|
| 144 |
+
statistics.std_dev,
|
| 145 |
+
statistics.median,
|
| 146 |
+
statistics.minimum,
|
| 147 |
+
statistics.maximum,
|
| 148 |
+
statistics.mode,
|
| 149 |
+
statistics.sample_size
|
| 150 |
+
],
|
| 151 |
+
percentile_data
|
| 152 |
+
])
|
| 153 |
+
end
|
| 154 |
+
|
| 155 |
+
defp maybe_ips(statistics, :run_time), do: [statistics.ips]
|
| 156 |
+
defp maybe_ips(_, _not_run_time), do: []
|
| 157 |
+
end
|
| 158 |
+
end
|
deps/benchee/lib/benchee/system.ex
ADDED
|
@@ -0,0 +1,295 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
defmodule Benchee.System do
|
| 2 |
+
@moduledoc """
|
| 3 |
+
Provides information about the system the benchmarks are run on.
|
| 4 |
+
|
| 5 |
+
Includes information such as elixir/erlang version, OS, CPU and memory.
|
| 6 |
+
|
| 7 |
+
So far supports/should work for Linux, MacOS, FreeBSD, Solaris and Windows.
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
alias Benchee.Conversion.Memory
|
| 11 |
+
alias Benchee.Suite
|
| 12 |
+
alias Benchee.Utility.ErlangVersion
|
| 13 |
+
|
| 14 |
+
@enforce_keys [
|
| 15 |
+
:elixir,
|
| 16 |
+
:erlang,
|
| 17 |
+
:jit_enabled?,
|
| 18 |
+
:num_cores,
|
| 19 |
+
:os,
|
| 20 |
+
:available_memory,
|
| 21 |
+
:cpu_speed
|
| 22 |
+
]
|
| 23 |
+
|
| 24 |
+
defstruct [
|
| 25 |
+
:elixir,
|
| 26 |
+
:erlang,
|
| 27 |
+
:jit_enabled?,
|
| 28 |
+
:num_cores,
|
| 29 |
+
:os,
|
| 30 |
+
:available_memory,
|
| 31 |
+
:cpu_speed
|
| 32 |
+
]
|
| 33 |
+
|
| 34 |
+
@type t :: %__MODULE__{
|
| 35 |
+
elixir: String.t(),
|
| 36 |
+
erlang: String.t(),
|
| 37 |
+
jit_enabled?: boolean(),
|
| 38 |
+
num_cores: pos_integer(),
|
| 39 |
+
os: :macOS | :Windows | :FreeBSD | :Solaris | :Linux,
|
| 40 |
+
cpu_speed: String.t(),
|
| 41 |
+
available_memory: String.t()
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
@doc """
|
| 45 |
+
Adds system information to the suite (currently elixir and erlang versions).
|
| 46 |
+
"""
|
| 47 |
+
@spec system(Suite.t()) :: Suite.t()
|
| 48 |
+
def system(suite = %Suite{}) do
|
| 49 |
+
erlang_version = erlang()
|
| 50 |
+
|
| 51 |
+
system_info = %__MODULE__{
|
| 52 |
+
elixir: elixir(),
|
| 53 |
+
erlang: erlang_version,
|
| 54 |
+
jit_enabled?: jit_enabled?(erlang_version),
|
| 55 |
+
num_cores: num_cores(),
|
| 56 |
+
os: os(),
|
| 57 |
+
available_memory: available_memory(),
|
| 58 |
+
cpu_speed: cpu_speed()
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
warn_about_performance_degrading_settings()
|
| 62 |
+
|
| 63 |
+
%Suite{suite | system: system_info}
|
| 64 |
+
end
|
| 65 |
+
|
| 66 |
+
defp erlang do
|
| 67 |
+
otp_release = :erlang.system_info(:otp_release)
|
| 68 |
+
file = Path.join([:code.root_dir(), "releases", otp_release, "OTP_VERSION"])
|
| 69 |
+
|
| 70 |
+
case File.read(file) do
|
| 71 |
+
{:ok, version} ->
|
| 72 |
+
String.trim(version)
|
| 73 |
+
|
| 74 |
+
# Livebook seemingly doesn't have the file where we expect it to be:
|
| 75 |
+
# https://github.com/bencheeorg/benchee/issues/367
|
| 76 |
+
{:error, reason} ->
|
| 77 |
+
IO.puts(
|
| 78 |
+
"Error trying to determine erlang version #{reason}, falling back to overall OTP version"
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
to_string(otp_release)
|
| 82 |
+
end
|
| 83 |
+
end
|
| 84 |
+
|
| 85 |
+
defp elixir, do: System.version()
|
| 86 |
+
|
| 87 |
+
@first_jit_version "24.0.0"
|
| 88 |
+
defp jit_enabled?(erlang_version) do
|
| 89 |
+
if ErlangVersion.includes_fixes_from?(erlang_version, @first_jit_version) do
|
| 90 |
+
:erlang.system_info(:emu_flavor) == :jit
|
| 91 |
+
else
|
| 92 |
+
false
|
| 93 |
+
end
|
| 94 |
+
end
|
| 95 |
+
|
| 96 |
+
defp num_cores do
|
| 97 |
+
System.schedulers_online()
|
| 98 |
+
end
|
| 99 |
+
|
| 100 |
+
defp os do
|
| 101 |
+
{_, name} = :os.type()
|
| 102 |
+
os(name)
|
| 103 |
+
end
|
| 104 |
+
|
| 105 |
+
defp os(:darwin), do: :macOS
|
| 106 |
+
defp os(:nt), do: :Windows
|
| 107 |
+
defp os(:freebsd), do: :FreeBSD
|
| 108 |
+
defp os(:sunos), do: :Solaris
|
| 109 |
+
defp os(_), do: :Linux
|
| 110 |
+
|
| 111 |
+
defp cpu_speed, do: cpu_speed(os())
|
| 112 |
+
|
| 113 |
+
defp cpu_speed(:Windows) do
|
| 114 |
+
parse_cpu_for(:Windows, system_cmd("WMIC", ["CPU", "GET", "NAME"]))
|
| 115 |
+
end
|
| 116 |
+
|
| 117 |
+
defp cpu_speed(:macOS) do
|
| 118 |
+
parse_cpu_for(:macOS, system_cmd("sysctl", ["-n", "machdep.cpu.brand_string"]))
|
| 119 |
+
end
|
| 120 |
+
|
| 121 |
+
defp cpu_speed(:FreeBSD) do
|
| 122 |
+
parse_cpu_for(:FreeBSD, system_cmd("sysctl", ["-n", "hw.model"]))
|
| 123 |
+
end
|
| 124 |
+
|
| 125 |
+
defp cpu_speed(:Solaris) do
|
| 126 |
+
parse_cpu_for(:Solaris, system_cmd("kstat", ["-p", "cpu_info:0::brand"]))
|
| 127 |
+
end
|
| 128 |
+
|
| 129 |
+
defp cpu_speed(:Linux) do
|
| 130 |
+
parse_cpu_for(:Linux, system_cmd("cat", ["/proc/cpuinfo"]))
|
| 131 |
+
end
|
| 132 |
+
|
| 133 |
+
@doc false
|
| 134 |
+
def parse_cpu_for(_, "N/A"), do: "N/A"
|
| 135 |
+
|
| 136 |
+
def parse_cpu_for(:Windows, raw_output) do
|
| 137 |
+
"Name" <> cpu_info = raw_output
|
| 138 |
+
String.trim(cpu_info)
|
| 139 |
+
end
|
| 140 |
+
|
| 141 |
+
def parse_cpu_for(:macOS, raw_output), do: String.trim(raw_output)
|
| 142 |
+
|
| 143 |
+
def parse_cpu_for(:FreeBSD, raw_output), do: String.trim(raw_output)
|
| 144 |
+
|
| 145 |
+
def parse_cpu_for(:Solaris, raw_output) do
|
| 146 |
+
solaris_cpubrand_regex = ~r/^cpu_info:0:cpu_info0:brand\s+(.*)\s*$/i
|
| 147 |
+
match_info = Regex.run(solaris_cpubrand_regex, raw_output, capture: :all_but_first)
|
| 148 |
+
|
| 149 |
+
case match_info do
|
| 150 |
+
[cpu_info] -> cpu_info
|
| 151 |
+
_ -> "Unrecognized processor"
|
| 152 |
+
end
|
| 153 |
+
end
|
| 154 |
+
|
| 155 |
+
def parse_cpu_for(:Linux, raw_output) do
|
| 156 |
+
linux_cpuinfo_regex = ~r/model name.*:([\w \(\)\-\@\.]*)/i
|
| 157 |
+
match_info = Regex.run(linux_cpuinfo_regex, raw_output, capture: :all_but_first)
|
| 158 |
+
|
| 159 |
+
case match_info do
|
| 160 |
+
[cpu_info] -> String.trim(cpu_info)
|
| 161 |
+
_ -> "Unrecognized processor"
|
| 162 |
+
end
|
| 163 |
+
end
|
| 164 |
+
|
| 165 |
+
defp available_memory, do: available_memory(os())
|
| 166 |
+
|
| 167 |
+
defp available_memory(:Windows) do
|
| 168 |
+
parse_memory_for(
|
| 169 |
+
:Windows,
|
| 170 |
+
system_cmd("WMIC", ["COMPUTERSYSTEM", "GET", "TOTALPHYSICALMEMORY"])
|
| 171 |
+
)
|
| 172 |
+
end
|
| 173 |
+
|
| 174 |
+
defp available_memory(:macOS) do
|
| 175 |
+
parse_memory_for(:macOS, system_cmd("sysctl", ["-n", "hw.memsize"]))
|
| 176 |
+
end
|
| 177 |
+
|
| 178 |
+
defp available_memory(:FreeBSD) do
|
| 179 |
+
parse_memory_for(:FreeBSD, system_cmd("sysctl", ["-n", "hw.physmem"]))
|
| 180 |
+
end
|
| 181 |
+
|
| 182 |
+
defp available_memory(:Solaris) do
|
| 183 |
+
parse_memory_for(:Solaris, system_cmd("prtconf", ["-m"]))
|
| 184 |
+
end
|
| 185 |
+
|
| 186 |
+
defp available_memory(:Linux) do
|
| 187 |
+
parse_memory_for(:Linux, system_cmd("cat", ["/proc/meminfo"]))
|
| 188 |
+
end
|
| 189 |
+
|
| 190 |
+
defp parse_memory_for(_, "N/A"), do: "N/A"
|
| 191 |
+
|
| 192 |
+
defp parse_memory_for(:Windows, raw_output) do
|
| 193 |
+
[memory] = Regex.run(~r/\d+/, raw_output)
|
| 194 |
+
{memory, _} = Integer.parse(memory)
|
| 195 |
+
Memory.format(memory)
|
| 196 |
+
end
|
| 197 |
+
|
| 198 |
+
defp parse_memory_for(:macOS, raw_output) do
|
| 199 |
+
{memory, _} = Integer.parse(raw_output)
|
| 200 |
+
Memory.format(memory)
|
| 201 |
+
end
|
| 202 |
+
|
| 203 |
+
defp parse_memory_for(:FreeBSD, raw_output) do
|
| 204 |
+
{memory, _} = Integer.parse(raw_output)
|
| 205 |
+
Memory.format(memory)
|
| 206 |
+
end
|
| 207 |
+
|
| 208 |
+
defp parse_memory_for(:Solaris, raw_output) do
|
| 209 |
+
{memory_in_megabytes, _} = Integer.parse(raw_output)
|
| 210 |
+
{memory_in_bytes, _} = Memory.convert({memory_in_megabytes, :megabyte}, :byte)
|
| 211 |
+
Memory.format(memory_in_bytes)
|
| 212 |
+
end
|
| 213 |
+
|
| 214 |
+
defp parse_memory_for(:Linux, raw_output) do
|
| 215 |
+
["MemTotal:" <> memory_info] = Regex.run(~r/MemTotal.*kB/, raw_output)
|
| 216 |
+
|
| 217 |
+
{memory_in_kilobytes, _} =
|
| 218 |
+
memory_info
|
| 219 |
+
|> String.trim()
|
| 220 |
+
|> String.trim_trailing(" kB")
|
| 221 |
+
|> Integer.parse()
|
| 222 |
+
|
| 223 |
+
{memory_in_bytes, _} =
|
| 224 |
+
Memory.convert(
|
| 225 |
+
{memory_in_kilobytes, :kilobyte},
|
| 226 |
+
:byte
|
| 227 |
+
)
|
| 228 |
+
|
| 229 |
+
Memory.format(memory_in_bytes)
|
| 230 |
+
end
|
| 231 |
+
|
| 232 |
+
@doc false
|
| 233 |
+
def system_cmd(cmd, args, system_func \\ &System.cmd/2) do
|
| 234 |
+
{output, exit_code} =
|
| 235 |
+
try do
|
| 236 |
+
system_func.(cmd, args)
|
| 237 |
+
rescue
|
| 238 |
+
e ->
|
| 239 |
+
message = Exception.format(:error, e, __STACKTRACE__)
|
| 240 |
+
|
| 241 |
+
# this mimics an error return code from the command,
|
| 242 |
+
# which is the same behavior we want to have
|
| 243 |
+
{message, 1}
|
| 244 |
+
end
|
| 245 |
+
|
| 246 |
+
if exit_code > 0 do
|
| 247 |
+
IO.puts("Something went wrong trying to get system information:")
|
| 248 |
+
IO.puts(output)
|
| 249 |
+
"N/A"
|
| 250 |
+
else
|
| 251 |
+
output
|
| 252 |
+
end
|
| 253 |
+
end
|
| 254 |
+
|
| 255 |
+
defp warn_about_performance_degrading_settings do
|
| 256 |
+
if !all_protocols_consolidated?() do
|
| 257 |
+
IO.puts("""
|
| 258 |
+
Not all of your protocols have been consolidated. In order to achieve the
|
| 259 |
+
best possible accuracy for benchmarks, please ensure protocol
|
| 260 |
+
consolidation is enabled in your benchmarking environment.
|
| 261 |
+
""")
|
| 262 |
+
end
|
| 263 |
+
end
|
| 264 |
+
|
| 265 |
+
# just made public for easy testing purposes
|
| 266 |
+
@doc false
|
| 267 |
+
def all_protocols_consolidated?(lib_dir_fun \\ &:code.lib_dir/1) do
|
| 268 |
+
case lib_dir_fun.(:elixir) do
|
| 269 |
+
# do we get a good old erlang charlist?
|
| 270 |
+
path when is_list(path) ->
|
| 271 |
+
ebin_path = Path.join(path, "ebin")
|
| 272 |
+
|
| 273 |
+
[ebin_path]
|
| 274 |
+
|> Protocol.extract_protocols()
|
| 275 |
+
|> Enum.all?(&Protocol.consolidated?/1)
|
| 276 |
+
|
| 277 |
+
_error ->
|
| 278 |
+
IO.puts(
|
| 279 |
+
"Could not check if protocols are consolidated. Running as escript? Defaulting to they are consolidated."
|
| 280 |
+
)
|
| 281 |
+
|
| 282 |
+
true
|
| 283 |
+
end
|
| 284 |
+
end
|
| 285 |
+
end
|
| 286 |
+
|
| 287 |
+
defimpl DeepMerge.Resolver, for: Benchee.System do
|
| 288 |
+
def resolve(original, override = %Benchee.System{}, resolver) do
|
| 289 |
+
Map.merge(original, override, resolver)
|
| 290 |
+
end
|
| 291 |
+
|
| 292 |
+
def resolve(original, override, resolver) when is_map(override) do
|
| 293 |
+
Map.merge(original, override, resolver)
|
| 294 |
+
end
|
| 295 |
+
end
|
deps/benchee/lib/benchee/utility/deep_convert.ex
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
defmodule Benchee.Utility.DeepConvert do
|
| 2 |
+
@moduledoc false
|
| 3 |
+
|
| 4 |
+
@doc """
|
| 5 |
+
Converts a deep keyword list to the corresponding deep map.
|
| 6 |
+
|
| 7 |
+
Exclusions can be provided for key names whose value should not be converted.
|
| 8 |
+
|
| 9 |
+
## Examples
|
| 10 |
+
|
| 11 |
+
iex> to_map(a: 1, b: 2)
|
| 12 |
+
%{a: 1, b: 2}
|
| 13 |
+
|
| 14 |
+
iex> to_map(a: [b: 2], c: [d: 3, e: 4, e: 5])
|
| 15 |
+
%{a: %{b: 2}, c: %{d: 3, e: 5}}
|
| 16 |
+
|
| 17 |
+
iex> to_map(a: [b: 2], c: [1, 2, 3], d: [])
|
| 18 |
+
%{a: %{b: 2}, c: [1, 2, 3], d: []}
|
| 19 |
+
|
| 20 |
+
iex> to_map(%{a: %{b: 2}, c: %{d: 3, e: 5}})
|
| 21 |
+
%{a: %{b: 2}, c: %{d: 3, e: 5}}
|
| 22 |
+
|
| 23 |
+
iex> to_map([])
|
| 24 |
+
%{}
|
| 25 |
+
|
| 26 |
+
iex> to_map([a: [b: [f: 5]]], [:a])
|
| 27 |
+
%{a: [b: [f: 5]]}
|
| 28 |
+
|
| 29 |
+
iex> to_map([a: [b: [f: 5]], c: [d: 3]], [:b])
|
| 30 |
+
%{a: %{b: [f: 5]}, c: %{d: 3}}
|
| 31 |
+
"""
|
| 32 |
+
def to_map(structure, exclusions \\ [])
|
| 33 |
+
def to_map([], _exclusions), do: %{}
|
| 34 |
+
def to_map(structure, exclusions), do: do_to_map(structure, exclusions)
|
| 35 |
+
|
| 36 |
+
defp do_to_map(kwlist = [{_key, _value} | _tail], exclusions) do
|
| 37 |
+
kwlist
|
| 38 |
+
|> Enum.map(fn tuple -> to_map_element(tuple, exclusions) end)
|
| 39 |
+
|> Map.new()
|
| 40 |
+
end
|
| 41 |
+
|
| 42 |
+
defp do_to_map(no_list, _exclusions), do: no_list
|
| 43 |
+
|
| 44 |
+
defp to_map_element({key, value}, exclusions) do
|
| 45 |
+
if Enum.member?(exclusions, key) do
|
| 46 |
+
{key, value}
|
| 47 |
+
else
|
| 48 |
+
{key, do_to_map(value, exclusions)}
|
| 49 |
+
end
|
| 50 |
+
end
|
| 51 |
+
end
|
deps/benchee/lib/benchee/utility/erlang_version.ex
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
defmodule Benchee.Utility.ErlangVersion do
|
| 2 |
+
@moduledoc false
|
| 3 |
+
|
| 4 |
+
# Internal module to deal with erlang version parsing oddity
|
| 5 |
+
|
| 6 |
+
@doc """
|
| 7 |
+
Was the given version before the reference version?
|
| 8 |
+
|
| 9 |
+
Used to check if a bugfix has already landed.
|
| 10 |
+
|
| 11 |
+
Applies some manual massaging, as erlang likes to report versions number not compatible with
|
| 12 |
+
SemVer. If we can't parse the version, to minimize false positives, we assume it's newer.
|
| 13 |
+
|
| 14 |
+
Only the `version_to_check` is treated loosely. `version_to_check` must be SemVer compatible,
|
| 15 |
+
as it is assumed to be provided by project maintainers.
|
| 16 |
+
|
| 17 |
+
## Examples
|
| 18 |
+
|
| 19 |
+
iex> includes_fixes_from?("22.0.0", "22.0.0")
|
| 20 |
+
true
|
| 21 |
+
|
| 22 |
+
iex> includes_fixes_from?("22.0.1", "22.0.0")
|
| 23 |
+
true
|
| 24 |
+
|
| 25 |
+
iex> includes_fixes_from?("22.0.0", "22.0.1")
|
| 26 |
+
false
|
| 27 |
+
|
| 28 |
+
iex> includes_fixes_from?("22.0.4", "22.0.5")
|
| 29 |
+
false
|
| 30 |
+
|
| 31 |
+
iex> includes_fixes_from?("22.0.4", "22.0.4")
|
| 32 |
+
true
|
| 33 |
+
|
| 34 |
+
iex> includes_fixes_from?("22.0.5", "22.0.4")
|
| 35 |
+
true
|
| 36 |
+
|
| 37 |
+
iex> includes_fixes_from?("21.999.9999", "22.0.0")
|
| 38 |
+
false
|
| 39 |
+
|
| 40 |
+
iex> includes_fixes_from?("23.0.0", "22.0.0")
|
| 41 |
+
true
|
| 42 |
+
|
| 43 |
+
# weird longer version numbers work
|
| 44 |
+
iex> includes_fixes_from?("22.0.0.0", "22.0.0")
|
| 45 |
+
true
|
| 46 |
+
|
| 47 |
+
iex> includes_fixes_from?("22.0.0.14", "22.0.0")
|
| 48 |
+
true
|
| 49 |
+
|
| 50 |
+
iex> includes_fixes_from?("23.3.5.14", "22.0.0")
|
| 51 |
+
true
|
| 52 |
+
|
| 53 |
+
iex> includes_fixes_from?("21.3.5.14", "22.0.0")
|
| 54 |
+
false
|
| 55 |
+
|
| 56 |
+
# weird shorter version numbers work
|
| 57 |
+
iex> includes_fixes_from?("22.0", "22.0.0")
|
| 58 |
+
true
|
| 59 |
+
|
| 60 |
+
iex> includes_fixes_from?("22.0", "22.0.1")
|
| 61 |
+
false
|
| 62 |
+
|
| 63 |
+
iex> includes_fixes_from?("22.1", "22.0.0")
|
| 64 |
+
true
|
| 65 |
+
|
| 66 |
+
iex> includes_fixes_from?("21.3", "22.0.0")
|
| 67 |
+
false
|
| 68 |
+
|
| 69 |
+
# rc version numbers work
|
| 70 |
+
iex> includes_fixes_from?("22.0-rc3", "22.0.0")
|
| 71 |
+
false
|
| 72 |
+
iex> includes_fixes_from?("23.0-rc0", "22.0.0")
|
| 73 |
+
true
|
| 74 |
+
|
| 75 |
+
# since we are falling back to general OTP versions now, test those as well
|
| 76 |
+
iex> includes_fixes_from?("21", "22.0.0")
|
| 77 |
+
false
|
| 78 |
+
iex> includes_fixes_from?("22", "22.0.0")
|
| 79 |
+
true
|
| 80 |
+
iex> includes_fixes_from?("23.0", "22.0.0")
|
| 81 |
+
true
|
| 82 |
+
|
| 83 |
+
# completely broken versions are assumed to be good to avoid false positives
|
| 84 |
+
# as this is not a main functionality but code to potentially work around an older erlang
|
| 85 |
+
# bug.
|
| 86 |
+
iex> includes_fixes_from?("super erlang", "22.0.0")
|
| 87 |
+
true
|
| 88 |
+
iex> includes_fixes_from?("", "22.0.0")
|
| 89 |
+
true
|
| 90 |
+
"""
|
| 91 |
+
def includes_fixes_from?(version_to_check, reference_version) do
|
| 92 |
+
erlang_version = parse_erlang_version(version_to_check)
|
| 93 |
+
|
| 94 |
+
case erlang_version do
|
| 95 |
+
{:ok, version} -> Version.compare(version, reference_version) != :lt
|
| 96 |
+
# we do not know which version this is, so don't trust it?
|
| 97 |
+
_ -> true
|
| 98 |
+
end
|
| 99 |
+
end
|
| 100 |
+
|
| 101 |
+
# `Version` only supports full SemVer, Erlang loves version numbers like `22.3.4.24` or `22.0`
|
| 102 |
+
# which makes `Version` error out so we gotta manually alter them so that it's `22.3.4`
|
| 103 |
+
defp parse_erlang_version(erlang_version) do
|
| 104 |
+
last_version_segment = ~r/\.\d+$/
|
| 105 |
+
|
| 106 |
+
# dot count is a heuristic but it should work
|
| 107 |
+
dot_count =
|
| 108 |
+
erlang_version
|
| 109 |
+
|> String.graphemes()
|
| 110 |
+
|> Enum.count(&(&1 == "."))
|
| 111 |
+
|
| 112 |
+
version =
|
| 113 |
+
case dot_count do
|
| 114 |
+
3 -> Regex.replace(last_version_segment, erlang_version, "")
|
| 115 |
+
1 -> deal_with_major_minor(erlang_version)
|
| 116 |
+
0 -> "#{erlang_version}.0.0"
|
| 117 |
+
_ -> erlang_version
|
| 118 |
+
end
|
| 119 |
+
|
| 120 |
+
Version.parse(version)
|
| 121 |
+
end
|
| 122 |
+
|
| 123 |
+
# Only major/minor seem to get the rc treatment
|
| 124 |
+
# but if it is major/minor/patch `Version` handles it correctly.
|
| 125 |
+
# For the 4 digit versions we don't really care right now/normally does not happen.
|
| 126 |
+
defp deal_with_major_minor(erlang_version) do
|
| 127 |
+
# -rc and other weird versions contain -
|
| 128 |
+
if String.contains?(erlang_version, "-") do
|
| 129 |
+
String.replace(erlang_version, "-", ".0-")
|
| 130 |
+
else
|
| 131 |
+
"#{erlang_version}.0"
|
| 132 |
+
end
|
| 133 |
+
end
|
| 134 |
+
end
|
deps/benchee/lib/benchee/utility/file_creation.ex
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
defmodule Benchee.Utility.FileCreation do
|
| 2 |
+
@moduledoc """
|
| 3 |
+
Methods to easily handle file creation used in plugins.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
alias Benchee.Benchmark
|
| 7 |
+
|
| 8 |
+
@doc """
|
| 9 |
+
Open a file for write for all key/value pairs, interleaves the file name with
|
| 10 |
+
the key and calls the given function with file, content and filename.
|
| 11 |
+
|
| 12 |
+
Uses `interleave/2` to get the base filename and
|
| 13 |
+
the given keys together to one nice file name, then creates these files and
|
| 14 |
+
calls the function with the file and the content from the given map so that
|
| 15 |
+
data can be written to the file.
|
| 16 |
+
|
| 17 |
+
If a directory is specified, it creates the directory.
|
| 18 |
+
|
| 19 |
+
Expects:
|
| 20 |
+
|
| 21 |
+
* names_to_content - a map from input name to contents that should go into
|
| 22 |
+
the corresponding file
|
| 23 |
+
* filename - the base file name as desired by the user
|
| 24 |
+
* function - a function that is then called for every file with the associated
|
| 25 |
+
file content from the map, defaults to just writing the file content via
|
| 26 |
+
`IO.write/2` and printing where it put the file.
|
| 27 |
+
|
| 28 |
+
## Examples
|
| 29 |
+
|
| 30 |
+
# Just writes the contents to a file
|
| 31 |
+
Benchee.Utility.FileCreation.each(%{"My Input" => "_awesome html content_"},
|
| 32 |
+
"my.html",
|
| 33 |
+
fn(file, content) -> IO.write(file, content) end)
|
| 34 |
+
"""
|
| 35 |
+
@spec each(%{(String.t() | list(String.t())) => String.t()}, String.t(), fun()) :: :ok
|
| 36 |
+
def each(names_to_content, filename, function \\ &default_each/3) do
|
| 37 |
+
ensure_directory_exists(filename)
|
| 38 |
+
|
| 39 |
+
Enum.each(names_to_content, fn {input_name, content} ->
|
| 40 |
+
input_filename = interleave(filename, input_name)
|
| 41 |
+
|
| 42 |
+
File.open!(input_filename, [:write, :utf8], fn file ->
|
| 43 |
+
function.(file, content, input_filename)
|
| 44 |
+
end)
|
| 45 |
+
end)
|
| 46 |
+
end
|
| 47 |
+
|
| 48 |
+
defp default_each(file, content, input_filename) do
|
| 49 |
+
:ok = IO.write(file, content)
|
| 50 |
+
IO.puts("Generated #{input_filename}")
|
| 51 |
+
end
|
| 52 |
+
|
| 53 |
+
@doc """
|
| 54 |
+
Make sure the directory for the given file name exists.
|
| 55 |
+
"""
|
| 56 |
+
def ensure_directory_exists(filename) do
|
| 57 |
+
directory = Path.dirname(filename)
|
| 58 |
+
File.mkdir_p!(directory)
|
| 59 |
+
end
|
| 60 |
+
|
| 61 |
+
@doc """
|
| 62 |
+
Gets file name/path, the input name and others together.
|
| 63 |
+
|
| 64 |
+
Takes a list of values to interleave or just a single value.
|
| 65 |
+
Handles the special no_input key to do no work at all.
|
| 66 |
+
|
| 67 |
+
## Examples
|
| 68 |
+
|
| 69 |
+
iex> interleave("abc.csv", "hello")
|
| 70 |
+
"abc_hello.csv"
|
| 71 |
+
|
| 72 |
+
iex> interleave("abc.csv", "Big Input")
|
| 73 |
+
"abc_big_input.csv"
|
| 74 |
+
|
| 75 |
+
iex> interleave("abc.csv", "String.length/1")
|
| 76 |
+
"abc_string_length_1.csv"
|
| 77 |
+
|
| 78 |
+
iex> interleave("bench/abc.csv", "Big Input")
|
| 79 |
+
"bench/abc_big_input.csv"
|
| 80 |
+
|
| 81 |
+
iex> interleave(
|
| 82 |
+
...> "bench/abc.csv",
|
| 83 |
+
...> ["Big Input"]
|
| 84 |
+
...> )
|
| 85 |
+
"bench/abc_big_input.csv"
|
| 86 |
+
|
| 87 |
+
iex> interleave("abc.csv", [])
|
| 88 |
+
"abc.csv"
|
| 89 |
+
|
| 90 |
+
iex> interleave(
|
| 91 |
+
...> "bench/abc.csv",
|
| 92 |
+
...> ["Big Input", "Comparison"]
|
| 93 |
+
...> )
|
| 94 |
+
"bench/abc_big_input_comparison.csv"
|
| 95 |
+
|
| 96 |
+
iex> interleave(
|
| 97 |
+
...> "bench/A B C.csv",
|
| 98 |
+
...> ["Big Input", "Comparison"]
|
| 99 |
+
...> )
|
| 100 |
+
"bench/A B C_big_input_comparison.csv"
|
| 101 |
+
|
| 102 |
+
iex> interleave(
|
| 103 |
+
...> "bench/abc.csv",
|
| 104 |
+
...> ["Big Input", "Comparison", "great Stuff"]
|
| 105 |
+
...> )
|
| 106 |
+
"bench/abc_big_input_comparison_great_stuff.csv"
|
| 107 |
+
|
| 108 |
+
iex> marker = Benchee.Benchmark.no_input()
|
| 109 |
+
...> interleave("abc.csv", marker)
|
| 110 |
+
"abc.csv"
|
| 111 |
+
iex> interleave("abc.csv", [marker])
|
| 112 |
+
"abc.csv"
|
| 113 |
+
iex> interleave(
|
| 114 |
+
...> "abc.csv",
|
| 115 |
+
...> [marker, "Comparison"]
|
| 116 |
+
...> )
|
| 117 |
+
"abc_comparison.csv"
|
| 118 |
+
iex> interleave(
|
| 119 |
+
...> "abc.csv",
|
| 120 |
+
...> ["Something cool", marker, "Comparison"]
|
| 121 |
+
...> )
|
| 122 |
+
"abc_something_cool_comparison.csv"
|
| 123 |
+
"""
|
| 124 |
+
@spec interleave(String.t(), String.t() | list(String.t())) :: String.t()
|
| 125 |
+
def interleave(filename, names) when is_list(names) do
|
| 126 |
+
file_names =
|
| 127 |
+
names
|
| 128 |
+
|> Enum.map(&to_filename/1)
|
| 129 |
+
|> prepend(Path.rootname(filename))
|
| 130 |
+
|> Enum.reject(fn string -> String.length(string) < 1 end)
|
| 131 |
+
|> Enum.join("_")
|
| 132 |
+
|
| 133 |
+
file_names <> Path.extname(filename)
|
| 134 |
+
end
|
| 135 |
+
|
| 136 |
+
def interleave(filename, name) do
|
| 137 |
+
interleave(filename, [name])
|
| 138 |
+
end
|
| 139 |
+
|
| 140 |
+
defp prepend(list, item) do
|
| 141 |
+
[item | list]
|
| 142 |
+
end
|
| 143 |
+
|
| 144 |
+
defp to_filename(name_string) do
|
| 145 |
+
no_input = Benchmark.no_input()
|
| 146 |
+
|
| 147 |
+
case name_string do
|
| 148 |
+
^no_input ->
|
| 149 |
+
""
|
| 150 |
+
|
| 151 |
+
_ ->
|
| 152 |
+
String.downcase(String.replace(name_string, ~r/[^0-9A-Z]/i, "_"))
|
| 153 |
+
end
|
| 154 |
+
end
|
| 155 |
+
end
|