Skip to content

Commit

Permalink
Rewrite the build script and match it with the latest Zig release
Browse files Browse the repository at this point in the history
  • Loading branch information
devraymondsh committed Jan 7, 2024
1 parent 1496956 commit 752b1ec
Show file tree
Hide file tree
Showing 6 changed files with 244 additions and 288 deletions.
15 changes: 8 additions & 7 deletions bench/bench-with-builtin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ import json from "big-json";
import { fileURLToPath } from "node:url";
import { Kivi } from "../src/drivers/js/index.js";
import { isBun } from "../src/drivers/js/runtime.js";
import { generateFakeData } from "./faker/generate.js";

const repeatBenchmark = 2;
const fakeDataJsonFile = "faker/data/data.json";
const dataJsonPath = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
fakeDataJsonFile
);

const assert = (name, left, right) => {
if (left !== right) {
Expand Down Expand Up @@ -116,15 +121,11 @@ const logRatio = () => {
};

let data;
const dataJsonPath = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
fakeDataJsonFile
);
if (!fs.existsSync(dataJsonPath)) {
await generateFakeData();
}
console.log("Loading the data. Please be patient...");
if (!isBun()) {
if (!fs.existsSync(dataJsonPath)) {
throw new Error(`Failed to read the '${fakeDataJsonFile}' file.`);
}
const readStream = fs.createReadStream(dataJsonPath);
const parseStream = json.createParseStream();
readStream.pipe(parseStream);
Expand Down
63 changes: 31 additions & 32 deletions bench/faker/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,37 @@ import { faker } from "@faker-js/faker";

const count = 1_000_000;

const getRandomInt = (min, max) => {
min = Math.ceil(min);
max = Math.floor(max);
export const generateFakeData = () =>
new Promise((resolve) => {
console.log("Generating fake data for the benchmark...");

return Math.floor(Math.random() * (max - min) + min);
};
const arr = [];
for (let i = 0; i < count; i++) {
arr.push({
key: faker.string.uuid() + "_" + faker.person.fullName(),
value: JSON.stringify({
bio: faker.person.bio(),
gender: faker.person.gender(),
jobArea: faker.person.jobArea(),
jobTitle: faker.person.jobTitle(),
jobType: faker.person.jobType(),
name: faker.person.fullName(),
}),
});
if (i % (count / 10) == 0 && i != 0) {
console.log((i / count) * 100 + "%");
}
}

const arr = [];
for (let i = 0; i < count; i++) {
arr.push({
key: faker.string.uuid() + "_" + faker.person.fullName(),
value: JSON.stringify({
bio: faker.person.bio(),
gender: faker.person.gender(),
jobArea: faker.person.jobArea(),
jobTitle: faker.person.jobTitle(),
jobType: faker.person.jobType(),
name: faker.person.fullName(),
}),
const dataJsonPath = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
"data/data.json"
);
console.log("Writing the data. Please be patient.");
const writeStream = fs.createWriteStream(dataJsonPath);
const stringifyStream = json.createStringifyStream({
body: arr,
});
const pipe = stringifyStream.pipe(writeStream);
pipe.on("finish", () => resolve());
});
if (i % (count / 10) == 0 && i != 0) {
console.log((i / count) * 100 + "%");
}
}

const dataJsonPath = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
"data/data.json"
);
console.log("Writing the data. Please be patient.");
const writeStream = fs.createWriteStream(dataJsonPath);
const stringifyStream = json.createStringifyStream({
body: arr,
});
stringifyStream.pipe(writeStream);
1 change: 0 additions & 1 deletion bench/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"scripts": {
"nodejs-bench": "node bench-with-builtin.js",
"bun-bench": " bun run bench-with-builtin.js",
"generate-fake-data": "node faker/generate.js",
"deno-bench": "deno run --unstable --allow-all --v8-flags='--max-heap-size=2000,--max-old-space-size=2000' bench-with-builtin.js"
},
"author": "",
Expand Down
Loading

0 comments on commit 752b1ec

Please sign in to comment.