/
benchmarks.spec.js
288 lines (218 loc) · 7.92 KB
/
benchmarks.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
'use strict'
var chalk = require('chalk')
var now = require('performance-now')
var shelljs = require('shelljs')
var imp = require('../_js/testImports')
var shelljsExec = require('../../index')
var COUNT = 5
var RANGE = imp.range(COUNT)
function report(cmd, oldTimeSum, newTimeSum, cmdTimeSum, shelljsCmd) {
function getWord(percent) {
if (percent < 100)
return chalk.green((100 / percent).toFixed(1) + ' times faster than shelljs.exec (this module)')
else
return chalk.red((percent / 100).toFixed(1) + ' times slower than shelljs.exec (this module)')
}
var oldTimeAvg, newTimeAvg, cmdTimeAvg
var oldTimePct, cmdTimePct, oldTimeWord, cmdTimeWord
newTimeAvg = newTimeSum / COUNT
oldTimeAvg = oldTimeSum / COUNT
oldTimePct = 100 / newTimeAvg * oldTimeAvg
oldTimeWord = getWord(oldTimePct)
if (cmdTimeSum) {
cmdTimeAvg = cmdTimeSum / COUNT
cmdTimePct = 100 / newTimeAvg * cmdTimeAvg
cmdTimeWord = getWord(cmdTimePct)
}
console.log(chalk.blue('COMMAND =', cmd))
console.log(chalk.grey('AVERAGE TIMES FOR ' + COUNT + ' RUNS'))
console.log(chalk.grey('shelljs.exec (from shelljs) -----> ', oldTimeAvg.toFixed(2) + 'ms', ' (' + oldTimePct.toFixed(2) + '%)', ' ' + oldTimeWord))
console.log(chalk.grey('shelljs.exec (this module) ------> ', newTimeAvg.toFixed(2) + 'ms', ' (100%)'))
if (cmdTimeSum && shelljsCmd) {
console.log(chalk.grey('shelljs.' + shelljsCmd + '> ', cmdTimeAvg.toFixed(2) + 'ms', ' (' + cmdTimePct.toFixed(2) + '%)', ' ' + cmdTimeWord))
}
}
describe('benchmarks: shelljs v shelljs.exec', function() {
var cmdObj, start, end
var oldTime, newTime, cmdTime
describe('shelljs supported commands', function() {
describe('echo', function() {
it('echo hello', function() {
var oldTimeSum = 0, newTimeSum = 0, cmdTimeSum = 0
RANGE.forEach(function() {
start = now()
cmdObj = shelljs.exec('echo hello', {silent: true})
end = now()
imp.expect(cmdObj.stdout).to.equal('hello' + imp.EOL)
oldTime = end - start
start = now()
cmdObj = shelljsExec('echo hello', {silent: true})
end = now()
imp.expect(cmdObj.stdout).to.equal('hello' + imp.EOL)
newTime = end - start
start = now()
cmdObj = shelljs.echo('hello')
end = now()
imp.expect(cmdObj.stdout).to.equal('hello')
cmdTime = end - start
oldTimeSum += oldTime
newTimeSum += newTime
cmdTimeSum += cmdTime
})
report('echo hello', oldTimeSum, newTimeSum, cmdTimeSum, 'echo --------------------')
})
})
describe('which', function() {
before(function() {
var which = shelljsExec('which which', {silent: true})
var git = shelljsExec('which git', {silent: true})
if (!which.ok || !git.ok) {
this.skip()
}
})
it('which git', function() {
var oldTimeSum = 0, newTimeSum = 0, cmdTimeSum = 0
RANGE.forEach(function() {
start = now()
cmdObj = shelljs.exec('which git', {silent: true})
end = now()
imp.expect(cmdObj.code).to.equal(0)
oldTime = end - start
start = now()
cmdObj = shelljsExec('which git', {silent: true})
end = now()
imp.expect(cmdObj.code).to.equal(0)
newTime = end - start
start = now()
cmdObj = shelljs.which('git')
end = now()
imp.expect(cmdObj).to.not.be.null
imp.expect(cmdObj.code).to.equal(0)
cmdTime = end - start
oldTimeSum += oldTime
newTimeSum += newTime
cmdTimeSum += cmdTime
})
report('which git', oldTimeSum, newTimeSum, cmdTimeSum, 'which -------------------')
})
})
describe('cat', function() {
before(function() {
var cat1 = shelljsExec('where cat', {silent: true})
var cat2 = shelljsExec('which cat', {silent: true})
if (!cat1.ok && !cat2.ok) {
this.skip()
}
})
it('cat file1 file2', function() {
var oldTimeSum = 0, newTimeSum = 0, cmdTimeSum = 0
var file1 = __dirname + '/file1'
var file2 = __dirname + '/file2'
RANGE.forEach(function() {
start = now()
cmdObj = shelljs.exec('cat ' + file1 + ' ' + file2, {silent: true})
end = now()
imp.expect(cmdObj.stdout).to.equal('hello\nworld\n')
oldTime = end - start
start = now()
cmdObj = shelljsExec('cat ' + file1 + ' ' + file2, {silent: true})
end = now()
imp.expect(cmdObj.stdout).to.equal('hello\nworld\n')
newTime = end - start
start = now()
cmdObj = shelljs.cat(file1, file2)
end = now()
imp.expect(cmdObj.stdout).to.equal('hello\nworld\n')
cmdTime = end - start
oldTimeSum += oldTime
newTimeSum += newTime
cmdTimeSum += cmdTime
})
report('cat file1 file2', oldTimeSum, newTimeSum, cmdTimeSum, 'cat ---------------------')
})
})
})
describe('shelljs unsupported commands', function() {
describe('printf', function() {
before(function() {
var printf1 = shelljsExec('where printf', {silent: true})
var printf2 = shelljsExec('which printf', {silent: true})
if (!printf1.ok && !printf2.ok) {
this.skip()
}
})
it('printf hello', function() {
var oldTimeSum = 0, newTimeSum = 0
RANGE.forEach(function() {
start = now()
cmdObj = shelljs.exec('printf hello', {silent: true})
end = now()
imp.expect(cmdObj.stdout).to.equal('hello')
oldTime = end - start
start = now()
cmdObj = shelljsExec('printf hello', {silent: true})
end = now()
imp.expect(cmdObj.stdout).to.equal('hello')
newTime = end - start
oldTimeSum += oldTime
newTimeSum += newTime
})
report('printf hello', oldTimeSum, newTimeSum)
})
})
describe('whoami', function() {
before(function() {
var whoami1 = shelljsExec('where whoami', {silent: true})
var whoami2 = shelljsExec('which whoami', {silent: true})
if (!whoami1.ok && !whoami2.ok) {
this.skip()
}
})
it('whoami', function() {
var oldTimeSum = 0, newTimeSum = 0
RANGE.forEach(function() {
start = now()
cmdObj = shelljs.exec('whoami', {silent: true})
end = now()
imp.expect(cmdObj.code).to.equal(0)
oldTime = end - start
start = now()
cmdObj = shelljsExec('whoami', {silent: true})
end = now()
imp.expect(cmdObj.code).to.equal(0)
newTime = end - start
oldTimeSum += oldTime
newTimeSum += newTime
})
report('whoami', oldTimeSum, newTimeSum)
})
})
describe('git', function() {
before(function() {
var git1 = shelljsExec('where git', {silent: true})
var git2 = shelljsExec('which git', {silent: true})
if (!git1.ok && !git2.ok) {
this.skip()
}
})
it('git rev-parse --is-inside-work-tree', function() {
var oldTimeSum = 0, newTimeSum = 0
RANGE.forEach(function() {
start = now()
cmdObj = shelljs.exec('git rev-parse --is-inside-work-tree', {silent: true})
end = now()
imp.expect(cmdObj.stdout).to.equal('true\n')
oldTime = end - start
start = now()
cmdObj = shelljsExec('git rev-parse --is-inside-work-tree', {silent: true})
end = now()
imp.expect(cmdObj.stdout).to.equal('true\n')
newTime = end - start
oldTimeSum += oldTime
newTimeSum += newTime
})
report('git rev-parse --is-inside-work-tree', oldTimeSum, newTimeSum)
})
})
})
})