Skip to content
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

Multi-threading headless API responses? #33

Open
MartijnR opened this issue Aug 16, 2023 · 13 comments
Open

Multi-threading headless API responses? #33

MartijnR opened this issue Aug 16, 2023 · 13 comments
Assignees
Milestone

Comments

@MartijnR
Copy link
Member

https://www.digitalocean.com/community/tutorials/how-to-use-multithreading-in-node-js

@MartijnR MartijnR added this to the Next milestone Aug 16, 2023
@MartijnR MartijnR self-assigned this Aug 16, 2023
@MartijnR
Copy link
Member Author

MartijnR commented Aug 16, 2023

Are the individual CPU cores used at/near 100% when failure occurs?

(if so, multithreading code won't be a solution)

@jkeremian
Copy link

Some stats on testing archival casebook on my local environment .

server: formPrint-dev

My local mac-pro laptop shows 6 cores:

Number of the thread I used 4, it took around 9 min to complete job, max CPU usage 55%

number of the thread I used 6, it took around 9 min to complete job, max CPU usage 55%

number of the thread I used 12, it took around 9 min to complete job, max CPU usage 55%

Regardless of the number of the thread used, the total time taken were the almost the same as well as CPU percentage.

From the CPU chart, at the start of the job, CPU usage goes max close to 55% , then comes down and averages between (27 -35) %

Screenshot 2023-08-22 at 5 18 05 PM

@jkeremian
Copy link

Looks like formPrint-dev server has only 2 cores.
Screenshot 2023-08-23 at 11 49 16 AM

@MartijnR
Copy link
Member Author

Thanks. The remote formprint-dev server only serves the XForms to your local app(s), right?

@MartijnR
Copy link
Member Author

MartijnR commented Aug 23, 2023

large number of PDF requests sent (called threads above)

My own benchmarking with Centro running and using production mode (npm start), running sh tools/benchmark-headless-pdf.sh which sends 12 requests simultaneously ON A MAC. I ran the tests 3 times.

8 cores - all used by Enketo (cluster workers) ("max processes": 16).

It took 21.648 seconds.
It took 21.806 seconds.
It took 22.909 seconds.

See three spikes below in Core 1,3,5,7 below (and barely used Core 2, 4, 6, and 8).

Screenshot 2023-08-23 at 4 15 20 PM

Green represents CPU utilization by user applications, red represents CPU utilization by Mac OS X itself, and blue indicates low-priority tasks.

4 cores set to be used by Enketo ("max processes": 4)

It took 22.016 seconds.
It took 22.043 seconds.
It took 20.652 seconds.
Screenshot 2023-08-23 at 4 20 19 PM

Note that the same 4 cores are used as in the previous set of tests!

1 core set to be used by Enketo ("max processes": 1)

It took 21.021 seconds.
It took 20.473 seconds.
It took 21.814 seconds.
Screenshot 2023-08-23 at 4 39 19 PM

Findings

  1. Clearly I am not understanding something about number of processes using clusters as the results are exactly the same with 8, 4 and 1 cluster worker (= enketo express process). Maybe something specific to using headless browsers.
  2. 4 CPUs are used 100% and 4 not used at all, which may indicate we can only get a 100% improvement, so 11 seconds is our goal.

@MartijnR
Copy link
Member Author

I am less confident the multi-threading approach will work here. We may not be able to run puppeteer in a worker_thread (am getting Illegal invocation error ) but will try more later.

Also found this that may possibly be interesting: https://github.com/thomasdondorf/puppeteer-cluster

@MartijnR
Copy link
Member Author

MartijnR commented Aug 30, 2023

  • run tests with old PDF code that launches browser for each request
  • Run on linux server, because the 4 core thing might be a Mac-specific thing
  • check out the puppeteer-cluster library? (max concurrency) no need, it seems

@MartijnR
Copy link
Member Author

MartijnR commented Sep 5, 2023

Old PDF Code

CPU core usage is similar to the previous tests.

8 cores - all used by Enketo (cluster workers) ("max processes": 16).

It took 28.170 seconds.
It took 26.171 seconds.
It took 23.776 seconds.

1 core set to be used by Enketo ("max processes": 1)

It took 29.548 seconds.
It took 23.964 seconds.
It took 23.400 seconds.

Findings

  • no change in CPU usage (on a Mac)
  • slightly slower, in particular when configuring Enketo to use more processes

@MartijnR
Copy link
Member Author

MartijnR commented Sep 6, 2023

Linux server findings:

  • all cores are used 100% (tested with 2 CPU and 8 CPU server - DigitalOcean dedicated "CPU-Optimized" droplet)
  • the "max processes" setting in Enketo has no effect
  • similar behavior with a complex form with lots of logic and a simple widgets form
  • increasing the headless timeout configuration setting in Enketo can avoid timeouts (though there always is some max limit of number of requests you can send without timing out of course).

@MartijnR
Copy link
Member Author

MartijnR commented Sep 6, 2023

script used (and a variant using a record, sent to .../instance/view/pdf instead):

#!/bin/sh
TIMEFORMAT='It took %R seconds.'

time {
    for i in {1..12}
        do
            curl --user enketorules: -d "server_url=http://localhost:3000&form_id=LabsReconciliation&ecid=a" http://localhost:8005/oc/api/v1/survey/view/pdf &
            pids[${i}]=$!
        done

    for pid in ${pids[*]}
        do
            wait $pid
        done
}

@jkeremian
Copy link

Screen.Recording.2023-09-07.at.9.20.18.AM.mov

I'm using 4 threads/requests in this test.

@MartijnR
Copy link
Member Author

MartijnR commented Oct 18, 2023

@MartijnR what is the error code you receive if you send more requests than Enketo can handle?

@MartijnR
Copy link
Member Author

MartijnR commented Oct 18, 2023

what is the error code you receive if you send more requests than Enketo can handle?

I'm getting a HTTP status code 408 (Request Timeout) response with body:

 {
    "message": "PDF generation failed: Navigation timeout of 60000 ms exceeded"
}

@MartijnR MartijnR transferred this issue from OpenClinica/enketo-express-oc May 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants