Skip to content

Commit e5c6695

Browse files
authored
docs: add JSDoc to functions (#69)
1 parent 4352266 commit e5c6695

File tree

8 files changed

+120
-0
lines changed

8 files changed

+120
-0
lines changed

lib/histogram.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
const { validateNumber } = require("./validators");
22

3+
/**
4+
* A class that calculates and maintains statistical measurements for a set of numeric samples.
5+
* Handles outlier removal, and calculates various statistical measures like mean, standard deviation,
6+
* coefficient of variation, and percentiles.
7+
*/
38
class StatisticalHistogram {
49
all = [];
510
min;

lib/lifecycle.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ function parsePluginsResult(plugins, name) {
3131
return result;
3232
}
3333

34+
/**
35+
* Calculates and returns the initial number of iterations for a benchmark
36+
* @param {import('./index').Benchmark} bench - The benchmark object to be executed
37+
* @returns {Promise<number>} The calculated number of initial iterations
38+
*/
3439
async function getInitialIterations(bench) {
3540
const { 0: duration, 1: realIterations } = await clockBenchmark(bench, 30);
3641

@@ -49,6 +54,15 @@ async function getInitialIterations(bench) {
4954
return getItersForOpDuration(durationPerOp, bench.minTime);
5055
}
5156

57+
/**
58+
* Executes the warmup phase of a benchmark
59+
* @param {import('./index').Benchmark} bench - The benchmark object to be executed
60+
* @param {number} initialIterations - The initial number of iterations to run
61+
* @param {Object} options - Warmup options
62+
* @param {number} [options.minTime] - Minimum time for warmup, defaults to bench.minTime
63+
* @param {number} [options.maxTime] - Maximum time for warmup, defaults to bench.minTime
64+
* @returns {Promise<void>}
65+
*/
5266
async function runWarmup(bench, initialIterations, { minTime, maxTime }) {
5367
minTime = minTime ?? bench.minTime;
5468
maxTime = maxTime ?? bench.minTime;
@@ -114,6 +128,14 @@ async function runBenchmarkOnce(
114128
return { iterations, timeSpent };
115129
}
116130

131+
/**
132+
* Executes a benchmark with the specified parameters
133+
* @param {import('./index').Benchmark} bench - The benchmark object to be executed
134+
* @param {number} initialIterations - The initial number of iterations to run
135+
* @param {number} repeatSuite - Number of times to repeat the benchmark suite
136+
* @param {number} minSamples - Minimum number of samples to collect
137+
* @returns {Promise<Object>} The benchmark results containing operations per second, iterations, histogram data and plugin results
138+
*/
117139
async function runBenchmark(bench, initialIterations, repeatSuite, minSamples) {
118140
const histogram = new StatisticalHistogram();
119141

lib/plugins.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ const { MemoryPlugin } = require("./plugins/memory");
66

77
const { validateFunction, validateArray } = require("./validators");
88

9+
/**
10+
* Validates that all plugins in the array implement the required plugin interface
11+
* and that they are supported in the current environment
12+
* @param {Array<Object>} plugins - Array of plugin objects to validate
13+
* @throws {Error} If any plugin doesn't meet the requirements or isn't supported
14+
*/
915
function validatePlugins(plugins) {
1016
for (p of plugins) {
1117
validateFunction(p.isSupported, "Plugins must have a isSupported method.");

lib/plugins/memory.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ const {
44
kStatisticalHistogramFinish,
55
} = require("../histogram");
66

7+
/**
8+
* Formats a byte value into a human-readable string with appropriate units (B, Kb, MB, GB)
9+
* @param {number} bytes - The number of bytes to format
10+
* @returns {string} Formatted string with appropriate unit suffix
11+
*/
712
function formatBytes(bytes) {
813
if (bytes < 1024) return `${Math.round(bytes)}B`;
914

@@ -17,6 +22,10 @@ function formatBytes(bytes) {
1722
return `${gbytes.toFixed(2)}GB`;
1823
}
1924

25+
/**
26+
* Plugin that measures memory usage during benchmark execution
27+
* Collects heap usage statistics and provides reporting capabilities
28+
*/
2029
class MemoryPlugin {
2130
static MEMORY_BEFORE_RUN = "memoryBeforeRun";
2231
static MEMORY_AFTER_RUN = "memoryAfterRun";

lib/report.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,24 @@ const { htmlReport } = require("./reporter/html");
44
const { jsonReport } = require("./reporter/json");
55
const { csvReport } = require("./reporter/csv");
66

7+
/**
8+
* @typedef {Object} BenchmarkResult
9+
* @property {string} name - The name of the benchmark
10+
* @property {number} opsSec - Operations per second
11+
* @property {number} iterations - Total number of iterations run
12+
* @property {Object} histogram - Statistical data about the benchmark runs
13+
* @property {Array} plugins - Results from plugins used during benchmarking
14+
*/
15+
16+
/**
17+
* Exports various report generators for benchmark results
18+
* @type {Object}
19+
* @property {function(BenchmarkResult[]): void} chartReport - Generates a chart visualization of benchmark results
20+
* @property {function(BenchmarkResult[]): void} textReport - Generates a text report of benchmark results
21+
* @property {function(BenchmarkResult[]): void} htmlReport - Generates an HTML report of benchmark results
22+
* @property {function(BenchmarkResult[]): string} jsonReport - Generates a JSON report of benchmark results
23+
* @property {function(BenchmarkResult[]): string} csvReport - Generates a CSV report of benchmark results
24+
*/
725
module.exports = {
826
chartReport,
927
textReport,

lib/reporter/chart.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ const formatter = Intl.NumberFormat(undefined, {
66
maximumFractionDigits: 2,
77
});
88

9+
/**
10+
* Draws a bar chart representation of a benchmark result
11+
* @param {string} label - The label for the bar (benchmark name)
12+
* @param {number} value - The value to display (operations per second)
13+
* @param {number} total - The maximum value in the dataset (for scaling)
14+
* @param {number} samples - Number of samples collected
15+
* @param {number} [length=30] - Length of the bar in characters
16+
*/
917
function drawBar(label, value, total, samples, length = 30) {
1018
const percentage = value / total;
1119
const filledLength = Math.round(length * percentage);
@@ -29,6 +37,11 @@ const environment = {
2937
hardware: `${cpus().length} vCPUs | ${(totalmem() / 1024 ** 3).toFixed(1)}GB Mem`,
3038
};
3139

40+
/**
41+
* Generates a chart visualization of benchmark results in the console
42+
* Displays system information and a bar chart of operations per second
43+
* @param {import('../report').BenchmarkResult[]} results - Array of benchmark results
44+
*/
3245
function chartReport(results) {
3346
const maxOpsSec = Math.max(...results.map((b) => b.opsSec));
3447

lib/reporter/text.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ const formatter = Intl.NumberFormat(undefined, {
66
maximumFractionDigits: 2,
77
});
88

9+
/**
10+
* Generates a text report of benchmark results, displaying each benchmark's name,
11+
* operations per second, number of samples, plugin results, and min/max timings
12+
* @param {import('../report').BenchmarkResult[]} results - Array of benchmark results
13+
*/
914
function textReport(results) {
1015
for (const result of results) {
1116
const opsSecReported =

lib/validators.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,33 @@
1+
/**
2+
* Creates an error object with ERR_INVALID_ARG_TYPE code
3+
* @param {string} message - The error message
4+
* @returns {Error} Error with code ERR_INVALID_ARG_TYPE
5+
*/
16
function ERR_INVALID_ARG_TYPE(message) {
27
const err = new Error(message);
38
err.code = "ERR_INVALID_ARG_TYPE";
49
return err;
510
}
611

12+
/**
13+
* Creates an error object with ERR_INVALID_ARG_VALUE code
14+
* @param {string} message - The error message
15+
* @returns {Error} Error with code ERR_INVALID_ARG_VALUE
16+
*/
717
function ERR_INVALID_ARG_VALUE(message) {
818
const err = new Error(message);
919
err.code = "ERR_INVALID_ARG_VALUE";
1020
return err;
1121
}
1222

23+
/**
24+
* Validates that a value is a number within an optional range
25+
* @param {any} value - The value to validate
26+
* @param {string} name - Name of the parameter being validated
27+
* @param {number} [min] - Optional minimum value (inclusive)
28+
* @param {number} [max] - Optional maximum value (inclusive)
29+
* @throws {Error} If validation fails
30+
*/
1331
function validateNumber(value, name, min, max) {
1432
if (typeof value !== "number")
1533
throw ERR_INVALID_ARG_TYPE(
@@ -27,6 +45,12 @@ function validateNumber(value, name, min, max) {
2745
}
2846
}
2947

48+
/**
49+
* Validates that a value is an object (not null and not an array)
50+
* @param {any} value - The value to validate
51+
* @param {string} name - Name of the parameter being validated
52+
* @throws {Error} If validation fails
53+
*/
3054
function validateObject(value, name) {
3155
if (value === null || Array.isArray(value)) {
3256
throw ERR_INVALID_ARG_TYPE(
@@ -41,20 +65,38 @@ function validateObject(value, name) {
4165
}
4266
}
4367

68+
/**
69+
* Validates that a value is a function
70+
* @param {any} value - The value to validate
71+
* @param {string} name - Name of the parameter being validated
72+
* @throws {Error} If validation fails
73+
*/
4474
function validateFunction(value, name) {
4575
if (typeof value !== "function")
4676
throw ERR_INVALID_ARG_TYPE(
4777
`value must be a function, name: ${name}, value: ${value}`,
4878
);
4979
}
5080

81+
/**
82+
* Validates that a value is a string
83+
* @param {any} value - The value to validate
84+
* @param {string} name - Name of the parameter being validated
85+
* @throws {Error} If validation fails
86+
*/
5187
function validateString(value, name) {
5288
if (typeof value !== "string")
5389
throw ERR_INVALID_ARG_TYPE(
5490
`value must be a string, name: ${name}, value: ${value}`,
5591
);
5692
}
5793

94+
/**
95+
* Validates that a value is an array
96+
* @param {any} value - The value to validate
97+
* @param {string} name - Name of the parameter being validated
98+
* @throws {Error} If validation fails
99+
*/
58100
function validateArray(value, name) {
59101
if (!Array.isArray(value))
60102
throw ERR_INVALID_ARG_TYPE(

0 commit comments

Comments
 (0)