Skip to content

Commit

Permalink
Merge pull request #138 from samchon/v3.0
Browse files Browse the repository at this point in the history
Close #135, Fix #136 and Close #137
  • Loading branch information
samchon authored Jul 4, 2022
2 parents 0317a90 + e420b8d commit 0dc17c0
Show file tree
Hide file tree
Showing 14 changed files with 555 additions and 183 deletions.
59 changes: 48 additions & 11 deletions benchmark/index.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
// import { NumberUtil } from "../test/internal/NumberUtil";
import os from "os";

import { benchmark_assert } from "./features/benchmark_assert";
import { benchmark_is } from "./features/benchmark_is";
import { benchmark_optimizer } from "./features/benchmark_optimizer";
import { benchmark_stringify } from "./features/benchmark_stringify";
import { WriteStream } from "./internal/WriteStream";

type Output = Record<string, number | null> & { name: string };

// const round = NumberUtil.elaborate(4)(Math.round);
function measure<T extends Output>(functor: () => (() => T)[]): void {
console.log(`## ${functor.name}`);
async function measure<T extends Output>(
stream: WriteStream,
functor: () => (() => T)[],
): Promise<void> {
await stream.write(`## ${functor.name}`);
console.log(" - " + functor.name);

const parameters: string[] = [];
const outputList: T[] = [];

for (const comp of functor()) {
// DO BENCHMARK
const output = comp();
outputList.push(output);
console.log(" - " + output.name);

// CONSTRUCT LABEL WITH PROPERTIES
const labeled: boolean = parameters.length !== 0;
if (labeled === false) {
parameters.push(
...Object.keys(output).filter((key) => key !== "name"),
);
console.log(" Components | " + parameters.join(" | ") + " ");
console.log(
await stream.write(" Components | " + parameters.join(" | ") + " ");
await stream.write(
"-".repeat(12) +
"|" +
parameters
Expand All @@ -34,7 +42,7 @@ function measure<T extends Output>(functor: () => (() => T)[]): void {
}

// REPORT
console.log(
await stream.write(
[
output.name +
" | " +
Expand All @@ -44,16 +52,45 @@ function measure<T extends Output>(functor: () => (() => T)[]): void {
].join(" | "),
);
}
console.log("\n\n");
await stream.write("\n");

// DRAW GRAPHS
for (const output of outputList) {
await stream.write("```mermaid");
await stream.write(`pie title ${functor.name} - ${output.name}`);

for (const [key, value] of Object.entries(output)) {
if (key === "name") continue;
await stream.write(` "${key}": ${value || 0}`);
}
await stream.write("```");
await stream.write("\n");
}

// TERMINATE
await stream.write("\n\n\n");
}

function main(): void {
const features = [
benchmark_is,
async function main(): Promise<void> {
const cpu: string = os.cpus()[0].model;
const memory: number = os.totalmem();

console.log(`Benchmark ${cpu}`);

const stream = new WriteStream(`${__dirname}/results/${cpu}.md`);
const functors = [
benchmark_assert,
benchmark_is,
benchmark_optimizer,
benchmark_stringify,
];
for (const f of features) measure(f as any);

await stream.write("# Benchmark of `typescript-json`");
await stream.write(`> CPU: ${cpu}`);
await stream.write(
`> Memory: ${Math.round(memory / 1024 / 1024).toLocaleString()} MB`,
);
await stream.write("\n");
for (const func of functors) await measure(stream, func as any);
}
main();
39 changes: 39 additions & 0 deletions benchmark/internal/WriteStream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import fs from "fs";

export class WriteStream {
private readonly stream_: fs.WriteStream;

public constructor(path: string) {
this.stream_ = fs.createWriteStream(path, "utf8");
}

public write(data: string): Promise<void> {
return new Promise((resolve, reject) => {
this.stream_.write(data + "\n", "utf8", (error) => {
if (error) reject(error);
else resolve();
});
});
}

public async close(): Promise<void> {
await this._End();
await this._Close();
}

private _End(): Promise<void> {
return new Promise((resolve) => {
this.stream_.on("finish", resolve);
this.stream_.end();
});
}

private _Close(): Promise<void> {
return new Promise((resolve, reject) => {
this.stream_.close((error) => {
if (error) reject(error);
else resolve();
});
});
}
}
Loading

0 comments on commit 0dc17c0

Please sign in to comment.