beradrian / jsbandwidth

Test bandwidth inside your browser
81 stars 21 forks source link

Bower NPM License Build Status

NPM

JSBandwidth

To test inside a browser the bandwidth and latency, there's no easy way. This is what JsBandwidth tries to achieve.

This project was initially forked from https://code.google.com/p/jsbandwidth/. At this moment it became a total rewrite.

License

I decided to keep the same license as the initial project, MIT.

Installation

npm install jsbandwidth

Browser support

This library is using XMLHttpRequest and Promise. XMLHttpRequest is supported starting with IE7. For older browsers (are you serious?) you can use this shim. For browsers that do not feature Promise support you can use any of the following shims:

Server-side set-up

  1. Set up a web server of your choice.
  2. Depending on your web server, drop the corresponding project files in your web server's document root (or a sub-directory, if you wish). What src/main/webapp/post.* file you should choose depends on your web server. The upload test needs to be able to send a POST request to the server. The receiving page doesn't have to do anything with the data. However, some servers will not allow you to send a POST request to a .htm file. Therefore, the project includes several blank server side script files (post.aspx, post.php, post.pl). src/main/webapp/test.bin is mandatory, but it's nothing more than random bytes.

Spring Controller

If you want to use a Spring Controller to post test data you can define a controller method like this

@RequestMapping("/test-post")
public @ResponseBody String testPost() {
    return "true";
}

and then specify options.uploadUrl='/test-post'. TestController is a fully working Spring controller.

Please be aware that some servers, like Tomcat, by their default setup can impose a limit on the upload data size to avoid DoS attacks. You either modify that setup or specify options.uploadDataMaxSize.

JavaScript API

The JavaScript API works with both Angular and jQuery, depending on what library is included (if both, Angular is preferred).

First you need to get hold of the jsBandwidth object.


myApp.controller('JsBandwidthTestController', ["$scope", "jsBandwidth", function ($scope, jsBandwidth) {
    $scope.test = function(options, callback, errorCallback) {
        jsBandwidth.testSpeed(options)
                .then(function(result) {
                        $scope.result = result;
                        callback();
                    }
                    , function(error) {
                        $scope.error = error;
                        errorCallback();
                    });
    };
}]);

    var jsBandwidth = require("jsbandwidth");

The jsBandwidth object has 3 methods with a similar signature:

The options parameter is an object and it has the following fields

All three methods return a promise and you can use the then method. That promise is also augmented with a cancel() method.

Example

var jsBandwidth = require("jsbandwidth");
jsBandwidth.testSpeed(options)
    .then(function (result) {
            console.log("Latency is " + result.latency + " ms, download speed is " + result.downloadSpeed + "bps and upload speed is " result.uploadSpeed + "bps");
        },
        function(error) {
            console.log("An error occured during net speed test.");
        });

Angular controller

An Angular controller, called JsBandwidthController, is provided for your convenience. The controller uses the service and it defines the following fields/methods in the scope

'complete` event is emitted when the test is completed or 'error' if an error occured.

Below is an example on how to use it in your page:

<div data-ng-controller="JsBandwidthController" class="netSpeedTest"
        data-ng-init="options.downloadUrl='/test.bin';">
    <span data-ng-if="error">
        <span>Error</span>: <span data-ng-bind="error.status"></span>
    </span>
    <span data-ng-if="result">
        <span>Latency:</span>
        <span data-ng-bind="result.latency"></span><span>ms</span>
        <span>Download speed:</span>
        <span data-ng-bind="convertToMbps(result.downloadSpeed)"></span><span>Mbps</span>
        <span>Upload speed</span>
        <span data-ng-bind="convertToMbps(result.uploadSpeed)"></span><span>Mbps</span>
    </span>
    <button data-ng-if="!test" data-ng-click="start()" class="start">Start test</button>
    <button data-ng-if="test" data-ng-click="cancel()" class="cancel">Cancel test</button>
</div>

Formatting

The speed is calculated in bps (bits per second). In the Angular controller you have the method convertToMbps for your convenience. If you want to format it differently, you can use js-quantities.

How to get support

Development

How to make a new release

  1. Change the version number in bower.json and package.json
  2. Create a new release in github with the same name for tag and title as the version number (e.g. 1.0.0). Do not forget to include the changelog in the release description.
  3. Run npm publish to publish the new version to npm

Testing

To run the tests do npm run test.