|
| 1 | +#!ts-node |
| 2 | +/* eslint-disable no-console */ |
| 3 | + |
| 4 | +import { encode, decode, decodeAsync } from "../src"; |
| 5 | +import { writeFileSync, unlinkSync, readFileSync, createReadStream } from "fs"; |
| 6 | +import { deepStrictEqual } from "assert"; |
| 7 | + |
| 8 | +(async () => { |
| 9 | + const data = []; |
| 10 | + for (let i = 0; i < 1000; i++) { |
| 11 | + const id = i + 1; |
| 12 | + data.push({ |
| 13 | + id, |
| 14 | + score: Math.round(Math.random() * Number.MAX_SAFE_INTEGER), |
| 15 | + title: `Hello, world! #${id}`, |
| 16 | + content: `blah blah blah `.repeat(20).trim(), |
| 17 | + createdAt: new Date(), |
| 18 | + }); |
| 19 | + } |
| 20 | + const encoded = encode(data); |
| 21 | + const file = "benchmark/tmp.msgpack"; |
| 22 | + writeFileSync(file, encoded); |
| 23 | + process.on("exit", () => unlinkSync(file)); |
| 24 | + console.log(`encoded size ${Math.round(encoded.byteLength / 1024)}KiB`); |
| 25 | + |
| 26 | + deepStrictEqual(decode(readFileSync(file)), data); |
| 27 | + deepStrictEqual(await decodeAsync(createReadStream(file)), data); |
| 28 | + |
| 29 | + // sync |
| 30 | + console.time("readFileSync |> decode"); |
| 31 | + for (let i = 0; i < 100; i++) { |
| 32 | + decode(readFileSync(file)); |
| 33 | + } |
| 34 | + console.timeEnd("readFileSync |> decode"); |
| 35 | + |
| 36 | + // async |
| 37 | + console.time("creteReadStream |> decodeAsync"); |
| 38 | + for (let i = 0; i < 100; i++) { |
| 39 | + await decodeAsync(createReadStream(file)); |
| 40 | + } |
| 41 | + console.timeEnd("creteReadStream |> decodeAsync"); |
| 42 | +})(); |
0 commit comments