summaryrefslogtreecommitdiff
path: root/cli/bench/fs/run.mjs
blob: 94240f20d4d1fea46e7be00f58c34351106f6c2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

let total = 5;
let current = "";
const values = {};
const runtime = typeof Deno !== "undefined" ? "deno" : "node";

function bench(fun, count = 100000) {
  if (total === 5) console.log(fun.toString());
  const start = Date.now();
  for (let i = 0; i < count; i++) fun();
  const elapsed = Date.now() - start;
  const rate = Math.floor(count / (elapsed / 1000));
  console.log(`time ${elapsed} ms rate ${rate}`);
  values[current] = values[current] || [];
  values[current].push(rate);
  if (--total) bench(fun, count);
  else total = 5;
}

let fs;
if (runtime === "node") {
  fs = await import("fs");
}

const getFunction = runtime === "deno"
  ? (name) => {
    current = name;
    return Deno[name];
  }
  : (name) => {
    current = name;
    return fs[name];
  };

const writeFileSync = getFunction("writeFileSync");
writeFileSync("test", new Uint8Array(1024 * 1024), { truncate: true });

const copyFileSync = getFunction("copyFileSync");
bench(() => copyFileSync("test", "test2"), 10000);

const truncateSync = getFunction("truncateSync");
bench(() => truncateSync("test", 0));

const lstatSync = getFunction("lstatSync");
bench(() => lstatSync("test"));

const { uid, gid } = lstatSync("test");

const chownSync = getFunction("chownSync");
bench(() => chownSync("test", uid, gid));

const chmodSync = getFunction("chmodSync");
bench(() => chmodSync("test", 0o666));

// const cwd = getFunction("cwd");
// bench(() => cwd());

// const chdir = getFunction("chdir");
// bench(() => chdir("/"));

const readFileSync = getFunction("readFileSync");
writeFileSync("test", new Uint8Array(1024), { truncate: true });
bench(() => readFileSync("test"));

writeFileSync(
  new URL(`./${runtime}.json`, import.meta.url),
  new TextEncoder().encode(JSON.stringify(values, null, 2)),
  { truncate: true },
);