Skip to content

Commit

Permalink
Add TypeScript and some refactoring to perfTest
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie committed Nov 13, 2024
1 parent 601a66c commit 072ba95
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 29 deletions.
6 changes: 4 additions & 2 deletions perfTest/graphile.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// import "graphile-config";
// @ts-check

// import { WorkerProPreset } from "../graphile-pro-worker/dist/index.js";
/** @typedef {import("../dist/index.js")} Worker */
// import type {} from "../src/index.js";

// import { WorkerProPreset } from "../graphile-pro-worker/dist/index.js";

/** @type {GraphileConfig.Preset} */
const preset = {
// extends: [WorkerProPreset],
Expand Down
14 changes: 0 additions & 14 deletions perfTest/graphile.config.ts

This file was deleted.

7 changes: 7 additions & 0 deletions perfTest/latencyTest.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
// @ts-check
const assert = require("assert");
const { Pool } = require("pg");
const { runTaskList } = require("../dist/main");
const { default: deferred } = require("../dist/deferred");
const preset = require("./graphile.config.js");

/** @type {(ms: number) => Promise<void>} */
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

/** @type {import('../dist/index.js').WorkerPoolOptions} */
const options = {
concurrency: 1,
preset,
};

async function main() {
const pgPool = new Pool({ connectionString: process.env.PERF_DATABASE_URL });
const startTimes = {};
let latencies = [];
const deferreds = {};
/** @type {import('../dist/index.js').TaskList} */
const tasks = {
latency: ({ id }) => {
latencies.push(process.hrtime(startTimes[id]));
Expand Down Expand Up @@ -86,6 +92,7 @@ main().catch((e) => {
process.exit(1);
});

/** @type {(pgPool: Pool) => Promise<void>} */
async function forEmptyQueue(pgPool) {
let remaining;
do {
Expand Down
33 changes: 20 additions & 13 deletions perfTest/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,14 @@ async function main() {
console.log();
console.log();
console.log("Timing startup/shutdown time...");
const startupTime = await time(() => {
execSync("node ../dist/cli.js --once", execOptions);
let result;
const startupTime = await time(async () => {
result = await exec(
`node ../dist/cli.js --once -j ${CONCURRENCY} -m ${CONCURRENCY + 1}`,
execOptions,
);
});
logResult(result);
console.log();

if (STUCK_JOB_COUNT > 0) {
Expand Down Expand Up @@ -83,17 +88,7 @@ async function main() {
),
);
}
(await Promise.all(promises)).map(({ error, stdout, stderr }) => {
if (error) {
throw error;
}
if (stdout) {
console.log(stdout);
}
if (stderr) {
console.error(stderr);
}
});
(await Promise.all(promises)).map(logResult);
});
console.log(
`Jobs per second: ${((1000 * JOB_COUNT) / (dur - startupTime)).toFixed(2)}`,
Expand All @@ -112,3 +107,15 @@ main().catch((e) => {
console.error(e);
process.exit(1);
});

function logResult({ error, stdout, stderr }) {
if (error) {
throw error;
}
if (stdout) {
console.log(stdout);
}
if (stderr) {
console.error(stderr);
}
}
10 changes: 10 additions & 0 deletions perfTest/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
declare global {
namespace GraphileWorker {
interface Tasks {
latency: { id: number };
}
}
}

// Has to be a module, so export something
export type Foo = "Foo";

0 comments on commit 072ba95

Please sign in to comment.