Skip to content

Performance of Windows PowerShell is better then PowerShell Core #7112

Closed

Description

@iSazonov

I get very strange and disturbing performance results for Import-Csv cmdlet.

Import 111 Mb csv log file:

  • On Windows PowerShell always ~ 3.7 sec. Memory ~ 10 Mb.
    -On PowerShell Core from 6 sec up to 27 sec - each subsequent launch increases the time by 1-2-3 seconds. Memory grow from 1 Mb to 105 Mb. After waiting some minutes memory decreases to ~1 MB and agaim the cmdlet take ~6 sec.

I suppose we get the same for any script.

Activity

  1. added
    WG-Engine-Performance
    Issue-Discussion
    on Jun 19, 2018
  2. markekraus commented on Jun 19, 2018

    @markekraus
    Contributor

    Ilya (@iSazonov) we have seen similar memory growth with string related activities in JSON too. I suspect .NET Core is just greedier with its GC than Framework.

    But the slow down.. that is concerning.

  3. iSazonov commented on Jun 19, 2018

    @iSazonov
    CollaboratorAuthor

    Dongbo Wang (@daxian-dbw) If you are working on performance tests this may be of interest to you.

  4. iSazonov commented on Jun 20, 2018

    @iSazonov
    CollaboratorAuthor

    Maybe related #6965 (memory).
    /cc alx9r

  5. alx9r commented on Jun 20, 2018

    @alx9r

    Ilya (@iSazonov) Your description sounds like it might be a case of the garbage collector falling behind. Do you have a repro I can run? It's probably worth measuring any difference between GCSettings.IsServerGC true and false.

    FWIW, GCSettings.IsServerGC seems to be false by default for both powershell.exe and pwsh.exe. As I understand it, that default means that there is one GC thread regardless of how many threads are simultaneously producing garbage. If I'm understanding this right, any managed code invoked by PowerShell that is both CPU-bound and well-parallelized is likely to run out of memory if you have enough cores and it runs long enough.

    It could be that whatever Import-Csv (and the JSON activities Mark Kraus (@markekraus) reported) are doing became more parallelized from Windows PowerShell to PowerShell Core.

  6. iSazonov commented on Jun 20, 2018

    @iSazonov
    CollaboratorAuthor

    alx9r The repo is simple

    Measure-Command { Import-Csv file.csv }

    The file I used has ~110 Mb size.

    It could be that whatever Import-Csv (and the JSON activities Mark Kraus (@markekraus) reported) are doing became more parallelized from Windows PowerShell to PowerShell Core.

    Then my test had to be even faster than in Windows PowerShell but it is 3-10(!) times slower from the first run.
    Although speed and memory can be different problems.

  7. alx9r commented on Jun 20, 2018

    @alx9r

    Ilya (@iSazonov) I'm not seeing the dramatic speed difference you reported. Perhaps the specifics of the .csv file matter. Here is my test code:

    $PSVersionTable
    
    "GCSettings.IsServerGC : $([System.Runtime.GCSettings]::IsServerGC)"
    $filePath = "$([System.IO.Path]::GetTempPath())5abb9571859c48cea807d755cc48058b.csv"
    
    # uncomment the following to generate a ~130MB .csv file first
    <#
    $numLines = (50 * [System.Math]::Pow(2,12))
    
    1..$numLines |
        % { ($_..($_ + 100) | % {"$_"}) -join ',' } |
        Set-Content $filePath
    #>
    
    "Import-Csv $filePath"
    Measure-Command { Import-Csv $filePath } | select TotalSeconds | Format-List

    These are the results for Windows PowerShell and PowerShell core, respectively:

    Name                           Value
    ----                           -----
    PSVersion                      5.1.14409.1012
    PSEdition                      Desktop
    PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
    BuildVersion                   10.0.14409.1012
    CLRVersion                     4.0.30319.42000
    WSManStackVersion              3.0
    PSRemotingProtocolVersion      2.3
    SerializationVersion           1.1.0.1
    GCSettings.IsServerGC : False
    Import-Csv C:\Users\un1\AppData\Local\Temp\5abb9571859c48cea807d755cc48058b.csv
    TotalSeconds : 37.3680663
    
    Name                           Value
    ----                           -----
    PSVersion                      6.1.0-preview.688
    PSEdition                      Core
    GitCommitId                    v6.1.0-preview.688
    OS                             Microsoft Windows 6.3.9600
    Platform                       Win32NT
    PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
    PSRemotingProtocolVersion      2.3
    SerializationVersion           1.1.0.1
    WSManStackVersion              3.0
    GCSettings.IsServerGC : False
    Import-Csv C:\Users\un1\AppData\Local\Temp\5abb9571859c48cea807d755cc48058b.csv
    TotalSeconds : 57.3899851
    

    Peak memory usage for the test runs was as follows:

    • Windows PowerShell: 23.8 MB
    • PowerShell Core: 35.4 MB
  8. iSazonov commented on Jun 20, 2018

    @iSazonov
    CollaboratorAuthor

    Perhaps the specifics of the .csv file matter.

    Yes, but Windows PowerShell frees the memory so quickly that sometimes memory consumption can not be seen.
    I also see that in your test PowerShell Core is much slower.

  9. rthardin commented on Jun 29, 2018

    @rthardin

    I did a bit of iterative testing with a smaller CSV file (104866 bytes). On my system, the results show that Import-Csv performance does vary between back-to-back runs. The minimum runtime was 71 ms and the maximum was 192 ms. It looks like the runtime would decrease each time the GC came along. I'm running PowerShell inside a Docker container (mcr.microsoft.com/powershell:centos-7).

    I understand this issue was filed based on processing much larger input files (>100 MB), so I hope these findings are not a distraction; I thought the findings may still be relevant.

    $PSVersionTable
    
    Write-Host "Creating source data..."
    "random_numbers" | Out-File source.csv
    Get-Random -SetSeed 1 | Out-Null
    for ($i=0; $i -le 10000; $i++) {
    	(Get-Random).ToString() | Out-File source.csv -Append
    }
    Write-Host "Measuring Import-Csv performance over time..."
    "index,duration_ms,bytes_consumed" | Out-File 7112_results.csv
    for ($i=0; $i -le 5000; $i++) {
    	$millis = (Measure-Command { Import-Csv stats.csv }).TotalMilliseconds
    	$memory = [System.GC]::GetTotalMemory($false)
    	$i.ToString() + "," + $millis.ToString() + "," + $memory | Out-File 7112_results.csv -Append
    }
    Write-Host "Done"
    $PSVersionTable                                                                                                                                                                                                                          
    
    Name                           Value                                                                                                                                                                                                          
    ----                           -----                                                                                                                                                                                                          
    PSVersion                      6.0.2                                                                                                                                                                                                          
    PSEdition                      Core                                                                                                                                                                                                           
    GitCommitId                    v6.0.2                                                                                                                                                                                                         
    OS                             Linux 4.9.87-linuxkit-aufs #1 SMP Wed Mar 14 15:12:16 UTC 2018                                                                                                                                                 
    Platform                       Unix                                                                                                                                                                                                           
    PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                                                                                                        
    PSRemotingProtocolVersion      2.3                                                                                                                                                                                                            
    SerializationVersion           1.1.0.1                                                                                                                                                                                                        
    WSManStackVersion              3.0             

    screen shot 2018-06-29 at 3 04 45 pm

    Quartile Runtime Memory
    Minimum 71.37 ms 10.19 MB
    Q1 125.87 ms 39.88 MB
    Median 138.86 ms 50.56 MB
    Q3 151.39 ms 59.35 MB
    Maximum 192.12 ms 65.38 MB

    source.csv.txt
    7112_results.csv.txt

  10. iSazonov commented on Jun 30, 2018

    @iSazonov
    CollaboratorAuthor

    Ryan Hardin (@rthardin) Thanks for great tests! Could you please continue and test on Windows - for PowerShell Core and Windows PowerShell for comparison?

  11. rthardin commented on Jul 2, 2018

    @rthardin

    Ilya (@iSazonov) Unfortunately I don't have the time to spare at the moment, but the test script is posted above for others to try.

  12. iSazonov commented on Jul 3, 2018

    @iSazonov
    CollaboratorAuthor

    Updated scripts for testing

    ##############################
    Write-Host "Creating source data..."
    "random_numbers" | Out-File source.csv
    Get-Random -SetSeed 1 | Out-Null
    for ($i=0; $i -le 10000; $i++) {
    	(Get-Random).ToString() | Out-File source.csv -Append
    }Write-Host "Measuring Import-Csv performance over time..."
    "index,duration_ms,bytes_consumed" | Out-File psobject_results.csv
    for ($i=0; $i -le 5000; $i++) {
    	$millis = (Measure-Command { Import-Csv source.csv }).TotalMilliseconds
    	$memory = [System.GC]::GetTotalMemory($false)
    	$i.ToString() + "," + $millis.ToString() + "," + $memory | Out-File psobject_results.csv -Append
    }
    Write-Host "Done"
    
    ######################
    Write-Host "Measuring Import-Csv performance over time..."
    "index,duration_ms,bytes_consumed" | Out-File psobject_results.csv
    for ($i=0; $i -le 5000; $i++) {
     $millis = (Measure-Command { [psobject]::new() }).TotalMilliseconds
     $memory = [System.GC]::GetTotalMemory($false)
     $i.ToString() + "," + $millis.ToString() + "," + $memory | Out-File psobject_results.csv -Append
    }
    Write-Host "Done"
    
    
    ######################
    Write-Host "Measuring Import-Csv performance over time..."
    "index,duration_ms,bytes_consumed" | Out-File psobject_results.csv
    for ($i=0; $i -le 5000; $i++) {
     $millis = (Measure-Command { [object]::new() }).TotalMilliseconds
     $memory = [System.GC]::GetTotalMemory($false)
     $i.ToString() + "," + $millis.ToString() + "," + $memory | Out-File psobject_results.csv -Append
    }
    Write-Host "Done"

    I started with Import-Csv and temporary disable output in the C# code. Results was well. So I guess that root of problems in creating PSObject and next two tests confirm this. Windows PowerShell is 30-100 percent faster.
    It's unclear where the problem is - in PowerShell Core or in .Net Core.

    /cc Steve Lee (@SteveL-MSFT) Dongbo Wang (@daxian-dbw)

  13. SteveL-MSFT commented on Jul 10, 2018

    @SteveL-MSFT
    Member

    Ilya (@iSazonov) to see if we slowed down creating PSObjects (looking at the Import-CSV cmdlet, it's really just adding NoteProperty members to a PSObject), I ran this test:

    measure-command { 1..1000 | % { $a = [PSObject]::new(); 1..10 | % { $a | Add-Member -NotePropertyName "a$" -NotePropertyValue $ }; $a | Add-Member -NotePropertyName id -NotePropertyValue $_; $a } }

    Windows PowerShell 5.1 consistently took around 15.7s on my machine. PSCore6.1-3 consistently took around 8.4s, so about twice as fast.

    Will have to investigate more where the bottleneck is.

  14. 41 remaining items

  15. vancem commented on Aug 28, 2018

    @vancem

    If you have symbols to system.management.Automation, setting up you symbol path (File -> Set Symbol Path) and source path (File -> Set Source Path), and then you can right click on a node name and select Goto Source, and PerfView will take you to the line (attributed with metric). This allows you to see exactly what line is involved.

    Note that inlining can often make it hard to follow because the name in the source is really the caller (or caller's caller …). Having the line number (which by the way is approximate, it may be the line before as well), is helpful to disambiguate.

    If you ever get suspicious about things, you can fall back to the debugger. Set breakpoints (probably with hit counts, every 1000 or so) for some of the locations that PerfView pointed you at and you can see if they agree with the debugger.

    I have looked at lots of traces, and I have only rarely seen something where a name could not be explained by inlining only very, very rarely (and it could have been me misinterpreting the code). If by checking with the debugger, you believe you have found such a case, I would need all the source, PDBs and repro information to really investigate the issue.

  16. iSazonov commented on Aug 29, 2018

    @iSazonov
    CollaboratorAuthor

    Vance Morrison (@vancem) We haven't published PDB files for RC1. In PerfView log I saw that it search crossgen.exe and coped the utility in startup directory - right? After that I can see entry points in system.management.Automation

  17. SteveL-MSFT commented on Jun 20, 2019

    @SteveL-MSFT
    Member

    Ilya (@iSazonov) now that we have moved to .NET Core 3.0, can you rerun your perf test?

  18. iSazonov commented on Jun 20, 2019

    @iSazonov
    CollaboratorAuthor

    I'm even afraid to think about it :-))))

    Will do.

  19. iSazonov commented on Jun 20, 2019

    @iSazonov
    CollaboratorAuthor
    - Windows PowerShell PowerShell Core
    One column 6-17s/9-16Mb 1.8-3.4s/7.6-9.3Mb
    700 columns 55s/16Mb 16s/10Mb
  20. daxian-dbw commented on Jun 20, 2019

    @daxian-dbw
    Member

    Ilya (@iSazonov) So the perf issue with Import-Csv is now gone after moving to .NET 3.0 preview? What do you mean by one column and 700 columns?

  21. removed
    Waiting - DotNetCore
    on Jun 20, 2019
  22. iSazonov commented on Jun 20, 2019

    @iSazonov
    CollaboratorAuthor

    Dongbo Wang (@daxian-dbw) After Staffan Gustafsson (@powercode) improvements for PSObject all cmdlets works faster with the objects and make less allocations.
    After moving to .Net Core 3.0 we get better GC. Now GC does cleanups even better .Net Framework (I say about the tests I did here). Perhaps .Net Core 3.0 add a performance too but I don't compare with previous PowerShell Core released versions.

    What do you mean by one column and 700 columns?

    In my tests I generate csv files. In first test the csv file consists of one column. Second - 700 columns. The first test shows a general increase in performance (I would assume that most of it comes from .Net Core 3.0). Second one explicitly shows an increase in performance which comes from Staffan Gustafsson (@powercode) PR.

  23. SteveL-MSFT commented on Jun 20, 2019

    @SteveL-MSFT
    Member

    Ilya (@iSazonov) so can we close this?

  24. iSazonov commented on Jun 21, 2019

    @iSazonov
    CollaboratorAuthor

    We get great progress for Import-Csv cmdlet so I close the issue.

    Make sense to open new issues if we will find another scenarios with bad performance.

    (Although I would like to have something to see performance history - improvements and regressions after new commits)

  25. daxian-dbw commented on Jun 24, 2019

    @daxian-dbw
    Member

    (Although I would like to have something to see performance history - improvements and regressions after new commits)

    Absolutely. We need to have some perf tests that serve as benchmarks to help us see the trend. Currently we have none of those tests. Opened #9996

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-DiscussionResolution-FixedWG-CmdletsWG-Engine-Performance

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions