Skip to content

Repo sync for protected branch #10412

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions docs/profiling/isolate-performance-issue.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "Isolate a performance issue"
description: "Learn how to use .NET Counters and the Instrumentation tools to identify, isolate, and resolve performance issues."
ms.date: 09/03/2024
ms.date: 09/20/2024
ms.topic: conceptual
dev_langs:
- "CSharp"
Expand Down Expand Up @@ -111,9 +111,8 @@ Double-click the `QueryCustomerDB` function to show the source code for the func
```csharp
public ActionResult<string> QueryCustomerDB()
{

Task dbTask = QueryCustomerFromDbAsync("Dana");
return "success:tasksleepwait";
Customer c = QueryCustomerFromDbAsync("Dana").Result;
return "success:taskwait";
}
```

Expand All @@ -122,19 +121,43 @@ With a little research, we discover that this code is calling an async API witho
To resolve, use await.

```csharp
public async Task<ActionResult<string>> TaskAsyncWait()
public async Task<ActionResult<string>> QueryCustomerDB()
{
Customer c = await PretendQueryCustomerFromDbAsync("Dana");
Customer c = await QueryCustomerFromDbAsync("Dana");
return "success:taskasyncwait";
}
```

> [!TIP]
> Alternatively, we can save time and let Copilot [do the research](#get-copilot-to-do-the-research) for us.

If you see performance issues related to database queries, you can use the [Database tool](../profiling/analyze-database.md) to investigate whether certain calls are slower. This data might indicate an opportunity to optimize queries. For a tutorial that shows how to use the Database tool to investigate a performance issue, see [Case study: Beginner's guide to optimizing code](../profiling/optimize-code-using-profiling-tools.md). The Database tool supports .NET Core with either ADO.NET or Entity Framework Core.

To get visualizations in Visual Studio for individual thread behavior, you can use the [Parallel Stacks](../debugger/get-started-debugging-multithreaded-apps.md#ParallelStacks) window while debugging. This window shows individual threads along with information about threads that are waiting, threads they're waiting on, and [deadlocks](../debugger/using-the-parallel-stacks-window.md#stack-frame-icons).

For additional information on thread pool starvation, see [Detecting threadpool starvation](/dotnet/core/diagnostics/debug-threadpool-starvation#detecting-threadpool-starvation).

## Get Copilot to do the research

If we're using Copilot, we can ask Copilot to research performance issues for us. Select **Ask Copilot** from the context menu and type the following question:

```cmd
Can you identify a performance issue in the QueryCustomerDB method?
```

> [!TIP]
> You can use slash commands such as [/optimize](../ide/copilot-chat-context.md#slash-commands) to help form good questions for Copilot.

In this example, Copilot gives the following code suggestion, the same answer we previously identified by research, along with an explanation.

```csharp
public async Task<ActionResult<string>> QueryCustomerDB()
{
Customer c = await QueryCustomerFromDbAsync("Dana");
return "success:taskwait";
}
```

## Next steps

The following articles and blog posts provide more information to help you learn to use the Visual Studio performance tools effectively.
Expand Down
33 changes: 32 additions & 1 deletion docs/profiling/optimize-code-using-profiling-tools.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "Beginner's guide to optimizing code"
description: "Learn how to optimize code and reduce compute costs using Visual Studio profiling tools such as the CPU Usage tool, the .NET Object Allocation tool, and the Database tool."
ms.date: 08/09/2024
ms.date: 09/20/2024
ms.topic: conceptual
ms.custom: "profiling-seo"
dev_langs:
Expand Down Expand Up @@ -138,6 +138,9 @@ This code uses `foreach` loops to search the database for any blogs with "Fred S

We do a little research and find some common recommendations for how to optimize LINQ queries and come up with this code.

> [!TIP]
> Alternatively, we can save time and let Copilot [do the research](#optimize-code-with-copilot) for us.

```csharp
foreach (var x in db.Posts.Where(p => p.Author.Contains("Fred Smith")).Select(b => b.Title).ToList())
{
Expand All @@ -152,6 +155,34 @@ In this code, we made several changes to help optimize the query:

Next, we retest using the profiling tools.

### Optimize code with Copilot

If we're using Copilot, we can ask Copilot to research performance issues for us. Select **Ask Copilot** from the context menu and type the following question:

```cmd
Can you make the LINQ query in this method faster?
```

> [!TIP]
> You can use slash commands such as [/optimize](../ide/copilot-chat-context.md#slash-commands) to help form good questions for Copilot.

In this example, Copilot gives the following suggested code changes, similar to our optimized query, along with an explanation.

```csharp
public void GetBlogTitleX()
{
var posts = db.Posts
.Where(post => post.Author == "Fred Smith")
.Select(post => post.Title)
.ToList();

foreach (var postTitle in posts)
{
Console.WriteLine($"Post: {postTitle}");
}
}
```

## Results

After updating the code, we re-run the CPU Usage tool to collect a trace. The **Call Tree** view shows that `GetBlogTitleX` is running only 1754 ms, using 37% of the app's CPU total, a significant improvement from 59%.
Expand Down