-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.js
129 lines (117 loc) · 3.77 KB
/
benchmark.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
const createTorrentPack = require("create-torrent")
const { createTorrent } = require("../dist")
const mockfs = require("mock-fs")
const { Writable, PassThrough } = require("stream")
const consumers = require("stream/consumers")
const fs = require("fs")
const Benchmarkify = require("benchmarkify")
const Torrent = require("node-torrent-stream")
const benchmark = new Benchmarkify("Comparison", { description: "Compares create-torrent to torrent-create-stream and torrent-create-stream in parallel", chartImage: true }).printHeader();
benchmark.createSuite("10MB File")
.setup(() => {
mockfs({
'file': Buffer.alloc(1024 * 1024 * 10)
});
})
.add("create-torrent", async (done) => {
createTorrentPack("./file", (err, torrent) => {
if (err) throw err
done()
})
})
.ref("torrent-create-stream", async (done) => {
let stream = new PassThrough()
let data = consumers.buffer(stream)
createTorrent({
files: {
length: fs.statSync("file").size,
path: "file",
getStream: (startAt, endAt) => fs.createReadStream("file", { start: startAt, end: endAt })
},
name: "Test Torrent",
}, stream)
await data
done()
}).ref("node-torrent-stream", async (done) => {
let stream = new PassThrough()
let data = consumers.buffer(stream)
let torrent = new Torrent({
announce: "http://localhost:8080/announce",
name: "Test Torrent",
})
fs.createReadStream("file").pipe(torrent)
torrent.pipe(stream)
await data
done()
})
benchmark.run();
benchmark.createSuite("2GB File")
.setup(() => {
mockfs({
'file': Buffer.alloc(1024 * 1024 * 1024 * 2)
});
})
.add("create-torrent", async (done) => {
createTorrentPack("./file", (err, torrent) => {
if (err) throw err
done()
})
})
.ref("torrent-create-stream", async (done) => {
let stream = new PassThrough()
let data = consumers.buffer(stream)
createTorrent({
files: {
length: fs.statSync("file").size,
path: "file",
getStream: (startAt, endAt) => fs.createReadStream("file", { start: startAt, end: endAt })
},
name: "Test Torrent",
}, stream)
await data
done()
}).ref("node-torrent-stream", async (done) => {
let stream = new PassThrough()
let data = consumers.buffer(stream)
let torrent = new Torrent({
announce: "http://localhost:8080/announce",
name: "Test Torrent",
})
fs.createReadStream("file").pipe(torrent)
torrent.pipe(stream)
await data
done()
})
benchmark.run();
// const start3 = Date.now()
// createTorrent({
// files: {
// length: 1024 * 1024 * 1024 * 10,
// path: "large_file",
// getStream: (startAt, endAt) => fs.createReadStream("large_file", { start: startAt, end: endAt })
// },
// parallelReads: 8,
// name: "Test Torrent",
// }, new Writable({
// write: (a,b,cb) => cb()
// })).then(() => {
// console.log("create-torrent-js (parallel) took", Date.now() - start3, "ms")
// const start = Date.now()
// createTorrentPack("./large_file", (err, torrent) => {
// if (err) throw err
// console.log("create-torrent took", Date.now() - start, "ms")
// const start2 = Date.now()
// createTorrent({
// files: {
// length: 1024 * 1024 * 1024 * 10,
// path: "large_file",
// getStream: (startAt, endAt) => fs.createReadStream("large_file", { start: startAt, end: endAt })
// },
// name: "Test Torrent",
// }, new Writable({
// write: (a,b,cb) => cb()
// })).then(() => {
// console.log("create-torrent-js took", Date.now() - start2, "ms")
// })
// })
// })