-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME.Rmd
More file actions
305 lines (225 loc) · 7.03 KB
/
Copy pathREADME.Rmd
File metadata and controls
305 lines (225 loc) · 7.03 KB
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# debrief <a href="https://r-lib.github.io/debrief/"><img src="man/figures/logo.png" align="right" height="139" alt="debrief website" /></a>
<!-- badges: start -->
[](https://lifecycle.r-lib.org/articles/stages.html#experimental)
[](https://CRAN.R-project.org/package=debrief)
[](https://github.com/r-lib/debrief/actions/workflows/R-CMD-check.yaml)
[](https://app.codecov.io/gh/r-lib/debrief)
<!-- badges: end -->
debrief provides text-based summaries and analysis tools for [profvis](https://rstudio.github.io/profvis/) profiling output. It's designed for terminal workflows and AI agent consumption, offering views including hotspot analysis, call trees, source context, caller/callee relationships, and memory allocation breakdowns.
## Installation
You can install the development version of debrief from [GitHub](https://github.com/) with:
``` r
# install.packages("pak")
pak::pak("r-lib/debrief")
```
## Quick Start
```r
library(profvis)
library(debrief)
# Profile some code
p <- profvis({
# your code here
})
# Get help on available functions
pv_help()
# Start with a summary
pv_print_debrief(p)
```
## Typical Workflow
debrief is designed for iterative profiling. Each function prints "Next steps" suggestions to guide you deeper:
```
1. pv_print_debrief(p)
-> Overview: identifies hot functions and lines
2. pv_focus(p, "hot_function")
-> Deep dive: time breakdown, callers, callees, source
3. pv_hot_lines(p)
-> Exact lines: find the specific code consuming time
4. pv_source_context(p, "file.R")
-> Code view: see source with profiling data overlay
5. pv_suggestions(p)
-> Actions: get specific optimization recommendations
```
The `pv_help()` function lists all available functions by category.
## Example
First, create a profvis profile of some code. To get source references in the
profile, write your code to a file and source it with `keep.source = TRUE`:
```{r example}
library(profvis)
library(debrief)
# Write functions to a temp file for source references
example_code <- '
process_data <- function(n) {
raw <- generate_data(n)
cleaned <- clean_data(raw)
summarize_data(cleaned)
}
generate_data <- function(n) {
x <- rnorm(n)
y <- runif(n)
data.frame(x = x, y = y, z = x * y)
}
clean_data <- function(df) {
df <- df[complete.cases(df), ]
df$x_scaled <- scale(df$x)
df$category <- cut(df$y, breaks = 5)
df
}
summarize_data <- function(df) {
list(
means = colMeans(df[, c("x", "y", "z")]),
sds = apply(df[, c("x", "y", "z")], 2, sd),
counts = table(df$category),
text = paste(round(df$x, 2), collapse = ", ")
)
}
'
writeLines(example_code, "analysis.R")
source("analysis.R", keep.source = TRUE)
# Profile the data pipeline
p <- profvis({
results <- lapply(1:5, function(i) process_data(1e5))
})
unlink("analysis.R")
```
### Quick Summary
Get a comprehensive overview with `pv_print_debrief()`:
```{r summary}
pv_print_debrief(p)
```
### Time Analysis
Analyze where time is spent:
```{r time}
# Self-time: time spent directly in each function
pv_self_time(p)
# Total time: time spent in function + all its callees
pv_total_time(p)
# Filter to significant functions only
pv_self_time(p, min_pct = 5) # >= 5% of time
```
### Hot Spots
Find the hottest lines and call paths:
```{r hotspots}
# Hot source lines with context
pv_print_hot_lines(p, n = 5, context = 3)
# Hot call paths
pv_print_hot_paths(p, n = 10)
```
### Function Analysis
Deep dive into a specific function:
```{r focus}
pv_focus(p, "clean_data")
```
### Call Relationships
Understand who calls what:
```{r calls}
# Who calls this function?
pv_callers(p, "clean_data")
# What does this function call?
pv_callees(p, "process_data")
# Full caller/callee analysis
pv_print_callers_callees(p, "summarize_data")
```
### Memory Analysis
Track memory allocations:
```{r memory}
# Memory by function
pv_print_memory(p, n = 10, by = "function")
# Memory by source line
pv_print_memory(p, n = 10, by = "line")
```
### Text-based Flame Graph
Visualize the call tree:
```{r flame}
pv_flame(p, width = 70, min_pct = 2)
```
### Compare Profiles
Measure optimization impact:
```{r compare}
# Approach 1: Growing vectors in a loop (slow)
p_slow <- profvis({
result <- c()
for (i in 1:20000) {
result <- c(result, sqrt(i) * log(i))
}
})
# Approach 2: Vectorized with memory allocation
p_fast <- profvis({
x <- rnorm(5e6)
y <- cumsum(x)
z <- paste(head(round(x, 2), 50000), collapse = ", ")
})
# Compare two profiles
pv_print_compare(p_slow, p_fast)
# Approach 3: Data frame operations
p_dataframe <- profvis({
df <- data.frame(
a = rnorm(1e6),
b = runif(1e6),
c = sample(letters, 1e6, replace = TRUE)
)
df$d <- df$a * df$b
result <- aggregate(d ~ c, data = df, FUN = mean)
})
# Compare all three approaches
pv_print_compare_many(
growing_vector = p_slow,
vectorized = p_fast,
dataframe_ops = p_dataframe
)
```
### Diagnostics
Detect GC pressure and get optimization suggestions:
```{r diagnostics}
# Detect GC pressure (indicates memory allocation issues)
pv_print_gc_pressure(p)
# Get actionable optimization suggestions
pv_print_suggestions(p)
```
### Export for AI Agents
Export structured data for programmatic access:
```{r export}
# Export as R list for programmatic access
results <- pv_to_list(p)
names(results)
# Data frame of functions by self-time
results$self_time
```
## Available Functions
| Category | Functions |
|----------|-----------|
| Overview | `pv_help()`, `pv_debrief()`, `pv_print_debrief()`, `pv_example()` |
| Time Analysis | `pv_self_time()`, `pv_total_time()` |
| Hot Spots | `pv_hot_lines()`, `pv_hot_paths()`, `pv_worst_line()`, `pv_print_hot_lines()`, `pv_print_hot_paths()` |
| Memory | `pv_memory()`, `pv_memory_lines()`, `pv_print_memory()` |
| Call Analysis | `pv_callers()`, `pv_callees()`, `pv_call_depth()`, `pv_call_stats()` |
| Function Analysis | `pv_focus()`, `pv_recursive()` |
| Source Context | `pv_source_context()`, `pv_file_summary()` |
| Visualization | `pv_flame()`, `pv_flame_condense()` |
| Comparison | `pv_compare()`, `pv_print_compare()`, `pv_compare_many()`, `pv_print_compare_many()` |
| Diagnostics | `pv_gc_pressure()`, `pv_suggestions()` |
| Export | `pv_to_json()`, `pv_to_list()` |
### Filtering Support
Time and hot spot functions support filtering:
```r
# Filter by percentage threshold
pv_self_time(p, min_pct = 5)
pv_hot_lines(p, min_pct = 10)
# Filter by time threshold
pv_self_time(p, min_time_ms = 100)
# Limit number of results
pv_self_time(p, n = 10)
# Combine filters
pv_hot_lines(p, n = 5, min_pct = 2, min_time_ms = 10)
```