const devnull = require("dev-null");
const util = require("util");
const sleep = util.promisify(setTimeout);
const benchmark = async (fn) => {
let time;
let success;
let data;
let started;
try {
started = Date.now();
data = await fn();
time = Date.now() - started;
success = true;
} catch (err) {
data = null;
time = Date.now() - started;
success = false;
console.error(err);
}
return { time, success, data };
}
const query = {
videoURL: "https://www.youtube.com/watch?v=Sn1rJbZ8nI4",
search: "faded",
playlistID: "PLNKs8mJ6MlqAx7nqsUi6tRJFDFBJxuLiV",
channelURL: "https://www.youtube.com/channel/UCBUK-I-ILqsQoqIe8i6zrVg"
};
const result = {
"ytsr": {},
"ytpl": {},
"youtube-sr": {},
"ytdl-core": {},
"youtube-ext": {}
};
async function ytext() {
const dl = require("youtube-ext");
const searchbm = await benchmark(dl.search.bind(null, query.search));
result["youtube-ext"].search = searchbm.time;
const infobm = await benchmark(dl.videoInfo.bind(null, query.videoURL));
result["youtube-ext"].information = infobm.time;
const playlistbm = await benchmark(dl.playlistInfo.bind(null, query.playlistID));
result["youtube-ext"].playlist = playlistbm.time;
const channelbm = await benchmark(dl.channelInfo.bind(null, query.channelURL));
result["youtube-ext"].channel = channelbm.time;
// const downloadbm = await benchmark(async () => {
// const formats = await dl.getFormats(infobm.data.streams);
// const format = formats.find(x => x.itag === 18);
// const vStream = await dl.getReadableStream(format);
// vStream.pipe(devnull());
// vStream.on("error", (err) => {
// throw err;
// });
// await new Promise(r => vStream.on("finish", r));
// });
// result["youtube-ext"].download = downloadbm.time;
}
async function ytdl() {
const dl = require("ytdl-core");
const infobm = await benchmark(dl.getBasicInfo.bind(null, query.videoURL));
result["ytdl-core"].information = infobm.time;
const downloadbm = await benchmark(async () => {
const vStream = dl(query.videoURL, {
quality: 18
});
vStream.on("error", (err) => {
console.error(err);
});
vStream.pipe(devnull());
await new Promise(r => vStream.on("finish", r));
});
result["ytdl-core"].download = downloadbm.time;
}
async function ytsr() {
const dl = require("ytsr");
const searchbm = await benchmark(dl.bind(null, query.search));
result["ytsr"].search = searchbm.time;
}
async function youtubesr() {
const dl = require("youtube-sr").default;
const searchbm = await benchmark(dl.search.bind(null, query.search));
result["youtube-sr"].search = searchbm.time;
const infobm = await benchmark(dl.getVideo.bind(null, query.videoURL));
result["youtube-sr"].information = infobm.time;
const playlistbm = await benchmark(dl.getPlaylist.bind(null, query.playlistID));
result["youtube-sr"].playlist = playlistbm.time;
}
async function ytpl() {
const dl = require("ytpl");
const playlistbm = await benchmark(dl.bind(null, query.playlistID));
result["ytpl"].playlist = playlistbm.time;
}
const start = async () => {
const pause = sleep.bind(null, 1000);
result["youtube-ext"].overall = (await benchmark(ytext)).time;
console.log("0");
await pause();
result["ytsr"].overall = (await benchmark(ytsr)).time;
console.log("1");
await pause();
result["ytpl"].overall = (await benchmark(ytpl)).time;
console.log("2");
await pause();
result["youtube-sr"].overall = (await benchmark(youtubesr)).time;
console.log("3");
await pause();
result["ytdl-core"].overall = (await benchmark(ytdl)).time;
console.log("4");
await pause();
const winners = {};
for (const key in result) {
const bms = result[key];
Object.entries(bms).forEach(([k, v]) => {
if (!winners[k]) winners[k] = [];
winners[k].push({
module: key,
time: v
});
});
}
let final = "Results\n";
for (const key in winners) {
if (key === "overall") continue;
const ele = winners[key];
final += ` * ${key}: ${ele.sort((a, b) => a.time - b.time).map(x => `${x.module} (${x.time}ms)`).join(" < ")}\n`;
}
console.log(final);
}
start();