Files
wekan/docs/Databases/FerretDB/1/SpeedTest/indexes.cjs
T
Lauri Ojansivu 70485c38df A database speed test: the common WeKan queries on every database, twice a production WeKan of data.
docs/Databases/FerretDB/1/SpeedTest/speedtest.sh (build.sh and build.bat ->
Tests -> Database speed test) is releases/db-conformance.sh with
WEKAN_DB_SUITE=speedtest: the same FerretDB built from source and the same
databases started the same way - SQLite, PostgreSQL, MySQL and MariaDB through
FerretDB v1, MongoDB 6, 7, 8 and 9, and now FerretDB 2 with PostgreSQL and
DocumentDB.

data.cjs generates, deterministically, twice the users, boards, lists,
swimlanes and cards of a production instance (615,070 cards), skewed the way
real ones are; indexes.cjs creates the indexes WeKan creates, read from its
ensureIndex calls; queries.cjs asks 18 common WeKan queries as WeKan's own code
builds them; report.cjs writes Results.md - a row per database, fastest at the
top, a column per query - and fails when a database answered or stored
anything differently from MongoDB. README.md describes it, what it found, what
was improved and what could still be.

db-conformance.sh: WEKAN_DB_BACKENDS runs only the named databases (to repeat
one into an earlier run's WEKAN_LOGDIR), containers are removed with their
volumes, SQLite's data is under .tools/tmp, and FerretDB 2 is a backend of the
speed test, and of the conformance run with WEKAN_CONFORMANCE_FERRETDB2=1. The
conformance catalogue gains a case of writes by string _id and _ids differing
only by case, which found that FerretDB on MariaDB 12.3 lost them.

tests/dbSpeedTest.test.cjs checks the data, the index list, the queries, the
report's order and that a difference fails it, the wiring and the Due Cards
board scope.

Thanks to xet7 !
2026-10-24 18:18:15 +03:00

128 lines
4.9 KiB
JavaScript

'use strict';
// The indexes WeKan creates on the five collections of the speed test, read from
// WeKan's own `ensureIndex(...)` calls rather than written down here, so the test
// always runs with the indexes the current WeKan really creates - a new index in
// server/models/cards.js is in the next run without anybody editing this file.
//
// Meteor's accounts-base package creates the users indexes that login uses
// (`username`, `emails.address`); those are not WeKan source, so they are listed.
const fs = require('fs');
const path = require('path');
const acorn = require('acorn');
const walk = require('acorn-walk');
const ROOT = path.resolve(__dirname, '../../../../..');
// The variable WeKan's source uses for each collection.
const COLLECTION_OF = {
Users: 'users',
'Meteor.users': 'users',
Boards: 'boards',
Lists: 'lists',
Swimlanes: 'swimlanes',
Cards: 'cards',
};
const ACCOUNTS_BASE = [
{ collection: 'users', key: { username: 1 }, options: { unique: true }, source: 'Meteor accounts-base' },
{ collection: 'users', key: { 'emails.address': 1 }, options: { unique: true }, source: 'Meteor accounts-base' },
];
function literal(node) {
if (!node) return undefined;
if (node.type === 'Literal') return node.value;
if (node.type === 'UnaryExpression' && node.operator === '-' && node.argument.type === 'Literal') return -node.argument.value;
if (node.type === 'ObjectExpression') {
const out = {};
for (const prop of node.properties) {
if (prop.type !== 'Property' || prop.computed) return undefined;
const name = prop.key.type === 'Identifier' ? prop.key.name : prop.key.value;
const value = literal(prop.value);
if (value === undefined) return undefined;
out[name] = value;
}
return out;
}
return undefined;
}
function nameOf(node) {
if (node.type === 'Identifier') return node.name;
if (node.type === 'MemberExpression' && !node.computed) return `${nameOf(node.object)}.${node.property.name}`;
return '';
}
function sourceFiles() {
const out = [];
const visit = (dir) => {
for (const e of fs.readdirSync(path.join(ROOT, dir), { withFileTypes: true })) {
if (['node_modules', '.npm', '_build', '.build'].includes(e.name)) continue;
const rel = path.join(dir, e.name);
if (e.isDirectory()) visit(rel);
else if (/\.js$/.test(e.name)) out.push(rel);
}
};
['server', 'models'].forEach(visit);
return out;
}
function wekanIndexes() {
const found = [];
const skipped = [];
for (const rel of sourceFiles()) {
const src = fs.readFileSync(path.join(ROOT, rel), 'utf8');
if (!src.includes('ensureIndex(')) continue;
let ast;
try {
ast = acorn.parse(src, { ecmaVersion: 'latest', sourceType: 'module', locations: true,
allowAwaitOutsideFunction: true, allowReturnOutsideFunction: true, allowHashBang: true });
} catch (e) {
skipped.push(`${rel}: could not parse (${e.message})`);
continue;
}
walk.simple(ast, {
CallExpression(node) {
if (nameOf(node.callee) !== 'ensureIndex' || node.arguments.length < 2) return;
const collection = COLLECTION_OF[nameOf(node.arguments[0])];
if (!collection) return;
const where = `${rel}:${node.loc.start.line}`;
const key = literal(node.arguments[1]);
const options = node.arguments[2] ? literal(node.arguments[2]) : {};
if (key && typeof key === 'object' && options !== undefined) {
found.push({ collection, key, options, source: where });
} else if (rel.endsWith('users.js') && node.arguments[1].type === 'Identifier') {
// The list sort indexes: ensureIndex(Lists, key) over
// listSortIndexKeys(allowedSortValues) (#6794).
const { listSortIndexKeys } = require(path.join(ROOT, 'models/lib/indexKeys.cjs'));
const values = /export const allowedSortValues = \[([^\]]*)\]/.exec(
fs.readFileSync(path.join(ROOT, 'models/users.js'), 'utf8'));
const sortValues = values ? values[1].match(/'[^']*'/g).map(v => v.slice(1, -1)) : [];
for (const k of listSortIndexKeys(sortValues)) found.push({ collection: 'lists', key: k, options: {}, source: where });
} else {
skipped.push(`${where}: key is not a literal`);
}
},
});
}
// ensureIndex() skips a key that already exists; so does this.
const seen = new Set();
const unique = [];
for (const ix of [...ACCOUNTS_BASE, ...found]) {
const id = `${ix.collection} ${JSON.stringify(ix.key)}`;
if (seen.has(id)) continue;
seen.add(id);
unique.push(ix);
}
return { indexes: unique, skipped };
}
module.exports = { wekanIndexes };
if (require.main === module) {
const { indexes, skipped } = wekanIndexes();
for (const ix of indexes) console.log(ix.collection.padEnd(10), JSON.stringify(ix.key), JSON.stringify(ix.options), ix.source);
for (const s of skipped) console.log('skipped:', s);
}