Permalink
Browse files

Stub out queue tests

  • Loading branch information...
1 parent ee415ec commit 13740a84783c2d3b2fc3761876038f2c446ba0c4 @pquerna committed Sep 17, 2010
Showing with 131 additions and 0 deletions.
  1. +29 −0 queues/array.js
  2. +36 −0 queues/array_offset.js
  3. +66 −0 queues/test.js
View
@@ -0,0 +1,29 @@
+/* This code is PUBLIC DOMAIN, and is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND. See the accompanying
+ * LICENSE file.
+ */
+
+function ArrayQueue()
+{
+ this.q = [];
+}
+
+ArrayQueue.prototype.insert = function(value)
+{
+ this.q.push(value);
+}
+
+ArrayQueue.prototype.shift = function()
+{
+ if (this.q.length != 0) {
+ return this.q.shift();
+ }
+ else {
+ return null;
+ }
+}
+
+exports.createQueue = function()
+{
+ return new ArrayQueue();
+}
View
@@ -0,0 +1,36 @@
+/* This code is PUBLIC DOMAIN, and is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND. See the accompanying
+ * LICENSE file.
+ */
+
+function ArrayOffsetQueue()
+{
+ this.offset = 0;
+ this.head = [];
+ this.tail = [];
+}
+
+ArrayOffsetQueue.prototype.insert = function(value)
+{
+ this.tail.push(value);
+}
+
+ArrayOffsetQueue.prototype.shift = function()
+{
+ if (this.offset === this.head.length) {
+ var tmp = this.head;
+ this.head = this.tail;
+ this.tail = tmp;
+ this.tail.length = 0;
+ this.offset = 0;
+ if (this.head.length === 0) {
+ return null;
+ }
+ }
+ return this.head[this.offset++];
+}
+
+exports.createQueue = function()
+{
+ return new ArrayOffsetQueue();
+}
View
@@ -0,0 +1,66 @@
+/* This code is PUBLIC DOMAIN, and is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND. See the accompanying
+ * LICENSE file.
+ */
+
+var assert = require('assert');
+
+var queues = {
+ 'array': require('./array'),
+ 'array_offset': require('./array_offset')
+};
+
+function DummyObject(value)
+{
+ this.foo = value;
+}
+
+DummyObject.prototype.some_method = function()
+{
+ return this.foo;
+}
+
+function benchmark_run(q, pull_size, insert_size, total_size)
+{
+ var i = 0;
+ var j, v;
+
+ while (i < total_size) {
+ for (j = 0; j < insert_size && i < total_size; j++, i++) {
+ q.insert(new DummyObject(i));
+ }
+ for (j = 0; j < pull_size; j++) {
+ v = q.shift();
+ if (v !== null) {
+ delete v;
+ }
+ }
+ }
+
+ while (true) {
+ v = q.shift();
+ if (v === null) {
+ break;
+ }
+ delete v;
+ };
+}
+
+function benchmark_method(m)
+{
+ var maxsize = 100000;
+ benchmark_run(m.createQueue(), 1, 10, maxsize);
+ benchmark_run(m.createQueue(), 10, 10, maxsize);
+ benchmark_run(m.createQueue(), 10, 1000, maxsize);
+ benchmark_run(m.createQueue(), 10, 1, maxsize);
+ benchmark_run(m.createQueue(), 100, 1, maxsize);
+}
+
+for (k in queues) {
+ var m = queues[k];
+ var start = Date.now();
+ benchmark_method(m);
+ var end = Date.now();
+ delete q;
+ console.log(k, "took", end-start, 'ms');
+}

0 comments on commit 13740a8

Please sign in to comment.