From aacdaf13d1333217cd9940d3a8eba19d71f47189 Mon Sep 17 00:00:00 2001 From: Sami Vaarala Date: Fri, 27 Nov 2015 14:31:45 +0200 Subject: [PATCH] Perf tests for base64 --- tests/perf/test-base64-decode-whitespace.js | 39 +++++++++++++++++++++ tests/perf/test-base64-decode-whitespace.py | 29 +++++++++++++++ tests/perf/test-base64-decode.js | 32 +++++++++++++++++ tests/perf/test-base64-decode.py | 22 ++++++++++++ tests/perf/test-base64-encode.js | 31 ++++++++++++++++ tests/perf/test-base64-encode.py | 21 +++++++++++ tests/perf/test-hex-encode.js | 4 +-- 7 files changed, 176 insertions(+), 2 deletions(-) create mode 100644 tests/perf/test-base64-decode-whitespace.js create mode 100644 tests/perf/test-base64-decode-whitespace.py create mode 100644 tests/perf/test-base64-decode.js create mode 100644 tests/perf/test-base64-decode.py create mode 100644 tests/perf/test-base64-encode.js create mode 100644 tests/perf/test-base64-encode.py diff --git a/tests/perf/test-base64-decode-whitespace.js b/tests/perf/test-base64-decode-whitespace.js new file mode 100644 index 00000000..a6a49330 --- /dev/null +++ b/tests/perf/test-base64-decode-whitespace.js @@ -0,0 +1,39 @@ +if (typeof print !== 'function') { print = console.log; } + +function test() { + var tmp1 = []; + var tmp2 = []; + var tmp3 = []; + var i, n, buf; + + print('build'); + buf = Duktape.Buffer(1024); + for (i = 0; i < 1024; i++) { + buf[i] = Math.random() * 256; + } + tmp1 = String(buf); + for (i = 0; i < 1024; i++) { + tmp2.push(tmp1); + } + tmp2 = tmp2.join(''); + tmp2 = Duktape.enc('base64', tmp2); + + // add newlines, intentionally not a multiple of 4 + for (i = 0; i < tmp2.length; i += 77) { + tmp3.push(tmp2.substring(i, i + 77)); + } + tmp2 = tmp3.join('\n') + '\n'; + + print(tmp2.length); + print('run'); + for (i = 0; i < 1000; i++) { + Duktape.dec('base64', tmp2); + } +} + +try { + test(); +} catch (e) { + print(e.stack || e); + throw e; +} diff --git a/tests/perf/test-base64-decode-whitespace.py b/tests/perf/test-base64-decode-whitespace.py new file mode 100644 index 00000000..fe7d9d8e --- /dev/null +++ b/tests/perf/test-base64-decode-whitespace.py @@ -0,0 +1,29 @@ +import math +import random + +def test(): + tmp1 = [] + tmp2 = [] + + print('build') + for i in xrange(1024): + tmp1.append('%x' % math.floor(random.random() * 16)) + tmp1 = ''.join(tmp1) + for i in xrange(1024): + tmp2.append(tmp1) + tmp2 = ''.join(tmp2) + tmp2 = tmp2.encode('base64') + + tmp3 = [] + i = 0 + while i < len(tmp2): + tmp3.append(tmp2[i:i+77]) + i += 77 + tmp2 = '\n'.join(tmp3) + '\n' + + print(len(tmp2)) + print('run') + for i in xrange(1000): + ign = tmp2.decode('base64') + +test() diff --git a/tests/perf/test-base64-decode.js b/tests/perf/test-base64-decode.js new file mode 100644 index 00000000..06a6a342 --- /dev/null +++ b/tests/perf/test-base64-decode.js @@ -0,0 +1,32 @@ +if (typeof print !== 'function') { print = console.log; } + +function test() { + var tmp1 = []; + var tmp2 = []; + var i, n, buf; + + print('build'); + buf = Duktape.Buffer(1024); + for (i = 0; i < 1024; i++) { + buf[i] = Math.random() * 256; + } + tmp1 = String(buf); + for (i = 0; i < 1024; i++) { + tmp2.push(tmp1); + } + tmp2 = tmp2.join(''); + tmp2 = Duktape.enc('base64', tmp2); + + print(tmp2.length); + print('run'); + for (i = 0; i < 1000; i++) { + Duktape.dec('base64', tmp2); + } +} + +try { + test(); +} catch (e) { + print(e.stack || e); + throw e; +} diff --git a/tests/perf/test-base64-decode.py b/tests/perf/test-base64-decode.py new file mode 100644 index 00000000..b90707ca --- /dev/null +++ b/tests/perf/test-base64-decode.py @@ -0,0 +1,22 @@ +import math +import random + +def test(): + tmp1 = [] + tmp2 = [] + + print('build') + for i in xrange(1024): + tmp1.append('%x' % math.floor(random.random() * 16)) + tmp1 = ''.join(tmp1) + for i in xrange(1024): + tmp2.append(tmp1) + tmp2 = ''.join(tmp2) + tmp2 = tmp2.encode('base64') + + print(len(tmp2)) + print('run') + for i in xrange(1000): + ign = tmp2.decode('base64') + +test() diff --git a/tests/perf/test-base64-encode.js b/tests/perf/test-base64-encode.js new file mode 100644 index 00000000..be743318 --- /dev/null +++ b/tests/perf/test-base64-encode.js @@ -0,0 +1,31 @@ +if (typeof print !== 'function') { print = console.log; } + +function test() { + var tmp1 = []; + var tmp2 = []; + var i, n, buf; + + print('build'); + buf = Duktape.Buffer(1024); + for (i = 0; i < 1024; i++) { + buf[i] = Math.random() * 256; + } + tmp1 = String(buf); + for (i = 0; i < 1024; i++) { + tmp2.push(tmp1); + } + tmp2 = Duktape.Buffer(tmp2.join('')); + + print(tmp2.length); + print('run'); + for (i = 0; i < 1000; i++) { + Duktape.enc('base64', tmp2); + } +} + +try { + test(); +} catch (e) { + print(e.stack || e); + throw e; +} diff --git a/tests/perf/test-base64-encode.py b/tests/perf/test-base64-encode.py new file mode 100644 index 00000000..170d01a1 --- /dev/null +++ b/tests/perf/test-base64-encode.py @@ -0,0 +1,21 @@ +import math +import random + +def test(): + tmp1 = [] + tmp2 = [] + + print('build') + for i in xrange(1024): + tmp1.append(chr(int(math.floor(random.random() * 256)))) + tmp1 = ''.join(tmp1) + for i in xrange(1024): + tmp2.append(tmp1) + tmp2 = ''.join(tmp2) + + print(len(tmp2)) + print('run') + for i in xrange(1000): + ign = tmp2.encode('base64') + +test() diff --git a/tests/perf/test-hex-encode.js b/tests/perf/test-hex-encode.js index b315383d..d38cdb61 100644 --- a/tests/perf/test-hex-encode.js +++ b/tests/perf/test-hex-encode.js @@ -8,13 +8,13 @@ function test() { print('build'); buf = Duktape.Buffer(1024); for (i = 0; i < 1024; i++) { - buf[0] = Math.random() * 256; + buf[i] = Math.random() * 256; } tmp1 = String(buf); for (i = 0; i < 1024; i++) { tmp2.push(tmp1); } - tmp2 = tmp2.join(''); + tmp2 = Duktape.Buffer(tmp2.join('')); print(tmp2.length); print('run');