nushell/crates/nu-std/std/bench/mod.nu
Douglas 2a3805c164
Virtual std module subdirectories (#14040)
# Description

Uses "normal" module `std/<submodule>/mod.nu` instead of renaming the
files (as requested in #13842).

# User-Facing Changes

No user-facing changes other than in `view files` results. Imports
remain the same after this PR.

# Tests + Formatting

- šŸŸ¢ `toolkit fmt`
- šŸŸ¢ `toolkit clippy`
- šŸŸ¢ `toolkit test`
- šŸŸ¢ `toolkit test stdlib`

Also manually confirmed that it does not interfere with nupm, since we
did have a conflict at one point (and it's not possible to test here).

# Performance Tests

## Linux

### Nushell Startup - No config

```nu
bench --pretty -n 200  { <path_to>/nu -c "exit" }
```

| Release | Startup Time |
| --- | --- |
| 0.98.0 | 22ms 730Āµs 768ns +/- 1ms 515Āµs 942ns
| This commit | 9ms 312Āµs 68ns +/- 709Āµs 378ns
| Yesterday's nightly | 9ms 230Āµs 953ns +/- 9ms 67Āµs 689ns

### Nushell Startup - Load full standard library

Measures relative impact of a full `use std *`, which isn't recommended,
but worth tracking.

```nu
bench --pretty -n 200  { <path_to>/nu -c "use std *; exit" }
```

| Release | Startup Time |
| --- | --- |
| 0.98.0 | 23ms 10Āµs 636ns +/- 1ms 277Āµs 854ns
| This commit | 26ms 922Āµs 769ns +/- 562Āµs 538ns
| Yesterday's nightly | 28ms 133Āµs 95ns +/- 761Āµs 943ns
| `deprecated_dirs` removal PR * | 23ms 610Āµs 333ns +/- 369Āµs 436ns

\* Current increase is partially due to double-loading `dirs` with
removal warning in older version.

# After Submitting

Still TODO - Update standard library doc
2024-10-10 06:56:37 -05:00

71 lines
2.5 KiB
Plaintext

# run a piece of `nushell` code multiple times and measure the time of execution.
#
# this command returns a benchmark report of the following form:
# ```
# record<
# mean: duration
# std: duration
# times: list<duration>
# >
# ```
#
# > **Note**
# > `std bench --pretty` will return a `string`.
#
# # Examples
# measure the performance of simple addition
# > std bench { 1 + 2 } -n 10 | table -e
# ā•­ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā•®
# ā”‚ mean ā”‚ 4Āµs 956ns ā”‚
# ā”‚ std ā”‚ 4Āµs 831ns ā”‚
# ā”‚ ā”‚ ā•­ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā•® ā”‚
# ā”‚ times ā”‚ ā”‚ 0 ā”‚ 19Āµs 402ns ā”‚ ā”‚
# ā”‚ ā”‚ ā”‚ 1 ā”‚ 4Āµs 322ns ā”‚ ā”‚
# ā”‚ ā”‚ ā”‚ 2 ā”‚ 3Āµs 352ns ā”‚ ā”‚
# ā”‚ ā”‚ ā”‚ 3 ā”‚ 2Āµs 966ns ā”‚ ā”‚
# ā”‚ ā”‚ ā”‚ 4 ā”‚ 3Āµs ā”‚ ā”‚
# ā”‚ ā”‚ ā”‚ 5 ā”‚ 3Āµs 86ns ā”‚ ā”‚
# ā”‚ ā”‚ ā”‚ 6 ā”‚ 3Āµs 84ns ā”‚ ā”‚
# ā”‚ ā”‚ ā”‚ 7 ā”‚ 3Āµs 604ns ā”‚ ā”‚
# ā”‚ ā”‚ ā”‚ 8 ā”‚ 3Āµs 98ns ā”‚ ā”‚
# ā”‚ ā”‚ ā”‚ 9 ā”‚ 3Āµs 653ns ā”‚ ā”‚
# ā”‚ ā”‚ ā•°ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā•Æ ā”‚
# ā•°ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā•Æ
#
# get a pretty benchmark report
# > std bench { 1 + 2 } --pretty
# 3Āµs 125ns +/- 2Āµs 408ns
export def main [
code: closure # the piece of `nushell` code to measure the performance of
--rounds (-n): int = 50 # the number of benchmark rounds (hopefully the more rounds the less variance)
--verbose (-v) # be more verbose (namely prints the progress)
--pretty # shows the results in human-readable format: "<mean> +/- <stddev>"
] {
let times = (
seq 1 $rounds | each {|i|
if $verbose { print -n $"($i) / ($rounds)\r" }
timeit { do $code } | into int | into float
}
)
if $verbose { print $"($rounds) / ($rounds)" }
let report = {
mean: ($times | math avg | from ns)
min: ($times | math min | from ns)
max: ($times | math max | from ns)
std: ($times | math stddev | from ns)
times: ($times | each { from ns })
}
if $pretty {
$"($report.mean) +/- ($report.std)"
} else {
$report
}
}
# convert an integer amount of nanoseconds to a real duration
def "from ns" [] {
[$in "ns"] | str join | into duration
}