-
Notifications
You must be signed in to change notification settings - Fork 41
/
23-performance.Rmd
231 lines (176 loc) · 5.64 KB
/
23-performance.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# Performance
## Restaurant Shiny
- Server =~ Restaurant
- User =~ Customer
- Request =~ Order
- R process =~ Chef
To make each chef more efficient
- Identify any slow steps
- Brainstorm to make them faster
To make the restaurant more efficient
- Hire more chefs
- Buy more restaurants
## Benchmarking
How does the app handle multiple users?
### Tools
- [shinyloadtest](https://rstudio.github.io/shinyloadtest/)
- Run an app
- Record how you interact with the app
- [`shinycannon`](https://rstudio.github.io/shinyloadtest/#shinycannon): rerun the session
multiple times
- analyse the
[log](https://rstudio.github.io/shinyloadtest/articles/analyzing-load-test-logs.html)
of the reran sessions
```{r, eval = FALSE}
runApp("some_app.R")
shinyloadtest::record_session("http://127.0.0.1:9876")
# See "./recording.log"
```
```bash
shinycannon ./recording.log http://127.0.0.1:9876 \
--workers 10 \
--loaded-duration-minutes 5 \
--output-dir run1
```
```{r, eval = FALSE}
library(shinyloadtest)
df <- load_runs("./run1")
shinyloadtest_report(df, "report.html")
```
## Profiling
Where is the app slow / memory-hungry?
### Tools
- profvis
The call stack and the "Flame graph"
```{r, eval = FALSE}
library(profvis)
f <- function() {
pause(0.2)
g()
h()
10
}
g <- function() {
pause(0.1)
h()
}
h <- function() {
pause(0.3)
}
profvis::profvis(f())
```
Similar for {shiny}, but need explicit call to start the app (and close it with [Ctrl-C])
```{r, eval = FALSE}
ui <- fluidPage(
actionButton("x", "Push me"),
textOutput("y")
)
server <- function(input, output, session) {
output$y <- eventReactive(input$x, f())
}
app <- shinyApp(ui, server)
```
```{r, eval = FALSE}
profvis::profvis(
shiny::runApp(
app()
)
)
```
Note that data downloads may not be tracked.
## Optimisation
### Cacheing
- `bindCache`
- cacheing of any reactive value or render function
- uses 'cache keys'
- keep them as simple as possible
- date/time-dependent results
- cache has fixed size
- scope (per session, or per process; in-memory or on-disk)
```{r, eval = FALSE}
r <- reactive(slow_function(input$x, input$y)) %>%
bindCache(input$x, input$y)
output$text <- renderText(slow_function2(input$z)) %>%
bindCache(input$z)
```
### Other Optimisations
- Do as little as possible
- Do your prep outside of the server
- Do your prep outside of shiny
- Use the fastest import method available
- Only do the slow stuff if requested
- Learn async programming
## Resources
- [https://rstudio.com/resources/rstudioconf-2019/shiny-in-production-principles-practices-and-tools/]()
- [https://rstudio.github.io/shinyloadtest/articles/analyzing-load-test-logs.html]()
- [https://queue.acm.org/detail.cfm?id=2927301]()
- [https://adv-r.hadley.nz/perf-improve.html]()
- [https://csgillespie.github.io/efficientR/]()
- [https://rstudio.com/resources/rstudioconf-2018/make-shiny-fast-by-doing-as-little-work-as-possible/]()
- [https://shiny.rstudio.com/articles/caching.html]()
- [https://shiny.rstudio.com/app-stories/weather-lookup-caching.html]()
## Meeting Videos
### Cohort 1
`r knitr::include_url("https://www.youtube.com/embed/LLO6vDLqhWw")`
<details>
<summary> Meeting chat log </summary>
```
00:07:09 Russ Hyde: Hi
00:45:54 Robert Overman: Are we having a session on shiny server?
01:01:58 [email protected]: Thanks!
01:04:51 [email protected]: Yes, sure
```
</details>
### Cohort 2
`r knitr::include_url("https://www.youtube.com/embed/UwrBxS39E3g")`
### Cohort 3
`r knitr::include_url("https://www.youtube.com/embed/GFbRR--E8Ow")`
<details>
<summary>Meeting chat log</summary>
```
00:06:14 Brendan Lam: What function records interactions with your Shiny app? How do you simulate users interacting with your app?
What is a flame graph and how do you interpret it?
What are the cache key and scope?
What are two ways of optimizing your app
00:09:25 Brendan Lam: https://rstudio.github.io/promises/
00:14:39 Brendan Lam: https://github.com/rstudio/shinycannon/issues/69#issuecomment-1322390215
00:22:46 Brendan Lam: We start with f().
Then f() calls g(),
Then g() calls h().
Then f() calls h().
00:28:03 Brendan Lam: https://memoise.r-lib.org/
00:38:16 Brendan Lam: arrow::write_feather()
00:38:23 Brendan Lam: arrow::read_feather()
00:38:30 Brendan Lam: qs::qread()/qs::qsave() instead of readRDS()/saveRDS()
00:41:15 Brendan Lam: https://rstudio.com/resources/rstudioconf-2019/shiny-in-production-principles-practices-and-tools/
https://rstudio.github.io/shinyloadtest/articles/analyzing-load-test-logs.html
https://queue.acm.org/detail.cfm?id=2927301
https://adv-r.hadley.nz/perf-improve.html
https://csgillespie.github.io/efficientR/
https://rstudio.com/resources/rstudioconf-2018/make-shiny-fast-by-doing-as-little-work-as-possible/
https://shiny.rstudio.com/articles/caching.html
https://shiny.rstudio.com/app-stories/weather-lookup-caching.html
00:45:15 Brendan Lam: https://twitter.com/brendannathanl1
```
</details>
### Cohort 4
`r knitr::include_url("https://www.youtube.com/embed/bxSj2VdA8XI")`
<details>
<summary>Meeting chat log</summary>
```
00:09:46 Trevin Flickinger: start
00:15:04 Trevin Flickinger: https://rstudio.github.io/shinyloadtest/articles/case-study-scaling.html
00:15:54 Trevin Flickinger: https://posit.co/resources/videos/shiny-in-production-principles-practices-and-tools/
00:22:35 Trevin Flickinger: https://rstudio.github.io/promises/
00:47:58 Matthew Efoli: https://www.youtube.com/watch?v=hltOgAC2mC4&list=PLexAKolMzPcriOdeLwoMxQOyHRnMguEv4&index=28
00:50:44 Trevin Flickinger: stop
```
</details>
### Cohort 5
`r knitr::include_url("https://www.youtube.com/embed/URL")`
<details>
<summary>Meeting chat log</summary>
```
LOG
```
</details>