Commit 826a63ce authored by Aral Balkan's avatar Aral Balkan

New tests. Add clean script

parent 6adf6ba1
rm -rf tasks
const levelup = require('levelup')
// Isomorphic: use leveldown LevelDB backend in Node and
// level.js IndexedDB façade in the browser.
const isBrowser = (typeof window !== 'undefined')
let leveldown = null
if (isBrowser) {
leveldown = require('level-js')
......@@ -9,9 +8,11 @@ if (isBrowser) {
leveldown = require('leveldown')
}
// Setup database
const storage = leveldown('tasks')
const db = levelup(storage)
const encode = require('encoding-down')
const levelup = require('levelup')
// Create/open database
const db = levelup(encode(leveldown('tasks'), {valueEncoding: 'json'}))
// Helpers
let timer = null
......@@ -26,22 +27,58 @@ function displayDurationFor (taskName) {
// Tests
async function start () {
try {
await db.put('name', 'Aral')
// await db.put('name', 'Aral')
const numInserts = 10000
let insertPromises = []
// // Naïve inserts
// let insertPromises = []
// for (let i = 0; i < numInserts; i++) {
// insertPromises.push(db.put(i, { date: new Date(), description: `Item: ${i}`, done: Math.round(Math.random()) }))
// }
// resetTimer()
// await Promise.all(insertPromises)
// displayDurationFor(`${numInserts} naïve inserts`)
// Batch inserts
let operations = []
for (let i = 0; i < numInserts; i++) {
insertPromises.push(db.put(i, { date: new Date(), description: `Item: ${i}`, done: Math.round(Math.random()) }))
let value = { date: new Date(), description: `Item: ${i}`, done: Math.round(Math.random()) }
operations.push({type: 'put', key: i, value: value})
}
resetTimer()
await Promise.all(insertPromises)
displayDurationFor(`${numInserts} inserts`)
await db.batch(operations)
displayDurationFor(`${numInserts} batch inserts`)
// const value = await db.get('name', { asBuffer: false })
// console.log(value)
await db.close()
// Read stream
// resetTimer()
// db.createReadStream({asBuffer: false})
// .on('data', (data) => {
// // console.log(JSON.stringify(data))
// })
// .on('end', () => {
// displayDurationFor('Read stream')
// })
// Single get
resetTimer()
const value = await db.get(2500)
displayDurationFor('Single get')
console.log(JSON.stringify(value))
// Multiple gets
let promises = []
for (let i = 0; i < numInserts; i++) {
promises.push(db.get(i) /*.then((value) => { console.log(value) }) */ )
}
resetTimer()
await Promise.all(promises)
displayDurationFor(`${numInserts} gets`)
// await db.close()
} catch (error) {
console.log(error)
}
......
......@@ -4,6 +4,7 @@
"main": "index.js",
"license": "MIT",
"dependencies": {
"encoding-down": "^5.0.2",
"level": "^4.0.0",
"level-js": "https://github.com/Level/level.js.git"
}
......
......@@ -93,7 +93,7 @@ detect-libc@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/detect-libc/-/detect-libc-1.0.3.tgz#fa137c4bd698edf55cd5cd02ac559f91a4c4ba9b"
encoding-down@~5.0.0:
encoding-down@^5.0.2, encoding-down@~5.0.0:
version "5.0.2"
resolved "https://registry.yarnpkg.com/encoding-down/-/encoding-down-5.0.2.tgz#ee861f19c70eb3f357eed15da40b787dfa9a87fe"
dependencies:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment