# Profiling

Canonical page: https://www.adaptabletools.com/docs/dev-guide-support-profiling

- AdapTable provides 3 very useful resources to help Support Teams profile AdapTable:
  - It will provide performance metrics (by leveraging logging)
  - It offers performance profiling (by leveraging the Chrome DevTools profiler)
  - The AdapTable DevTools browser extension has a dedicated Performance section

AdapTable provides some very useful resources to help Support Teams profile AdapTable.

They time the same AdapTable operations, and show them in different places:

| What | Turned on by | Where you see it |
| --- | --- | --- |
| [Performance Metrics](#performance-metrics) (`perf` log messages) | `localStorage.debug = 'Adaptable:*:perf'` | The browser console |
| [Chrome DevTools Profiler](#chrome-devtools-profiler) tracks | A recording in the AdapTable DevTools **Performance** section, `localStorage.adaptableProfileTracks = 'true'`, or the `perf` debug channel | The Chrome **Performance** panel, next to everything else the page does |
| The AdapTable DevTools **Performance** section | **Record** in that section | Timings, Hot Paths (the cost of every cell callback, by AdapTable object), long frames and memory (the JS heap and the size of AdapTable's structures) |

The quickest way to find what is slow is the **Performance** section of the AdapTable DevTools browser extension: it needs no setup, and it links the time spent to the AdapTable objects that caused it (see [AdapTable DevTools](https://www.adaptabletools.com/docs/dev-guide-support-devtools/index.md))

## Performance Metrics

AdapTable logs performance metrics for certain operations.

Consult the [Guide to Logging AdapTable Activity](https://www.adaptabletools.com/docs/dev-guide-support-logging/index.md) for full details on how AdapTable enables comprehensive logging

These are logged as `perf` messages.

Make sure to set the Browser's console level to `Verbose` to see these messages ([read instructions for using Chrome](https://developer.chrome.com/docs/devtools/console/log/#level))

```
// log all performance metrics for all AdapTable instances
localStorage.debug = 'Adaptable:*:perf'
```

```
// log all performance metrics for a specific AdapTable instance
localStorage.debug = 'Adaptable:<adaptableId>:perf'
```

The `perf` messages will look like this:

```
Adaptable:Trades:perf [BEGIN] - initAgGrid() +1ms
[...]
Adaptable:Trades:perf [END]   - initAgGrid() :: [delta +122ms] +1ms
```

The delta value (`[+122ms]`) is the time taken for the operation (`initAgGrid()`) to complete.

The last diff value (`+1ms`) is displayed for all messages and is the time spent between the previous message and the current one.

## Performance Profiling

Another way to profile performance in AdapTable is to use the Chrome DevTools profiler.

AdapTable will show up as a custom track in your recorded profile. 

This is a very powerful tool which will give you a better sense of the timing for various operations.

It includes stacktraces and allows you to see the correlation between user actions, other browser events and the AdapTable instance.

### Chrome DevTools Profiler

To start a profiling operation, open your DevTools, go to the `Performance` tab and start recording.

AdapTable writes its tracks in 3 cases:

- While a recording runs in the **Performance** section of [AdapTable DevTools](https://www.adaptabletools.com/docs/dev-guide-support-devtools/index.md): click **Record** there (or **Record and reload** to include the start-up), then record in the Chrome Performance panel at the same time; `startPerfRecording()` of the [DevTools In-page API](https://www.adaptabletools.com/docs/dev-guide-support-devtools-api/index.md) does the same
- All the time, when the `Adaptable:<adaptableId>:perf` debug channel is enabled (see below)
- All the time, without the extension, when the `adaptableProfileTracks` key is `"true"` in your `localStorage`:

```tsx
localStorage.adaptableProfileTracks = 'true'
```

- AdapTable reads the key again within a second, so no reload is needed; reload to include the start-up
- `window.__ADAPTABLE_DEVTOOLS__.enableProfiling()` sets it too (see [DevTools In-page API](https://www.adaptabletools.com/docs/dev-guide-support-devtools-api/index.md))
- Remove the key when you are done: it writes a track entry for every operation, all the time

Another option would be to enable the debug channels for performance metrics, as already described above

```tsx
localStorage.debug = 'Adaptable:*:perf'
```

This will show you all AdapTable instances - each under the `AdapTable (<adaptableId>)` track group.

If you have many AdapTable instances, but only want to profile one of them, you can be more specific and enable profiling only for that single instance, enabling the `Adaptable:<ADAPTABLE_ID>:perf` channel.

For instance you have a 'BigData' AdapTable instance, then you will do:
```tsx
localStorage.debug = 'Adaptable:BigData:perf'
```

Or you can simply enable all debug channels:

```tsx
localStorage.debug = '*'
```

In this picture, you can see a recorded profile and how AdapTable displays its custom tracks.

![AdapTable shows up in Chrome DevTools Performance Profiler tracks](https://www.adaptabletools.com/images/custom-dev-tracks.png)

In your recorded profiling traces, you can click various segments to get more info on the specific operation.

In the screenshot above, we selected the `'Apply table layout'` operation to get details on it. 

Beyond timing (this one took 370ms), we have info on the Layout's Name, Columns, Row Grouping and other useful Layout properties.

### Available Tracks and Labels

Each profiled AdapTable instance will show up as a group, under the name `AdapTable (<adaptableId>)`.

Inside the group for each AdapTable instance, the following tracks are available:

| Track | Labels | Shows |
| --- | --- | --- |
| `Initialisation` | `Init`, `Loading store`, `Init store`, `AG Grid init` | The start-up phases (`Init` spans all of them) |
| `Layout Manager` | `Setting layout`, `Apply table layout`, `Apply pivot layout` | Layout changes applied to the underlying AG Grid instance |
| `Runtime` | `Update column model & refresh grid`, `Derive column state from AG Grid`, `Setup columns`, `Redraw body`, `Refresh cells`, `Refresh header`, `Setting layout` | Column model refreshes (e.g. after a State change) and their steps |
| `Redux` | the action's name, e.g. `LAYOUT_SELECT` | Every `Redux` action AdapTable dispatches, how long it took, and which State sections it changed |

Each of those tracks can have multiple labels/segments show up.

![AdapTable Chrome DevTools Profiler multiple segments](https://www.adaptabletools.com/images/custom-dev-tracks2.png)
