Permalink
Browse files

collection is all asynchronous, no event emitters and key_map

  • Loading branch information...
1 parent 0ab1dc5 commit 30d78f6b9a7f12515963785e9c17a0557a9df98e @pgte committed Nov 22, 2010
View
18 LICENSE
@@ -0,0 +1,18 @@
+Copyright 2010 Pedro Teixeira. All rights reserved.
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to
+deal in the Software without restriction, including without limitation the
+rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+sell copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+IN THE SOFTWARE.
@@ -3,54 +3,68 @@ module.exports.run = function(benchmark, next) {
var OBJECT_COUNT= 100000;
benchmark.start('opening collection');
- var collection = require(__dirname + '/../lib/alfred/collection.js').open(__dirname + '/../tmp/collection_bench.alf');
- benchmark.end();
- var assert = require('assert');
+ require(__dirname + '/../lib/alfred/collection.js').open(__dirname + '/../tmp/collection_bench.alf', function(err, collection) {
+ if (err) {
+ throw err;
+ }
+
+ benchmark.end();
+ var assert = require('assert');
- var createRandomString = function(string_length) {
- var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
- var randomstring = '';
- for (var i=0; i<string_length; i++) {
- var rnum = Math.floor(Math.random() * chars.length);
- randomstring += chars.substring(rnum,rnum+1);
- }
- return randomstring;
- };
+ var createRandomString = function(string_length) {
+ var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
+ var randomstring = '';
+ for (var i=0; i<string_length; i++) {
+ var rnum = Math.floor(Math.random() * chars.length);
+ randomstring += chars.substring(rnum,rnum+1);
+ }
+ return randomstring;
+ };
- var createRandomObject = function() {
- return {
- a: createRandomString(10),
- b: createRandomString(50),
- c: createRandomString(50)
+ var createRandomObject = function() {
+ return {
+ a: createRandomString(10),
+ b: createRandomString(50),
+ c: createRandomString(50)
+ };
};
- };
- benchmark.start('prunning collection');
- collection.prune(function() {
- benchmark.end();
- benchmark.start('populating collection', OBJECT_COUNT);
- for (var i = 0; i < OBJECT_COUNT; i ++) {
- record = createRandomObject();
- collection.write(record);
- }
- benchmark.end();
-
- // wait for flush
- setTimeout(function() {
- var index = 0;
- benchmark.start('reading collection', OBJECT_COUNT);
- collection.read(function(error, record) {
- assert.equal(error, null);
- if (record === null) {
- benchmark.end();
- collection.end();
- next();
- } else {
- index ++;
- }
- });
- }, 2000);
-
+ benchmark.start('prunning collection');
+ collection.clear(function(err) {
+ if (err) {
+ throw err;
+ }
+ benchmark.end();
+ benchmark.start('populating collection', OBJECT_COUNT);
+ for (var i = 0; i < OBJECT_COUNT; i ++) {
+ record = createRandomObject();
+ collection.write(record, function(err) {
+ if (err) {
+ throw err;
+ }
+ });
+ }
+ benchmark.end();
+
+ // wait for flush
+ setTimeout(function() {
+ var index = 0;
+ benchmark.start('reading collection', OBJECT_COUNT);
+ collection.read(function(error, record) {
+ assert.equal(error, null);
+ if (record === null) {
+ benchmark.end();
+ collection.end();
+ next();
+ } else {
+ index ++;
+ }
+ });
+ }, 2000);
+
+ });
+
+
});
}
@@ -1,60 +1,69 @@
module.exports.run = function(benchmark, next) {
-
- var collection = require(__dirname + '/../lib/alfred/collection.js').open(__dirname + '/../tmp/collection_filter_benchmark.alf');
+
var assert = require('assert');
- var OBJECT_COUNT = 100000;
-
- collection.on('error', function(error) {
- throw error;
- });
+ require(__dirname + '/../lib/alfred/collection.js').open(__dirname + '/../tmp/collection_filter_benchmark.alf', function(err, collection) {
+ if (err) {
+ throw err;
+ }
+ var OBJECT_COUNT = 100000;
- var createRandomString = function(string_length) {
- var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
- var randomstring = '';
- for (var i=0; i<string_length; i++) {
- var rnum = Math.floor(Math.random() * chars.length);
- randomstring += chars.substring(rnum,rnum+1);
- }
- return randomstring;
- };
-
- var createRandomObject = function(c_value) {
- return {
- a: createRandomString(10),
- b: createRandomString(50),
- c: c_value
+ var createRandomString = function(string_length) {
+ var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
+ var randomstring = '';
+ for (var i=0; i<string_length; i++) {
+ var rnum = Math.floor(Math.random() * chars.length);
+ randomstring += chars.substring(rnum,rnum+1);
+ }
+ return randomstring;
};
- };
- var compareObjects = function(a, b) {
- return a.a === b.a && a.b === b.b && a.c === b.c;
- };
+ var createRandomObject = function(c_value) {
+ return {
+ a: createRandomString(10),
+ b: createRandomString(50),
+ c: c_value
+ };
+ };
- var c_values = [createRandomString(50), createRandomString(50), createRandomString(50)]
-
- collection.prune(function() {
- for (var i = 0; i < OBJECT_COUNT; i ++) {
- record = createRandomObject(c_values[i % c_values.length]);
- collection.write(record);
- }
+ var compareObjects = function(a, b) {
+ return a.a === b.a && a.b === b.b && a.c === b.c;
+ };
+
+ var c_values = [createRandomString(50), createRandomString(50), createRandomString(50)]
+
+ collection.clear(function(err) {
+ if (err) {
+ throw err;
+ }
+ for (var i = 0; i < OBJECT_COUNT; i ++) {
+ record = createRandomObject(c_values[i % c_values.length]);
+ collection.write(record, function(err) {
+ if (err)
+ throw err;
+ });
+ }
+
+ // wait for flush
+ setTimeout(function() {
+ var index = 0;
+ benchmark.start('Collection filter', OBJECT_COUNT);
+ collection.filter(function(record) {
+ return record.c == c_values[1];
+ }, function(error, result) {
+ if (error) {
+ throw error;
+ }
+ if (result === null) {
+ benchmark.end();
+ next();
+ }
+ });
+ }, 2000);
+
+ });
- // wait for flush
- setTimeout(function() {
- var index = 0;
- benchmark.start('Collection filter', OBJECT_COUNT);
- collection.filter(function(record) {
- return record.c == c_values[1];
- }, function(error, result) {
- if (error) {
- throw error;
- }
- benchmark.end();
- next();
- });
- }, 2000);
-
});
+
-}
-
+}
@@ -0,0 +1,87 @@
+module.exports.run = function(benchmark, next) {
+
+ var assert = require('assert');
+
+ require(__dirname + '/../lib/alfred/key_map.js').open(__dirname + '/../tmp/key_map_benchmark.alf', function(err, key_map) {
+ if (err) {
+ throw err;
+ }
+
+ var KEY_COUNT = 50000;
+ var RETRIEVE_RANDOM_COUNT = 10;
+
+ var createRandomString = function(string_length) {
+ var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
+ var randomstring = '';
+ for (var i=0; i<string_length; i++) {
+ var rnum = Math.floor(Math.random() * chars.length);
+ randomstring += chars.substring(rnum,rnum+1);
+ }
+ return randomstring;
+ };
+
+ var createRandomObject = function() {
+ return {
+ a: createRandomString(10),
+ b: createRandomString(50),
+ c: createRandomString(50)
+ };
+ };
+
+ var compareObjects = function(a, b) {
+ return a.a === b.a && a.b === b.b && a.c === b.c;
+ };
+
+ var keys = [];
+ var map = {};
+
+ key_map.clear(function(err) {
+ if (err) {
+ throw err;
+ }
+ benchmark.start('Insert object into key map', KEY_COUNT);
+ for (var i = 0; i < KEY_COUNT; i ++) {
+ var value = createRandomObject();
+ var key = createRandomString(16);
+ keys.push(key);
+ map[key] = value;
+ key_map.put(key, value, function(err) {
+ if (err) {
+ throw err;
+ }
+ });
+ }
+ benchmark.end();
+
+ // wait for flush
+ setTimeout(function() {
+
+ var sample_key_index = Math.floor(Math.random() * KEY_COUNT)
+ var key = keys[sample_key_index];
+ var retrieved_keys = 0;
+
+ benchmark.start('Retrieve a random object by key from a ' + KEY_COUNT + ' hash map', RETRIEVE_RANDOM_COUNT);
+ for(var i = 0; i < RETRIEVE_RANDOM_COUNT; i++) {
+ key_map.get(key, function(err, record) {
+ if (err) {
+ throw err;
+ }
+ retrieved_keys ++;
+ if (retrieved_keys == RETRIEVE_RANDOM_COUNT) {
+ benchmark.end();
+ next();
+ }
+ });
+ }
+
+ // test than when we try to retrieve with a non-existing ket, we get null back
+
+ }, 5000);
+
+ });
+
+ });
+
+
+}
+
Oops, something went wrong.

0 comments on commit 30d78f6

Please sign in to comment.