AutomatedLab / AutomatedLab.Common

PowerShell module for daily administrative tasks and general helper functions
MIT License
65 stars 24 forks source link

Redesign of algorithm #118

Closed raandree closed 1 year ago

raandree commented 1 year ago

Description

Redesign of the function Split-Array. Most of the time it was not working as expected, especially when requesting larger ChunkCounts.

A test with the old version shows the issue:

ValuesCount ValuesRequested ChunksCreated    OK
----------- --------------- -------------    --
       1000               1             1  True
       1000               2             2  True
       1000               3             3  True
       1000               4             4  True
       1000               5             5  True
       1000               6             6  True
       1000               7             7  True
       1000               8             8  True
       1000               9             9  True
       1000              10            10  True
       1000              11            11  True
       1000              12            12  True
       1000              13            13  True
       1000              14            14  True
       1000              15            15  True
       1000              16            16  True
       1000              17            17  True
       1000              18            18  True
       1000              19            19  True
       1000              20            20  True
       1000              21            21  True
       1000              22            22  True
       1000              23            23  True
       1000              24            24  True
       1000              25            25  True
       1000              26            26  True
       1000              27            27  True
       1000              28            28  True
       1000              29            29  True
       1000              30            30  True
       1000              31            31  True
       1000              32            32  True
       1000              33            33  True
       1000              34            34  True
       1000              35            35  True
       1000              36            36  True
       1000              37            36 False
       1000              38            38  True
       1000              39            39  True
       1000              40            40  True
       1000              41            40 False
       1000              42            42  True
       1000              43            42 False
       1000              44            44  True
       1000              45            44 False
       1000              46            46  True
       1000              47            46 False
       1000              48            48  True
       1000              49            48 False
       1000              50            50  True
       1000              51            50 False
       1000              52            50 False
       1000              53            53  True
       1000              54            53 False
       1000              55            53 False
       1000              56            56  True
       1000              57            56 False
       1000              58            56 False
       1000              59            59  True
       1000              60            59 False
       1000              61            59 False
       1000              62            59 False
       1000              63            63  True
       1000              64            63 False
       1000              65            63 False
       1000              66            63 False
       1000              67            67  True
       1000              68            67 False
       1000              69            67 False
       1000              70            67 False
       1000              71            67 False
       1000              72            72  True
       1000              73            72 False
       1000              74            72 False
       1000              75            72 False
       1000              76            72 False
       1000              77            77  True
       1000              78            77 False
       1000              79            77 False
       1000              80            77 False
       1000              81            77 False
       1000              82            77 False
       1000              83            77 False
       1000              84            84  True
       1000              85            84 False
       1000              86            84 False
       1000              87            84 False
       1000              88            84 False
       1000              89            84 False
       1000              90            84 False
       1000              91            91  True
       1000              92            91 False
       1000              93            91 False
       1000              94            91 False
       1000              95            91 False
       1000              96            91 False
       1000              97            91 False
       1000              98            91 False
       1000              99            91 False
       1000             100           100  True
       1000             101           100 False
       1000             102           100 False
       1000             103           100 False
       1000             104           100 False
       1000             105           100 False
       1000             106           100 False
       1000             107           100 False
       1000             108           100 False
       1000             109           100 False
       1000             110           100 False
       1000             111           100 False
       1000             112           112  True
       1000             113           112 False
       1000             114           112 False
       1000             115           112 False
       1000             116           112 False
       1000             117           112 False
       1000             118           112 False
       1000             119           112 False
       1000             120           112 False
       1000             121           112 False
       1000             122           112 False
       1000             123           112 False
       1000             124           112 False
       1000             125           125  True
       1000             126           125 False
       1000             127           125 False
       1000             128           125 False
       1000             129           125 False
       1000             130           125 False
       1000             131           125 False
       1000             132           125 False
       1000             133           125 False
       1000             134           125 False
       1000             135           125 False
       1000             136           125 False
       1000             137           125 False
       1000             138           125 False
       1000             139           125 False
       1000             140           125 False
       1000             141           125 False
       1000             142           125 False
       1000             143           143  True
       1000             144           143 False
       1000             145           143 False
       1000             146           143 False

Type of change

How was the change tested?

Test for MaxChunkSize:

5..100 | ForEach-Object {
    $values = 1..4533 | ForEach-Object { "Test $_" }
    $chunkSize = $_
    $r = Split-Array -List $values -MaxChunkSize $chunkSize
    [pscustomobject]@{
        ValuesCount = $values.Count
        ValueCountEqual = ($r | ForEach-Object { $_.Count } | Measure-Object -Sum).Sum -eq $values.Count
        ExeedingMaxChunkSize = [bool]($r | Where-Object { $_.Count -gt $chunkSize })

    }
}

Test for ChunkCount:

1..10000 | ForEach-Object {
    $values = 1..1000
    $r = Split-Array -List $values -ChunkCount $_
    [pscustomobject]@{
        ValuesCount = $values.Count
        ValuesRequested = $_
        ChunksCreated = $r.Count
        OK = $_ -eq $r.Count
    }
}