# Profiling

{% hint style="info" %}
Profiling only works for the integrated environment.
{% endhint %}

PowerShell Universal provides a performance profiler for debugging issues where the platform may exhibit slow responses. This is primarily useful when building Dashboards.

## Enable Profiling

Profiling will increase memory usage as profiling data is stored in memory. You can enable profiling by configuring the `Profiling` appsettings.json property. It is `false` by default. You will need to restart PowerShell Universal after enabling or disabling profiling.

```json
"Profiling": true
```

## Access Profiling Data

You can access profiling data by navigating to `/profiler/results-index` . You will need to be logged in as administrator before being able to access this URL.

You will see a list of requests and their timings.

![Result Index](/files/XE2GTbdHUIOuTtc9PkYI)

Clicking on the request will display a break-down of the timings.

![Timings](/files/yZcPlffmHWKViI59UXIl)

## Profiling an API Endpoint

You can profile an API endpoint using `Measure-PSUBlock`. The API will be listed by URL in the result index.

For example, assume an API called `/process`.

```powershell
New-PSUEndpoint -Url "/process" -Endpoint {
    Measure-PSUBlock -Name 'Api' -ScriptBlock {
        Get-Process | Select-Object name
    }
} -Authentication -Timeout 0 
```

The result of profiling this API would be listed by URL.

![](/files/DAN6RlwESD7lL8TgTuWQ)

Viewing the profile for this API would list the block we measured.

![](/files/SotJxm6aqWKV2eYcexDL)

## Profiling an App

{% hint style="info" %}
`Measure-UDBlock` is an alias for `Measure-PSUBlock`
{% endhint %}

You can use the built-in profiler with Apps. By default, certain internal action timings are recorded. You can also use the `Measure-PSUBlock` cmdlet to measure specific blocks within your dashboard.

For example, this app uses `Measure-PSUBlock` to measure the performance of the `Start-Sleep` cmdlet. The result is the block will take one second to execute.

```powershell
New-UDApp -Title 'App' -Content {
    New-UDDynamic -Id 'MyElement' -Content {
        Measure-PSUBlock -Name 'WithinDashboard' -ScriptBlock {
            Start-Sleep 1
        }
    }
}
```

When reviewing requests within the profiler, you will want to look for `UDComponent\ElementPost` and `UDComponent\Element`. These are requests for elements within Apps.

Below is the example output from the app shown above. Notice that the URL includes the element's ID `MyElement`. You'll also notice that the `WithinDashboard` block takes about 1 second.

![Dashboard Timing](/files/kBwro0tvTYL09eIMcN9W)

Not all timing will be displayed by default. You can click `show trivial` to expand all timings.

![All Timings](/files/KS8CBVW23NnD5RoH4Q8G)

## Profiling a Job

You can profile jobs by using `Measure-PSUBlock` within your script.

For example, assume we have a script that calls `Get-Service`.

```powershell
Measure-PSUBlock -ScriptBlock {
    Get-Service
} -Name 'Get-Service'
```

Running a profile on this script will list the job as `JobProfiler/{id}`. The id will match the job ID shown in the admin console.

![](/files/QNsspNnIjWLs9Lt3aRq2)

The profile will include the block that was measured.

![](/files/q9DfC5om73hxtqfxGcRd)


---

# 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.powershelluniversal.com/development/profiling.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.
