Skip to content

Commit 4b8e06e

Browse files
Merge pull request #13263 from Mikejo5000/mikejo-br24
Update Copilot information in the VS Profiling case studies
2 parents 53e6c89 + 6cd07da commit 4b8e06e

File tree

2 files changed

+23
-48
lines changed

2 files changed

+23
-48
lines changed

docs/profiling/isolate-performance-issue.md

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Isolate a performance issue"
33
description: "Learn how to use .NET Counters and the Instrumentation tools to identify, isolate, and resolve performance issues."
4-
ms.date: 09/20/2024
4+
ms.date: 10/24/2024
55
ms.topic: conceptual
66
dev_langs:
77
- "CSharp"
@@ -117,30 +117,9 @@ public ActionResult<string> QueryCustomerDB()
117117
}
118118
```
119119

120-
With a little research, we discover that this code is calling an async API without using await. This is the [sync-over-async](https://devblogs.microsoft.com/pfxteam/should-i-expose-synchronous-wrappers-for-asynchronous-methods/) code pattern, which is a common cause of threadpool starvation, and may block threads.
120+
We do a little research. Alternatively, we can save time and let [Copilot](../ide/visual-studio-github-copilot-extension.md) do the research for us.
121121

122-
To resolve, use await.
123-
124-
```csharp
125-
public async Task<ActionResult<string>> QueryCustomerDB()
126-
{
127-
Customer c = await QueryCustomerFromDbAsync("Dana");
128-
return "success:taskasyncwait";
129-
}
130-
```
131-
132-
> [!TIP]
133-
> Alternatively, we can save time and let Copilot [do the research](#get-copilot-to-research-the-issue) for us.
134-
135-
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.
136-
137-
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).
138-
139-
For additional information on thread pool starvation, see [Detecting threadpool starvation](/dotnet/core/diagnostics/debug-threadpool-starvation#detecting-threadpool-starvation).
140-
141-
## Get Copilot to research the issue
142-
143-
If we're using [Copilot](../ide/visual-studio-github-copilot-extension.md), we can ask Copilot to research performance issues for us. Select **Ask Copilot** from the context menu and type the following question:
122+
If we're using [Copilot](../ide/visual-studio-github-copilot-extension.md), select **Ask Copilot** from the context menu and type the following question:
144123

145124
```cmd
146125
Can you identify a performance issue in the QueryCustomerDB method?
@@ -149,7 +128,9 @@ Can you identify a performance issue in the QueryCustomerDB method?
149128
> [!TIP]
150129
> You can use slash commands such as [/optimize](../ide/copilot-chat-context.md#slash-commands) to help form good questions for Copilot.
151130
152-
In this example, Copilot gives the following code suggestion, the same answer we previously identified by research, along with an explanation.
131+
Copilot tells us that this code is calling an async API without using await. This is the [sync-over-async](https://devblogs.microsoft.com/pfxteam/should-i-expose-synchronous-wrappers-for-asynchronous-methods/) code pattern, which is a common cause of threadpool starvation, and may block threads.
132+
133+
To resolve, use await. In this example, Copilot gives the following code suggestion along with the explanation.
153134

154135
```csharp
155136
public async Task<ActionResult<string>> QueryCustomerDB()
@@ -159,6 +140,12 @@ public async Task<ActionResult<string>> QueryCustomerDB()
159140
}
160141
```
161142

143+
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.
144+
145+
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).
146+
147+
For additional information on thread pool starvation, see [Detecting threadpool starvation](/dotnet/core/diagnostics/debug-threadpool-starvation#detecting-threadpool-starvation).
148+
162149
## Next steps
163150

164151
The following articles and blog posts provide more information to help you learn to use the Visual Studio performance tools effectively.

docs/profiling/optimize-code-using-profiling-tools.md

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Beginner's guide to optimizing code"
33
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."
4-
ms.date: 09/20/2024
4+
ms.date: 10/24/2024
55
ms.topic: conceptual
66
ms.custom: "profiling-seo"
77
dev_langs:
@@ -137,28 +137,9 @@ foreach (var blog in db.Blogs.Select(b => new { b.Url, b.Posts }).ToList())
137137

138138
This code uses `foreach` loops to search the database for any blogs with "Fred Smith" as the author. Looking at it, you can see that a lot of objects are getting generated in memory: a new object array for each blog in the database, associated strings for each URL, and values for properties contained in the posts, such as blog ID.
139139

140-
We do a little research and find some common recommendations for how to optimize LINQ queries and come up with this code.
140+
We do a little research and find some common recommendations for how to optimize LINQ queries. Alternatively, we can save time and let [Copilot](../ide/visual-studio-github-copilot-extension.md) do the research for us.
141141

142-
> [!TIP]
143-
> Alternatively, we can save time and let Copilot [do the research](#optimize-code-with-copilot) for us.
144-
145-
```csharp
146-
foreach (var x in db.Posts.Where(p => p.Author.Contains("Fred Smith")).Select(b => b.Title).ToList())
147-
{
148-
Console.WriteLine("Post: " + x);
149-
}
150-
```
151-
152-
In this code, we made several changes to help optimize the query:
153-
154-
- Added the `Where` clause and eliminate one of the `foreach` loops.
155-
- Projected only the Title property in the `Select` statement, which is all we need in this example.
156-
157-
Next, we retest using the profiling tools.
158-
159-
### Optimize code with Copilot
160-
161-
If we're using [Copilot](../ide/visual-studio-github-copilot-extension.md), we can ask Copilot to research performance issues for us. Select **Ask Copilot** from the context menu and type the following question:
142+
If we're using Copilot, we select **Ask Copilot** from the context menu and type the following question:
162143

163144
```cmd
164145
Can you make the LINQ query in this method faster?
@@ -167,7 +148,7 @@ Can you make the LINQ query in this method faster?
167148
> [!TIP]
168149
> You can use slash commands such as [/optimize](../ide/copilot-chat-context.md#slash-commands) to help form good questions for Copilot.
169150
170-
In this example, Copilot gives the following suggested code changes, similar to our optimized query, along with an explanation.
151+
In this example, Copilot gives the following suggested code changes, along with an explanation.
171152

172153
```csharp
173154
public void GetBlogTitleX()
@@ -184,6 +165,13 @@ public void GetBlogTitleX()
184165
}
185166
```
186167

168+
This code includes several changes to help optimize the query:
169+
170+
- Added the `Where` clause and eliminated one of the `foreach` loops.
171+
- Projected only the Title property in the `Select` statement, which is all we need in this example.
172+
173+
Next, we retest using the profiling tools.
174+
187175
## Results
188176

189177
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%.

0 commit comments

Comments
 (0)