1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | require('colors');
|
9 | var fs = require('fs') ;
|
10 |
|
11 | try {
|
12 | var babel = require("@babel/core") ;
|
13 | } catch (ex) {
|
14 | console.log("#### To run the tests you need to "+"npm install @babel/core".yellow+" (run 'npm i' in this directory). This additional module is only required for testing, not deployment.") ;
|
15 | process.exit(-1) ;
|
16 | }
|
17 |
|
18 | try {
|
19 | global.gc() ;
|
20 | } catch (ex) {
|
21 | console.log("You get more accurate timings by running with the node option --expose-gc") ;
|
22 | global.gc = function(){} ;
|
23 | }
|
24 |
|
25 | console.log("\nNB:The timings here are only indicative. GC and poor sampling generate variable results. More detailed performance tests can be found in "+"nodent".cyan+"\nStarting tests...");
|
26 |
|
27 | var nodent = require('nodent') ;
|
28 | var systemPromise = global.Promise || nodent.EagerThenable() ;
|
29 |
|
30 | global.Promise = nodent.EagerThenable() ;
|
31 |
|
32 | var testCode = require('fs').readFileSync(__dirname+'/test-input.js').toString() ;
|
33 |
|
34 | var eagerName = 'fast-async (nodent-'+nodent.EagerThenable().name+')' ;
|
35 | var transformers = {
|
36 | 'fast-async (es7-lazy)': {plugins:[[require('../plugin.js'),{runtimePatten:'directive',compiler:{promises:false,es7:true,lazyThenables:true}}]]},
|
37 | 'fast-async (spec:true)': {plugins:[[require('../plugin.js'),{runtimePatten:null,compiler:{promises:true,es7:true,noRuntime:true,wrapAwait:true}}]]},
|
38 | };
|
39 | transformers[eagerName] = {plugins:[[require('../plugin.js'),{runtimePatten:'directive',compiler:{promises:false,es7:true,lazyThenables:false}}]]} ;
|
40 |
|
41 |
|
42 | var needRegenerator = Object.keys(transformers).length ;
|
43 |
|
44 | var requires ;
|
45 | try {
|
46 | requires = ['babel-plugin-transform-async-to-generator','babel-preset-es2015'] ;
|
47 | requires.map(require) ;
|
48 | transformers['transform-async-to-generator'] = {"presets": ["es2015"],plugins:['transform-async-to-generator']} ;
|
49 | } catch (ex) {
|
50 |
|
51 | console.log("'"+("npm install "+requires.join(' ')).yellow+"' to compare against babel plugin 'transform-async-to-generator'") ;
|
52 | }
|
53 |
|
54 | try {
|
55 | requires = ['babel-plugin-transform-async-to-module-method','babel-preset-es2015','bluebird','coroutine'] ;
|
56 | requires.map(require) ;
|
57 | transformers['transform-async-to-module-method'] = {"presets": ["es2015"],plugins:[["transform-async-to-module-method",{module: 'bluebird',method: 'coroutine'}]]}
|
58 | } catch (ex) {
|
59 |
|
60 | console.log("'"+("npm install "+requires.join(' ')).yellow+"' to compare against babel plugin 'transform-async-to-module-method'") ;
|
61 | }
|
62 |
|
63 | function loadRegenerator(){
|
64 | try {
|
65 | function walkSync(dir,match) {
|
66 | if( dir[dir.length-1] != '/') dir=dir.concat('/')
|
67 | var fs = fs || require('fs'),
|
68 | files = fs.readdirSync(dir);
|
69 | files.forEach(function(file) {
|
70 | var stat = fs.lstatSync(dir + file) ;
|
71 | if (!stat.isSymbolicLink()) {
|
72 | if (file==match)
|
73 | throw dir + file ;
|
74 | if (stat.isDirectory())
|
75 | walkSync(dir + file + '/',match);
|
76 | }
|
77 | });
|
78 | };
|
79 | walkSync('node_modules','regenerator') ;
|
80 | console.log("Couldn't locate regenerator runtime") ;
|
81 | } catch (path) {
|
82 | global.regeneratorRuntime = require("./"+path);
|
83 | console.log("Loaded regenerator runtime from "+path+" ",regeneratorRuntime.toString().yellow) ;
|
84 | global.Promise = systemPromise ;
|
85 | }
|
86 | }
|
87 |
|
88 | var keys = Object.keys(transformers) ;
|
89 | (function nextTest(i){
|
90 | try {
|
91 | if (i===needRegenerator)
|
92 | loadRegenerator() ;
|
93 |
|
94 | console.log("Transforming with "+keys[i]);
|
95 | var t = babel.transform(testCode, transformers[keys[i]]);
|
96 | var f = new Function("require,resolve,reject",t.code) ;
|
97 |
|
98 | global.gc() ;
|
99 | f(require,
|
100 | function(result){
|
101 | console.log(keys[i],result.green) ;
|
102 | next() ;
|
103 | },function(error){
|
104 | console.log(keys[i],(error.stack || error).red) ;
|
105 | next() ;
|
106 | }) ;
|
107 | function next() {
|
108 | i++ ;
|
109 | if (i<keys.length)
|
110 | nextTest(i) ;
|
111 | }
|
112 | } catch (ex) {
|
113 | console.error(ex.stack.red) ;
|
114 | next() ;
|
115 | }
|
116 | })(0) ;
|