[Pkg-javascript-commits] [node-leveldown] 80/492: added basic benchmark suite

Andrew Kelley andrewrk-guest at moszumanska.debian.org
Sun Jul 6 17:13:47 UTC 2014


This is an automated email from the git hooks/post-receive script.

andrewrk-guest pushed a commit to annotated tag rocksdb-0.10.1
in repository node-leveldown.

commit 8603b3eaf155990858998980ed0a69b17c5a8772
Author: Rod Vagg <rod at vagg.org>
Date:   Mon Nov 19 11:13:27 2012 +1100

    added basic benchmark suite
---
 test/benchmarks/README.md     |  7 ++++
 test/benchmarks/benchmarks.js | 89 +++++++++++++++++++++++++++++++++++++++++++
 test/benchmarks/index.js      | 58 ++++++++++++++++++++++++++++
 test/benchmarks/package.json  | 13 +++++++
 4 files changed, 167 insertions(+)

diff --git a/test/benchmarks/README.md b/test/benchmarks/README.md
new file mode 100644
index 0000000..08c4acf
--- /dev/null
+++ b/test/benchmarks/README.md
@@ -0,0 +1,7 @@
+# LevelUP Benchmarks
+
+Set up by running `npm install` in this directory.
+
+Run with `node ./`.
+
+Edit *benchmarks.js* to add more benchmarks to the suite.
\ No newline at end of file
diff --git a/test/benchmarks/benchmarks.js b/test/benchmarks/benchmarks.js
new file mode 100644
index 0000000..d5ad40f
--- /dev/null
+++ b/test/benchmarks/benchmarks.js
@@ -0,0 +1,89 @@
+/*
+ * `exports` is a map of benchmark name to benchmark function:
+ *    'name': function (database, callback) { callback() }
+ * If a benchmark needs a setup function then it should take the form:
+ *    'name': {
+ *        'setup': function (database, callback) { callback() }
+ *      , 'fn': function (database, callback) { callback() }
+ *    }
+ *
+ * The setup function and the benchmark function both receive a new and empty
+ * instance of LevelUP which will be deleted after the benchmark is executed.
+ *
+ * The setup function will only be run once but the benchmark function will
+ * be run multiple times to get a good sample. You can store values on `this`
+ * across separate cycles so use it to avoid conflicts in `put()` for example.
+ */
+
+module.exports = {
+
+    // Simple put() operation, 1000 objects at a time
+
+    'LevelUP#put(int, string) x 1000': function (db, cb) {
+      var puts = 1000
+        , received = 0
+        , after = function (err) {
+            if (err) throw err
+            if (++received == puts) cb()
+          }
+
+      if (this.cycle == null) this.cycle = 0
+      else this.cycle++
+
+      for (var i = 0; i < puts; i++)
+        db.put(
+            this.cycle * puts + i
+          , "It'll be top end no worries stands out like a bushie. It'll be cream no dramas flat out like a rotten. As busy as a slabs bloody built like a stonkered. Get a dog up ya oldies no dramas lets get some bottle-o. Built like a schooner as busy as a big smoke. You little ripper ute my you little ripper dag."
+          , after
+        )
+    }
+
+    // Simple put() operation, 100,000 objects at a time
+
+  , 'LevelUP#put(int, string) x 100,000': function (db, cb) {
+      var puts = 100000
+        , received = 0
+        , after = function (err) {
+            if (err) throw err
+            if (++received == puts) cb()
+          }
+
+      if (this.cycle == null) this.cycle = 0
+      else this.cycle++
+
+      for (var i = 0; i < puts; i++)
+        db.put(
+            this.cycle * puts + i
+          , "It'll be top end no worries stands out like a bushie. It'll be cream no dramas flat out like a rotten. As busy as a slabs bloody built like a stonkered. Get a dog up ya oldies no dramas lets get some bottle-o. Built like a schooner as busy as a big smoke. You little ripper ute my you little ripper dag."
+          , after
+        )
+    }
+
+  , 'LevelUP#get(int) x 1000': {
+        'setup': function (db, cb) {
+          var count = 1000
+            , data = []
+
+          for (var i = 0; i < count; i++)
+            data.push({
+                type: 'put'
+              , key: i
+              , value: "It'll be top end no worries stands out like a bushie. It'll be cream no dramas flat out like a rotten. As busy as a slabs bloody built like a stonkered. Get a dog up ya oldies no dramas lets get some bottle-o. Built like a schooner as busy as a big smoke. You little ripper ute my you little ripper dag."
+            })
+
+          db.batch(data, cb)
+        }
+
+      , 'fn': function (db, cb) {
+          var count = 1000
+            , received = 0
+            , after = function (err) {
+                if (err) throw err
+                if (++received == count) cb()
+              }
+
+          for (var i = 0; i < count; i++)
+            db.get(i, after)
+        }
+    }
+}
\ No newline at end of file
diff --git a/test/benchmarks/index.js b/test/benchmarks/index.js
new file mode 100644
index 0000000..c09044f
--- /dev/null
+++ b/test/benchmarks/index.js
@@ -0,0 +1,58 @@
+var Benchmark = require('benchmark')
+  , rimraf = require('rimraf')
+  , async = require('async')
+  , colors = require('colors')
+  , levelup = require('../../')
+  , benchmarks = require('./benchmarks')
+  , dbidx = 0
+
+  , K = function (db, cb) { cb() }
+
+  , makedb = function (cb) {
+      levelup('./bench_' + (dbidx++) + '.db', { createIfMissing: true, errorIfExists: true }, function (err, db) {
+        if (err) throw err
+        cb(db)
+      })
+    }
+
+  , rmdb = function (db, cb) {
+      rimraf(db._location, cb)
+    }
+
+  , run = function (name, setup, fn, cb) {
+      function exec (db, cb) {
+        new Benchmark(name, {
+          'defer': true,
+          'fn': function  (deferred) {
+            fn(db, deferred.resolve.bind(deferred))
+          }
+        })
+        /*.on('cycle', function(event) {
+          console.log(String(event.target))
+        })*/
+        .on('complete', function(event) {
+          console.log(String(event.target).green.bold)
+          cb()
+        })
+        .run({ async: true })
+      }
+
+      makedb(function (db) {
+        setup(db, function () {
+          exec(db, rmdb.bind(null, db, cb))
+        })
+      })
+    }
+
+async.forEachSeries(
+    Object.keys(benchmarks)
+  , function (name, cb) {
+      var setup = typeof benchmarks[name] == 'function' ? K : benchmarks[name].setup
+        , fn = typeof benchmarks[name] == 'function' ? benchmarks[name] : benchmarks[name].fn
+
+      run(name, setup, fn, cb)
+    }
+  , function () {
+      //console.log('Done!')
+    }
+)
\ No newline at end of file
diff --git a/test/benchmarks/package.json b/test/benchmarks/package.json
new file mode 100644
index 0000000..cae7b2e
--- /dev/null
+++ b/test/benchmarks/package.json
@@ -0,0 +1,13 @@
+{
+  "name": "levelup-benchmarks",
+  "version": "0.0.0",
+  "description": "benchmarks for LevelUP",
+  "main": "index.js",
+  "private": true,
+  "dependencies": {
+    "benchmark": "~1.0.0",
+    "colors": "~0.6.0-1",
+    "rimraf": "~2.0.2",
+    "async": "~0.1.22"
+  }
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-leveldown.git



More information about the Pkg-javascript-commits mailing list