You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/profiling/isolate-performance-issue.md
+12-25Lines changed: 12 additions & 25 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: "Isolate a performance issue"
3
3
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
5
5
ms.topic: conceptual
6
6
dev_langs:
7
7
- "CSharp"
@@ -117,30 +117,9 @@ public ActionResult<string> QueryCustomerDB()
117
117
}
118
118
```
119
119
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.
> 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:
144
123
145
124
```cmd
146
125
Can you identify a performance issue in the QueryCustomerDB method?
@@ -149,7 +128,9 @@ Can you identify a performance issue in the QueryCustomerDB method?
149
128
> [!TIP]
150
129
> You can use slash commands such as [/optimize](../ide/copilot-chat-context.md#slash-commands) to help form good questions for Copilot.
151
130
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.
@@ -159,6 +140,12 @@ public async Task<ActionResult<string>> QueryCustomerDB()
159
140
}
160
141
```
161
142
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
+
162
149
## Next steps
163
150
164
151
The following articles and blog posts provide more information to help you learn to use the Visual Studio performance tools effectively.
Copy file name to clipboardExpand all lines: docs/profiling/optimize-code-using-profiling-tools.md
+11-23Lines changed: 11 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: "Beginner's guide to optimizing code"
3
3
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
5
5
ms.topic: conceptual
6
6
ms.custom: "profiling-seo"
7
7
dev_langs:
@@ -137,28 +137,9 @@ foreach (var blog in db.Blogs.Select(b => new { b.Url, b.Posts }).ToList())
137
137
138
138
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.
139
139
140
-
We do a little research and find some common recommendations for how to optimize LINQ queriesand 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.
141
141
142
-
> [!TIP]
143
-
> Alternatively, we can save time and let Copilot [do the research](#optimize-code-with-copilot) for us.
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:
162
143
163
144
```cmd
164
145
Can you make the LINQ query in this method faster?
@@ -167,7 +148,7 @@ Can you make the LINQ query in this method faster?
167
148
> [!TIP]
168
149
> You can use slash commands such as [/optimize](../ide/copilot-chat-context.md#slash-commands) to help form good questions for Copilot.
169
150
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.
171
152
172
153
```csharp
173
154
publicvoidGetBlogTitleX()
@@ -184,6 +165,13 @@ public void GetBlogTitleX()
184
165
}
185
166
```
186
167
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
+
187
175
## Results
188
176
189
177
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