From 0a900949c83503c2af75e8f1ea9bbbc6e947887d Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Sat, 4 Jan 2020 11:21:06 +0100 Subject: [PATCH] Added textencoder benchmark (#3589) --- cli/tests/text_encoder_perf.js | 33 +++++++++++++++++++++++++++++++++ tools/benchmark.py | 1 + 2 files changed, 34 insertions(+) create mode 100644 cli/tests/text_encoder_perf.js diff --git a/cli/tests/text_encoder_perf.js b/cli/tests/text_encoder_perf.js new file mode 100644 index 0000000000..6f61f019ef --- /dev/null +++ b/cli/tests/text_encoder_perf.js @@ -0,0 +1,33 @@ +const mixed = "@ฤ€เน๐Ÿ˜€"; + +function generateRandom(bytes) { + let result = ""; + let i = 0; + while (i < bytes) { + const toAdd = Math.floor(Math.random() * Math.min(4, bytes - i)); + switch (toAdd) { + case 0: + result += mixed[0]; + i++; + break; + case 1: + result += mixed[1]; + i++; + break; + case 2: + result += mixed[2]; + i++; + break; + case 3: + result += mixed[3]; + result += mixed[4]; + i += 2; + break; + } + } + return result; +} + +const randomData = generateRandom(1024); +const encoder = new TextEncoder(); +for (let i = 0; i < 10_000; i++) encoder.encode(randomData); diff --git a/tools/benchmark.py b/tools/benchmark.py index e36541bb9a..d665069584 100755 --- a/tools/benchmark.py +++ b/tools/benchmark.py @@ -28,6 +28,7 @@ exec_time_benchmarks = [ ("workers_startup", ["tests/workers_startup_bench.ts"]), ("workers_round_robin", ["tests/workers_round_robin_bench.ts"]), ("text_decoder", ["cli/tests/text_decoder_perf.js"]), + ("text_encoder", ["cli/tests/text_encoder_perf.js"]), ]