Test gun by passing options, Browser constructor automatically begins a test on a new window, added package.json, patch function for options (validation/default values).

Exposed a function named "test" that takes an options object, with things like how many gun requests to fire in total, what data to send, where to put it, what peers to connect to, etc...

Exposed a Browser constructor that opens up a new window and runs the test function with the options provided, returning an interface to the window.

Added a patch function to validate the option input and provide defaults where there aren't any values.
This commit is contained in:
Jesse Gibson
2015-12-17 23:20:57 -07:00
parent aa4d9b550c
commit d38154cb6f
6 changed files with 119 additions and 2 deletions

BIN
.DS_Store vendored

Binary file not shown.

26
browser.js Normal file
View File

@@ -0,0 +1,26 @@
/*globals patch*/
var Browser;
(function () {
'use strict';
Browser = function (opt) {
if (!(this instanceof Browser)) {
return new Browser(opt);
}
var browser = this;
browser.opt = patch(opt);
browser.window = window.open('./', browser.opt.id);
browser.window.addEventListener('load', function () {
browser.window.test(browser.opt);
});
};
Browser.prototype = {
constructor: Browser,
close: function () {
this.window.close();
return this;
}
};
}());

View File

@@ -65,7 +65,9 @@
Any help on building this out would be appreciated!
</p>
<script src="gun.js"></script>
<script src="patch.js"></script>
<script src="index.js"></script>
<script src="browser.js"></script>
</body>
</html>

View File

@@ -1,2 +1,24 @@
/*globals Gun, console */
var gun = new Gun('http://localhost:8080/gun');
/*globals Gun, console, patch */
var test, gun;
test = function (opt) {
'use strict';
opt = patch(opt);
var i, count, db = gun.get(opt.key).set();
gun = new Gun(opt.peers);
count = 0;
function ack(num) {
return function (err, ok) {
count += 1;
console.log('ACK:', err, ok, 'on', num);
if (count === opt.amount) {
console.log('0% loss');
}
};
}
for (i = 1; i <= opt.amount; i += 1) {
db.set(opt.data, ack(i));
}
return db;
};

30
package.json Normal file
View File

@@ -0,0 +1,30 @@
{
"name": "panic",
"version": "0.1.0",
"description": "Test gun against a storm of requests",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/PsychoLlama/panic.git"
},
"keywords": [
"gun",
"gundb",
"test",
"testing",
"distributed"
],
"author": "Jesse Gibson <jesse@gundb.io> (http://techllama.com)",
"license": "(Zlib OR MIT OR Apache-2.0)",
"bugs": {
"url": "https://github.com/PsychoLlama/panic/issues"
},
"homepage": "https://github.com/PsychoLlama/panic#readme",
"dependencies": {
"gun-level": "^2.1.1"
}
}

37
patch.js Normal file
View File

@@ -0,0 +1,37 @@
/*globals Gun*/
/*
Expect an object like this:
var thing = {
interval: Number,
peers: Array,
amount: Number,
id: String,
key: String,
path: String,
data: *
};
*/
function patch(opt) {
'use strict';
if (!opt) {
throw new Error('No options provided');
}
if (!opt.id) {
throw new Error('No ID given');
}
var url = location.protocol + '//';
url += location.host;
url += '/gun';
opt.key = opt.key || 'panic/test/';
opt.interval = opt.interval || 20;
opt.peers = opt.peers || [url];
opt.amount = opt.amount || 1000;
opt.path = opt.path || Gun.text.random();
opt.data = opt.data || Gun.text.random();
return opt;
}