Rayrun
← Back to Discord Forum

How to optimize CPU usage

tphillips8117

Also, why are you using 'networkidle'?

This thread is trying to answer question "How to optimize CPU usage in a Playwright-based script?"

11 replies
tphillips8117
image.png
tphillips8117

Also you're missing the code that gets res

tphillips8117

I am also not sure what you're meaning when you say "with 12 requests at the same time"

tphillips8117

it looks like you're creating 3 pages one after the other, and doing everything sequentially

tphillips8117

You also say "generating errors" - what are the errors?

tphillips8117

Also, why are you setting the user agent yourself?

Would agree networkIdle should be avoided generally especially if doing UI functionality. In the years i've use PW i've only had one use case where i really needed it, but then again it was straight up API testing not UI testing...

Hard to say without seeing more of the source code, but one general optimization you could probably do would be to only create one browser/context and spawn your workers with context.newPage() Something like:

const browserContext = await createPlaywrightInstance();
  const threadCount = 12;
  
  const promArray = Array.from({length: threadCount}, () => navigatePage(browserContext));
  
  const responses = await Promise.all(promArray);
  
  async function navigatePage(browserContext: BrowserContext): Promise<Response> {
    const page = await browserContext.newPage();
    const responsePromise = page.waitForResponse('https://www.google.com');
    await page.goto('https://www.google.com');
    const response = await responsePromise;
    return response;
  }

Also the flavor for the Docker image, 1 core lowest CPU/Memory possible. But seriously ever seeing:

await page.waitForTimeout(10000);

Will raise questions...

Another thing if talking about optimized, first question is why is the code written in such a synchronous manor? Seems the code is serial async code... Why aren't the page requests made in an async manor? Hard to see one asking for optimized code when the code isn't written to be optimal?

Thanks for your response. I use page.waitForTimeout(10000) only to observe where the CPU usage increases.

When I say I make 12 requests simultaneously, I mean that I simulate 12 users making a request to the same endpoint using Postman. The endpoint then executes the code I shared. This code is only an example I use to identify which Playwright methods consume CPU and to determine how many requests can be handled simultaneously before reaching the CPU usage limit.

I am doing this because my app uses 90% of the CPU when receiving 4 simultaneous requests. I need to identify the problem, and for that reason, I would like to know if it is normal to have high CPU usage or if there might be an issue with how I am using the library.

@dand33 The code is writen like that only to see where the CPU usage increases.

Hard to say performance tuning and optimizing you will need to get really invasive and start instrumenting API calls, the server code you are hitting. One observation if your client code wasn't async code, and doing serial async synchronously, if that pattern is indicative of the rest of you code on the server, think you should be able to make some decent gains, general performance and optimizing your app, seems a bit out of the scope for PW. But seems you are tyring to benchmark the server from a client app. Is the server code instrumented? Just seems the server code is what needs more attention? Might suggest starting with 1 request, then 2 and compare the test runs, and later cold vs warm starts. Seems really counter productive if 4 request tank the machine. Given 4 request bumps the usage up to 90%. Okay the machine is dead and hard to get meaningful numbers. Nothing feels better then taking a processes at work once and changing stuff around that took 40 minutes and get it down to 5 minutes. But first need to find what is taking all the time.

As for PW's perf i've never questioned it or had a concern about it to be honest, but would be all relative to the environment/machine it is run on. One app i test locally for my own stuff, generally it will averages 21 compete request per second (request sent and response received and processed) and this is with UI updates too.

If i were you, sounds like you really need to instrument the server code and take the time to see where your app is bottlenecking and see how/where to speed it up.

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.