├── .editorconfig ├── .gitattributes ├── .github └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── cli.js ├── license ├── package.json ├── readme.md ├── screenshot.gif └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 16 14 | - 14 15 | - 12 16 | steps: 17 | - uses: actions/checkout@v2 18 | - uses: actions/setup-node@v2 19 | with: 20 | node-version: ${{ matrix.node-version }} 21 | - run: npm install 22 | - run: npm test 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /cli.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | import process from 'node:process'; 3 | import {URL} from 'node:url'; 4 | import meow from 'meow'; 5 | import speedtest from 'speedtest-net'; 6 | import {roundTo} from 'round-to'; 7 | import chalk from 'chalk'; 8 | import logUpdate from 'log-update'; 9 | import logSymbols from 'log-symbols'; 10 | import Ora from 'ora'; 11 | 12 | const cli = meow(` 13 | Usage 14 | $ speed-test 15 | 16 | Options 17 | --json -j Output the result as JSON 18 | --bytes -b Output the result in megabytes per second (MBps) 19 | --verbose -v Output more detailed information 20 | `, { 21 | importMeta: import.meta, 22 | flags: { 23 | json: { 24 | type: 'boolean', 25 | alias: 'j', 26 | }, 27 | bytes: { 28 | type: 'boolean', 29 | alias: 'b', 30 | }, 31 | verbose: { 32 | type: 'boolean', 33 | alias: 'v', 34 | }, 35 | }, 36 | }); 37 | 38 | const stats = { 39 | ping: '', 40 | download: '', 41 | upload: '', 42 | }; 43 | 44 | let state = 'ping'; 45 | const spinner = new Ora(); 46 | const unit = cli.flags.bytes ? 'MBps' : 'Mbps'; 47 | const multiplier = cli.flags.bytes ? 1 / 8 : 1; 48 | 49 | const getSpinnerFromState = inputState => inputState === state ? chalk.gray.dim(spinner.frame()) : ' '; 50 | 51 | const logError = error => { 52 | if (cli.flags.json) { 53 | console.error(JSON.stringify({error})); 54 | } else { 55 | console.error(logSymbols.error, error); 56 | } 57 | }; 58 | 59 | function render() { 60 | if (cli.flags.json) { 61 | console.log(JSON.stringify(stats)); 62 | return; 63 | } 64 | 65 | let output = ` 66 | Ping ${getSpinnerFromState('ping')}${stats.ping} 67 | Download ${getSpinnerFromState('download')}${stats.download} 68 | Upload ${getSpinnerFromState('upload')}${stats.upload}`; 69 | 70 | if (cli.flags.verbose) { 71 | output += [ 72 | '', 73 | ' Server ' + (stats.data === undefined ? '' : chalk.cyan(stats.data.server.host)), 74 | ' Location ' + (stats.data === undefined ? '' : chalk.cyan(stats.data.server.location + chalk.dim(' (' + stats.data.server.country + ')'))), 75 | ' Distance ' + (stats.data === undefined ? '' : chalk.cyan(roundTo(stats.data.server.distance, 1) + chalk.dim(' km'))), 76 | ].join('\n'); 77 | } 78 | 79 | logUpdate(output); 80 | } 81 | 82 | function setState(newState) { 83 | state = newState; 84 | 85 | if (newState && newState.length > 0) { 86 | stats[newState] = chalk.yellow(`0 ${chalk.dim(unit)}`); 87 | } 88 | } 89 | 90 | function map(server) { 91 | server.host = new URL(server.url).host; 92 | server.location = server.name; 93 | server.distance = server.dist; 94 | return server; 95 | } 96 | 97 | const speedTest = speedtest({maxTime: 20_000}); 98 | 99 | if (!cli.flags.json) { 100 | setInterval(render, 50); 101 | } 102 | 103 | speedTest.once('testserver', server => { 104 | if (cli.flags.verbose) { 105 | stats.data = { 106 | server: map(server), 107 | }; 108 | } 109 | 110 | setState('download'); 111 | const ping = Math.round(server.bestPing); 112 | stats.ping = cli.flags.json ? ping : chalk.cyan(ping + chalk.dim(' ms')); 113 | }); 114 | 115 | speedTest.on('downloadspeedprogress', speed => { 116 | if (state === 'download' && cli.flags.json !== true) { 117 | speed *= multiplier; 118 | const download = roundTo(speed, speed >= 10 ? 0 : 1); 119 | stats.download = chalk.yellow(`${download} ${chalk.dim(unit)}`); 120 | } 121 | }); 122 | 123 | speedTest.on('uploadspeedprogress', speed => { 124 | if (state === 'upload' && cli.flags.json !== true) { 125 | speed *= multiplier; 126 | const upload = roundTo(speed, speed >= 10 ? 0 : 1); 127 | stats.upload = chalk.yellow(`${upload} ${chalk.dim(unit)}`); 128 | } 129 | }); 130 | 131 | speedTest.once('downloadspeed', speed => { 132 | setState('upload'); 133 | speed *= multiplier; 134 | const download = roundTo(speed, speed >= 10 && !cli.flags.json ? 0 : 1); 135 | stats.download = cli.flags.json ? download : chalk.cyan(download + ' ' + chalk.dim(unit)); 136 | }); 137 | 138 | speedTest.once('uploadspeed', speed => { 139 | setState(''); 140 | speed *= multiplier; 141 | const upload = roundTo(speed, speed >= 10 && !cli.flags.json ? 0 : 1); 142 | stats.upload = cli.flags.json ? upload : chalk.cyan(upload + ' ' + chalk.dim(unit)); 143 | }); 144 | 145 | speedTest.on('data', data => { 146 | if (cli.flags.verbose) { 147 | stats.data = data; 148 | } 149 | 150 | render(); 151 | }); 152 | 153 | speedTest.on('done', () => { 154 | console.log(); 155 | process.exit(); 156 | }); 157 | 158 | speedTest.on('error', error => { 159 | if (error.code === 'ENOTFOUND') { 160 | logError('Please check your internet connection'); 161 | } else { 162 | logError(error); 163 | } 164 | 165 | process.exit(1); 166 | }); 167 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "speed-test", 3 | "version": "3.0.0", 4 | "description": "Test your internet connection speed and ping using speedtest.net from the CLI", 5 | "license": "MIT", 6 | "repository": "sindresorhus/speed-test", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "bin": "./cli.js", 15 | "engines": { 16 | "node": "^12.20.0 || ^14.13.1 || >=16.0.0" 17 | }, 18 | "scripts": { 19 | "test": "xo && ava" 20 | }, 21 | "files": [ 22 | "cli.js" 23 | ], 24 | "keywords": [ 25 | "cli-app", 26 | "cli", 27 | "speed", 28 | "test", 29 | "tester", 30 | "speed-test", 31 | "speedtest", 32 | "connection", 33 | "internet", 34 | "bandwidth", 35 | "ping", 36 | "measure", 37 | "check" 38 | ], 39 | "dependencies": { 40 | "chalk": "^4.1.2", 41 | "log-symbols": "^5.0.0", 42 | "log-update": "^5.0.0", 43 | "meow": "^10.1.2", 44 | "ora": "^6.0.1", 45 | "round-to": "^6.0.0", 46 | "speedtest-net": "^1.6.2" 47 | }, 48 | "devDependencies": { 49 | "ava": "^3.15.0", 50 | "execa": "^6.0.0", 51 | "p-event": "^5.0.1", 52 | "xo": "^0.46.4" 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # speed-test 2 | 3 | > Test your internet connection speed and ping using [speedtest.net](https://www.speedtest.net) from the CLI 4 | 5 | 6 | 7 | ## Install 8 | 9 | Ensure you have [Node.js](https://nodejs.org) version 12.20+ installed. Then run the following: 10 | 11 | ```sh 12 | npm install --global speed-test 13 | ``` 14 | 15 | ## Usage 16 | 17 | ``` 18 | $ speed-test --help 19 | 20 | Usage 21 | $ speed-test 22 | 23 | Options 24 | --json -j Output the result as JSON 25 | --bytes -b Output the result in megabytes per second (MBps) 26 | --verbose -v Output more detailed information 27 | ``` 28 | 29 | ## Links 30 | 31 | - [Product Hunt post](https://www.producthunt.com/posts/speed-test-cli) 32 | 33 | ## Related 34 | 35 | - [fast-cli](https://github.com/sindresorhus/fast-cli) - Test your download speed using fast.com 36 | -------------------------------------------------------------------------------- /screenshot.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sindresorhus/speed-test/200dda1b649eb00864a183912a9720ce2def7ae3/screenshot.gif -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import childProcess from 'node:child_process'; 2 | import test from 'ava'; 3 | import {execa} from 'execa'; 4 | import {pEvent} from 'p-event'; 5 | 6 | test('main', async t => { 7 | const subProcess = childProcess.spawn('./cli.js', {stdio: 'inherit'}); 8 | t.is(await pEvent(subProcess, 'close'), 0); 9 | }); 10 | 11 | test('--json', async t => { 12 | const {stdout} = await execa('./cli.js', ['--json']); 13 | const parsed = JSON.parse(stdout); 14 | t.truthy(parsed.ping); 15 | t.truthy(parsed.upload); 16 | t.truthy(parsed.download); 17 | t.falsy(parsed.data); 18 | }); 19 | 20 | test('--verbose --json', async t => { 21 | const {stdout} = await execa('./cli.js', ['--verbose', '--json']); 22 | const parsed = JSON.parse(stdout); 23 | t.truthy(parsed.ping); 24 | t.truthy(parsed.upload); 25 | t.truthy(parsed.download); 26 | t.truthy(parsed.data); 27 | }); 28 | --------------------------------------------------------------------------------