Skip to content

Commit 080d4d6

Browse files
committed
abstract out timer code from benchmarks
1 parent 0b195dc commit 080d4d6

31 files changed

+70
-228
lines changed

benchmarks/create/node-tar-file-async.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,8 @@ const fs = require('fs')
55
process.on('exit', _ => fs.unlinkSync(file))
66

77
const tar = require('../..')
8-
const start = process.hrtime()
8+
const timer = require('../timer.js')()
99
tar.c({
1010
file: file,
1111
cwd: cwd
12-
}, ['']).then(_ => {
13-
const end = process.hrtime(start)
14-
const ms = Math.round(end[0]*1e6 + end[1]/1e3)/1e3
15-
const s = Math.round(end[0]*10 + end[1]/1e8)/10
16-
const ss = s <= 1 ? '' : ' (' + s + 's)'
17-
console.error('%d%s', ms, ss)
18-
})
12+
}, ['']).then(timer)

benchmarks/create/node-tar-file-sync.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,10 @@ const fs = require('fs')
55
process.on('exit', _ => fs.unlinkSync(file))
66

77
const tar = require('../..')
8-
const start = process.hrtime()
8+
const timer = require('../timer.js')()
99
tar.c({
1010
file: file,
1111
sync: true,
1212
cwd: cwd
1313
}, [''])
14-
15-
const end = process.hrtime(start)
16-
const ms = Math.round(end[0]*1e6 + end[1]/1e3)/1e3
17-
const s = Math.round(end[0]*10 + end[1]/1e8)/10
18-
const ss = s <= 1 ? '' : ' (' + s + 's)'
19-
console.error('%d%s', ms, ss)
14+
timer()

benchmarks/create/node-tar-stream-async.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,6 @@ const fs = require('fs')
55
process.on('exit', _ => fs.unlinkSync(file))
66

77
const tar = require('../..')
8-
const start = process.hrtime()
8+
const timer = require('../timer.js')()
99
const c = tar.c({ cwd: cwd }, [''])
10-
c.pipe(fs.createWriteStream(file)).on('close', _ => {
11-
const end = process.hrtime(start)
12-
const ms = Math.round(end[0]*1e6 + end[1]/1e3)/1e3
13-
const s = Math.round(end[0]*10 + end[1]/1e8)/10
14-
const ss = s <= 1 ? '' : ' (' + s + 's)'
15-
console.error('%d%s', ms, ss)
16-
})
10+
c.pipe(fs.createWriteStream(file)).on('close', timer)

benchmarks/create/node-tar-stream-sync.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ const fs = require('fs')
55
process.on('exit', _ => fs.unlinkSync(file))
66

77
const tar = require('../..')
8-
const start = process.hrtime()
8+
const timer = require('../timer.js')()
99
const c = tar.c({ cwd: cwd, sync: true }, [''])
1010
fs.writeFileSync(file, c.read())
11-
const end = process.hrtime(start)
12-
const ms = Math.round(end[0]*1e6 + end[1]/1e3)/1e3
13-
const s = Math.round(end[0]*10 + end[1]/1e8)/10
14-
const ss = s <= 1 ? '' : ' (' + s + 's)'
15-
console.error('%d%s', ms, ss)
11+
timer()

benchmarks/create/old-async.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,9 @@ process.on('exit', _ => fs.unlinkSync(file))
66

77
const Reader = require('fstream').Reader
88
const Pack = require('tar').Pack
9-
const start = process.hrtime()
9+
const timer = require('../timer.js')()
1010
const d = new Reader({ path: cwd })
1111
const p = new Pack()
1212
const fstr = fs.createWriteStream(file)
1313
d.pipe(p).pipe(fstr)
14-
fstr.on('close', _ => {
15-
const end = process.hrtime(start)
16-
const ms = Math.round(end[0]*1e6 + end[1]/1e3)/1e3
17-
const s = Math.round(end[0]*10 + end[1]/1e8)/10
18-
const ss = s <= 1 ? '' : ' (' + s + 's)'
19-
console.error('%d%s', ms, ss)
20-
})
14+
fstr.on('close', timer)

benchmarks/create/pack-async.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,7 @@ const fs = require('fs')
55
process.on('exit', _ => fs.unlinkSync(file))
66

77
const Pack = require('../../lib/pack.js')
8-
const start = process.hrtime()
8+
const timer = require('../timer.js')()
99
const p = new Pack({ cwd: cwd })
1010
p.add('').end()
11-
p.pipe(fs.createWriteStream(file)).on('finish', _ => {
12-
const end = process.hrtime(start)
13-
const ms = Math.round(end[0]*1e6 + end[1]/1e3)/1e3
14-
const s = Math.round(end[0]*10 + end[1]/1e8)/10
15-
const ss = s <= 1 ? '' : ' (' + s + 's)'
16-
console.error('%d%s', ms, ss)
17-
})
11+
p.pipe(fs.createWriteStream(file)).on('finish', timer)

benchmarks/create/pack-sync.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,8 @@ const fs = require('fs')
55
process.on('exit', _ => fs.unlinkSync(file))
66

77
const Pack = require('../../lib/pack.js')
8-
const start = process.hrtime()
8+
const timer = require('../timer.js')()
99
const p = new Pack.Sync({ cwd: cwd })
1010
p.add('')
1111
fs.writeFileSync(file, p.read())
12-
const end = process.hrtime(start)
13-
const ms = Math.round(end[0]*1e6 + end[1]/1e3)/1e3
14-
const s = Math.round(end[0]*10 + end[1]/1e8)/10
15-
const ss = s <= 1 ? '' : ' (' + s + 's)'
16-
console.error('%d%s', ms, ss)
12+
timer()

benchmarks/create/tar-fs-async.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,7 @@ const file = '/tmp/benchmark.tar'
44
const fs = require('fs')
55
process.on('exit', _ => fs.unlinkSync(file))
66

7-
const start = process.hrtime()
87
const tar = require('tar-fs')
8+
const timer = require('../timer.js')()
99
const p = tar.pack(cwd)
10-
p.pipe(fs.createWriteStream(file)).on('close', _ => {
11-
const end = process.hrtime(start)
12-
const ms = Math.round(end[0]*1e6 + end[1]/1e3)/1e3
13-
const s = Math.round(end[0]*10 + end[1]/1e8)/10
14-
const ss = s <= 1 ? '' : ' (' + s + 's)'
15-
console.error('%d%s', ms, ss)
16-
})
10+
p.pipe(fs.createWriteStream(file)).on('close', timer)

benchmarks/extract/node-tar-file-async.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,8 @@ const path = require('path')
77
const file = process.argv[2] || path.resolve(__dirname, '../npm.tar')
88

99
const tar = require('../..')
10-
const start = process.hrtime()
10+
const timer = require('../timer.js')()
1111
tar.x({
1212
file: file,
1313
cwd: cwd
14-
}).then(_ => {
15-
const end = process.hrtime(start)
16-
const ms = Math.round(end[0]*1e6 + end[1]/1e3)/1e3
17-
const s = Math.round(end[0]*10 + end[1]/1e8)/10
18-
const ss = s <= 1 ? '' : ' (' + s + 's)'
19-
console.error('%d%s', ms, ss)
20-
})
14+
}).then(timer)

benchmarks/extract/node-tar-file-sync.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,10 @@ const path = require('path')
77
const file = process.argv[2] || path.resolve(__dirname, '../npm.tar')
88

99
const tar = require('../..')
10-
const start = process.hrtime()
10+
const timer = require('../timer.js')()
1111
tar.x({
1212
file: file,
1313
sync: true,
1414
cwd: cwd
1515
})
16-
17-
const end = process.hrtime(start)
18-
const ms = Math.round(end[0]*1e6 + end[1]/1e3)/1e3
19-
const s = Math.round(end[0]*10 + end[1]/1e8)/10
20-
const ss = s <= 1 ? '' : ' (' + s + 's)'
21-
console.error('%d%s', ms, ss)
16+
timer()

benchmarks/extract/node-tar-stream-async.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,7 @@ const file = process.argv[2] || path.resolve(__dirname, '../npm.tar')
88

99
const fs = require('fs')
1010
const tar = require('../..')
11-
const start = process.hrtime()
11+
const timer = require('../timer.js')()
1212
const x = tar.x({ cwd: cwd })
13-
x.on('end', _ => {
14-
const end = process.hrtime(start)
15-
const ms = Math.round(end[0]*1e6 + end[1]/1e3)/1e3
16-
const s = Math.round(end[0]*10 + end[1]/1e8)/10
17-
const ss = s <= 1 ? '' : ' (' + s + 's)'
18-
console.error('%d%s', ms, ss)
19-
})
13+
x.on('end', timer)
2014
fs.createReadStream(file).pipe(x)

benchmarks/extract/node-tar-stream-sync.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,7 @@ const file = process.argv[2] || path.resolve(__dirname, '../npm.tar')
88

99
const fs = require('fs')
1010
const tar = require('../..')
11-
const start = process.hrtime()
11+
const timer = require('../timer.js')()
1212
const x = tar.x({ sync: true, cwd: cwd })
13-
x.on('end', _ => {
14-
const end = process.hrtime(start)
15-
const ms = Math.round(end[0]*1e6 + end[1]/1e3)/1e3
16-
const s = Math.round(end[0]*10 + end[1]/1e8)/10
17-
const ss = s <= 1 ? '' : ' (' + s + 's)'
18-
console.error('%d%s', ms, ss)
19-
})
13+
x.on('end', timer)
2014
x.end(fs.readFileSync(file))

benchmarks/extract/old-async.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,8 @@ const file = process.argv[2] || path.resolve(__dirname, '../npm.tar')
88

99
const fs = require('fs')
1010
const Extract = require('tar').Extract
11-
const start = process.hrtime()
11+
const timer = require('../timer.js')()
1212
const x = new Extract({ path: cwd })
1313
x.on('entry', entry => entry.resume())
14-
x.on('close', _ => {
15-
const end = process.hrtime(start)
16-
const ms = Math.round(end[0]*1e6 + end[1]/1e3)/1e3
17-
const s = Math.round(end[0]*10 + end[1]/1e8)/10
18-
const ss = s <= 1 ? '' : ' (' + s + 's)'
19-
console.error('%d%s', ms, ss)
20-
})
14+
x.on('close', timer)
2115
fs.createReadStream(file).pipe(x)

benchmarks/extract/old-sync.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,8 @@ const file = process.argv[2] || path.resolve(__dirname, '../npm.tar')
99
const fs = require('fs')
1010
const Extract = require('tar').Extract
1111
const data = fs.readFileSync(file)
12-
const start = process.hrtime()
12+
const timer = require('../timer.js')()
1313
const x = new Extract({ path: cwd })
1414
x.on('entry', entry => entry.resume())
15-
x.on('close', _ => {
16-
const end = process.hrtime(start)
17-
const ms = Math.round(end[0]*1e6 + end[1]/1e3)/1e3
18-
const s = Math.round(end[0]*10 + end[1]/1e8)/10
19-
const ss = s <= 1 ? '' : ' (' + s + 's)'
20-
console.error('%d%s', ms, ss)
21-
})
15+
x.on('close', timer)
2216
x.end(data)

benchmarks/extract/tar-fs-async.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,7 @@ const file = process.argv[2] || path.resolve(__dirname, '../npm.tar')
88

99
const tar = require('tar-fs')
1010
const fs = require('fs')
11-
const start = process.hrtime()
11+
const timer = require('../timer.js')()
1212
const e = tar.extract(cwd)
13-
process.on('exit', (code, signal) => {
14-
const end = process.hrtime(start)
15-
const ms = Math.round(end[0]*1e6 + end[1]/1e3)/1e3
16-
const s = Math.round(end[0]*10 + end[1]/1e8)/10
17-
const ss = s <= 1 ? '' : ' (' + s + 's)'
18-
console.error('%d%s', ms, ss)
19-
})
13+
process.on('exit', timer)
2014
fs.createReadStream(file).pipe(e)

benchmarks/extract/tar-fs-sync.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,7 @@ const fs = require('fs')
1010
const tar = require('tar-fs')
1111
const data = fs.readFileSync(file)
1212

13-
const start = process.hrtime()
13+
const timer = require('../timer.js')()
1414
const e = tar.extract(cwd)
15-
process.on('exit', (code, signal) => {
16-
const end = process.hrtime(start)
17-
const ms = Math.round(end[0]*1e6 + end[1]/1e3)/1e3
18-
const s = Math.round(end[0]*10 + end[1]/1e8)/10
19-
const ss = s <= 1 ? '' : ' (' + s + 's)'
20-
console.error('%d%s', ms, ss)
21-
})
15+
process.on('exit', timer)
2216
e.end(data)

benchmarks/extract/unpack-async.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,7 @@ const file = process.argv[2] || path.resolve(__dirname, '../npm.tar')
99
const Unpack = require('../../lib/unpack.js')
1010
const fs = require('fs')
1111

12-
const start = process.hrtime()
12+
const timer = require('../timer.js')()
1313
const u = new Unpack({ cwd: cwd })
14-
u.on('close', _ => {
15-
const end = process.hrtime(start)
16-
const ms = Math.round(end[0]*1e6 + end[1]/1e3)/1e3
17-
const s = Math.round(end[0]*10 + end[1]/1e8)/10
18-
const ss = s <= 1 ? '' : ' (' + s + 's)'
19-
console.error('%d%s', ms, ss)
20-
})
14+
u.on('close', timer)
2115
fs.createReadStream(file).pipe(u)

benchmarks/extract/unpack-sync.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ const fs = require('fs')
1010
const Unpack = require('../../lib/unpack.js')
1111
const data = fs.readFileSync(file)
1212

13-
const start = process.hrtime()
13+
const timer = require('../timer.js')()
1414
const u = new Unpack.Sync({ cwd: cwd })
1515
u.end(data)
16-
const end = process.hrtime(start)
17-
const ms = Math.round(end[0]*1e6 + end[1]/1e3)/1e3
18-
const s = Math.round(end[0]*10 + end[1]/1e8)/10
19-
const ss = s <= 1 ? '' : ' (' + s + 's)'
20-
console.error('%d%s', ms, ss)
16+
timer()

benchmarks/parse/fast-scan-no-body.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,12 @@ const fs = require('fs')
44
const data = fs.readFileSync(file)
55
const Header = require('../../lib/header.js')
66

7-
const start = process.hrtime()
7+
const timer = require('../timer.js')()
88
for (let position = 0; position < data.length; position += 512) {
99
const h = new Header(data, position)
1010
if (h.size) {
1111
const blockSize = Math.ceil(h.size / 512) * 512
1212
position += blockSize
1313
}
1414
}
15-
16-
const end = process.hrtime(start)
17-
const ms = Math.round(end[0]*1e6 + end[1]/1e3)/1e3
18-
const s = Math.round(end[0]*10 + end[1]/1e8)/10
19-
const ss = s <= 1 ? '' : ' (' + s + 's)'
20-
console.error('%d%s', ms, ss)
15+
timer()

benchmarks/parse/fast-scan.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const onentry = (header, stream) => {
1111
stream.resume()
1212
}
1313

14-
const start = process.hrtime()
14+
const timer = require('../timer.js')()
1515
for (let position = 0; position < data.length; position += 512) {
1616
const h = new Header(data, position)
1717
const s = new MiniPass() // new stream.PassThrough()
@@ -24,8 +24,4 @@ for (let position = 0; position < data.length; position += 512) {
2424
}
2525
onentry(h, s)
2626
}
27-
const end = process.hrtime(start)
28-
const ms = Math.round(end[0]*1e6 + end[1]/1e3)/1e3
29-
const s = Math.round(end[0]*10 + end[1]/1e8)/10
30-
const ss = s <= 1 ? '' : ' (' + s + 's)'
31-
console.error('%d%s', ms, ss)
27+
timer()

benchmarks/parse/node-tar-file-async.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,7 @@ const tar = require('../..')
22
const path = require('path')
33
const file = process.argv[2] || path.resolve(__dirname, '../npm.tar')
44

5-
const start = process.hrtime()
5+
const timer = require('../timer.js')()
66
tar.t({
77
file: file
8-
}).then(_ => {
9-
const end = process.hrtime(start)
10-
const ms = Math.round(end[0]*1e6 + end[1]/1e3)/1e3
11-
const s = Math.round(end[0]*10 + end[1]/1e8)/10
12-
const ss = s <= 1 ? '' : ' (' + s + 's)'
13-
console.error('%d%s', ms, ss)
14-
})
8+
}).then(timer)

benchmarks/parse/node-tar-file-sync.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,11 @@ const tar = require('../..')
22
const path = require('path')
33
const file = process.argv[2] || path.resolve(__dirname, '../npm.tar')
44

5-
const start = process.hrtime()
5+
const timer = require('../timer.js')()
66
tar.t({
77
file: file,
88
sync: true,
99
maxReadSize: 17371648,
1010
noMtime: true
1111
})
12-
13-
const end = process.hrtime(start)
14-
const ms = Math.round(end[0]*1e6 + end[1]/1e3)/1e3
15-
const s = Math.round(end[0]*10 + end[1]/1e8)/10
16-
const ss = s <= 1 ? '' : ' (' + s + 's)'
17-
console.error('%d%s', ms, ss)
12+
timer()

benchmarks/parse/node-tar-stream-async.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,7 @@ const path = require('path')
33
const file = process.argv[2] || path.resolve(__dirname, '../npm.tar')
44
const fs = require('fs')
55

6-
const start = process.hrtime()
6+
const timer = require('../timer.js')()
77
const p = tar.t()
8-
p.on('end', _ => {
9-
const end = process.hrtime(start)
10-
const ms = Math.round(end[0]*1e6 + end[1]/1e3)/1e3
11-
const s = Math.round(end[0]*10 + end[1]/1e8)/10
12-
const ss = s <= 1 ? '' : ' (' + s + 's)'
13-
console.error('%d%s', ms, ss)
14-
})
8+
p.on('end', timer)
159
fs.createReadStream(file).pipe(p)

benchmarks/parse/node-tar-stream-sync.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,7 @@ const path = require('path')
33
const file = process.argv[2] || path.resolve(__dirname, '../npm.tar')
44
const fs = require('fs')
55

6-
const start = process.hrtime()
6+
const timer = require('../timer.js')()
77
const p = tar.t({ sync: true })
8-
p.on('end', _ => {
9-
const end = process.hrtime(start)
10-
const ms = Math.round(end[0]*1e6 + end[1]/1e3)/1e3
11-
const s = Math.round(end[0]*10 + end[1]/1e8)/10
12-
const ss = s <= 1 ? '' : ' (' + s + 's)'
13-
console.error('%d%s', ms, ss)
14-
})
8+
p.on('end', timer)
159
p.end(fs.readFileSync(file))

0 commit comments

Comments
 (0)