Skip to content

Add --verbose flag #41

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

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 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,28 @@ 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(),
isDone: stats.isDone && Boolean(
$('#latency-value.succeeded') && $('#bufferbloat-value.succeeded')
)
};
}

return stats;
}, options);

if (result.downloadSpeed > 0 && !equals(result, prevResult)) {
observer.next(result);
Expand Down
15 changes: 13 additions & 2 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const cli = meow(`

Options
--upload, -u Measure upload speed in addition to download speed
--verbose Get verbose logging on latency and request metadata

Examples
$ fast --upload > file && cat file
Expand All @@ -24,10 +25,15 @@ const cli = meow(`
upload: {
type: 'boolean',
alias: 'u'
},
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 @@ -57,17 +63,22 @@ const speedText = () =>
downloadColor(downloadSpeed());

const speed = () => speedText() + '\n\n';
const getVerboseLog = () => ` Latency: ${data.latency}${data.latencyUnit} ${chalk.dim('(unloaded)')} ${data.bufferbloat}${data.bufferbloatUnit} ${chalk.dim('(loaded)')}\n Client: ${data.client.location} ${data.client.ip} ${data.client.isp}\n Server(s): ${data.serverLocations}`;

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

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

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

console.log(output);
}

Expand All @@ -89,7 +100,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 @@ -27,6 +27,7 @@ $ fast --help

Options
--upload, -u Measure upload speed in addition to download speed
--verbose Get verbose logging on latency and request metadata

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