summaryrefslogtreecommitdiff
path: root/cli/bench/stdio
diff options
context:
space:
mode:
authorDivy Srivastava <dj.srivastava23@gmail.com>2022-12-01 22:05:18 -0800
committerGitHub <noreply@github.com>2022-12-02 11:35:18 +0530
commit6982c74e11ec8294ed5bd53d2a9d316d7e20e7d2 (patch)
tree8bc1447379c010914c84f793ad7cef43a232a7e8 /cli/bench/stdio
parent9b2b8df927ac23cfa99016a684179f2a3198ba2e (diff)
perf: use fast api for io read/write sync (#15863)
``` $ dd if=/dev/zero bs=65536 count=500000 | ./stdio # C baseline 500000+0 records in 500000+0 records out 32768000000 bytes transferred in 4.126087 secs (7941664827 bytes/sec) c: size 32768000000 reads 500000 blocksize 65536 ``` ``` $ dd if=/dev/zero bs=65536 count=500000 | deno run stdio.js # Deno 500000+0 records in 500000+0 records out 32768000000 bytes transferred in 4.279032 secs (7657806719 bytes/sec) deno: size 32768000000 reads 500000 blocksize 65536 ```
Diffstat (limited to 'cli/bench/stdio')
-rw-r--r--cli/bench/stdio/README.md9
-rw-r--r--cli/bench/stdio/stdio.c29
-rw-r--r--cli/bench/stdio/stdio.js16
3 files changed, 54 insertions, 0 deletions
diff --git a/cli/bench/stdio/README.md b/cli/bench/stdio/README.md
new file mode 100644
index 000000000..b2d4875ac
--- /dev/null
+++ b/cli/bench/stdio/README.md
@@ -0,0 +1,9 @@
+## `stdio` benchmarks
+
+Compile the C baseline and run the benchmark:
+
+```bash
+cc stdio.c -o stdio -O3
+time dd if=/dev/zero bs=65536 count=500000 | ./stdio
+time dd if=/dev/zero bs=65536 count=500000 | deno run stdio.js
+```
diff --git a/cli/bench/stdio/stdio.c b/cli/bench/stdio/stdio.c
new file mode 100644
index 000000000..8125a0d00
--- /dev/null
+++ b/cli/bench/stdio/stdio.c
@@ -0,0 +1,29 @@
+// From https://github.com/just-js/benchmarks/tree/main/01-stdio
+// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <errno.h>
+#include <string.h>
+
+int main(int argc, char *argv[]) {
+ unsigned int blocksize = 65536;
+ if (argc == 2) {
+ blocksize = atoi(argv[1]);
+ }
+ char buf[blocksize];
+ unsigned long size = 0;
+ unsigned int reads = 0;
+ int n = read(STDIN_FILENO, buf, blocksize);
+ while (n > 0) {
+ reads++;
+ size += n;
+ n = read(STDIN_FILENO, buf, blocksize);
+ }
+ if (n < 0) {
+ fprintf(stderr, "read: %s (%i)\n", strerror(errno), errno);
+ exit(1);
+ }
+ fprintf(stdout, "size %lu reads %u blocksize %u\n", size, reads, blocksize);
+} \ No newline at end of file
diff --git a/cli/bench/stdio/stdio.js b/cli/bench/stdio/stdio.js
new file mode 100644
index 000000000..0663d6fd7
--- /dev/null
+++ b/cli/bench/stdio/stdio.js
@@ -0,0 +1,16 @@
+// From https://github.com/just-js/benchmarks/tree/main/01-stdio
+// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
+
+const blocksize = parseInt(Deno.args[0] || 65536);
+const buf = new Uint8Array(blocksize);
+let size = 0;
+let reads = 0;
+
+let n = Deno.stdin.readSync(buf);
+while (n > 0) {
+ reads++;
+ size += n;
+ n = Deno.stdin.readSync(buf);
+}
+if (n < 0) throw new Error("Bad Read");
+console.log(`size ${size} reads ${reads} blocksize ${blocksize}`);