From d38154cb6f1fa877fb4e214a1216ccf5aa236542 Mon Sep 17 00:00:00 2001 From: Jesse Gibson Date: Thu, 17 Dec 2015 23:20:57 -0700 Subject: [PATCH] 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. --- .DS_Store | Bin 6148 -> 6148 bytes browser.js | 26 ++++++++++++++++++++++++++ index.html | 2 ++ index.js | 26 ++++++++++++++++++++++++-- package.json | 30 ++++++++++++++++++++++++++++++ patch.js | 37 +++++++++++++++++++++++++++++++++++++ 6 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 browser.js create mode 100644 package.json create mode 100644 patch.js diff --git a/.DS_Store b/.DS_Store index ad238ee6bdfcc1b41333dd55640a191610e6e2da..73013b7b403c5fd6fdd60eb3d63864c9f89b2d25 100644 GIT binary patch literal 6148 zcmeHK%TD7k6g`F@rJY%HIm?I@t0Mj&gUU#i1{SSY18FNBlWD3Zone{Jn2(wd;BVmE zYlGq@Y!M(|$v%#Kk9~cv8#e&V@n(Jm3;@I|f>EDUhsn5UC8q8jH-Km6zDLBp^A z>_^{gET7*EO^%tgI2qp{1^%MMf~_I$Q2Of#$BU=O`Inf}|DOIcGPfDrffrc^V^w88 zH)XY^=RVHe>Lm^t%O8y-*ZAk`8S%NqFI8*B=L}bDSIF64=~%^HQMMHqS2=U5bc~YI z_c%A4?>BOP|M)?_e+`{ukJHji^V0O+(@14kTQh4mnx0=ZYohE0<0z={M!DPiN)~+~?e3BEHUr^F#+oVQolIxF^E*_?Kq zE>X*4M9lSydzkq%w@x8L`%uO^TP*%!G1NhUpg>UIMFDv~BrJld!`h<0I#}r^05M{> z8r$-hLO6-T)M0IrBQ#^FL`ya96T?_K`xEDvI;<^PI*j}HFm7ezzEF&=&iNB{he<7l zIw%kn@DB`mu~2NHo}wrd0|Nsi1A_nqLj{8#Lo!1#LvcaL#KPs14MbQbGqcXx sEW#nova!LNX)`+qKL=3XW@~ diff --git a/browser.js b/browser.js new file mode 100644 index 0000000..66435f8 --- /dev/null +++ b/browser.js @@ -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; + } + }; + +}()); diff --git a/index.html b/index.html index 96cc25a..7ad482a 100644 --- a/index.html +++ b/index.html @@ -65,7 +65,9 @@ Any help on building this out would be appreciated!

+ + diff --git a/index.js b/index.js index 454c2a6..0101d38 100644 --- a/index.js +++ b/index.js @@ -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; +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..ced00ee --- /dev/null +++ b/package.json @@ -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 (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" + } +} diff --git a/patch.js b/patch.js new file mode 100644 index 0000000..5ce0638 --- /dev/null +++ b/patch.js @@ -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; +}