-
-
Save garrensmith/9bd090cb00f6b641e58a3c490b2300e7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| To run this: | |
| Copy file to `index.js` | |
| $ npm install pouchdb-node | |
| $ node index.js | |
| */ | |
| const PouchDB = require("pouchdb-node"); | |
| const TOTAL_DOCS = 300001; | |
| /* | |
| fdb> writemode on | |
| fdb> clearrange "" \xFF | |
| */ | |
| const ddoc = { | |
| _id: "_design/perf_reduce", | |
| views: { | |
| index: { | |
| map: function (doc) { | |
| if (doc.date) { | |
| emit(doc.date, doc.value); | |
| } | |
| }.toString(), | |
| reduce: "_sum", | |
| }, | |
| }, | |
| }; | |
| const getRandom = (min, max) => { | |
| min = Math.ceil(min); | |
| max = Math.floor(max); | |
| return Math.floor(Math.random() * (max - min)) + min; //The maximum is exclusive and the minimum is inclusive | |
| }; | |
| const getRandomKey = (min, max) => { | |
| return [ | |
| getRandom(min, max), | |
| getRandom(1, TOTAL_DOCS), | |
| getRandom(1, TOTAL_DOCS), | |
| ]; | |
| }; | |
| const worstKeyGen = (key) => { | |
| return [key + 2, key + 1, key]; | |
| }; | |
| const groupingKeyGen = (key) => { | |
| return [key % 5, key % 4, key]; | |
| }; | |
| const setup = async () => { | |
| let db = new PouchDB("http://adm:pass@localhost:15984/test-reduce-db-1?q=1"); | |
| const info = await db.info(); | |
| console.log(info, info.doc_count, TOTAL_DOCS + 1); | |
| if (info.doc_count < TOTAL_DOCS) { | |
| // await db.destroy(); | |
| // db = new PouchDB("http://adm:pass@localhost:15984/test-reduce-db?q=1"); | |
| await createDocs(db); | |
| } | |
| // warm the view | |
| try { | |
| const start = process.hrtime.bigint(); | |
| const resp = await db.query("perf_reduce/index?reduce=false&limit=10"); | |
| const end = process.hrtime.bigint(); | |
| const diff = end - start; | |
| const diffms = Number(diff) / 1000000; | |
| console.log(`Insert Time `, diffms, "ms"); | |
| // console.log(resp); | |
| } catch (e) { | |
| console.log(e); | |
| } | |
| return db; | |
| }; | |
| const createDocs = async (db) => { | |
| let docs = []; | |
| for (let i = 0; i <= TOTAL_DOCS; i++) { | |
| docs.push({ | |
| _id: `doc-${i}`, | |
| value: getRandom(1, 20), | |
| date: groupingKeyGen(i), | |
| }); | |
| if (docs.length === 2000) { | |
| // console.log(i); | |
| await db.bulkDocs(docs); | |
| docs = []; | |
| } | |
| } | |
| if (docs !== []) { | |
| await db.bulkDocs(docs); | |
| } | |
| await db.put(ddoc); | |
| console.log("docs loaded"); | |
| }; | |
| const perfRun = async (db, groupLevel, id) => { | |
| try { | |
| const opts = { | |
| reduce: true, | |
| group_level: groupLevel, | |
| // group: true | |
| }; | |
| const start = process.hrtime.bigint(); | |
| const out = await db.query("perf_reduce/index", opts); | |
| const end = process.hrtime.bigint(); | |
| const diff = end - start; | |
| // console.log(diff); | |
| const diffms = Number(diff) / 1000000; | |
| console.log(`Group ${groupLevel} Time`, diffms, "ms"); | |
| console.log("Result Length", out.rows.length); | |
| } catch (e) { | |
| console.log(`PER ERROR for ${id}`, e); | |
| } | |
| }; | |
| const perf = async (db) => { | |
| await perfRun(db, 1, 3); | |
| await perfRun(db, 2, 1); | |
| await perfRun(db, 3, 2); | |
| // await perfRun(db, 0, 4); | |
| }; | |
| const run = async () => { | |
| const db = await setup(); | |
| perf(db).then(() => { | |
| console.log("FF"); | |
| return db.destroy(); | |
| }); | |
| }; | |
| run() | |
| .then(() => console.log("done")) | |
| .catch((e) => console.log("dd", e)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment