|
| 1 | +/** |
| 2 | + * @since 20180828 13:34 |
| 3 | + * @author vivaxy |
| 4 | + */ |
| 5 | + |
| 6 | +const path = require('path'); |
| 7 | +const Benchmark = require('benchmark'); |
| 8 | +const fse = require('fs-extra'); |
| 9 | +const glob = require('fast-glob'); |
| 10 | + |
| 11 | +const parse = require('../lib/parse.js'); |
| 12 | +const { parse: babelParse } = require('@babel/parser'); |
| 13 | +const { parse: acornParse } = require('acorn'); |
| 14 | + |
| 15 | +const suite = new Benchmark.Suite(); |
| 16 | + |
| 17 | +(async () => { |
| 18 | + const dirs = ['execute', 'parse', 'serialize']; |
| 19 | + const baseDir = path.join(__dirname, '..', 'lib', '__tests__', 'fixtures'); |
| 20 | + const getTestCaseTasks = dirs.map((dir) => { |
| 21 | + const task = async () => { |
| 22 | + const testCases = await glob('*', { |
| 23 | + cwd: path.join(baseDir, dir), |
| 24 | + onlyDirectories: true, |
| 25 | + }); |
| 26 | + return testCases.map((testCase) => { |
| 27 | + return path.join(dir, testCase); |
| 28 | + }); |
| 29 | + }; |
| 30 | + return task(); |
| 31 | + }); |
| 32 | + const testCaseNames = (await Promise.all(getTestCaseTasks)).reduce( |
| 33 | + (arr, list) => { |
| 34 | + return arr.concat(list); |
| 35 | + }, |
| 36 | + [] |
| 37 | + ); |
| 38 | + const getTestCaseInputTasks = testCaseNames.map((testCaseName) => { |
| 39 | + const task = async () => { |
| 40 | + return fse.readFile(path.join(baseDir, testCaseName, 'input.js'), 'utf8'); |
| 41 | + }; |
| 42 | + return task(); |
| 43 | + }); |
| 44 | + const testCaseInputs = await Promise.all(getTestCaseInputTasks); |
| 45 | + |
| 46 | + let message = ''; |
| 47 | + |
| 48 | + suite |
| 49 | + .add('@vivaxy/javascript#parse', function() { |
| 50 | + testCaseInputs.forEach((input) => { |
| 51 | + parse(input); |
| 52 | + }); |
| 53 | + }) |
| 54 | + .add('@babel/parser#parse', function() { |
| 55 | + testCaseInputs.forEach((input) => { |
| 56 | + babelParse(input); |
| 57 | + }); |
| 58 | + }) |
| 59 | + .add('acorn#parse', function() { |
| 60 | + testCaseInputs.forEach((input) => { |
| 61 | + acornParse(input); |
| 62 | + }); |
| 63 | + }) |
| 64 | + .on('cycle', function(event) { |
| 65 | + message += String(event.target) + '\n'; |
| 66 | + }) |
| 67 | + .on('complete', async function() { |
| 68 | + message += 'Fastest is ' + this.filter('fastest').map('name'); |
| 69 | + |
| 70 | + const readmePath = path.join(__dirname, '..', 'README.md'); |
| 71 | + const splitString = '\n## '; |
| 72 | + const readme = await fse.readFile(readmePath, 'utf8'); |
| 73 | + const readmes = readme.split(splitString).map((readmeSection) => { |
| 74 | + if (readmeSection.startsWith('Benchmark')) { |
| 75 | + return 'Benchmark\n\n' + message + '\n'; |
| 76 | + } |
| 77 | + return readmeSection; |
| 78 | + }); |
| 79 | + await fse.writeFile(readmePath, readmes.join(splitString)); |
| 80 | + }) |
| 81 | + .run(); |
| 82 | +})(); |
0 commit comments