Skip to content

Commit

Permalink
format test
Browse files Browse the repository at this point in the history
  • Loading branch information
jchris committed Feb 14, 2024
1 parent 4d2370a commit 5b13dbb
Showing 1 changed file with 118 additions and 125 deletions.
243 changes: 118 additions & 125 deletions packages/fireproof/test/fireproof.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ describe('benchmarking with compaction', function () {
await resetDirectory(dataDir, 'test-benchmark-compaction')
db = new Database('test-benchmark-compaction', { autoCompact: 3, public: true })
})
it.skip('insert during compaction', async function () {
it.skip('passing: insert during compaction', async function () {
const ok = await db.put({ _id: 'test', foo: 'fast' })
assert(ok)
equals(ok.id, 'test')
Expand All @@ -143,9 +143,9 @@ describe('benchmarking with compaction', function () {

const numDocs = 20000
const batchSize = 500
console.time(`insert and read ${numDocs} records`)
console.time(`insert and read ${numDocs} records`)

let doing = null
let doing = null
for (let i = 0; i < numDocs; i += batchSize) {
const ops = []
db.put({ foo: 'fast' })
Expand All @@ -154,26 +154,23 @@ describe('benchmarking with compaction', function () {
db.put({ foo: 'fast' })
for (let j = 0; j < batchSize && i + j < numDocs; j++) {
ops.push(
db
.put({
data: Math.random(),
fire: Math.random()
.toString()
.repeat(25 * 1024)
})

db.put({
data: Math.random(),
fire: Math.random()
.toString()
.repeat(25 * 1024)
})
)
}
const label = `write ${i} log ${db._crdt.blockstore.loader.carLog.length}`
console.time(label)
db.put({
data: Math.random(),
fire: Math.random()
.toString()
.repeat(25 * 1024)
})


data: Math.random(),
fire: Math.random()
.toString()
.repeat(25 * 1024)
})

await Promise.all(ops)
console.timeEnd(label)
}
Expand All @@ -198,137 +195,133 @@ describe('benchmarking a database', function () {
// npm test -- --grep 'insert and read many records'
//
// eslint-disable-next-line mocha/no-skipped-tests
it.skip(
// it(
'insert and read many records',
async function () {
const ok = await db.put({ _id: 'test', foo: 'fast' })
assert(ok)
equals(ok.id, 'test')

assert(db._crdt.clock.head)
equals(db._crdt.clock.head.length, 1)

const numDocs = 2500
const batchSize = 500
console.time(`insert and read ${numDocs} records`)

for (let i = 0; i < numDocs; i += batchSize) {
const ops = []
for (let j = 0; j < batchSize && i + j < numDocs; j++) {
ops.push(
db
.put({
_id: `test${i + j}`,
fire: Math.random()
.toString()
.repeat(25 * 1024)
})
.then(ok => {
db.get(`test${i + j}`).then(doc => {
assert(doc.fire)
})
it.skip('passing: insert and read many records', async function () {
const ok = await db.put({ _id: 'test', foo: 'fast' })
assert(ok)
equals(ok.id, 'test')

assert(db._crdt.clock.head)
equals(db._crdt.clock.head.length, 1)

const numDocs = 2500
const batchSize = 500
console.time(`insert and read ${numDocs} records`)

for (let i = 0; i < numDocs; i += batchSize) {
const ops = []
for (let j = 0; j < batchSize && i + j < numDocs; j++) {
ops.push(
db
.put({
_id: `test${i + j}`,
fire: Math.random()
.toString()
.repeat(25 * 1024)
})
.then(ok => {
db.get(`test${i + j}`).then(doc => {
assert(doc.fire)
})
)
}
await Promise.all(ops)
})
)
}
await Promise.all(ops)
}

console.timeEnd(`insert and read ${numDocs} records`)
console.timeEnd(`insert and read ${numDocs} records`)

// console.time('allDocs')
// const allDocsResult2 = await db.allDocs()
// console.timeEnd('allDocs')
// equals(allDocsResult2.rows.length, numDocs+1)
// console.time('allDocs')
// const allDocsResult2 = await db.allDocs()
// console.timeEnd('allDocs')
// equals(allDocsResult2.rows.length, numDocs+1)

console.time('open new DB')
const newDb = new Database('test-benchmark', { autoCompact: 100000, public: true })
const doc = await newDb.get('test')
equals(doc.foo, 'fast')
console.timeEnd('open new DB')
console.time('open new DB')
const newDb = new Database('test-benchmark', { autoCompact: 100000, public: true })
const doc = await newDb.get('test')
equals(doc.foo, 'fast')
console.timeEnd('open new DB')

console.time('changes')
const result = await db.changes() // takes 1.5 seconds (doesn't have to load blocks from cars)
console.timeEnd('changes')
equals(result.rows.length, numDocs + 1)
console.time('changes')
const result = await db.changes() // takes 1.5 seconds (doesn't have to load blocks from cars)
console.timeEnd('changes')
equals(result.rows.length, numDocs + 1)

// this takes 1 minute w 1000 docs
console.time('changes new DB')
const result2 = await newDb.changes()
console.timeEnd('changes new DB')
equals(result2.rows.length, numDocs + 1)
// this takes 1 minute w 1000 docs
console.time('changes new DB')
const result2 = await newDb.changes()
console.timeEnd('changes new DB')
equals(result2.rows.length, numDocs + 1)

await sleep(1000)
await sleep(1000)

console.log('begin compact')
console.log('begin compact')

await sleep(100)
await sleep(100)

console.time('COMPACT')
await db.compact()
console.timeEnd('COMPACT')
console.time('COMPACT')
await db.compact()
console.timeEnd('COMPACT')

// todo compaction should not need this write to show in the new db
await db.put({ _id: 'compacted-test', foo: 'bar' })
// todo compaction should not need this write to show in the new db
await db.put({ _id: 'compacted-test', foo: 'bar' })

// console.log('car log length', db._crdt.blockstore.loader.carLog.length)
equals(db._crdt.blockstore.loader.carLog.length, 2)
// console.log('car log length', db._crdt.blockstore.loader.carLog.length)
equals(db._crdt.blockstore.loader.carLog.length, 2)

// console.time('allDocs new DB') // takes forever on 5k
// const allDocsResult = await newDb.allDocs()
// console.timeEnd('allDocs new DB')
// equals(allDocsResult.rows.length, numDocs+1)
await sleep(100)
// console.time('allDocs new DB') // takes forever on 5k
// const allDocsResult = await newDb.allDocs()
// console.timeEnd('allDocs new DB')
// equals(allDocsResult.rows.length, numDocs+1)
await sleep(100)

console.time('compacted reopen again')
const newDb2 = new Database('test-benchmark', { autoCompact: 100000, public: true })
const doc21 = await newDb2.get('test')
equals(doc21.foo, 'fast')
console.time('compacted reopen again')
const newDb2 = new Database('test-benchmark', { autoCompact: 100000, public: true })
const doc21 = await newDb2.get('test')
equals(doc21.foo, 'fast')

equals(newDb2._crdt.blockstore.loader.carLog.length, 2)
equals(newDb2._crdt.blockstore.loader.carLog.length, 2)

const doc2 = await newDb2.get('compacted-test')
const doc2 = await newDb2.get('compacted-test')

equals(doc2.foo, 'bar')
equals(doc2.foo, 'bar')

equals(doc2.foo, 'bar')
console.timeEnd('compacted reopen again')
equals(doc2.foo, 'bar')
console.timeEnd('compacted reopen again')

await sleep(100)
await sleep(100)

console.time('compacted changes new DB2')
const result3 = await newDb2.changes()
console.timeEnd('compacted changes new DB2')
equals(result3.rows.length, numDocs + 2)
console.time('compacted changes new DB2')
const result3 = await newDb2.changes()
console.timeEnd('compacted changes new DB2')
equals(result3.rows.length, numDocs + 2)

console.time('compacted newDb2 insert and read 100 records')
const ops2 = []
for (let i = 0; i < 100; i++) {
const ok = newDb2
.put({
_id: `test${i}`,
fire: Math.random()
.toString()
.repeat(25 * 1024)
})
.then(ok => {
newDb2.get(`test${i}`).then(doc => {
assert(doc.fire)
})
console.time('compacted newDb2 insert and read 100 records')
const ops2 = []
for (let i = 0; i < 100; i++) {
const ok = newDb2
.put({
_id: `test${i}`,
fire: Math.random()
.toString()
.repeat(25 * 1024)
})
.then(ok => {
newDb2.get(`test${i}`).then(doc => {
assert(doc.fire)
})
ops2.push(ok)
}
await Promise.all(ops2)
console.timeEnd('compacted newDb2 insert and read 100 records')

// triggers OOM on my machine
// await sleep(100)
// console.time('compacted allDocs new DB2')
// const allDocsResult3 = await newDb2.allDocs()
// console.timeEnd('compacted allDocs new DB2')
// equals(allDocsResult3.rows.length, numDocs+2)
})
ops2.push(ok)
}
).timeout(20000000)
await Promise.all(ops2)
console.timeEnd('compacted newDb2 insert and read 100 records')

// triggers OOM on my machine
// await sleep(100)
// console.time('compacted allDocs new DB2')
// const allDocsResult3 = await newDb2.allDocs()
// console.timeEnd('compacted allDocs new DB2')
// equals(allDocsResult3.rows.length, numDocs+2)
}).timeout(20000000)
})

describe('Reopening a database', function () {
Expand Down

0 comments on commit 5b13dbb

Please sign in to comment.