

# Compare two profiles

The `Diff` operation compares two flame graphs, a left side and a right side. Use it to find regressions or improvements, for example before and after a deployment, or between two hosts. Each side is a complete flame graph query. For more information about flame graph queries, see [Retrieve a flame graph](CloudWatch-Profiles-query-example.md).

You can compare two profiles in the CloudWatch console or with the query API. This topic describes the console first, and then the API.

## Prerequisites
<a name="CloudWatch-Profiles-query-compare-prereqs"></a>

Before you begin, make sure that you have the following:
+ An identity with the `cloudwatch:GetProfileRecords` permission. For more information, see [IAM permissions required](CloudWatch-Profiles-query.md#CloudWatch-Profiles-query-iam). You need this permission to view profiles in the CloudWatch console and to call the query API.
+ The profile type ID and the labels to compare. For instructions, see [Discover profile types, services, and labels](CloudWatch-Profiles-query-discover.md).

## Compare two profiles in the CloudWatch console
<a name="CloudWatch-Profiles-query-compare-console"></a>

In the CloudWatch console, you use **Compare with** to compare the current selection with an earlier time range (**Time**) or with a second label set (**Labels**). While you compare, you can't change the service or the profile type.

**To compare two time ranges or label sets in the CloudWatch console**

1. Open the CloudWatch console at [https://console.aws.amazon.com/cloudwatch/](https://console.aws.amazon.com/cloudwatch/).

1. In the navigation pane, choose **Profiles**.

1. In the navigation bar at the top, select the Region where you send your profile data.

1. For **Service**, select the service to compare, for example, `example-java-service`.

1. For **Profile type**, select the profile type to compare. Both selections use this profile type.

1. For **Compare with**, choose **Time** or **Labels**.

Next, set up the comparison selection and run the query. Use one of the following procedures.

**To compare with an earlier time range**

1. For the time offset, choose how far back to compare, for example, **1h ago**.

1. Choose **Run**.

**To compare two label sets**

1. In the first **Label** row, for **Label name**, select a label, for example, `k8s.pod.name`.

1. For **Label value**, select the value for the baseline.

1. In the second **Label** row, for **Label name**, select the same label.

1. For **Label value**, select the value to compare.

1. Choose **Run**.

The flame graph shows the differences between the two selections. Its legend marks each function as **Added**, **Grew**, **Removed**, **Shrank**, or **Unchanged**. In a time comparison, the chart legend labels the two series as the baseline (now) and the comparison, for example, 1h ago.

## Comparison examples
<a name="CloudWatch-Profiles-query-compare-requests"></a>

To compare two profiles with the API, send a `Diff` request. The following examples show common requests and the response.

The `left` and `right` objects each take the same fields as a `SelectMergeStacktraces` request. CloudWatch validates each side separately, and each side's time range can't be longer than 24 hours. For all request fields, see [Diff](CloudWatch-Profiles-query-api-reference.md#CloudWatch-Profiles-query-api-diff) in the query API reference.

**Important**  
Use the same `profileTypeID` on both sides. The `Diff` operation doesn't reject sides with different profile types, but the result isn't meaningful.

**Example – compare two time ranges**  <a name="CloudWatch-Profiles-query-compare-time"></a>
This request compares the CPU profile of `example-java-service` in the first half hour of a window with the second half hour. For example, you can use the time of a deployment as the boundary.  

```bash
curl --request POST \
  --aws-sigv4 "aws:amz:us-east-1:monitoring" \
  --user "$AWS_ACCESS_KEY_ID:$AWS_SECRET_ACCESS_KEY" \
  --header "x-amz-security-token: $AWS_SESSION_TOKEN" \
  --header "Content-Type: application/json" \
  --data '{
    "left": {
      "profileTypeID": "process_cpu:cpu:nanoseconds:cpu:nanoseconds",
      "labelSelector": "{service_name=\"example-java-service\"}",
      "maxNodes": 4,
      "start": 1793793600000,
      "end":   1793795400000
    },
    "right": {
      "profileTypeID": "process_cpu:cpu:nanoseconds:cpu:nanoseconds",
      "labelSelector": "{service_name=\"example-java-service\"}",
      "maxNodes": 4,
      "start": 1793795400000,
      "end":   1793797200000
    }
  }' \
  https://monitoring.us-east-1.amazonaws.com/querier.v1.QuerierService/Diff
```

**Example – compare two hosts**  <a name="CloudWatch-Profiles-query-compare-labels"></a>
This request body compares two hosts that run `example-java-service` over the same time range.  

```json
{
  "left": {
    "profileTypeID": "process_cpu:cpu:nanoseconds:cpu:nanoseconds",
    "labelSelector": "{service_name=\"example-java-service\",\"@resource.host.name\"=\"host-a.example.internal\"}",
    "start": 1793793600000,
    "end":   1793797200000
  },
  "right": {
    "profileTypeID": "process_cpu:cpu:nanoseconds:cpu:nanoseconds",
    "labelSelector": "{service_name=\"example-java-service\",\"@resource.host.name\"=\"host-b.example.internal\"}",
    "start": 1793793600000,
    "end":   1793797200000
  }
}
```

**Example – read the comparison response**  <a name="CloudWatch-Profiles-query-compare-response"></a>
The response contains a `flamegraph` object that combines both sides. The following response is trimmed.  

```json
{
  "flamegraph": {
    "names": ["total", "..."],
    "levels": [
      {"values": ["0", "36049186800", "0", "0", "34779903100", "0", "0"]}
    ],
    "total": "70829089900",
    "maxSelf": "6557877300",
    "leftTicks": "36049186800",
    "rightTicks": "34779903100"
  }
}
```
Each `values` list holds seven numbers for each node: the left offset, left total, and left self values, then the right offset, right total, and right self values, and finally the index of the node's name in `names`. `leftTicks` and `rightTicks` are the totals of each side, and `total` is their sum.