Skip to content

Commit

Permalink
chore: use sync-request
Browse files Browse the repository at this point in the history
  • Loading branch information
adriencaccia committed Jan 17, 2024
1 parent af8f5b9 commit cdb8d17
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 50 deletions.
3 changes: 2 additions & 1 deletion packages/benchmark.js-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"dependencies": {
"@codspeed/core": "workspace:^2.3.1",
"lodash": "^4.17.10",
"stack-trace": "1.0.0-pre2"
"stack-trace": "1.0.0-pre2",
"sync-request": "^6.1.0"
},
"peerDependencies": {
"benchmark": "^2.1.0"
Expand Down
15 changes: 9 additions & 6 deletions packages/benchmark.js-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
tryIntrospect,
} from "@codspeed/core";
import Benchmark from "benchmark";
import request from "sync-request";
import buildSuiteAdd from "./buildSuiteAdd";
import getCallingFile from "./getCallingFile";
import { CodSpeedBenchmark } from "./types";
Expand All @@ -15,10 +16,12 @@ declare const __VERSION__: string;

tryIntrospect();

async function doSomeWork() {
for (let i = 0; i < 1000; i++) {
Math.random();
}
function doSomeWorkSync() {
const result = request("GET", "https://google.com");

console.log(`request ended with status code ${result.statusCode}`);

return result;
}
interface WithCodSpeedBenchmark
extends Omit<
Expand Down Expand Up @@ -195,7 +198,7 @@ async function runBenchmarks({
await bench.options.setup();
}

await doSomeWork();
doSomeWorkSync();

if (isAsync) {
await optimizeFunction(benchPayload);
Expand All @@ -213,7 +216,7 @@ async function runBenchmarks({
})();
}

await doSomeWork();
doSomeWorkSync();

if (typeof bench.options.teardown === "function") {
await bench.options.teardown();
Expand Down
3 changes: 2 additions & 1 deletion packages/tinybench-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
},
"dependencies": {
"@codspeed/core": "workspace:^2.3.1",
"stack-trace": "1.0.0-pre2"
"stack-trace": "1.0.0-pre2",
"sync-request": "^6.1.0"
},
"peerDependencies": {
"tinybench": "^2.3.0"
Expand Down
15 changes: 9 additions & 6 deletions packages/tinybench-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@ import {
} from "@codspeed/core";
import path from "path";
import { get as getStackTrace } from "stack-trace";
import request from "sync-request";
import { Bench, Task } from "tinybench";
import { fileURLToPath } from "url";

declare const __VERSION__: string;

tryIntrospect();

async function doSomeWork() {
for (let i = 0; i < 1000; i++) {
Math.random();
}
function doSomeWorkSync() {
const result = request("GET", "https://google.com");

console.log(`request ended with status code ${result.statusCode}`);

return result;
}

type CodSpeedBenchOptions = Task["opts"] & {
Expand Down Expand Up @@ -62,7 +65,7 @@ export function withCodSpeed(bench: Bench): Bench {

await task.opts.beforeAll?.call(task);

await doSomeWork();
doSomeWorkSync();

// run optimizations
await optimizeFunction(async () => {
Expand All @@ -71,7 +74,7 @@ export function withCodSpeed(bench: Bench): Bench {
await task.opts.afterEach?.call(task);
});

await doSomeWork();
doSomeWorkSync();

// run instrumented benchmark
await task.opts.beforeEach?.call(task);
Expand Down
3 changes: 2 additions & 1 deletion packages/vitest-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"bench": "vitest bench"
},
"dependencies": {
"@codspeed/core": "workspace:^2.3.1"
"@codspeed/core": "workspace:^2.3.1",
"sync-request": "^6.1.0"
},
"peerDependencies": {
"vite": "^4.2.0 || ^5.0.0",
Expand Down
17 changes: 11 additions & 6 deletions packages/vitest-plugin/src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ import {
teardownCore,
} from "@codspeed/core";
import path from "path";
import request from "sync-request";
import { Benchmark, Suite } from "vitest";
import { NodeBenchmarkRunner } from "vitest/runners";
import { getBenchFn } from "vitest/suite";

async function doSomeWork() {
for (let i = 0; i < 1000; i++) {
Math.random();
}
function doSomeWorkSync() {
const result = request("GET", "https://google.com");

console.log(`request ended with status code ${result.statusCode}`);

return result;
}

const currentFileName =
typeof __filename === "string"
? __filename
Expand Down Expand Up @@ -53,14 +57,15 @@ async function runBenchmarkSuite(suite: Suite, parentSuiteName?: string) {
const fn = getBenchFn(benchmark);

await optimizeFunction(fn);
await doSomeWork();
console.log(`[CodSpeed] ${uri} running`);
doSomeWorkSync();
await (async function __codspeed_root_frame__() {
Measurement.startInstrumentation();
// @ts-expect-error we do not need to bind the function to an instance of tinybench's Bench
await fn();
Measurement.stopInstrumentation(uri);
})();
await doSomeWork();
doSomeWorkSync();

logCodSpeed(`${uri} done`);
}
Expand Down
Loading

0 comments on commit cdb8d17

Please sign in to comment.