├── .gitignore
├── .travis.yml
├── README.md
├── bower.json
├── package.json
└── src
├── main
├── java
│ └── TestController.java
├── js
│ └── jsbandwidth.js
└── webapp
│ ├── post.aspx
│ ├── post.jsp
│ ├── post.php
│ ├── post.pl
│ └── test.bin
└── test
├── karma.conf.js
└── spec
└── JsBandwidthSpec.js
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | npm-debug.log
3 | .project
4 | .settings
5 | build
6 | index.js
7 |
8 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: node_js
2 | node_js:
3 | - stable
4 | before_script:
5 | - export DISPLAY=:99.0
6 | - sh -e /etc/init.d/xvfb start
7 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |  [](https://www.npmjs.com/package/jsbandwidth) 
2 | [](https://travis-ci.org/beradrian/jsbandwidth)
3 |
4 | [](https://nodei.co/npm/jsbandwidth/)
5 |
6 | # JSBandwidth
7 |
8 | To test inside a browser the bandwidth and latency, there's no easy way. This is what JsBandwidth tries to achieve.
9 |
10 | This project was initially forked from https://code.google.com/p/jsbandwidth/. At this moment it became a total rewrite.
11 |
12 | ## License
13 | I decided to keep the same license as the initial project, [MIT](http://opensource.org/licenses/mit-license.php).
14 |
15 | ## Installation
16 |
17 | npm install jsbandwidth
18 |
19 | ## Browser support
20 | This library is using `XMLHttpRequest` and `Promise`. XMLHttpRequest is supported starting with IE7. For older browsers (are you serious?) you can use this [shim](https://gist.github.com/beradrian/5d30bcbf7e8e0d9b5090). For browsers that do not feature [Promise support](http://caniuse.com/#search=promise) you can use any of the following shims:
21 | - https://github.com/jakearchibald/es6-promise
22 | - https://github.com/taylorhakes/promise-polyfill
23 | - https://github.com/getify/native-promise-only
24 |
25 | **If your shim is not here, please drop me an email or enter a new issue to include it.**
26 |
27 | ## Server-side set-up
28 | 1. Set up a web server of your choice.
29 | 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.
30 |
31 | ### Spring Controller
32 |
33 | If you want to use a Spring Controller to post test data you can define a controller method like this
34 |
35 | @RequestMapping("/test-post")
36 | public @ResponseBody String testPost() {
37 | return "true";
38 | }
39 |
40 | and then specify `options.uploadUrl='/test-post'`. [TestController](src/main/java/TestController.java) is a fully working Spring controller.
41 |
42 | 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`.
43 |
44 | ## JavaScript API
45 | The JavaScript API works with both Angular and jQuery, depending on what library is included (if both, Angular is preferred).
46 |
47 | First you need to get hold of the `jsBandwidth` object.
48 |
49 | - In Angular
50 |
51 |
70 | var jsBandwidth = require("jsbandwidth");
71 |
72 |
73 | The `jsBandwidth` object has 3 methods with a similar signature:
74 | - `testLatency(options)`
75 | - `testDownloadSpeed(options)`
76 | - `testUploadSpeed(options)`
77 | - `testSpeed(options)` which combines all the above into one
78 |
79 | The `options` parameter is an object and it has the following fields
80 | - `latencyTestUrl` the URL used for latency testing. Usually a big binary content is expected to be downloaded. If not specified, it is considered to be the same as `downloadUrl`, but requested with `HEAD` method.
81 | - `downloadUrl` the URL used for download speed testing. Usually a big binary content is expected to be downloaded.
82 | - `uploadUrl` the URL used for upload speed testing. It should accept a POST method.
83 | - `uploadData` the data that is sent to the server to test the upload
84 | - `uploadDataMaxSize` if specified `uploadData` is going to be truncated to this maximum length. 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 setting or use `options.uploadDataMaxSize`. The usual limit is 2Mb.
85 | - `uploadDataSize` if `uploadData` is not specified, then a chunk of this size is randomly generated instead
86 | - 'ajax' the AJAX service, either from jQuery or $http from Angular. If not specified, it will be automatically detected depending whether jQuery or Angular is included.
87 |
88 | All three methods return a [promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) and you can use the `then` method. That promise is also augmented with a `cancel()` method.
89 |
90 | ### Example
91 |
92 | var jsBandwidth = require("jsbandwidth");
93 | jsBandwidth.testSpeed(options)
94 | .then(function (result) {
95 | console.log("Latency is " + result.latency + " ms, download speed is " + result.downloadSpeed + "bps and upload speed is " result.uploadSpeed + "bps");
96 | },
97 | function(error) {
98 | console.log("An error occured during net speed test.");
99 | });
100 |
101 | ### Angular controller
102 | 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
103 | - `test` this is the service running the speed test. If null or undefined, there's no test currently running, so it can be used for checking if a speed test is currently running.
104 | - `options` the options used to run the speed test
105 | - `result.latency` the estimated latency in ms. If `result` is null or undefined, the test is in progress or ended with an error.
106 | - `result.downloadSpeed` the estimated download speed in bps.
107 | - `result.uploadSpeed` the estimated upload speed in bps.
108 | - `error` if null or undefined, then a test is in progress or completed successfully. If not null, then an error occured during the last speed test.
109 | - `error.status` the error status
110 |
111 | 'complete` event is emitted when the test is completed or 'error' if an error occured.
112 |
113 | Below is an example on how to use it in your page:
114 |
115 |