2016-02-20 02:03:16 +01:00
|
|
|
'use strict';
|
2016-05-15 20:53:08 +02:00
|
|
|
const common = require('../common.js');
|
|
|
|
const StringDecoder = require('string_decoder').StringDecoder;
|
2015-03-19 22:31:34 +01:00
|
|
|
|
2016-05-15 20:53:08 +02:00
|
|
|
const bench = common.createBenchmark(main, {
|
|
|
|
encoding: ['ascii', 'utf8', 'base64-utf8', 'base64-ascii', 'utf16le'],
|
|
|
|
inlen: [32, 128, 1024, 4096],
|
2015-03-19 22:31:34 +01:00
|
|
|
chunk: [16, 64, 256, 1024],
|
2016-05-15 20:53:08 +02:00
|
|
|
n: [25e5]
|
2015-03-19 22:31:34 +01:00
|
|
|
});
|
|
|
|
|
2016-05-15 20:53:08 +02:00
|
|
|
const UTF8_ALPHA = 'Blåbærsyltetøy';
|
|
|
|
const ASC_ALPHA = 'Blueberry jam';
|
|
|
|
const UTF16_BUF = Buffer.from('Blåbærsyltetøy', 'utf16le');
|
2015-03-19 22:31:34 +01:00
|
|
|
|
|
|
|
function main(conf) {
|
2016-05-15 20:53:08 +02:00
|
|
|
const encoding = conf.encoding;
|
|
|
|
const inLen = conf.inlen | 0;
|
|
|
|
const chunkLen = conf.chunk | 0;
|
|
|
|
const n = conf.n | 0;
|
2015-03-19 22:31:34 +01:00
|
|
|
|
|
|
|
var alpha;
|
2016-05-15 20:53:08 +02:00
|
|
|
var buf;
|
|
|
|
const chunks = [];
|
2015-03-19 22:31:34 +01:00
|
|
|
var str = '';
|
2016-05-15 20:53:08 +02:00
|
|
|
const isBase64 = (encoding === 'base64-ascii' || encoding === 'base64-utf8');
|
2016-02-28 06:56:18 +01:00
|
|
|
var i;
|
2015-03-19 22:31:34 +01:00
|
|
|
|
|
|
|
if (encoding === 'ascii' || encoding === 'base64-ascii')
|
|
|
|
alpha = ASC_ALPHA;
|
|
|
|
else if (encoding === 'utf8' || encoding === 'base64-utf8')
|
2016-05-15 20:53:08 +02:00
|
|
|
alpha = UTF8_ALPHA;
|
|
|
|
else if (encoding === 'utf16le') {
|
|
|
|
buf = UTF16_BUF;
|
|
|
|
str = Buffer.alloc(0);
|
|
|
|
} else
|
2015-03-19 22:31:34 +01:00
|
|
|
throw new Error('Bad encoding');
|
|
|
|
|
2016-05-15 20:53:08 +02:00
|
|
|
const sd = new StringDecoder(isBase64 ? 'base64' : encoding);
|
2015-03-19 22:31:34 +01:00
|
|
|
|
2016-02-28 06:56:18 +01:00
|
|
|
for (i = 0; i < inLen; ++i) {
|
2015-03-19 22:31:34 +01:00
|
|
|
if (i > 0 && (i % chunkLen) === 0 && !isBase64) {
|
2016-05-15 20:53:08 +02:00
|
|
|
if (alpha) {
|
|
|
|
chunks.push(Buffer.from(str, encoding));
|
|
|
|
str = '';
|
|
|
|
} else {
|
|
|
|
chunks.push(str);
|
|
|
|
str = Buffer.alloc(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (alpha)
|
|
|
|
str += alpha[i % alpha.length];
|
|
|
|
else {
|
|
|
|
var start = i;
|
|
|
|
var end = i + 2;
|
|
|
|
if (i % 2 !== 0) {
|
|
|
|
++start;
|
|
|
|
++end;
|
|
|
|
}
|
|
|
|
str = Buffer.concat([
|
|
|
|
str,
|
|
|
|
buf.slice(start % buf.length, end % buf.length)
|
|
|
|
]);
|
2015-03-19 22:31:34 +01:00
|
|
|
}
|
|
|
|
}
|
2016-05-15 20:53:08 +02:00
|
|
|
|
|
|
|
if (!alpha) {
|
|
|
|
if (str.length > 0)
|
|
|
|
chunks.push(str);
|
|
|
|
} else if (str.length > 0 && !isBase64)
|
2016-01-26 00:00:06 +01:00
|
|
|
chunks.push(Buffer.from(str, encoding));
|
2016-05-15 20:53:08 +02:00
|
|
|
|
2015-03-19 22:31:34 +01:00
|
|
|
if (isBase64) {
|
2016-01-26 00:00:06 +01:00
|
|
|
str = Buffer.from(str, 'utf8').toString('base64');
|
2015-03-19 22:31:34 +01:00
|
|
|
while (str.length > 0) {
|
2016-05-15 20:53:08 +02:00
|
|
|
const len = Math.min(chunkLen, str.length);
|
2016-01-26 00:00:06 +01:00
|
|
|
chunks.push(Buffer.from(str.substring(0, len), 'utf8'));
|
2015-03-19 22:31:34 +01:00
|
|
|
str = str.substring(len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-14 03:48:53 +02:00
|
|
|
const nChunks = chunks.length;
|
2015-03-19 22:31:34 +01:00
|
|
|
|
|
|
|
bench.start();
|
2016-02-28 06:56:18 +01:00
|
|
|
for (i = 0; i < n; ++i) {
|
2015-03-19 22:31:34 +01:00
|
|
|
for (var j = 0; j < nChunks; ++j)
|
|
|
|
sd.write(chunks[j]);
|
|
|
|
}
|
|
|
|
bench.end(n);
|
|
|
|
}
|