Skip to content

Bump node and switch tinybench to vitest #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .moon/tasks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ tasks:
- "@globs(sources)"
- "@globs(tests)"
- "@globs(configs)"
fix-format:
local: true
command: "prettier --config @in(0) --ignore-path @in(1) --write ."
inputs:
- "/.prettierrc.json"
- "/.prettierignore"
- "@globs(sources)"
- "@globs(tests)"
- "@globs(configs)"
lint:
command: "eslint ."
inputs:
Expand Down
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
18.16.0
20.5.1
86 changes: 86 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# CodSpeed Node Repository Layout

## Repository Structure

This is a monorepo containing CodSpeed plugins for various Node.js benchmarking frameworks.

### Root Level
- `package.json` - Root package configuration
- `pnpm-workspace.yaml` - PNPM workspace configuration
- `lerna.json` - Lerna monorepo configuration
- `tsconfig.base.json` - Base TypeScript configuration
- `rollup.options.js` - Rollup bundler configuration
- `scripts/` - Build and release scripts
- `docs/` - Documentation files
- `examples/` - Example projects using the plugins

### Packages (`packages/`)

#### Core Package (`packages/core/`)
- **Purpose**: Core measurement and instrumentation functionality
- **Key files**:
- `src/index.ts` - Main exports, setupCore/teardownCore functions
- `src/mongoMeasurement.ts` - MongoDB measurement handling
- `src/optimization.ts` - Function optimization utilities
- `src/native_core/` - Native C++ bindings for performance measurement
- `src/introspection.ts` - V8 flags and runtime introspection

#### Tinybench Plugin (`packages/tinybench-plugin/`)
- **Purpose**: CodSpeed integration for tinybench framework
- **Key files**:
- `src/index.ts` - Main plugin implementation with `withCodSpeed()` function
- `tests/index.integ.test.ts` - Integration tests
- `benches/` - Benchmark examples

#### Benchmark.js Plugin (`packages/benchmark.js-plugin/`)
- **Purpose**: CodSpeed integration for benchmark.js framework
- **Key files**:
- `src/index.ts` - Main plugin implementation
- `src/buildSuiteAdd.ts` - Suite building utilities

#### Vitest Plugin (`packages/vitest-plugin/`)
- **Purpose**: CodSpeed integration for Vitest framework
- **Key files**:
- `src/index.ts` - Main plugin implementation
- `src/runner.ts` - Custom test runner
- `src/globalSetup.ts` - Global setup configuration

### Examples Directory (`examples/`)
- `with-javascript-cjs/` - CommonJS JavaScript examples
- `with-javascript-esm/` - ESM JavaScript examples
- `with-typescript-cjs/` - CommonJS TypeScript examples
- `with-typescript-esm/` - ESM TypeScript examples
- `with-typescript-simple-cjs/` - Simple CommonJS TypeScript examples
- `with-typescript-simple-esm/` - Simple ESM TypeScript examples

## Tinybench Plugin Architecture

### Current Stats/Measurements Access

The tinybench plugin currently has **limited stats exposure**:

1. **No direct stats API**: The `withCodSpeed()` function wraps a tinybench instance but doesn't expose measurement results
2. **Console-only output**: Results are only printed to console via `console.log()`
3. **Core measurement**: Uses `@codspeed/core` for actual measurement via:
- `mongoMeasurement.start(uri)` / `mongoMeasurement.stop(uri)`
- `Measurement.startInstrumentation()` / `Measurement.stopInstrumentation(uri)`

### Current Workflow
1. User calls `withCodSpeed(new Bench())` to wrap their tinybench instance
2. Plugin intercepts `bench.run()` to add CodSpeed instrumentation
3. Each benchmark task runs with measurement instrumentation
4. Results are logged to console but not returned as structured data

### Key Functions in tinybench plugin
- `withCodSpeed(bench: Bench): Bench` - Main wrapper function
- `setupInstruments(body)` - Dynamic instrument setup
- `getCallingFile()` - Helper to generate unique URIs for benchmarks

## Potential Enhancement Areas

Based on the codebase analysis, to add stats access features:

1. **Extend return value**: Modify `bench.run()` to return structured measurement data
2. **Add stats methods**: Add methods like `getStats()`, `getResults()`, `getLastRunStats()`
3. **Integrate with core**: Leverage `@codspeed/core` measurement data
4. **Maintain tinybench compatibility**: Ensure existing `bench.table()` still works
2 changes: 1 addition & 1 deletion examples/with-javascript-cjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
"@codspeed/benchmark.js-plugin": "workspace:*",
"@codspeed/tinybench-plugin": "workspace:*",
"benchmark": "^2.1.4",
"tinybench": "^2.5.0"
"tinybench": "^4.0.1"
}
}
2 changes: 1 addition & 1 deletion examples/with-javascript-esm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
"@codspeed/benchmark.js-plugin": "workspace:*",
"@codspeed/tinybench-plugin": "workspace:*",
"benchmark": "^2.1.4",
"tinybench": "^2.5.0"
"tinybench": "^4.0.1"
}
}
2 changes: 1 addition & 1 deletion examples/with-typescript-cjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"@types/benchmark": "^2.1.2",
"benchmark": "^2.1.4",
"esbuild-register": "^3.4.2",
"tinybench": "^2.5.0",
"tinybench": "^4.0.1",
"typescript": "^5.1.3"
}
}
2 changes: 1 addition & 1 deletion examples/with-typescript-esm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"@types/benchmark": "^2.1.2",
"benchmark": "^2.1.4",
"esbuild-register": "^3.4.2",
"tinybench": "^2.5.0",
"tinybench": "^4.0.1",
"typescript": "^5.1.3",
"vitest": "^1.2.2"
}
Expand Down
2 changes: 1 addition & 1 deletion examples/with-typescript-simple-cjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"@types/benchmark": "^2.1.2",
"benchmark": "^2.1.4",
"esbuild-register": "^3.4.2",
"tinybench": "^2.5.0",
"tinybench": "^4.0.1",
"typescript": "^5.1.3"
}
}
2 changes: 1 addition & 1 deletion examples/with-typescript-simple-esm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"@types/benchmark": "^2.1.2",
"benchmark": "^2.1.4",
"esbuild-register": "^3.4.2",
"tinybench": "^2.5.0",
"tinybench": "^4.0.1",
"typescript": "^5.1.3"
}
}
61 changes: 61 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 53 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
description = "CodSpeed Node development environment";

inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.05";
flake-utils.url = "github:numtide/flake-utils";
};

outputs =
{
self,
nixpkgs,
flake-utils,
}:
flake-utils.lib.eachDefaultSystem (
system:
let
pkgs = import nixpkgs { inherit system; };
commonBuildInputs = with pkgs; [
# Needed for node-gyp
(python314.withPackages (
ps: with ps; [
setuptools
]
))
valgrind # TODO: Remove this in favor of codspeed's valgrind
];

in
{
devShells = {
default = pkgs.mkShell {
buildInputs = commonBuildInputs;
shellHook = ''
echo "CodSpeed Node development environment"
'';
};

lsp = pkgs.mkShell {
buildInputs =
with pkgs;
[
typescript-language-server
]
++ commonBuildInputs;
shellHook = ''
echo "CodSpeed Node development environment with LSP"
'';
};
};
}
);
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"devDependencies": {
"@commitlint/cli": "^17.5.1",
"@commitlint/config-conventional": "^17.4.4",
"@moonrepo/cli": "^1.19.3",
"@moonrepo/cli": "1.37.3",
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-json": "^6.0.1",
"@rollup/plugin-node-resolve": "^15.2.3",
Expand Down Expand Up @@ -33,6 +33,6 @@
},
"packageManager": "[email protected]",
"engines": {
"node": "18.16.0"
"node": "20.5.1"
}
}
21 changes: 21 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,26 @@ export const isBound = native_core.isBound;

export const mongoMeasurement = new MongoMeasurement();

export enum MeasurementMode {
Instrumentation = "instrumentation",
WallTime = "walltime",
}

export function getMeasurementMode(): MeasurementMode {
const isCodSpeedEnabled = process.env.CODSPEED_ENV !== undefined;
if (isCodSpeedEnabled) {
// If CODSPEED_ENV is set, check CODSPEED_RUNNER_MODE
if (process.env.CODSPEED_RUNNER_MODE === "walltime") {
return MeasurementMode.WallTime;
} else {
return MeasurementMode.Instrumentation;
}
}

// Default to walltime mode when CODSPEED_ENV is not set
return MeasurementMode.WallTime;
}

export const setupCore = () => {
native_core.Measurement.stopInstrumentation(
`Metadata: codspeed-node ${__VERSION__}`
Expand All @@ -29,4 +49,5 @@ export type {
export { getV8Flags, tryIntrospect } from "./introspection";
export { optimizeFunction, optimizeFunctionSync } from "./optimization";
export * from "./utils";
export * from "./walltime";
export const Measurement = native_core.Measurement;
Loading