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

Replace usage of Split-Path to improve performance #60

Open
IMJLA opened this issue Jan 21, 2024 · 0 comments
Open

Replace usage of Split-Path to improve performance #60

IMJLA opened this issue Jan 21, 2024 · 0 comments
Labels
enhancement-performance improve performance without sacrificing functionality

Comments

@IMJLA
Copy link
Owner

IMJLA commented Jan 21, 2024

# Here is the prep code.  We're going to prep some fake test data instead down below
$DirectoryPath = 'WinNT://Sample-TestPC/Authenticated Users'

# Here we will list the various methods whose performance we want to test:
$Scriptblocks = @(
    { 
        # Here is the prep code we will use to generate fake test data.100k fake servers, and 10k fake serial numbers to lookup.
        $n = 100000
        Write-Host "Looping $n times..."
        $Servers = for ($i = 0; $i -lt $n; $i++) {
            $Length = $DirectoryPath.LastIndexOf('/')
            $DirectoryPath.Substring(0,$Length)
        }
    },


    {
        # Here is the prep code we will use to generate fake test data.100k fake servers, and 10k fake serial numbers to lookup.
        $n = 100000
        Write-Host "Looping $n times..."
        $Servers = for ($i = 0; $i -lt $n; $i++) {
            $DirectoryPath | Split-Path -Parent
        }
    }
)


# Here is our function to test the performance of each method
Function Test-ScriptBlockPerformance {
    param (
        [Parameter(ValueFromPipeline)]
        [scriptblock[]]$ScriptBlock
    )
    process {
        ForEach ($ThisScript in $ScriptBlock) {
            Write-Host "Starting test of expression {$ThisScript}"
            $Result = Measure-Command -Expression $ThisScript
            [pscustomobject]@{
                Expression = "{$ThisScript}"
                Result = $Result
                Ticks = $Result.Ticks
            }
        }
    }
}


# Here is where we invoke our function to run the performance tests, and sort the results to find a winner (the fastest method)
$Leaderboard = $Scriptblocks | Test-ScriptBlockPerformance | Sort-Object -Property Ticks
$Leaderboard | Format-Table Ticks,Expression -AutoSize

Expected Behavior

Code is similar to the second sample code completes in only 3873692 tickets (~69% reduction)

Current Behavior

Export-Permission and dependencies contain code similar to the first sample code which completes in 123239274 ticks

Possible Solution

Replace Split-Path with .Net string manipulation methods

@IMJLA IMJLA added the enhancement-performance improve performance without sacrificing functionality label Jan 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement-performance improve performance without sacrificing functionality
Projects
None yet
Development

No branches or pull requests

1 participant