Code Monkey home page Code Monkey logo

Comments (8)

rhubarb-geek-nz avatar rhubarb-geek-nz commented on June 14, 2024 3

Compare with

pwsh -c "Get-Process" >o.csv

which takes less than a second.

Now compare that with the information written with

 Get-Process | ConvertTo-JSON  | Set-Content -path "o.json"

And now it takes for ages because for each process it tries to get detailed information including what DLLs are loaded into each process, threads in the process, memory usage etc..

Compare that with

get-process | foreach-object { Write-Output $_.Id , $_.MainModule.ModuleName }

Which is less than a second again.

So in summary, be careful what you ask for. Get-Process is quick until you start inquiring for detailed information for each process, which Export-CSV and ConvertTo-JSON will do because they are generic.

So try

get-process | Select-Object -Property Id,MainModule | Export-Csv -LiteralPath o.csv

Which takes less than a second again

However

get-process | Select-Object -Property Id,CommandLine | Export-Csv -LiteralPath o.csv

Takes for ages, so ask for what you need.

from powershell.

yg-i avatar yg-i commented on June 14, 2024 1

So in summary, be careful what you ask for. Get-Process is quick until you start inquiring for detailed information for each process, which Export-CSV and ConvertTo-JSON will do because they are generic.

Thanks, that's helpful. It seems that there's a subset of approximately 10 properties, out of a total of 69, that especially slow down the read process (the 'Module' and 'Parent' properties are the slowest among them). When these 10 properties are excluded, the operation completes in just a second or two. Including all 10 them causes the total execution time to skyrocket to about 200 seconds!

image

PS C:\tmp> $allProps = (gps | gm -MemberType properties).Name;
PS C:\tmp> $slowProps = ('Modules', 'MainModule', 'Parent', 'CommandLine', 'Company', 'Path', 'Description', 'Product', 'ProductVersion', 'FileVersion');
PS C:\tmp> (measure-command {gps | select -Property ($allProps | Where-Object {$_ -notIn ($slowProps | select -SkipLast 0)}) | Export-Clixml o.xml}).TotalSeconds
1.7832752
PS C:\tmp> (measure-command {gps | select -Property ($allProps | Where-Object {$_ -notIn ($slowProps | select -SkipLast 1)}) | Export-Clixml o.xml}).TotalSeconds
3.719695
PS C:\tmp> (measure-command {gps | select -Property ($allProps | Where-Object {$_ -notIn ($slowProps | select -SkipLast 2)}) | Export-Clixml o.xml}).TotalSeconds
5.3285927
PS C:\tmp> (measure-command {gps | select -Property ($allProps | Where-Object {$_ -notIn ($slowProps | select -SkipLast 3)}) | Export-Clixml o.xml}).TotalSeconds
7.072522
PS C:\tmp> (measure-command {gps | select -Property ($allProps | Where-Object {$_ -notIn ($slowProps | select -SkipLast 4)}) | Export-Clixml o.xml}).TotalSeconds
8.9156198
PS C:\tmp> (measure-command {gps | select -Property ($allProps | Where-Object {$_ -notIn ($slowProps | select -SkipLast 5)}) | Export-Clixml o.xml}).TotalSeconds
10.5895199
PS C:\tmp> (measure-command {gps | select -Property ($allProps | Where-Object {$_ -notIn ($slowProps | select -SkipLast 6)}) | Export-Clixml o.xml}).TotalSeconds
12.3948109
PS C:\tmp> (measure-command {gps | select -Property ($allProps | Where-Object {$_ -notIn ($slowProps | select -SkipLast 7)}) | Export-Clixml o.xml}).TotalSeconds
35.2305779
PS C:\tmp> (measure-command {gps | select -Property ($allProps | Where-Object {$_ -notIn ($slowProps | select -SkipLast 8)}) | Export-Clixml o.xml}).TotalSeconds
91.9951524
PS C:\tmp> (measure-command {gps | select -Property ($allProps | Where-Object {$_ -notIn ($slowProps | select -SkipLast 9)}) | Export-Clixml o.xml}).TotalSeconds
92.1810648
PS C:\tmp> (measure-command {gps | select -Property ($allProps | Where-Object {$_ -notIn ($slowProps | select -SkipLast 10)}) | Export-Clixml o.xml}).TotalSeconds
206.8004993

from powershell.

237dmitry avatar 237dmitry commented on June 14, 2024 1

In my opinion it works pretty fast:

$ Get-Process | Export-Csv -path "o.csv"
$ (Get-History -Count 1).Duration.TotalMilliseconds   
411.0004

$ (Import-Csv ./o.csv | Get-Member -MemberType NoteProperty).Count
69

$ (Get-Content ./o.csv).Count
203

$ (Get-Item ./o.csv).Size
117463

7.5.0-preview.1 on Linux.

from powershell.

237dmitry avatar 237dmitry commented on June 14, 2024 1

this is not the same as the original posters environment

I thought about this aspect. But I can’t test on Windows (replaced it with Linux).
I posted it just for comparison and the overall picture.

from powershell.

SteveL-MSFT avatar SteveL-MSFT commented on June 14, 2024 1

@rhubarb-geek-nz provided the right solution which is to only serialize what you actually need. Getting the .NET object is fast because some properties are just references that aren't retrieved until actually used like during serialization.

from powershell.

rhubarb-geek-nz avatar rhubarb-geek-nz commented on June 14, 2024

In my opinion it works pretty fast:
7.5.0-preview.1 on Linux.

Yes, this is not the same as the original posters environment on Win10 machine (PSVersion 7.4.2; OS version 10.0.19045),

Process management on Windows is completely different to that on Linux, both conceptually and the API used to retrieve the information.

from powershell.

microsoft-github-policy-service avatar microsoft-github-policy-service commented on June 14, 2024

This issue has been marked as answered and has not had any activity for 1 day. It has been closed for housekeeping purposes.

from powershell.

microsoft-github-policy-service avatar microsoft-github-policy-service commented on June 14, 2024

πŸ“£ Hey @yg-i, how did we do? We would love to hear your feedback with the link below! πŸ—£οΈ

πŸ”— https://aka.ms/PSRepoFeedback

from powershell.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.