Skip to content

Commit 8245367

Browse files
committed
chore: add instructions in the Readmes
1 parent 5b68849 commit 8245367

File tree

4 files changed

+169
-0
lines changed

4 files changed

+169
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,16 @@ Node.js libraries to create CodSpeed benchmarks
55

66
[![CI](https://github.com/CodSpeedHQ/codspeed-node/actions/workflows/ci.yml/badge.svg)](https://github.com/CodSpeedHQ/codspeed-node/actions/workflows/ci.yml)
77
[![npm (scoped)](https://img.shields.io/npm/v/@codspeed/core)](https://www.npmjs.com/org/codspeed)
8+
[![Discord](https://img.shields.io/badge/chat%20on-discord-7289da.svg)](https://discord.com/invite/MxpaCfKSqF)
89

910
</div>
1011

12+
## Documentation
13+
14+
Check out the [documentation](https://docs.codspeed.io/benchmarks/nodejs) for complete integration instructions.
15+
16+
## Packages
17+
1118
This mono-repo contains the integration packages for using CodSpeed with Node.js:
1219

1320
- [`@codspeed/tinybench-plugin`](./packages/tinybench-plugin): tinybench compatibility layer for CodSpeed

packages/benchmark.js-plugin/README.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,81 @@ Benchmark.js compatibility layer for CodSpeed
55

66
[![CI](https://github.com/CodSpeedHQ/codspeed-node/actions/workflows/ci.yml/badge.svg)](https://github.com/CodSpeedHQ/codspeed-node/actions/workflows/ci.yml)
77
[![npm (scoped)](https://img.shields.io/npm/v/@codspeed/benchmark.js-plugin)](https://www.npmjs.com/package/@codspeed/benchmark.js-plugin)
8+
[![Discord](https://img.shields.io/badge/chat%20on-discord-7289da.svg)](https://discord.com/invite/MxpaCfKSqF)
89

910
</div>
11+
12+
## Documentation
13+
14+
Check out the [documentation](https://docs.codspeed.io/benchmarks/nodejs) for complete integration instructions.
15+
16+
## Installation
17+
18+
First install the plugin [`@codspeed/benchmark.js-plugin`](https://www.npmjs.com/package/@codspeed/tinybench-plugin) and `benchmark.js` (if not already installed):
19+
20+
```sh
21+
npm install --save-dev @codspeed/benchmark.js-plugin benchmark.js
22+
```
23+
24+
or with `yarn`:
25+
26+
```sh
27+
yarn add --dev @codspeed/benchmark.js-plugin benchmark.js
28+
```
29+
30+
or with `pnpm`:
31+
32+
```sh
33+
pnpm add --save-dev @codspeed/benchmark.js-plugin benchmark.js
34+
```
35+
36+
## Usage
37+
38+
Let's create a fibonacci function and benchmark it with benchmark.js and the CodSpeed plugin:
39+
40+
```js title="benches/bench.mjs"
41+
import Benchmark from "benchmark";
42+
import { withCodSpeed } from "@codspeed/benchmark.js-plugin";
43+
44+
function fibonacci(n) {
45+
if (n < 2) {
46+
return n;
47+
}
48+
return fibonacci(n - 1) + fibonacci(n - 2);
49+
}
50+
51+
const suite = withCodSpeed(new Benchmark.Suite());
52+
53+
suite
54+
.add("fibonacci10", () => {
55+
fibonacci(10);
56+
})
57+
.add("fibonacci15", () => {
58+
fibonacci(15);
59+
})
60+
.on("cycle", function (event: Benchmark.Event) {
61+
console.log(String(event.target));
62+
})
63+
.run();
64+
```
65+
66+
Here, a few things are happening:
67+
68+
- We create a simple recursive fibonacci function.
69+
- We create a new `Benchmark.Suite` instance with CodSpeed support by using the **`withCodSpeed`** helper. This step is **critical** to enable CodSpeed on your benchmarks.
70+
- We add two benchmarks to the suite and launch it, benching our `fibonacci` function with 10 and 15.
71+
72+
Now, we can run our benchmarks locally to make sure everything is working as expected:
73+
74+
```sh
75+
$ node benches/bench.mjs
76+
[CodSpeed] 2 benches detected but no instrumentation found
77+
[CodSpeed] falling back to benchmark.js
78+
79+
fibonacci10 x 2,155,187 ops/sec ±0.50% (96 runs sampled)
80+
fibonacci15 x 194,742 ops/sec ±0.48% (95 runs sampled)
81+
```
82+
83+
And... Congrats🎉, CodSpeed is installed in your benchmarking suite! Locally, CodSpeed will fallback to tinybench since the instrumentation is only available in the CI environment for now.
84+
85+
You can now [run those benchmark in your CI](https://docs.codspeed.io/benchmarks/nodejs#running-the-benchmarks-in-your-ci) to continuously get consistent performance measurements.

packages/core/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ The core Node library used to integrate with Codspeed runners
55

66
[![CI](https://github.com/CodSpeedHQ/codspeed-node/actions/workflows/ci.yml/badge.svg)](https://github.com/CodSpeedHQ/codspeed-node/actions/workflows/ci.yml)
77
[![npm (scoped)](https://img.shields.io/npm/v/@codspeed/core)](https://www.npmjs.com/package/@codspeed/core)
8+
[![Discord](https://img.shields.io/badge/chat%20on-discord-7289da.svg)](https://discord.com/invite/MxpaCfKSqF)
89

910
</div>
1011

packages/tinybench-plugin/README.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,90 @@ tinybench compatibility layer for CodSpeed
55

66
[![CI](https://github.com/CodSpeedHQ/codspeed-node/actions/workflows/ci.yml/badge.svg)](https://github.com/CodSpeedHQ/codspeed-node/actions/workflows/ci.yml)
77
[![npm (scoped)](https://img.shields.io/npm/v/@codspeed/tinybench-plugin)](https://www.npmjs.com/package/@codspeed/tinybench-plugin)
8+
[![Discord](https://img.shields.io/badge/chat%20on-discord-7289da.svg)](https://discord.com/invite/MxpaCfKSqF)
89

910
</div>
11+
12+
## Documentation
13+
14+
Check out the [documentation](https://docs.codspeed.io/benchmarks/nodejs) for complete integration instructions.
15+
16+
## Installation
17+
18+
First install the plugin [`@codspeed/tinybench-plugin`](https://www.npmjs.com/package/@codspeed/tinybench-plugin) and `tinybench` (if not already installed):
19+
20+
```sh
21+
npm install --save-dev @codspeed/tinybench-plugin tinybench
22+
```
23+
24+
or with `yarn`:
25+
26+
```sh
27+
yarn add --dev @codspeed/tinybench-plugin tinybench
28+
```
29+
30+
or with `pnpm`:
31+
32+
```sh
33+
pnpm add --save-dev @codspeed/tinybench-plugin tinybench
34+
```
35+
36+
## Usage
37+
38+
Let's create a fibonacci function and benchmark it with tinybench and the CodSpeed plugin:
39+
40+
```js title="benches/bench.mjs"
41+
import { Bench } from "tinybench";
42+
import { withCodSpeed } from "@codspeed/tinybench-plugin";
43+
44+
function fibonacci(n) {
45+
if (n < 2) {
46+
return n;
47+
}
48+
return fibonacci(n - 1) + fibonacci(n - 2);
49+
}
50+
51+
const bench = withCodSpeed(new Bench());
52+
53+
bench
54+
.add("fibonacci10", () => {
55+
fibonacci(10);
56+
})
57+
.add("fibonacci15", () => {
58+
fibonacci(15);
59+
});
60+
61+
await bench.run();
62+
console.table(
63+
bench.tasks.map(({ name, result }) => ({
64+
"Task Name": name,
65+
"Average Time (ps)": result?.mean * 1000,
66+
}))
67+
);
68+
```
69+
70+
Here, a few things are happening:
71+
72+
- We create a simple recursive fibonacci function.
73+
- We create a new `Bench` instance with CodSpeed support by using the **`withCodSpeed`** helper. This step is **critical** to enable CodSpeed on your benchmarks.
74+
75+
- We add two benchmarks to the suite and launch it, benching our `fibonacci` function for 10 and 15.
76+
77+
Now, we can run our benchmarks locally to make sure everything is working as expected:
78+
79+
```sh
80+
$ node benches/bench.mjs
81+
[CodSpeed] 2 benches detected but no instrumentation found
82+
[CodSpeed] falling back to tinybench
83+
84+
┌─────────┬───────────────┬────────────────────┐
85+
│ (index) │ Task Name │ Average Time (ps) │
86+
├─────────┼───────────────┼────────────────────┤
87+
0'fibonacci10'0.5660083779532603
88+
1'fibonacci15'5.2475729101797635
89+
└─────────┴───────────────┴────────────────────┘
90+
```
91+
92+
And... Congrats🎉, CodSpeed is installed in your benchmarking suite! Locally, CodSpeed will fallback to tinybench since the instrumentation is only available in the CI environment for now.
93+
94+
You can now [run those benchmark in your CI](https://docs.codspeed.io/benchmarks/nodejs#running-the-benchmarks-in-your-ci) to continuously get consistent performance measurements.

0 commit comments

Comments
 (0)