<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/measuring-monitoring-internet-speed-with-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://rud.is/b/2017/11/11/measuring-monitoring-internet-speed-with-r/"> R – rud.is</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Working remotely has many benefits, but if you work remotely in an area like, say, rural Maine, one of those benefits is not massively speedy internet connections. Being able to go fast and furious on the internet is one of the many things I miss about our time in Seattle and it is unlikely that we’ll be seeing Google Fiber in my small town any time soon. One other issue is that residential plans from evil giants like Comcast come with things like “bandwidth caps”. I suspect many WFH-ers can live within those limits, but I work with internet-scale data and often shunt extracts or whole datasets to the DatCave™ server farm for local processing. As such, I pay an extra penalty as a Comcast “Business-class” user that has little benefit besides getting slightly higher QoS and some faster service response times when there are issues. </p>
<p>Why go into all that? Well, I recently decided to bump up the connection from 100 Mb/s to 150 Mb/s (and managed to do so w/o increasing the overall monthly bill at all) but wanted to have a way to regularly verify I am getting what I’m paying for <strong>without</strong> having to go to an interactive “speed test” site.</p>
<p>There’s a handy <a rel="nofollow" href="https://github.com/sivel/speedtest-cli/" target="_blank"><code>speedtest-cli</code><img alt="🔗" src="https://s.w.org/images/core/emoji/2.3/72x72/1f517.png" class="wp-smiley"></a>, which is a python-based module with a command-line interface that can perform speed tests against <a rel="nofollow" href="http://speedtest.net/" target="_blank">Ookla’s legacy server</a>. (You’ll notice that link forwards you to their new socket-based test service; neither <code>speedtest-cli</code> nor the code in the package being shown here uses that yet)</p>
<p>I run plenty of ruby, python, node and go (et al) programs on the command-line, but I wanted a way to measure this in R-proper (storing individual test results) on a regular basis as well as run something from the command-line whenever I wanted an interactive test. Thus begat <a rel="nofollow" href="https://github.com/hrbrmstr/speedtest" target="_blank"><code>speedtest</code><img alt="🔗" src="https://s.w.org/images/core/emoji/2.3/72x72/1f517.png" class="wp-smiley"></a>.</p>
<h3>Testing the Need for Speed</h3>
<p>After you <code>devtools::install_github(&quot;hrbrmstr/speedtest&quot;)</code> the package, you can either type <code>speedtest::spd_test()</code> at an R console or <code>Rscript --quiet -e 'speedtest::spd_test()'</code> on the command-line to see something like the following:</p>
<p><a rel="nofollow" href="https://i2.wp.com/rud.is/b/wp-content/uploads/2017/11/spdtst.gif?ssl=1" target="_blank"><img alt="" src="https://i2.wp.com/rud.is/b/wp-content/uploads/2017/11/spdtst.gif?resize=780%2C421&ssl=1" class="wp-image-7052" data-recalc-dims="1"></a></p>
<p>What you see there is a short-hand version of what’s available in the package:</p>
<ul>
<li><code>spd_best_servers</code>: Find “best” servers (latency-wise) from master server list</li>
<li><code>spd_closest_servers</code>: Find “closest” servers (geography-wise) from master server list</li>
<li><code>spd_compute_bandwidth</code>: Compute bandwidth from bytes transferred and time taken</li>
<li><code>spd_config</code>: Retrieve client configuration information for the speedtest</li>
<li><code>spd_download_test</code>: Perform a download speed/bandwidth test</li>
<li><code>spd_servers</code>: Retrieve a list of SpeedTest servers</li>
<li><code>spd_upload_test</code>: Perform an upload speed/bandwidth test</li>
<li><code>spd_test</code>: Test your internet speed/bandwidth</li>
</ul>
<p>The general idiom is to grab the test configuration file, collect the master list of servers, figure out which servers you’re going to test against and perform upload/download tests + collect the resultant statistics:</p>
<pre><code>library(speedtest)
library(stringi)
library(hrbrthemes)
library(ggbeeswarm)
library(tidyverse)

config &lt;- spd_config()

servers &lt;- spd_servers(config=config)
closest_servers &lt;- spd_closest_servers(servers, config=config)
only_the_best_severs &lt;- spd_best_servers(closest_servers, config)

glimpse(spd_download_test(closest_servers[1,], config=config))
## Observations: 1
## Variables: 15
## $ url     </code></pre>