Skip to content

Commit

Permalink
bench server
Browse files Browse the repository at this point in the history
  • Loading branch information
enisdenjo committed May 8, 2024
1 parent 536c749 commit d9b4fa7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
16 changes: 9 additions & 7 deletions e2e/sqlite-chinook/sqlite-chinook.loadtest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ beforeAll(async () => {
const threshold: TbenchResult = {
maxCpu: Infinity, // we dont care
maxMem: 500, // MB
slowestRequest: 0.5, // seconds
slowestRequest: 1, // second
};

it(`should perform within threshold ${JSON.stringify(threshold)}`, async () => {
const { output } = await compose({ output: 'graphql' });

const { maxCpu, maxMem, slowestRequest } = await tbench.serveSustain({
serve: await serve({ fusiongraph: output }),
const server = await serve({ fusiongraph: output });
const result = await tbench.sustain({
server,
params: {
query: /* GraphQL */ `
query Albums {
Expand All @@ -38,7 +38,9 @@ it(`should perform within threshold ${JSON.stringify(threshold)}`, async () => {
},
});

expect(maxCpu).toBeLessThan(threshold.maxCpu);
expect(maxMem).toBeLessThan(threshold.maxMem);
expect(slowestRequest).toBeLessThan(threshold.slowestRequest);
console.debug(result);

expect(result.maxCpu).toBeLessThan(threshold.maxCpu);
expect(result.maxMem).toBeLessThan(threshold.maxMem);
expect(result.slowestRequest).toBeLessThan(threshold.slowestRequest);
});
21 changes: 8 additions & 13 deletions e2e/utils/tbench.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { setTimeout } from 'timers/promises';
import { spawn, Thread, Worker } from 'threads';
import { timeout as jestTimeout, Serve } from './tenv';
import { timeout as jestTimeout, Server } from './tenv';
import type { benchGraphQLServer } from './workers/benchGraphQLServer';

const leftovers = new Set<Thread>();
Expand All @@ -12,9 +12,9 @@ afterAll(async () => {
});
});

export interface ServeSustainOptions {
/** The serve process to benchmark. */
serve: Serve;
export interface TbenchSustainOptions {
/** The server process to benchmark. */
server: Server;
/**
* How long should the benchmark run for.
* @default jest.timeout - 10 seconds
Expand Down Expand Up @@ -46,7 +46,7 @@ export interface TbenchResult {
}

export interface Tbench {
serveSustain(opts: ServeSustainOptions): Promise<TbenchResult>;
sustain(opts: TbenchSustainOptions): Promise<TbenchResult>;
}

/**
Expand All @@ -60,18 +60,13 @@ export async function createTbench(vusCount: number): Promise<Tbench> {
);
vus.forEach(worker => leftovers.add(worker));
return {
async serveSustain({
serve,
duration = jestTimeout - 10_000,
parallelRequestsPerVU = 10,
params,
}) {
async sustain({ server, duration = jestTimeout - 10_000, parallelRequestsPerVU = 10, params }) {
let maxCpu = 0;
let maxMem = 0;
const signal = AbortSignal.timeout(duration);
(async () => {
while (!signal.aborted) {
const { cpu, mem } = await serve.getStats();
const { cpu, mem } = await server.getStats();
if (maxCpu < cpu) {
maxCpu = cpu;
}
Expand All @@ -85,7 +80,7 @@ export async function createTbench(vusCount: number): Promise<Tbench> {
let slowestRequest = 0;
for (const slowestRequestInVU of await Promise.all(
vus.map(benchGraphQLServer =>
benchGraphQLServer(serve.port, duration, parallelRequestsPerVU, params),
benchGraphQLServer(server.port, duration, parallelRequestsPerVU, params),
),
)) {
if (slowestRequestInVU > slowestRequest) {
Expand Down

0 comments on commit d9b4fa7

Please sign in to comment.