|
| 1 | +# Angular Devkit Benchmark |
| 2 | + |
| 3 | +This tool provides benchmark information for processes. |
| 4 | +The tool will gathering metrics from a given command, then average them out over the total runs. |
| 5 | + |
| 6 | +It currently shows only time, process, cpu, and memory used but might be extended in the future. |
| 7 | + |
| 8 | +This tool was created to provide an objective and reproducible way of benchmarking process |
| 9 | +performance. |
| 10 | + |
| 11 | +Given a process (or its source code), process inputs and environment, keeping two of these elements |
| 12 | +constant while varying the third should allow for meaningful benchmarks over time. |
| 13 | + |
| 14 | +In the context of the DevKit, we publish many CLI tools and have access to their source code. |
| 15 | +By tracking tool resource usage we can catch performance regressions or improvements on our CI. |
| 16 | + |
| 17 | + |
| 18 | +## STABILITY AND SUPPORT DISCLAIMER |
| 19 | + |
| 20 | +This package is not currently stable. Usage, output and API may change at any time. |
| 21 | +Support is not ensured. |
| 22 | + |
| 23 | +## Installation |
| 24 | + |
| 25 | +You can install the benchmark tool via `npm install -g benchmark` for a global install, or without |
| 26 | +`-g` for a local install. |
| 27 | +Installing globally gives you access to the `benchmark` binary in your `PATH`. |
| 28 | + |
| 29 | + |
| 30 | +## CLI Usage |
| 31 | + |
| 32 | +Call the `benchmark` binary, followed by options, then double dash, then the command to benchmark. |
| 33 | + |
| 34 | +For more information on the available options, run `benchmark --help`: |
| 35 | +``` |
| 36 | +$ benchmark --help |
| 37 | +[benchmark] benchmark [options] -- [command to benchmark] |
| 38 | +
|
| 39 | +Collects process stats from running the command. |
| 40 | +
|
| 41 | +Options: |
| 42 | + --help Show this message. |
| 43 | + (... other available options) |
| 44 | +
|
| 45 | +Example: |
| 46 | + benchmark --iterations=3 -- node my-script.js |
| 47 | +``` |
| 48 | + |
| 49 | + |
| 50 | +## Example |
| 51 | + |
| 52 | +Given the naive implementation of a fibonacci number calculator below: |
| 53 | +``` |
| 54 | +// fibonacci.js |
| 55 | +const fib = (n) => n > 1 ? fib(n - 1) + fib(n - 2) : n; |
| 56 | +console.log(fib(parseInt(process.argv[2]))); |
| 57 | +``` |
| 58 | + |
| 59 | +Run `benchmark -- node fibonacci.js 40` to benchmark calculating the 40th fibonacci number: |
| 60 | + |
| 61 | +``` |
| 62 | +$ benchmark -- node fibonacci.js 40 |
| 63 | +[benchmark] Benchmarking process over 5 iterations, with up to 5 retries. |
| 64 | +[benchmark] node fibonacci.js 40 (at D:\sandbox\latest-project) |
| 65 | +[benchmark] Process Stats |
| 66 | +[benchmark] Elapsed Time: 2365.40 ms (2449.00, 2444.00, 2357.00, 2312.00, 2265.00) |
| 67 | +[benchmark] Average Process usage: 1.00 process(es) (1.00, 1.00, 1.00, 1.00, 1.00) |
| 68 | +[benchmark] Peak Process usage: 1.00 process(es) (1.00, 1.00, 1.00, 1.00, 1.00) |
| 69 | +[benchmark] Average CPU usage: 4.72 % (5.03, 4.86, 4.50, 4.50, 4.69) |
| 70 | +[benchmark] Peak CPU usage: 23.40 % (25.00, 23.40, 21.80, 21.80, 25.00) |
| 71 | +[benchmark] Average Memory usage: 22.34 MB (22.32, 22.34, 22.34, 22.35, 22.35) |
| 72 | +[benchmark] Peak Memory usage: 22.34 MB (22.32, 22.34, 22.34, 22.35, 22.35) |
| 73 | +``` |
| 74 | + |
| 75 | + |
| 76 | +## API Usage |
| 77 | + |
| 78 | +You can also use the benchmarking API directly: |
| 79 | + |
| 80 | +``` |
| 81 | +import { Command, defaultStatsCapture, runBenchmark } from '@angular-devkit/benchmark'; |
| 82 | +
|
| 83 | +const command = new Command('node', ['fibonacci.js', '40']); |
| 84 | +const captures = [defaultStatsCapture]; |
| 85 | +
|
| 86 | +runBenchmark({ command, command }).subscribe(results => { |
| 87 | + // results is:[{ |
| 88 | + // "name": "Process Stats", |
| 89 | + // "metrics": [{ |
| 90 | + // "name": "Elapsed Time", "unit": "ms", "value": 1883.6, |
| 91 | + // "componentValues": [1733, 1957, 1580, 1763, 2385] |
| 92 | + // }, { |
| 93 | + // "name": "Average Process usage", "unit": "process(es)", "value": 1, |
| 94 | + // "componentValues": [1, 1, 1, 1, 1] |
| 95 | + // }, { |
| 96 | + // "name": "Peak Process usage", "unit": "process(es)", "value": 1, |
| 97 | + // "componentValues": [1, 1, 1, 1, 1] |
| 98 | + // }, { |
| 99 | + // "name": "Average CPU usage", "unit": "%", "value": 3.0855555555555556, |
| 100 | + // "componentValues": [1.9625, 1.9500000000000002, 1.9500000000000002, 4.887499999999999, 4.677777777777778] |
| 101 | + // }, { |
| 102 | + // "name": "Peak CPU usage", "unit": "%", "value": 19.380000000000003, |
| 103 | + // "componentValues": [15.7, 15.6, 15.6, 25, 25] |
| 104 | + // }, { |
| 105 | + // "name": "Average Memory usage", "unit": "MB", "value": 22.364057600000002, |
| 106 | + // "componentValues": [22.383104, 22.332416, 22.401024, 22.355968, 22.347776] |
| 107 | + // }, { |
| 108 | + // "name": "Peak Memory usage", "unit": "MB", "value": 22.3649792, |
| 109 | + // "componentValues": [22.384639999999997, 22.335487999999998, 22.401024, 22.355968, 22.347776] |
| 110 | + // }] |
| 111 | + // }] |
| 112 | +}); |
| 113 | +``` |
| 114 | + |
| 115 | +A good example of API usage is the `main` binary itself, found in `./src/main.ts`. |
| 116 | +We recommend using TypeScript to get full access to the interfaces included. |
0 commit comments