-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: performance evaluation #61
Open
jirispilka
wants to merge
8
commits into
feat/block-media
Choose a base branch
from
feat/perf-eval
base: feat/block-media
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
13e1501
fix: sort defaults
jirispilka e708227
fix preNavigationHooks
jirispilka 8b38952
fix: input
jirispilka 9c2ab32
fix: input and headless
jirispilka 5268750
fix: false positive issue
jirispilka 888a714
fix: add blocking into a function, pass blockMedia in userData
jirispilka 78934b9
fix: add blocking into a function, pass blockMedia in userData
jirispilka 4e88619
fix: update README.md
jirispilka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/** | ||
* Performance evaluation of the RAG Web Browser with respect to different settings. | ||
* This script runs a series of queries and saves performance results into a dataset. | ||
* The results include average time for each time measure event. | ||
* | ||
* The evaluation is performed with different combinations of the following parameters: | ||
* - `scrapingTool`: The tool used for scraping (e.g., `raw-http`, `browser-playwright`). | ||
* - `mediaBlocked`: Whether media content is blocked during scraping (true/false). | ||
* - `maxResults`: The maximum number of results to scrape (e.g., 1, 3). | ||
* | ||
* The script performs the following steps: | ||
* 1. Runs a set of predefined queries using different combinations of parameters. | ||
* 2. Fetches the results and computes the average time for each time measure event. | ||
* 3. Logs the performance results, including average latency for each combination of parameters. | ||
* 4. Aborts the last run of the actor to ensure no resources are wasted. | ||
* | ||
* The results are stored in a table format, showing the average latency for each combination of parameters. | ||
* | ||
* Usage: | ||
* - Ensure the `APIFY_TOKEN` environment variable is set with your Apify API token. | ||
* - Run the script to perform the performance evaluation. | ||
* - The results will be logged to the console. | ||
*/ | ||
|
||
import { log } from 'apify'; | ||
|
||
import { Output } from './types'; | ||
|
||
const EVALUATION_QUERIES = [ | ||
'apify', | ||
'donald trump', | ||
'ai agents', | ||
]; | ||
|
||
const apifyToken = process.env.APIFY_TOKEN; | ||
|
||
const user = 'jiri-spilka'; | ||
const actorId = 'apify~rag-web-browser'; | ||
const urlUserActor = `${user}--rag-web-browser-task`; | ||
|
||
const memory = 8; // memory can't be changed in the standby mode | ||
const scrapingToolSet = ['raw-http', 'browser-playwright']; | ||
const mediaBlockedSet = [true, false]; | ||
const maxResultsSet = [1, 3]; | ||
|
||
const url = `https://${urlUserActor}.apify.actor`; | ||
|
||
const headers = { | ||
Accept: 'application/json', | ||
Authorization: `Bearer ${apifyToken}`, | ||
}; | ||
|
||
const results = new Map<string, Output[]>(); | ||
const resultsTable = []; | ||
|
||
for (const scrapingTool of scrapingToolSet) { | ||
for (const blockMedia of mediaBlockedSet) { | ||
for (const maxResults of maxResultsSet) { | ||
log.info(`Running ${EVALUATION_QUERIES.length} query/queries with ${scrapingTool}, mediaBlocked=${blockMedia}, maxResults=${maxResults}`); | ||
log.info('Start in standby mode'); | ||
const r1 = await fetch(url, { method: 'GET', headers }); | ||
if (!r1.ok) { | ||
throw new Error(`Failed to run the actor: ${JSON.stringify(await r1.json())}`); | ||
} else { | ||
// sleep for 10 seconds to let the actor start | ||
await new Promise((resolve) => setTimeout(resolve, 10000)); | ||
} | ||
for (const q of EVALUATION_QUERIES) { | ||
const queryParams = new URLSearchParams({ query: q, scrapingTool, blockMedia: blockMedia.toString(), debugMode: 'true', maxResults: maxResults.toString() }); | ||
const urlWithParams = `${url}/search?${queryParams.toString()}`; | ||
log.info(`Running ${urlWithParams}`); | ||
const res = await fetch(urlWithParams, { method: 'GET', headers }); | ||
if (!res.ok) { | ||
throw new Error(`Failed to run the actor: ${JSON.stringify(await res.json())}`); | ||
} | ||
const data: Output[] = await res.json(); | ||
log.info(`Received number of results: ${data.length}`); | ||
const k = `${scrapingTool}__${blockMedia ? 'blocked' : 'allowed'}__${maxResults}`; | ||
if (results.has(k)) { | ||
results.set(k, [...results.get(k)!, ...data]); | ||
} else { | ||
results.set(k, data); | ||
} | ||
} | ||
log.info(`Get the last run: ${actorId}`); | ||
const response = await fetch(`https://api.apify.com/v2/acts/${actorId}/runs/last`, { headers }); | ||
const resp = await response.json(); | ||
const { id: runId } = resp.data; | ||
|
||
// it is better to abort run not to mix results and involve autoscaling into the mix | ||
log.info(`Abort run ${runId}`); | ||
const r = await fetch(`https://api.apify.com/v2/actor-runs/${runId}/abort`, { method: 'POST', headers }); | ||
log.info(`The last run has been aborted status=${r.status}`); | ||
} | ||
} | ||
} | ||
|
||
for (const [key, data] of results) { | ||
const remoteDataset = data; | ||
log.info('Compute average time for each time measure event'); | ||
const timeMeasuresMap = new Map<string, number[]>(); | ||
const timeMeasuresTimeTaken = []; | ||
|
||
// compute average time for the timeMeasures | ||
for (const item of remoteDataset) { | ||
const { timeMeasures } = item.crawl.debug ?? {}; | ||
if (!timeMeasures) { | ||
continue; | ||
} | ||
for (const measure of timeMeasures) { | ||
if (!timeMeasuresMap.has(measure.event)) { | ||
timeMeasuresMap.set(measure.event, []); | ||
} | ||
timeMeasuresMap.set(measure.event, [...timeMeasuresMap.get(measure.event)!, measure.timeDeltaPrevMs]); | ||
if (measure.event === 'playwright-before-response-send' || measure.event === 'cheerio-before-response-send') { | ||
timeMeasuresTimeTaken.push(measure.timeMs); | ||
} | ||
} | ||
} | ||
log.info(`Performance for key: ${key}`); | ||
log.info('Average time for each time measure event:', timeMeasuresMap); | ||
|
||
for (const [k, value] of timeMeasuresMap) { | ||
const sum = value.reduce((a, b) => a + b, 0); | ||
const avg = sum / value.length; | ||
log.info(`${k}: ${avg.toFixed(0)} ms`); | ||
} | ||
|
||
const avgLatency = timeMeasuresTimeTaken.reduce((a, b) => a + b, 0) / timeMeasuresTimeTaken.length / 1000; | ||
log.info('Time taken for each request:', timeMeasuresTimeTaken); | ||
log.info('Time taken on average', { average: avgLatency.toFixed(1) }); | ||
|
||
// Store results for the table | ||
const [scrapingTool, mediaBlocked, maxResults] = key.split('__'); | ||
resultsTable.push(`| ${memory} | ${scrapingTool} | ${mediaBlocked} | ${maxResults} | ${avgLatency.toFixed(1)} |`); | ||
} | ||
|
||
// Print the results table | ||
log.info('\nPerformance Results:'); | ||
log.info('| Memory (GB) | Scraping Tool | Media | Max Results | Latency (sec) |'); | ||
log.info('|-------------|---------------|---------------|-------------|---------------|'); | ||
resultsTable.forEach((row) => log.info(row)); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
page.route
disables native browser cache which is whyblockRequests
is normally recommended (that is a native Chromium CDP call). The cache disabling is only bad if you do more requests for the same site. I would do a perf test on more URLs of the same site and test more sites because this could slow us down as well.