Skip to content
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

Performance Issue with IsWindowsService due to Repeated Process Lookups #93613

Closed
JKamsker opened this issue Oct 17, 2023 · 4 comments · Fixed by #93771
Closed

Performance Issue with IsWindowsService due to Repeated Process Lookups #93613

JKamsker opened this issue Oct 17, 2023 · 4 comments · Fixed by #93771
Labels
area-Extensions-Hosting help wanted [up-for-grabs] Good issue for external contributors tenet-performance Performance related issue
Milestone

Comments

@JKamsker
Copy link

Description

The WindowsServiceHelpers.IsWindowsService method in the Microsoft.Extensions.Hosting.WindowsServices package has a performance issue. It retrieves its parent process each time it is called, which takes approximately 35.93ms. While this may not seem significant, the time accumulates when the method is used multiple times, leading to noticeable delays.

To reproduce this issue, simply call the WindowsServiceHelpers.IsWindowsService method multiple times in a loop.

Configuration

The issue was discovered on .NET 6.0, running on a Windows 11 Pro x64 (build 22621.1555) machine with the Microsoft.Extensions.Hosting.WindowsServices package version 6.0.2.
However, it is likely to be present on any system where this package is used. The machine had about 600 processes running at the time of testing. The test was done in release mode.

Regression?

This issue has been present since the introduction of the WindowsServiceHelpers.IsWindowsService method. It is not a regression from a previous build or release.

Data

Each call to WindowsServiceHelpers.IsWindowsService takes approximately 35.93ms due to the method retrieving its parent process each time. When used multiple times, these delays stack up. For example, calling the method 100 times would result in a delay of approximately 3.59 seconds.

Analysis

The issue seems to stem from the method retrieving its parent process each time it is called. A potential solution could be to cache the parent process of the current process when the method is called for the first time, and then use this cached value in subsequent calls. This would prevent the need for repeated, time-consuming lookups.

@JKamsker JKamsker added the tenet-performance Performance related issue label Oct 17, 2023
@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Oct 17, 2023
@ghost ghost added the untriaged New issue has not been triaged by the area owner label Oct 17, 2023
@vcsjones vcsjones added area-Extensions-Hosting and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Oct 17, 2023
@ghost
Copy link

ghost commented Oct 17, 2023

Tagging subscribers to this area: @dotnet/area-extensions-hosting
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

The WindowsServiceHelpers.IsWindowsService method in the Microsoft.Extensions.Hosting.WindowsServices package has a performance issue. It retrieves its parent process each time it is called, which takes approximately 35.93ms. While this may not seem significant, the time accumulates when the method is used multiple times, leading to noticeable delays.

To reproduce this issue, simply call the WindowsServiceHelpers.IsWindowsService method multiple times in a loop.

Configuration

The issue was discovered on .NET 6.0, running on a Windows 11 Pro x64 (build 22621.1555) machine with the Microsoft.Extensions.Hosting.WindowsServices package version 6.0.2.
However, it is likely to be present on any system where this package is used. The machine had about 600 processes running at the time of testing. The test was done in release mode.

Regression?

This issue has been present since the introduction of the WindowsServiceHelpers.IsWindowsService method. It is not a regression from a previous build or release.

Data

Each call to WindowsServiceHelpers.IsWindowsService takes approximately 35.93ms due to the method retrieving its parent process each time. When used multiple times, these delays stack up. For example, calling the method 100 times would result in a delay of approximately 3.59 seconds.

Analysis

The issue seems to stem from the method retrieving its parent process each time it is called. A potential solution could be to cache the parent process of the current process when the method is called for the first time, and then use this cached value in subsequent calls. This would prevent the need for repeated, time-consuming lookups.

Author: JKamsker
Assignees: -
Labels:

tenet-performance, untriaged, area-Extensions-Hosting

Milestone: -

@buyaa-n buyaa-n added this to the 9.0.0 milestone Oct 18, 2023
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Oct 18, 2023
@buyaa-n buyaa-n added the help wanted [up-for-grabs] Good issue for external contributors label Oct 18, 2023
@buyaa-n
Copy link
Member

buyaa-n commented Oct 18, 2023

We might want to cache the parent process

@danmoseley
Copy link
Member

@JKamsker any interest in offering a PR?

@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Oct 20, 2023
@JKamsker
Copy link
Author

Whoops, I somehow didn't get the notifications 😅
Seems like @WeihanLi already drafted a PR which looks good to me 👍

@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Oct 31, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Nov 30, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-Extensions-Hosting help wanted [up-for-grabs] Good issue for external contributors tenet-performance Performance related issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants