# Profile Neuron calculations

With [Neuron](/build-spark-services/neuron.md), we can evaluate the accuracy and performance of compiled Wasm by using a `Debug` build.

## Prepare the Excel file

1. With your Excel file, apply the appropriate mappings to identify inputs and outputs.
2. Add a new named range referenced to a cell called `xBuildParams`. Set its value to `'--build=Debug` as shown in the screenshot.\
   ![](/files/tn4eHvgbADXB7Rvzku4P)

## Examine self-test results

1. Make sure to complete the steps in [#prepare-the-excel-file](#prepare-the-excel-file "mention").
2. Upload the service to Spark. Note that `Debug` builds are slower to compile and execute and should only be used for debugging purposes only.
3. Upon the completion of the upload, the compiler logs will contain detailed information about the [Neuron](/build-spark-services/neuron.md) self-test (see [Upload log messages](/build-spark-services/upload-log-messages.md#neuron-compiler-messages)). Download the log and inspect the file contents. Look for the `trace` object.
   * Calculations are listed in the order they are performed.
   * `c` cell address.
   * `v` value calculated by [Neuron](/build-spark-services/neuron.md).
   * `e` expected calculation by Excel using the value saved into the file.
   * `match` indicates whether or not `v` = `e` within the accuracy described in the compiler message.
   * `soe` and `moe` are error metrics described in [Upload log messages](/build-spark-services/upload-log-messages.md#self-test).

## Profile the calculations

1. Make sure to complete the steps in [#prepare-the-excel-file](#prepare-the-excel-file "mention").
2. Navigate to the [API Tester](/navigation/api-tester.md).
3. In the *Raw view* add the additional parameters to the `request_meta`:
   * `"profile": true` enables the profiling actions. This records the start and end timestamp for the calculation of each cell.
4. Optionally in the `request_meta` you can include `"top_n_profile": {number>0}`:
   * This returns the calculation time for the `{number}` of sheets, columns, rows, and cells each respectively.
   * If this parameter is included, then the Perfetto visualization described below cannot be used.
5. Remember to separate these parameters with a comma (`,`).
6. Send the API call a few times to ensure the service is cached in the Spark environment.
7. The profile results will not be displayed in the API Tester as the contents may be very long and affect the user interface.
8. Open your browser's Developer Tools Network Inspector ([Chrome](https://developer.chrome.com/docs/devtools/network), [Edge](https://learn.microsoft.com/en-us/microsoft-edge/devtools-guide-chromium/network/), [Firefox](https://firefox-source-docs.mozilla.org/devtools-user/network_monitor/), [Safari](https://developer.apple.com/documentation/safari-developer-tools/web-inspector)).
9. Submit an additional API call and identify the call to the `Execute` endpoint. You can view the response of the API call. Note that the response should include an object for `traceEvents`. The output depends on whether the optional `"top_n_profile": {number}` was provided.

   | "profile": true                                                                                                                 | "profile": true and "top\_n\_profile": n |
   | ------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------- |
   | `traceEvents` follow the [Trace Event Format](https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU). | This output cannot be used in Perfetto.  |
10. Copy the entire response into a text file and save the filename with a `.json` file extension.
11. If you skipped the `"top_n_profile"` parameter, then you can visualize the trace. Go to [Perfetto UI](https://ui.perfetto.dev/). Select *Open trace file* and open the saved response.
12. It will display a [trace visualization](https://perfetto.dev/docs/#trace-visualization) of the calculation time trace for the formulas within a workbook.
    * The bars listed the highest denote the time it takes for formulas that are calculated last. They depend on the calculations from formulas that take place below them.
    * The lowest and longest item indicated in the trace usually indicates an area for improvement. The slower calculation time for these cells prevents the dependent cells above them from being calculated sooner.

## Identify the longest calculation chain

Very long calculation chains can consume memory and lead to incomplete calculations. These long calculation chains can be identified using the steps below.

1. Make sure to complete the steps in [#prepare-the-excel-file](#prepare-the-excel-file "mention").
2. Upload the service to Spark. Note that `Debug` builds are slower to compile and execute and should only be used for debugging purposes only.
3. Navigate to the [API Tester](/navigation/api-tester.md).
4. In the *Raw view* add the additional parameters to the `request_meta`:
   * `"show_max_calc_chain": true` enables logging the longest calculation chain.
5. There will be 2 additional returned objects:
   * `outputs.additional_outputs.max_calc_info`  notes the longest calculation depth and the location of the cell.
   * `outputs.additional_outputs.max_calc_chain`  indicates the cells that are part of the calculation chain.
6. To resolve this issue we can use the `Xprecalc` function that can evaluate long calculation chains in the middle breaking it into smaller chains.
   1. Identify a cell that is midway in the calculation chain.
   2. In an empty part of the workbook create a new Named range called `Xprecalc`. In this cell, create a formula that references the previously identified cell. For example if the middle of the calculation chain was `A123`, then set `Xprecalc` to `=COUNT(A123)`.
   3. If there are still issues, identify more breakpoints for long calculation chains and add them to the `Xprecalc`. For example, if we also identify `B456` as good cell to split the same or another calculation chain, set `Xprecalc` to `=COUNT(A123,B456)`.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.coherent.global/build-spark-services/neuron/profile-neuron-calculations.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
