|
| 1 | +--- |
| 2 | +title: Profile a Transaction |
| 3 | +description: How to profile the computational performance of a Flow transaction |
| 4 | +sidebar_position: 9 |
| 5 | +keywords: |
| 6 | + - flow cli |
| 7 | + - transactions |
| 8 | + - profiling |
| 9 | + - performance |
| 10 | + - optimization |
| 11 | + - computation |
| 12 | + - pprof |
| 13 | + - gas optimization |
| 14 | + - cadence profiling |
| 15 | +--- |
| 16 | + |
| 17 | +The Flow CLI provides a command to profile the computational performance of sealed transactions on any Flow network. This diagnostic tool generates detailed CPU profiles in the industry-standard `pprof` format, allowing you to analyze exactly where computation is being spent during transaction execution. |
| 18 | + |
| 19 | +:::info |
| 20 | +The command works by forking the blockchain state and replaying the transaction in an isolated environment, ensuring accurate profiling results that match the original execution. Learn more about state forking in the [Fork Testing guide](../fork-testing.md). |
| 21 | +::: |
| 22 | + |
| 23 | +```shell |
| 24 | +flow transactions profile <tx_id> --network <network_name> [flags] |
| 25 | +``` |
| 26 | + |
| 27 | +## Use Cases |
| 28 | + |
| 29 | +Transaction profiling helps developers: |
| 30 | + |
| 31 | +- **Optimize Transaction Costs**: Identify computational bottlenecks and optimize gas-heavy operations |
| 32 | +- **Debug High Gas Usage**: Understand why a transaction consumed more computation than expected |
| 33 | +- **Analyze Production Transactions**: Profile real transactions on mainnet or testnet to understand actual performance |
| 34 | +- **Compare Implementations**: Evaluate different approaches by comparing their computational profiles |
| 35 | +- **Find Performance Issues**: Trace computation usage through contract calls and dependencies |
| 36 | + |
| 37 | +## Example Usage |
| 38 | + |
| 39 | +Profile a mainnet transaction: |
| 40 | + |
| 41 | +```shell |
| 42 | +> flow transactions profile 07a8...b433 --network mainnet |
| 43 | + |
| 44 | +Transaction Profiling Report |
| 45 | +━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ |
| 46 | + |
| 47 | +Transaction ID: 07a8...b433 |
| 48 | +Network: mainnet |
| 49 | +Block Height: 12345678 |
| 50 | +Status: SEALED |
| 51 | +Events emitted: 5 |
| 52 | +Computation: 1234 |
| 53 | + |
| 54 | +Profile saved: profile-07a8b433.pb.gz |
| 55 | + |
| 56 | +Analyze with: |
| 57 | + go tool pprof -http=:8080 profile-07a8b433.pb.gz |
| 58 | +``` |
| 59 | + |
| 60 | +Profile with custom output location: |
| 61 | + |
| 62 | +```shell |
| 63 | +> flow transactions profile 0xabc123 --network testnet --output my-profile.pb.gz |
| 64 | + |
| 65 | +Profile saved: my-profile.pb.gz |
| 66 | +``` |
| 67 | + |
| 68 | +Profile an emulator transaction: |
| 69 | + |
| 70 | +```shell |
| 71 | +> flow transactions profile 0xdef456 --network emulator |
| 72 | +``` |
| 73 | + |
| 74 | +## Analyzing Profile Data |
| 75 | + |
| 76 | +The generated `.pb.gz` file can be analyzed using Go's pprof tools. If you don't have Go installed, see the [Go installation guide](https://go.dev/doc/install). |
| 77 | + |
| 78 | +### Interactive Web Interface |
| 79 | + |
| 80 | +Open the profile in an interactive web interface: |
| 81 | + |
| 82 | +```bash |
| 83 | +go tool pprof -http=:8080 profile-07a8b433.pb.gz |
| 84 | +``` |
| 85 | + |
| 86 | +Then navigate to `http://localhost:8080` in your browser. |
| 87 | + |
| 88 | +The pprof web interface provides several visualization options: |
| 89 | + |
| 90 | +| View | Description | |
| 91 | +|------|-------------| |
| 92 | +| **Flame Graph** | Visual representation of call stacks with computation costs | |
| 93 | +| **Graph** | Directed graph showing call relationships | |
| 94 | +| **Top** | List of functions sorted by computation usage | |
| 95 | +| **Source** | Source code annotated with computation costs | |
| 96 | + |
| 97 | +### Command-Line Analysis |
| 98 | + |
| 99 | +View top computation consumers: |
| 100 | + |
| 101 | +```bash |
| 102 | +go tool pprof -top profile-07a8b433.pb.gz |
| 103 | +``` |
| 104 | + |
| 105 | +List all functions with costs: |
| 106 | + |
| 107 | +```bash |
| 108 | +go tool pprof -list=. profile-07a8b433.pb.gz |
| 109 | +``` |
| 110 | + |
| 111 | +Generate a flame graph image: |
| 112 | + |
| 113 | +```bash |
| 114 | +go tool pprof -png profile-07a8b433.pb.gz > profile.png |
| 115 | +``` |
| 116 | + |
| 117 | +For comprehensive information on analyzing computation profiles, see the [Cadence Computation Profiling guide](../../../cadence/advanced-concepts/computation-profiling.md). |
| 118 | + |
| 119 | +## How It Works |
| 120 | + |
| 121 | +The profiling process: |
| 122 | + |
| 123 | +1. **Fetches the Transaction**: Retrieves the target sealed transaction by ID from the specified network |
| 124 | +2. **Forks Blockchain State**: Creates a fork of the blockchain state from the block immediately before the transaction's block (uses the same forking mechanism as [Fork Testing](../fork-testing.md)) |
| 125 | +3. **Replays Execution**: Replays all prior transactions in the same block to recreate the exact state |
| 126 | +4. **Profiles Target Transaction**: Executes the target transaction with Cadence runtime profiling enabled |
| 127 | +5. **Exports Profile**: Saves the profiling data to a pprof-compatible file |
| 128 | + |
| 129 | +This ensures the profile accurately reflects the transaction's execution in its original context. |
| 130 | + |
| 131 | +:::info |
| 132 | +The transaction profiling command uses Flow's state forking capabilities under the hood to create an accurate execution environment. Learn more about state forking in the [Fork Testing guide](../fork-testing.md). |
| 133 | +::: |
| 134 | + |
| 135 | +## Arguments |
| 136 | + |
| 137 | +### Transaction ID |
| 138 | + |
| 139 | +- Name: `<tx_id>` |
| 140 | +- Valid Input: transaction ID (with or without `0x` prefix) |
| 141 | + |
| 142 | +The transaction ID to profile. The transaction must be sealed. |
| 143 | + |
| 144 | +## Flags |
| 145 | + |
| 146 | +### Network |
| 147 | + |
| 148 | +- Flag: `--network` |
| 149 | +- Short Flag: `-n` |
| 150 | +- Valid inputs: the name of a network defined in `flow.json` |
| 151 | +- **Required** |
| 152 | + |
| 153 | +Specify which network the transaction was executed on (e.g., `mainnet`, `testnet`, `emulator`). |
| 154 | + |
| 155 | +### Output |
| 156 | + |
| 157 | +- Flag: `--output` |
| 158 | +- Short Flag: `-o` |
| 159 | +- Valid inputs: valid file path |
| 160 | +- Default: `profile-{tx_id_prefix}.pb.gz` |
| 161 | + |
| 162 | +Custom output file path for the profile data. The file will be saved in compressed pprof format (`.pb.gz`). |
| 163 | + |
| 164 | +### Host |
| 165 | + |
| 166 | +- Flag: `--host` |
| 167 | +- Valid inputs: an IP address or hostname |
| 168 | +- Default: `127.0.0.1:3569` (Flow Emulator) |
| 169 | + |
| 170 | +Specify the hostname of the Access API that will be used to fetch transaction data. This flag overrides any host defined by the `--network` flag. |
| 171 | + |
| 172 | +### Network Key |
| 173 | + |
| 174 | +- Flag: `--network-key` |
| 175 | +- Valid inputs: A valid network public key of the host in hex string format |
| 176 | + |
| 177 | +Specify the network public key of the Access API that will be used to create a secure GRPC client when executing the command. |
| 178 | + |
| 179 | +### Filter |
| 180 | + |
| 181 | +- Flag: `--filter` |
| 182 | +- Short Flag: `-x` |
| 183 | +- Valid inputs: a case-sensitive name of the result property |
| 184 | + |
| 185 | +Specify any property name from the result you want to return as the only value. |
| 186 | + |
| 187 | +### Output Format |
| 188 | + |
| 189 | +- Flag: `--output` |
| 190 | +- Short Flag: `-o` |
| 191 | +- Valid inputs: `json`, `inline` |
| 192 | + |
| 193 | +Specify the format of the command results displayed in the console. |
| 194 | + |
| 195 | +### Save |
| 196 | + |
| 197 | +- Flag: `--save` |
| 198 | +- Short Flag: `-s` |
| 199 | +- Valid inputs: a path in the current filesystem |
| 200 | + |
| 201 | +Specify the filename where you want the result summary to be saved. |
| 202 | + |
| 203 | +### Log |
| 204 | + |
| 205 | +- Flag: `--log` |
| 206 | +- Short Flag: `-l` |
| 207 | +- Valid inputs: `none`, `error`, `debug` |
| 208 | +- Default: `info` |
| 209 | + |
| 210 | +Specify the log level. Control how much output you want to see during command execution. |
| 211 | + |
| 212 | +### Configuration |
| 213 | + |
| 214 | +- Flag: `--config-path` |
| 215 | +- Short Flag: `-f` |
| 216 | +- Valid inputs: a path in the current filesystem |
| 217 | +- Default: `flow.json` |
| 218 | + |
| 219 | +Specify the path to the `flow.json` configuration file. |
| 220 | + |
| 221 | +### Version Check |
| 222 | + |
| 223 | +- Flag: `--skip-version-check` |
| 224 | +- Default: `false` |
| 225 | + |
| 226 | +Skip version check during start up to speed up process for slow connections. |
| 227 | + |
| 228 | +## Requirements |
| 229 | + |
| 230 | +### Transaction Must Be Sealed |
| 231 | + |
| 232 | +Only sealed transactions can be profiled. Attempting to profile a pending or finalized transaction will result in an error. |
| 233 | + |
| 234 | +```shell |
| 235 | +Error: transaction is not sealed (status: PENDING) |
| 236 | +``` |
| 237 | + |
| 238 | +Wait for the transaction to be sealed before profiling, or use a different transaction. |
| 239 | + |
| 240 | +### Network Configuration |
| 241 | + |
| 242 | +The network must be properly configured in your `flow.json` file: |
| 243 | + |
| 244 | +```json |
| 245 | +{ |
| 246 | + "networks": { |
| 247 | + "mainnet": "access.mainnet.nodes.onflow.org:9000", |
| 248 | + "testnet": "access.devnet.nodes.onflow.org:9000" |
| 249 | + } |
| 250 | +} |
| 251 | +``` |
| 252 | + |
| 253 | +### Go Toolchain (for analysis) |
| 254 | + |
| 255 | +To analyze the generated profile files, you need Go installed on your system. The `pprof` tool is included in the standard Go distribution. |
| 256 | + |
| 257 | +Install Go from: https://go.dev/doc/install |
| 258 | + |
| 259 | +## Related Documentation |
| 260 | + |
| 261 | +- **[Cadence Computation Profiling](../../../cadence/advanced-concepts/computation-profiling.md)** - Comprehensive guide on profiling and optimization |
| 262 | +- **[Fork Testing Guide](../fork-testing.md)** - Learn more about the state forking capabilities used by this command |
| 263 | +- **[Testing Strategy](../../../cadence/smart-contracts/testing-strategy.md)** - How profiling fits into your overall testing and optimization workflow |
| 264 | +- **[Transaction Fees](../../../cadence/basics/fees.md)** - Understanding computation costs and fee optimization |
0 commit comments