Debian Codegraph @

linkify-it/benchmark/benchmark.js

raw
1 #!/usr/bin/env node
2 /*eslint no-console:0*/
3
4 'use strict';
5
6 var path = require('path');
7 var fs = require('fs');
8 var util = require('util');
9 var Benchmark = require('benchmark');
10 var ansi = require('ansi');
11 var cursor = ansi(process.stdout);
12
13 var IMPLS_DIRECTORY = path.join(__dirname, 'implementations');
14 var IMPLS_PATHS = {};
15 var IMPLS = [];
16
17
18 fs.readdirSync(IMPLS_DIRECTORY).sort().forEach(function (name) {
19 var file = path.join(IMPLS_DIRECTORY, name);
20 var code = require(file);
21
22 IMPLS_PATHS[name] = file;
23 IMPLS.push({
24 name: name,
25 code: code
26 });
27 });
28
29
30 var SAMPLES_DIRECTORY = path.join(__dirname, 'samples');
31 var SAMPLES = [];
32
33 fs.readdirSync(SAMPLES_DIRECTORY).sort().forEach(function (sample) {
34 var filepath = path.join(SAMPLES_DIRECTORY, sample),
35 extname = path.extname(filepath),
36 basename = path.basename(filepath, extname);
37
38 var content = {};
39
40 content.string = fs.readFileSync(filepath, 'utf8');
41
42 var title = util.format('(%d bytes)', content.string.length);
43
44 function onComplete() {
45 cursor.write('\n');
46 }
47
48
49 var suite = new Benchmark.Suite(title, {
50
51 onStart: function onStart() {
52 console.log('\nSample: %s %s', sample, title);
53 },
54
55 onComplete: onComplete
56
57 });
58
59
60 IMPLS.forEach(function (impl) {
61 suite.add(impl.name, {
62
63 onCycle: function onCycle(event) {
64 cursor.horizontalAbsolute();
65 cursor.eraseLine();
66 cursor.write(' > ' + event.target);
67 },
68
69 onComplete: onComplete,
70
71 fn: function () {
72 impl.code.run(content.string);
73 return;
74 }
75 });
76 });
77
78
79 SAMPLES.push({
80 name: basename,
81 title: title,
82 content: content,
83 suite: suite
84 });
85 });
86
87
88 function select(patterns) {
89 var result = [];
90
91 if (!(patterns instanceof Array)) {
92 patterns = [ patterns ];
93 }
94
95 function checkName(name) {
96 return patterns.length === 0 || patterns.some(function (regexp) {
97 return regexp.test(name);
98 });
99 }
100
101 SAMPLES.forEach(function (sample) {
102 if (checkName(sample.name)) {
103 result.push(sample);
104 }
105 });
106
107 return result;
108 }
109
110
111 function run(files) {
112 var selected = select(files);
113
114 if (selected.length > 0) {
115 console.log('Selected samples: (%d of %d)', selected.length, SAMPLES.length);
116 selected.forEach(function (sample) {
117 console.log(' > %s', sample.name);
118 });
119 } else {
120 console.log('There isn\'t any sample matches any of these patterns: %s', util.inspect(files));
121 }
122
123 selected.forEach(function (sample) {
124 sample.suite.run();
125 });
126 }
127
128 module.exports.IMPLS_DIRECTORY = IMPLS_DIRECTORY;
129 module.exports.IMPLS_PATHS = IMPLS_PATHS;
130 module.exports.IMPLS = IMPLS;
131 module.exports.SAMPLES_DIRECTORY = SAMPLES_DIRECTORY;
132 module.exports.SAMPLES = SAMPLES;
133 module.exports.select = select;
134 module.exports.run = run;
135
136 run(process.argv.slice(2).map(function (source) {
137 return new RegExp(source, 'i');
138 }));