102 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			102 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| #!/usr/bin/env node
 | |
| 
 | |
| const BASE_URL = process.env.BASE_URL || 'http://localhost:4000';
 | |
| 
 | |
| async function testPagination() {
 | |
|   console.log('🧪 Testing Pagination Performance\n');
 | |
| 
 | |
|   try {
 | |
|     // Test 1: First page
 | |
|     console.log('📄 Test 1: Loading first page...');
 | |
|     const start1 = performance.now();
 | |
|     const response1 = await fetch(`${BASE_URL}/api/vault/metadata/paginated?limit=50`);
 | |
|     const data1 = await response1.json();
 | |
|     const time1 = performance.now() - start1;
 | |
| 
 | |
|     if (!response1.ok) {
 | |
|       console.error('❌ Failed to load first page:', data1);
 | |
|       return;
 | |
|     }
 | |
| 
 | |
|     console.log(`✅ First page: ${data1.items.length} items in ${time1.toFixed(2)}ms`);
 | |
|     console.log(`📊 Total available: ${data1.total} items`);
 | |
|     console.log(`   Has more: ${data1.hasMore}`);
 | |
|     console.log(`   Next cursor: ${data1.nextCursor}\n`);
 | |
| 
 | |
|     // Test 2: Pagination through 5 pages
 | |
|     console.log('📜 Test 2: Simulating scroll through 5 pages...');
 | |
|     let totalTime = 0;
 | |
|     let totalItems = 0;
 | |
|     let cursor = null;
 | |
| 
 | |
|     for (let page = 0; page < 5; page++) {
 | |
|       const start = performance.now();
 | |
|       const params = new URLSearchParams({ limit: '50' });
 | |
|       if (cursor !== null) params.set('cursor', cursor.toString());
 | |
| 
 | |
|       const response = await fetch(`${BASE_URL}/api/vault/metadata/paginated?${params}`);
 | |
|       const data = await response.json();
 | |
|       const time = performance.now() - start;
 | |
| 
 | |
|       if (!response.ok) {
 | |
|         console.error(`❌ Failed to load page ${page + 1}:`, data);
 | |
|         break;
 | |
|       }
 | |
| 
 | |
|       totalTime += time;
 | |
|       totalItems += data.items.length;
 | |
|       cursor = data.nextCursor;
 | |
| 
 | |
|       console.log(`  Page ${page + 1}: ${data.items.length} items in ${time.toFixed(2)}ms`);
 | |
| 
 | |
|       if (!data.hasMore) {
 | |
|         console.log(`  (End of list reached)\n`);
 | |
|         break;
 | |
|       }
 | |
|     }
 | |
| 
 | |
|     console.log(`\n📊 Pagination Results:`);
 | |
|     console.log(`   Total items loaded: ${totalItems}`);
 | |
|     console.log(`   Total time: ${totalTime.toFixed(2)}ms`);
 | |
|     console.log(`   Average per page: ${(totalTime / 5).toFixed(2)}ms`);
 | |
|     console.log(`   Memory efficient: Only ${totalItems} items in memory\n`);
 | |
| 
 | |
|     // Test 3: Search with pagination
 | |
|     console.log('🔍 Test 3: Search with pagination...');
 | |
|     const searchStart = performance.now();
 | |
|     const searchResponse = await fetch(`${BASE_URL}/api/vault/metadata/paginated?limit=50&search=test`);
 | |
|     const searchData = await searchResponse.json();
 | |
|     const searchTime = performance.now() - searchStart;
 | |
| 
 | |
|     if (!searchResponse.ok) {
 | |
|       console.error('❌ Search failed:', searchData);
 | |
|     } else {
 | |
|       console.log(`✅ Search results: ${searchData.items.length} items in ${searchTime.toFixed(2)}ms`);
 | |
|       console.log(`   Total matches: ${searchData.total}`);
 | |
|       console.log(`   Has more: ${searchData.hasMore}\n`);
 | |
|     }
 | |
| 
 | |
|     // Test 4: Large cursor offset
 | |
|     console.log('⏭️  Test 4: Large cursor offset...');
 | |
|     const largeOffsetStart = performance.now();
 | |
|     const largeOffsetResponse = await fetch(`${BASE_URL}/api/vault/metadata/paginated?limit=50&cursor=1000`);
 | |
|     const largeOffsetData = await largeOffsetResponse.json();
 | |
|     const largeOffsetTime = performance.now() - largeOffsetStart;
 | |
| 
 | |
|     if (!largeOffsetResponse.ok) {
 | |
|       console.error('❌ Large offset failed:', largeOffsetData);
 | |
|     } else {
 | |
|       console.log(`✅ Large offset (cursor=1000): ${largeOffsetData.items.length} items in ${largeOffsetTime.toFixed(2)}ms`);
 | |
|       console.log(`   Has more: ${largeOffsetData.hasMore}\n`);
 | |
|     }
 | |
| 
 | |
|     console.log('✅ All tests completed successfully!\n');
 | |
| 
 | |
|   } catch (error) {
 | |
|     console.error('❌ Test failed:', error);
 | |
|     process.exit(1);
 | |
|   }
 | |
| }
 | |
| 
 | |
| testPagination();
 |