Permalink
Browse files

Added new demo. Added gc hook to both demos.

  • Loading branch information...
1 parent 95057fd commit f6366166e0412023fc0f761f9d3147ff5a46adf0 @justmoon justmoon committed Aug 12, 2012
Showing with 58 additions and 0 deletions.
  1. +2 −0 demo/million-batch.coffee
  2. +56 −0 demo/write-read.coffee
@@ -33,5 +33,7 @@ leveldb.open path, create_if_missing: true, (err, db) ->
db.write batch, (err) ->
throw err if err
if i < totalSize then bench() else callback()
+ db = null
+ gc?() # explicit gc if enabled - useful for debugging memleaks
bench()
View
@@ -0,0 +1,56 @@
+assert = require 'assert'
+leveldb = require '../lib'
+
+console.log 'Creating test database'
+path = '/tmp/wr.db'
+
+batchSize = 100
+totalSize = 100000
+
+leveldb.open path, create_if_missing: true, (err, db) ->
+ assert.ifError err
+
+ start = Date.now()
+ i = 0
+
+ writeBenchStart = ->
+ console.log 'Serializing and inserting 100,000 rows...'
+ writeBench()
+
+ writeBench = ->
+ console.log "i = #{i}" if i % 10000 == 0
+ batch = new(leveldb.Batch)
+ for j in [0...batchSize]
+ key = "row#{i}"
+ value = JSON.stringify
+ index: i
+ name: "Tim"
+ age: 28
+ batch.put key, value
+ ++i
+
+ db.write batch, (err) ->
+ throw err if err
+ if i < totalSize then writeBench() else readBenchStart()
+
+ readBenchStart = ->
+ console.log 'Fetching and deserializing 100,000 rows...'
+ readBench()
+
+ readBench = ->
+ --i
+ console.log "i = #{i}" if i % 10000 == 0
+ key = "row#{i}"
+ db.get key, (err, value) ->
+ throw err if err
+ value = JSON.parse value
+ assert.equal value.index, i
+ if i > 0 then readBench() else callback()
+
+ callback = ->
+ delta = Date.now() - start;
+ console.log 'Completed in %d ms', delta
+ db = null
+ gc?() # explicit gc if enabled - useful for debugging memleaks
+
+ writeBenchStart()

0 comments on commit f636616

Please sign in to comment.