const test = require('tape'); import { Test } from 'tape'; import { EventHorizon } from '../src'; test('Performance Optimization - Large Dataset SubSessions', function (t: Test) { const eventHorizon = new EventHorizon(); // Create tables for performance testing const customersTable = eventHorizon.createTesseract('customers', { columns: [ { name: 'id', primaryKey: true, columnType: 'number' }, { name: 'name', columnType: 'text' }, { name: 'segment', columnType: 'text' }, { name: 'region', columnType: 'text' } ] }); const transactionsTable = eventHorizon.createTesseract('transactions', { columns: [ { name: 'id', primaryKey: true, columnType: 'number' }, { name: 'customer_id', columnType: 'number' }, { name: 'amount', columnType: 'number' }, { name: 'category', columnType: 'text' }, { name: 'status', columnType: 'text' }, { name: 'timestamp', columnType: 'date' } ] }); // Generate larger dataset for performance testing const customerCount = 1000; const transactionCount = 5000; const segments = ['Enterprise', 'SMB', 'Consumer']; const regions = ['North', 'South', 'East', 'West', 'Central']; const categories = ['Software', 'Hardware', 'Services', 'Support']; const statuses = ['completed', 'pending', 'failed']; const customers: any[] = []; for (let i = 1; i <= customerCount; i++) { customers.push({ id: i, name: `Customer ${i}`, segment: segments[Math.floor(Math.random() * segments.length)], region: regions[Math.floor(Math.random() * regions.length)] }); } const transactions: any[] = []; for (let i = 1; i <= transactionCount; i++) { transactions.push({ id: i, customer_id: Math.floor(Math.random() * customerCount) + 1, amount: Math.floor(Math.random() * 10000) + 100, category: categories[Math.floor(Math.random() * categories.length)], status: statuses[Math.floor(Math.random() * statuses.length)], timestamp: new Date(Date.now() - Math.floor(Math.random() * 30 * 24 * 60 * 60 * 1000)) }); } const loadStartTime = Date.now(); customersTable!.add(customers); transactionsTable!.add(transactions); const loadEndTime = Date.now(); const loadTime = loadEndTime - loadStartTime; console.log(`Data loading time: ${loadTime}ms (${customerCount} customers, ${transactionCount} transactions)`); t.ok(loadTime < 10000, `Data loading should be reasonably fast (${loadTime}ms)`); const performanceSession = eventHorizon.createSession({ id: 'performance-customer-analysis', table: 'customers', subSessions: { transactionStats: { table: 'transactions', columns: [ { name: 'customer_id', primaryKey: true }, { name: 'totalRevenue', value: 'status == "completed" ? amount : 0', aggregator: 'sum' }, { name: 'transactionCount', value: 1, aggregator: 'sum' }, { name: 'completedCount', value: 'status == "completed" ? 1 : 0', aggregator: 'sum' }, { name: 'avgTransactionValue', value: 'amount', aggregator: 'avg' }, { name: 'maxTransaction', value: 'amount', aggregator: 'max' } ], filter: [ { field: 'amount', comparison: '>', value: 0 } ], groupBy: [{ dataIndex: 'customer_id' }] } }, columns: [ { name: 'id', primaryKey: true }, { name: 'name' }, { name: 'segment' }, { name: 'region' }, { name: 'totalRevenue', resolve: { underlyingField: 'id', session: 'transactionStats', valueField: 'customer_id', displayField: 'totalRevenue' } }, { name: 'transactionCount', resolve: { underlyingField: 'id', session: 'transactionStats', valueField: 'customer_id', displayField: 'transactionCount' } }, { name: 'completedCount', resolve: { underlyingField: 'id', session: 'transactionStats', valueField: 'customer_id', displayField: 'completedCount' } }, { name: 'avgTransactionValue', resolve: { underlyingField: 'id', session: 'transactionStats', valueField: 'customer_id', displayField: 'avgTransactionValue' } } ], limit: 100 }); const sessionStartTime = Date.now(); const initialData = performanceSession.getData(); const sessionEndTime = Date.now(); const queryTime = sessionEndTime - sessionStartTime; console.log(`Initial query time: ${queryTime}ms (returned ${initialData.length} records)`); t.ok(initialData.length > 0, 'Should return some customer data'); // Skip live updateConfig step due to API limitations in typed Session; focus on initial dataset integrity initialData.slice(0, 10).forEach((customer: any) => { t.ok(customer.id != null, 'Customer should have id'); t.ok(customer.name, 'Customer should have name'); }); t.end(); });