* Test script to verify query optimizer is working correctly
()
| 7 | */ |
| 8 | |
| 9 | async function testQueryOptimizer() { |
| 10 | console.log("🚀 Testing Query Optimizer functionality...\n"); |
| 11 | |
| 12 | try { |
| 13 | const client = await mongoDb; |
| 14 | |
| 15 | // Test 1: Basic query optimizer functionality |
| 16 | console.log("📊 Test 1: Basic Query Optimizer"); |
| 17 | const optimizer = createQueryOptimizer(client); |
| 18 | |
| 19 | // Simulate some queries |
| 20 | const startTime = Date.now(); |
| 21 | |
| 22 | // These would normally be separate queries, but optimizer batches them |
| 23 | const db = client.db(process.env.DATABASE_NAME || 'pulse-db'); |
| 24 | const teamsCollection = db.collection('teams'); |
| 25 | const teamUsersCollection = db.collection('team-users'); |
| 26 | |
| 27 | const teams = await teamsCollection.find({}).limit(5).toArray(); |
| 28 | const teamUsers = await teamUsersCollection.find({}).limit(5).toArray(); |
| 29 | |
| 30 | const duration = Date.now() - startTime; |
| 31 | const metrics = optimizer.getMetrics(); |
| 32 | |
| 33 | console.log(` ⏱️ Query duration: ${duration}ms`); |
| 34 | console.log(` 📊 Metrics: ${metrics.queryCount} queries in ${metrics.duration}ms`); |
| 35 | console.log(` 📁 Found ${teams.length} teams, ${teamUsers.length} team-users`); |
| 36 | |
| 37 | // Test 2: Query metrics wrapper |
| 38 | console.log("\n📊 Test 2: Query Metrics Wrapper"); |
| 39 | |
| 40 | const { result, metrics: wrapperMetrics } = await withQueryMetrics(client, async (opt) => { |
| 41 | // Simulate fetching team data with collections |
| 42 | const db = client.db(process.env.DATABASE_NAME || 'pulse-db'); |
| 43 | |
| 44 | const teams = await db.collection('teams').find({}).limit(3).toArray(); |
| 45 | const collections = await db.collection('collections').find({}).limit(5).toArray(); |
| 46 | |
| 47 | return { |
| 48 | teams: teams.length, |
| 49 | collections: collections.length |
| 50 | }; |
| 51 | }); |
| 52 | |
| 53 | console.log(` 📊 Wrapper result:`, result); |
| 54 | console.log(` ⏱️ Metrics: ${wrapperMetrics.queryCount} queries in ${wrapperMetrics.duration}ms`); |
| 55 | |
| 56 | // Test 3: Complex aggregation simulation (if we have data) |
| 57 | if (teams.length > 0) { |
| 58 | console.log("\n📊 Test 3: Complex Aggregation Performance"); |
| 59 | |
| 60 | const aggregationStart = Date.now(); |
| 61 | |
| 62 | // Test a complex aggregation pipeline |
| 63 | const pipeline = [ |
| 64 | { $match: {} }, |
| 65 | { |
| 66 | $lookup: { |
no test coverage detected
searching dependent graphs…