import { Cache, caching } from 'cache-manager'; import { EkepHttpEmddClient, EmddProcessInfo } from '../../../src/'; describe('load all processes ', () => { let client: EkepHttpEmddClient; let cache: Cache; beforeAll(async () => { cache = await caching('memory', { max: 100000, ttl: 2 * 60 * 1000 /*2 hours*/, }); cache.reset(); const cacheConfig = { cache, ttl: 2 * 60 * 1000 }; client = new EkepHttpEmddClient({ cacheConfig }); }); it.only( 'Read all processes using cache', async () => { const kepProcesses = (await client.getProcesses({ filter: 'kep' })) ?? []; const eugoProcesses = (await client.getProcesses({ filter: 'eu-go' })) ?? []; const allProcesses = kepProcesses.concat(eugoProcesses); const chunkSize = 200; const runChunk = async (chunk: EmddProcessInfo[], i: number) => { console.time(`Fetched chunk ${i}`); const promises: Array> = []; for (const process of chunk) { promises.push( client.getProcess(process.id).then((r) => { console.log(`fetched process ${r.id}`); return r; }), ); } const res = await Promise.all(promises); console.timeEnd(`Fetched chunk ${i}`); return res; }; if (allProcesses) { console.log( `fetching ${allProcesses.length} in chunks of ${chunkSize}`, ); const chunks: Array = []; for (let i = 0; i < allProcesses.length; i += chunkSize) { chunks.push(allProcesses.slice(i, i + chunkSize)); } console.log(`processing ${chunks.length} chunks`); let j = 0; console.time(`Fetching lasted`); for (const chunk of chunks) { await runChunk(chunk, ++j); } console.timeEnd(`Fetching lasted`); console.log(`cacheHits ${client.processTransformer.cacheHits}`); } }, 1 * 60 * 60 * 1000, ); });