Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save leesahanders/f6c105f21b332cc84737863cb58b1f7a to your computer and use it in GitHub Desktop.
Save leesahanders/f6c105f21b332cc84737863cb58b1f7a to your computer and use it in GitHub Desktop.
2023-09-27 Shinyloadtest and Post files working example

Goal

Explain how to include the <output_file>.post. files when running shinyloadtest.

Explanation

What's happening is that for shiny apps where data is being uploaded (for example using fileInput() ), that uploaded data won't be include in the recording file but in auxiliary "post" files. Shinycannon / Shinyloadtest will automatically detect and use the recording.log and recording.log.post files as long as the tests are being run from the same directory that those files are saved into.
 
A nice addition added by the team was that when the recording completes a message will display with the names of the created files, for example (from the github issue here: rstudio/shinyloadtest#114 ) like this:
 

> record_session("https://my.rsc.com/content/4223/", output_file = "upload.log")
Listening on 127.0.0.1:8600
Navigating to: http://127.0.0.1:8600/
Client connected
Client disconnected
Stopping server
Note: uploaded files to keep with recording file:
 upload.log.post.0
 upload.log.post.1
Server disconnected

 
 
For example, we can walk through an example using this file-upload shiny app example from the gallery to show how this could work: https://shiny.posit.co/r/gallery/widgets/file-upload/
 
This app can be deployed to your Connect server where we will be running the load tests.
 
We'll need an API key in order to give shinyloadtest permission to access the deployed app, I'm a fan of storing it in environment variables. See the user guide for how to acquire an API key (https://docs.posit.co/connect/user/api-keys/ ). The Connect API key is stored as the r environment variable connect_api_key. It can be edited/modified using the usethis package with:

library(usethis)
usethis::edit_r_environ()

 
Create the recording:

library(shinyloadtest)

shinyloadtest::record_session(
    target_app_url='<REDACTED>',
  connect_api_key=Sys.getenv("CONNECT_API_KEY"))

 
We'll see a message like this in the console:

Authenticating using provided connect_api_key.
Listening on 127.0.0.1:8600
Navigating to: http://127.0.0.1:8600/
Client connected
Client disconnected
Stopping server
Note: uploaded files to keep with recording file:
 recording.log.post.0

 
And we'll now see two files created:

  • recording.log

  • recording.log.post.0

 
We can now use shinycannon to run the load tests (either directly from bash/terminal, or using the system() command from R):
 
Export the API key as a variable using the terminal:

export SHINYCANNON_CONNECT_API_KEY=<add your key here>

 
Make sure you are in the correct folder where the .jar file for shinycannon has been downloaded (from https://rstudio.github.io/shinyloadtest/articles/shinycannon.html ) and your recording files are saved to by using cd.
 
Run the load test:

java -jar shinycannon-1.1.3-dd43f6b.jar recording.log <URL REDACTED> --workers 1 --loaded-duration-minutes 2 --output-dir run1 --overwrite-output

 
shinyloadtest will automatically detect the recording.log.post.0 file(s) given the address for the recording.log file.
 
Change the output directory and number of workers and run it again:

java -jar shinycannon-1.1.3-dd43f6b.jar recording.log <URL REDACTED> --workers 3 --loaded-duration-minutes 2 --output-dir run2 --overwrite-output

 
Analyze the results:

library(shinyloadtest)
library(dplyr)

df <- load_runs(
1 user = "run1",
3 users = "run2"
)

shinyloadtest_report(df, "report.html")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment