# 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](https://1373299915-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8F6PrkNTG8Y34hADzKOL%2Fuploads%2Fgit-blob-3b9440123704426f62ac5135b3cc01251b8c56d0%2Fimage%20\(432\).png?alt=media)

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

![Timings](https://1373299915-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8F6PrkNTG8Y34hADzKOL%2Fuploads%2Fgit-blob-86dec8fbee0bb49e6a8260003e93073a6f7fbc4a%2Fimage%20\(266\).png?alt=media)

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

![](https://1373299915-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8F6PrkNTG8Y34hADzKOL%2Fuploads%2Fgit-blob-90d0ed95e7af20afbf6f83422563c1f8e19795a2%2Fimage%20\(440\).png?alt=media)

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

![](https://1373299915-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8F6PrkNTG8Y34hADzKOL%2Fuploads%2Fgit-blob-39e46d3a41103043d9c8eea35c5d57fe4e982419%2Fimage%20\(556\).png?alt=media)

## 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](https://1373299915-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8F6PrkNTG8Y34hADzKOL%2Fuploads%2Fgit-blob-44927d0b8d6428df12d6bca4432ca1526ffb6fa0%2Fimage%20\(264\).png?alt=media)

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

![All Timings](https://1373299915-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8F6PrkNTG8Y34hADzKOL%2Fuploads%2Fgit-blob-aea57bc5b672e552dc0902c7fceb66770c59fe36%2Fimage%20\(549\).png?alt=media)

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

![](https://1373299915-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8F6PrkNTG8Y34hADzKOL%2Fuploads%2Fgit-blob-42e704ea3011e0e9e03aa9c67705fbc0eeb8576c%2Fimage%20\(554\).png?alt=media)

The profile will include the block that was measured.

![](https://1373299915-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8F6PrkNTG8Y34hADzKOL%2Fuploads%2Fgit-blob-b8ea7cfe8f5a29d8aac140e2f917c13b088c452e%2Fimage%20\(111\).png?alt=media)
