Skip to content

Commit

Permalink
refactor: readability attempted noise reduction
Browse files Browse the repository at this point in the history
  • Loading branch information
NickAccesSOS committed Mar 18, 2024
1 parent 71b99fa commit 0ae5b6c
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 24 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ run.setOptions({

test("Iterations Executor Test @iterations-executor", async ({ request }) => {
await run.go(async () => {
const response = await request.get("/public/crocodiles/");
const response = await request.get("https://yesno.wtf/api");
expect(response.ok()).toBeTruthy();
});
});
Expand All @@ -71,7 +71,7 @@ run.setOptions({

test("Duration Executor Test @duration-executor", async ({ request }) => {
await run.go(async () => {
const response = await request.get("/public/crocodiles/");
const response = await request.get("https://yesno.wtf/api");
expect(response.ok()).toBeTruthy();
});
});
Expand All @@ -94,7 +94,7 @@ test("Iterations per Second Executor Test @iterations-per-second-executor", asyn
request,
}) => {
await run.go(async () => {
const response = await request.get("/public/crocodiles/");
const response = await request.get("https://yesno.wtf/api");
expect(response.ok()).toBeTruthy();
});
});
Expand Down
44 changes: 27 additions & 17 deletions src/runner.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { Options } from "./types";

const write = process.stdout.write;

const timeout = setTimeout;

const sleep = async (duration: number) => {
await new Promise((resolve) => setTimeout(resolve, duration));
};

export default class Runner {
private options: Options;
private resolved: any[] = [];
Expand All @@ -19,46 +27,48 @@ export default class Runner {
this.resolved.push(await func());
}

private async makeSequentialRequests(func: Function) {
await this.makeSyncronousRequests(func);
}

private async makeConcurrentRequests(func: Function) {
this.promises.push(
new Promise((resolve) => {
return resolve(func());
}),
})
);
}

private async iterationsExecutor(func: Function) {
for (let i = 0; i < this.options.iterations!; i++) {
await this.makeSequentialRequests(func);
process.stdout.write("|");
await this.makeSyncronousRequests(func);
write("|");
}
}

private async durationExecutor(func: Function) {
let end = performance.now() + this.options.duration! * 1000;
while (performance.now() < end) {
await this.makeSequentialRequests(func);
process.stdout.write("|");
let go = true;
timeout(() => {
go = false;
}, this.options.duration! * 1000);
while (go) {
await this.makeSyncronousRequests(func);
write("|");
}
}

private async iterationsPerSecondExecutor(func: Function) {
let end = performance.now() + this.options.duration! * 1000;
while (performance.now() < end) {
let go = true;
timeout(() => {
go = false;
}, this.options.duration! * 1000);
while (go) {
for (let i = 0; i < this.options.ips!; i++) {
this.makeConcurrentRequests(func);
}
await new Promise((resolve) => setTimeout(resolve, 1000));
process.stdout.write("|");
await sleep(1000);
write("|");
}
}

async go(func: Function) {
process.stdout.write("\nRUNNING:");
write("\nRUNNING:");
switch (this.options.executor !== null) {
case this.options.executor === "iterations": {
await this.iterationsExecutor(func);
Expand All @@ -74,6 +84,6 @@ export default class Runner {
}
}
await this.resolvePromises();
process.stdout.write("...COMPLETE!");
write("...COMPLETE!");
}
}
2 changes: 1 addition & 1 deletion tests/duration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ run.setOptions({

test("Duration Executor Test @duration-executor", async ({ request }) => {
await run.go(async () => {
const response = await request.get("/public/crocodiles/");
const response = await request.get("/api");
expect(response.ok()).toBeTruthy();
});
});
2 changes: 1 addition & 1 deletion tests/iterations.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ run.setOptions({

test("Iterations Executor Test @iterations-executor", async ({ request }) => {
await run.go(async () => {
const response = await request.get("/public/crocodiles/");
const response = await request.get("/api");
expect(response.ok()).toBeTruthy();
});
});
2 changes: 1 addition & 1 deletion tests/iterationsPerSecond.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ test("Iterations per Second Executor Test @iterations-per-second-executor", asyn
request,
}) => {
await run.go(async () => {
const response = await request.get("/public/crocodiles/");
const response = await request.get("/api");
expect(response.ok()).toBeTruthy();
});
});
2 changes: 1 addition & 1 deletion tests/playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import { defineConfig } from "@playwright/test";
export default defineConfig({
use: {
// All requests we send go to this API endpoint.
baseURL: "https://test-api.k6.io",
baseURL: "https://yesno.wtf",
},
});

0 comments on commit 0ae5b6c

Please sign in to comment.