Files
back_end_oudelaa/scripts/perf-search-plans.js

73 lines
3.7 KiB
JavaScript

const { performance } = require('node:perf_hooks');
const assert = require('node:assert/strict');
module.exports = async function measureSearchPlans(Post, viewerId) {
const reports = [];
for (const text of ['music', 'genre0', 'post 12345', 'not-present-in-fixture']) {
const regex = new RegExp(text, 'i');
const filter = {
isDeleted: { $ne: true }, processingStatus: { $nin: ['pending', 'processing', 'failed'] },
isArchived: { $ne: true }, moderationStatus: { $ne: 'hidden' },
$and: [{ $or: [{ visibility: 'public' }, { authorId: viewerId }] },
{ $or: ['content', 'hashtags', 'style', 'maqam', 'rhythmSignature'].map((field) => ({ [field]: regex })) }],
};
const pipeline = [{ $match: filter }, { $project: { _id: 1, createdAt: 1 } }, { $facet: {
items: [{ $sort: { createdAt: -1 } }, { $limit: 20 }], total: [{ $count: 'count' }],
} }];
const operations = {
separate: async () => {
const [items, total] = await Promise.all([
Post.find(filter).select({ _id: 1, createdAt: 1 }).sort({ createdAt: -1 }).limit(20).lean().exec(),
Post.countDocuments(filter).exec(),
]);
return { ids: items.map((item) => String(item._id)), total };
},
naturalCount: async () => {
const [items, total] = await Promise.all([
Post.find(filter).select({ _id: 1, createdAt: 1 }).sort({ createdAt: -1 }).limit(20).lean().exec(),
Post.countDocuments(filter).hint({ $natural: 1 }).exec(),
]);
return { ids: items.map((item) => String(item._id)), total };
},
inferredTotal: async () => {
const items = await Post.find(filter).select({ _id: 1, createdAt: 1 })
.sort({ createdAt: -1 }).limit(20).lean().exec();
const total = items.length < 20 ? items.length : await Post.countDocuments(filter).exec();
return { ids: items.map((item) => String(item._id)), total };
},
orderedScan: async () => {
const [items, total] = await Promise.all([
Post.find(filter).select({ _id: 1, createdAt: 1 }).sort({ createdAt: -1 }).hint({ createdAt: -1 }).limit(20).lean().exec(),
Post.countDocuments(filter).exec(),
]);
return { ids: items.map((item) => String(item._id)), total };
},
facet: async () => {
const [result] = await Post.aggregate(pipeline).exec();
return { ids: result.items.map((item) => String(item._id)), total: result.total[0]?.count ?? 0 };
},
};
const expected = await operations.separate();
for (const operation of Object.values(operations)) assert.deepEqual(await operation(), expected);
const samples = Object.fromEntries(Object.keys(operations).map((name) => [name, []]));
for (let round = 0; round < 6; round++) {
const names = ['separate', 'inferredTotal'];
if (round % 2) names.reverse();
for (const name of names) {
const started = performance.now();
await operations[name]();
samples[name].push(performance.now() - started);
}
}
const timings = Object.fromEntries(Object.entries(samples).filter(([, values]) => values.length).map(([name, values]) => {
values.sort((a, b) => a - b);
return [name, { medianMs: +values[Math.ceil(values.length / 2) - 1].toFixed(2), maxMs: +values[values.length - 1].toFixed(2) }];
}));
const plan = await Post.find(filter).sort({ createdAt: -1 }).limit(20).explain('executionStats');
reports.push({ text, total: expected.total, equivalent: true, timings,
findPlan: { docsExamined: plan.executionStats.totalDocsExamined,
keysExamined: plan.executionStats.totalKeysExamined, plan: plan.queryPlanner.winningPlan } });
}
return reports;
};