Skip to content

Instantly share code, notes, and snippets.

@PsychoLlama
Created June 2, 2016 21:25
Show Gist options
  • Save PsychoLlama/58033f8a38ac9985b97b3ae45039b9d4 to your computer and use it in GitHub Desktop.
Save PsychoLlama/58033f8a38ac9985b97b3ae45039b9d4 to your computer and use it in GitHub Desktop.
Load testing gun using panic
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Panic load testing</title>
</head>
<body>
<script src='panic.js'></script>
<script src='gun.js'></script>
<script>
panic.server('http://localhost:8080');
</script>
</body>
</html>
/*
This file should be one level
deeper than index.html.
*/
'use strict';
var panic = require('panic-server');
var wd = require('selenium-webdriver');
var Gun = require('gun');
var http = require('http');
var path = require('path');
var fs = require('fs');
var port = process.env.PORT || 8080;
var gunPort = process.env.GUN_PORT || 3000;
var dataFile = path.join(__dirname, Math.random().toString() + '.json');
function open(type) {
var driver = new wd.Builder()
.forBrowser(type)
.build();
var home = 'http://localhost:' + port + '/index.html';
driver.get(home);
return driver;
}
var server = new http.Server(function (req, res) {
if (req.url === '/') {
req.url = '/index.html';
}
try {
var file = path.join(__dirname, '..', req.url);
var page = fs.readFileSync(file);
res.end(page);
} catch (err) {
// don't care
}
});
server.listen(8080);
var gun = new Gun({
file: dataFile
});
server.on('request', gun.wsp.server);
var gunServer = new http.Server(gun.wsp.server);
gun.wsp(gunServer);
gunServer.listen(gunPort);
panic.server(server);
var browsers = panic.clients.filter(function (client) {
return client.platform.name !== 'Node.js';
});
function waitFor (num, list) {
return new Promise(function (res) {
function ready() {
if (list.length < num) {
return;
}
res();
list.removeListener('add', ready);
return true;
}
if (!ready()) {
list.on('add', ready);
}
});
}
function load (done) {
var script = document.createElement('script');
script.src = src;
script.onload = done;
document.body.appendChild(script);
}
before(function () {
this.timeout(15000);
open('phantomjs');
open('phantomjs');
return waitFor(2, browsers);
});
describe('Gun under heavy load', function () {
this.timeout(1500000);
var alice = browsers.pluck(1);
var bob = browsers.excluding(alice).pluck(1);
var scope = {
key: Math.random().toString(16).slice(2),
gunPort: gunPort,
server: 'http://localhost:' + gunPort + '/gun',
times: 10,
'@scope': true
};
before(function () {
return browsers.run(load, {
src: 'https://cdn.jsdelivr.net/es6-promise/3.1.2/es6-promise.js',
'@scope': true
});
});
before(function () {
return browsers.run(function () {
window.gun = new Gun(server).get('data').put({
'ignore me': true
});
Gun.chain.promise = function () {
var gun = this;
return new Promise(function (res) {
gun.val(res);
});
}
}, scope);
});
it('should still be able to transmit data', function () {
return alice.run(function () {
for (var i = 0; i < times; i++) {
gun.path(i).put({
name: 'User #' + i
});
}
}, scope)
.then(function () {
return bob.run(function (done) {
var data = [];
var test = this;
for (var i = 0; i < times; i++) {
data.push(gun.path(i).promise());
}
Promise.all(data).then(function (users) {
users.forEach(function (user, index) {
if (user.name !== 'User #' + index) {
test.fail('Incorrect data. Expected "' + user + '" to be "User #' + index + '"');
}
});
done();
});
}, scope);
});
});
});
after(function () {
var file = path.join(__dirname, '..', '..', 'delete-me.json');
try {
fs.unlinkSync(dataFile);
} catch (err) {
}
});
{
"name": "load-test",
"version": "0.1.0",
"description": "Load testing using panic",
"main": "index.js",
"scripts": {
"test": "mocha test/mocha"
},
"keywords": [
"Load-testing",
"gun",
"gunDB",
"panic",
"distributed"
],
"author": "Jesse Gibson <jesse@gundb.io> (http://techllama.com)",
"license": "MIT",
"dependencies": {
"gun": "^0.3.92",
"mocha": "^2.5.3",
"panic-server": "^0.3.0",
"selenium-webdriver": "^2.53.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment