# 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.&#x20;

## 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.&#x20;

```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.&#x20;

You will see a list of requests and their timings.&#x20;

![Result Index](https://427210397-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZsd6kaluvxbsdXs9MWxX%2Fuploads%2FTmKVqpJOEgxkSfVXLJJ1%2Fimage.png?alt=media\&token=bd7954d3-4f81-424d-91ab-6be492e0b9ad)

Clicking on the request will display a break-down of the timings.&#x20;

![Timings](https://427210397-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZsd6kaluvxbsdXs9MWxX%2Fuploads%2FfbJYI6n7KtqUeiUGxplI%2Fimage.png?alt=media\&token=3f69ddb1-551f-44ad-990b-264ef7c70647)

## Profiling an API Endpoint

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

For example, assume an API called `/process`.&#x20;

```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.

![](https://427210397-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZsd6kaluvxbsdXs9MWxX%2Fuploads%2F1uYwvx8ywQnK3JYcQsGb%2Fimage.png?alt=media\&token=f55ae5ed-f554-45e7-8659-1421cf1db8c5)

Viewing the profile for this API would list the block we measured.&#x20;

![](https://427210397-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZsd6kaluvxbsdXs9MWxX%2Fuploads%2FRBy2eSNi5aJrxcHv7tGV%2Fimage.png?alt=media\&token=5c39ccb6-0888-44d5-a343-fba769f54ad8)

## 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.&#x20;

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.&#x20;

```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.&#x20;

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.&#x20;

![Dashboard Timing](https://427210397-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZsd6kaluvxbsdXs9MWxX%2Fuploads%2FLrTlTYObLoNUSiLstaHb%2Fimage.png?alt=media\&token=d1acc348-ac30-4604-87a6-25bdf5e280bc)

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

![All Timings](https://427210397-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZsd6kaluvxbsdXs9MWxX%2Fuploads%2FXkgbXaYEBTvOHS31xdic%2Fimage.png?alt=media\&token=649e7ca1-610e-496a-a278-9f4bd4090afb)

## Profiling a Job

You can profile jobs by using `Measure-PSUBlock` within your script.&#x20;

For example, assume we have a script that calls `Get-Service`.&#x20;

```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.&#x20;

![](https://427210397-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZsd6kaluvxbsdXs9MWxX%2Fuploads%2FGjfdm3SPTGdqhIOrhH6U%2Fimage.png?alt=media\&token=8bda9d50-447f-4b82-b89d-a64fa54b2099)

The profile will include the block that was measured.&#x20;

![](https://427210397-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZsd6kaluvxbsdXs9MWxX%2Fuploads%2FebHRXjusKOlWVBnlFMtq%2Fimage.png?alt=media\&token=3de9d566-d393-4995-a84b-71c2cfa34ba6)
