Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --verbose flag v 3.0 #52

Closed
wants to merge 10 commits into from
26 changes: 23 additions & 3 deletions api.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ async function init(browser, page, observer, options) {

/* eslint-disable no-constant-condition, no-await-in-loop */
while (true) {
const result = await page.evaluate(() => {
const result = await page.evaluate(options => {
const $ = document.querySelector.bind(document);

return {
let stats = {
downloadSpeed: Number($('#speed-value').textContent),
uploadSpeed: Number($('#upload-value').textContent),
downloadUnit: $('#speed-units').textContent.trim(),
Expand All @@ -22,7 +22,27 @@ async function init(browser, page, observer, options) {
$('#speed-value.succeeded') && $('#upload-value.succeeded')
)
};
});

if (options.verbose) {
stats = {
...stats,
latency: Number($('#latency-value').textContent),
bufferbloat: Number($('#bufferbloat-value').textContent),
latencyUnit: $('#latency-units').textContent.trim(),
bufferbloatUnit: $('#bufferbloat-units').textContent.trim(),
client: {
location: $('#user-location').textContent.trim(),
ip: $('#user-ip').textContent.trim(),
isp: $('#user-isp').textContent.trim()
},
serverLocations: $('#server-locations').textContent.trim(),
isLatencyDone: Boolean($('#latency-value.succeeded')),
isBufferbloatDone: Boolean($('#bufferbloat-value.succeeded'))
};
}

return stats;
}, options);

if (result.downloadSpeed > 0 && !equals(result, prevResult)) {
observer.next(result);
Expand Down
34 changes: 30 additions & 4 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const cli = meow(`
Options
--upload, -u Measure upload speed in addition to download speed
--single-line Reduce spacing and output to a single line
--verbose Include info on latency and request metadata

Examples
$ fast --upload > file && cat file
Expand All @@ -28,10 +29,15 @@ const cli = meow(`
},
singleLine: {
type: 'boolean'
},
verbose: {
type: 'boolean'
}
}
});

cli.flags.upload = cli.flags.upload || cli.flags.verbose;

// Check connections
dns.lookup('fast.com', error => {
if (error && error.code === 'ENOTFOUND') {
Expand Down Expand Up @@ -62,21 +68,41 @@ const uploadColor = string => (data.isDone ? chalk.green(string) : chalk.cyan(st

const downloadColor = string => ((data.isDone || data.uploadSpeed) ? chalk.green(string) : chalk.cyan(string));

const latencyColor = string => (data.isLatencyDone ? chalk.white(string) : chalk.cyan(string));
const bufferbloatColor = string => (data.isBufferbloatDone ? chalk.white(string) : chalk.cyan(string));

const speedText = () =>
cli.flags.upload ?
`${downloadColor(downloadSpeed())} ${chalk.dim('/')} ${uploadColor(uploadSpeed())}` :
downloadColor(downloadSpeed());

const speed = () => speedText() + lineBreak(2);
const latencyText = () => `Latency: ${latencyColor(data.latency + data.latencyUnit)} ${chalk.dim('(unloaded)')} ${bufferbloatColor(data.bufferbloat + data.bufferbloatUnit)} ${chalk.dim('(loaded)')}`;

const speed = () => {
let speedLog = speedText() + lineBreak(2);
if (cli.flags.verbose) {
speedLog += `${cli.flags.singleLine ? '\n' : ''}${spacing(4)}${latencyText()}\n`;
}

return speedLog;
};

const getVerboseLog = () => `${spacing(4)}${latencyText()}\n${spacing(5)}Client: ${data.client.location} ${data.client.ip} ${data.client.isp}\n${spacing(4)}Servers: ${data.serverLocations}`;

function exit() {
if (process.stdout.isTTY) {
logUpdate(`${lineBreak(2)}${spacing(4)}${speed()}`);
logUpdate(`${lineBreak(2)}${spacing(4)}${speed()}${cli.flags.verbose ? `${lineBreak(1)}${getVerboseLog()}` : ''}`);
} else {
let output = `${data.downloadSpeed} ${data.downloadUnit}`;

if (cli.flags.upload) {
output += `\n${data.uploadSpeed} ${data.uploadUnit}`;
output += `${cli.flags.singleLine ? ' / ' : '\n'}${data.uploadSpeed} ${data.uploadUnit}`;
}

output += "\n"

if (cli.flags.verbose) {
output += `\n${getVerboseLog()}`;
}

console.log(output);
Expand All @@ -100,7 +126,7 @@ if (process.stdout.isTTY) {

(async () => {
try {
await api({measureUpload: cli.flags.upload}).forEach(result => {
await api({measureUpload: cli.flags.upload, verbose: cli.flags.verbose}).forEach(result => {
data = result;
});

Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ $ fast --help
Options
--upload, -u Measure upload speed in addition to download speed
--single-line Reduce spacing and output to a single line
--verbose Include info on latency and request metadata

Examples
$ fast --upload > file && cat file
Expand Down