11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciconst bench = common.createBenchmark(main, {
51cb0ef41Sopenharmony_ci  type: ['one_byte', 'two_bytes', 'three_bytes', 'four_bytes'],
61cb0ef41Sopenharmony_ci  encoding: ['utf8', 'base64'],
71cb0ef41Sopenharmony_ci  repeat: [1, 2, 16, 256], // x16
81cb0ef41Sopenharmony_ci  n: [4e6],
91cb0ef41Sopenharmony_ci});
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci// 16 chars each
121cb0ef41Sopenharmony_ciconst chars = {
131cb0ef41Sopenharmony_ci  one_byte: 'hello brendan!!!',
141cb0ef41Sopenharmony_ci  two_bytes: 'ΰαβγδεζηθικλμνξο',
151cb0ef41Sopenharmony_ci  three_bytes: '挰挱挲挳挴挵挶挷挸挹挺挻挼挽挾挿',
161cb0ef41Sopenharmony_ci  four_bytes: '����������������',
171cb0ef41Sopenharmony_ci};
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_cifunction getInput(type, repeat, encoding) {
201cb0ef41Sopenharmony_ci  const original = (repeat === 1) ? chars[type] : chars[type].repeat(repeat);
211cb0ef41Sopenharmony_ci  if (encoding === 'base64') {
221cb0ef41Sopenharmony_ci    Buffer.from(original, 'utf8').toString('base64');
231cb0ef41Sopenharmony_ci  }
241cb0ef41Sopenharmony_ci  return original;
251cb0ef41Sopenharmony_ci}
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_cifunction main({ n, repeat, encoding, type }) {
281cb0ef41Sopenharmony_ci  const data = getInput(type, repeat, encoding);
291cb0ef41Sopenharmony_ci  const expected = Buffer.byteLength(data, encoding);
301cb0ef41Sopenharmony_ci  let changed = false;
311cb0ef41Sopenharmony_ci  bench.start();
321cb0ef41Sopenharmony_ci  for (let i = 0; i < n; i++) {
331cb0ef41Sopenharmony_ci    const actual = Buffer.byteLength(data, encoding);
341cb0ef41Sopenharmony_ci    if (expected !== actual) { changed = true; }
351cb0ef41Sopenharmony_ci  }
361cb0ef41Sopenharmony_ci  bench.end(n);
371cb0ef41Sopenharmony_ci  if (changed) {
381cb0ef41Sopenharmony_ci    throw new Error('Result changed during iteration');
391cb0ef41Sopenharmony_ci  }
401cb0ef41Sopenharmony_ci}
41