Skip to content

Commit

Permalink
[BcNuGet] Reduce memory consumption, improve performance, prevent mem…
Browse files Browse the repository at this point in the history
…ory leaks (#3760)

We found that `Compress-Archive` and especially storing the
`octet-stream` inside a string instead of using a Stream causes a very
high memory consumption and a bad performance.

We're invoking the BcNuGet Cmdlets from our own C# API and therefore
were able to profile the memory and performance. The behaviour however
is similar when invoking the Cmdlets directly (PowerShell Core 7.4.6).

**Memory (Note: around 340 MB is the base load our API brings with it)**

Before: Memory spikes when uploading big apps, around 1 GB memory
consumption from BcNuGet Cmdlets
<img width="502" alt="image"
src="https://github.com/user-attachments/assets/81d81377-304f-459f-a8b3-dd1fbe187b8c">

After: 
<img width="497" alt="image"
src="https://github.com/user-attachments/assets/6bb2fcd3-0889-4619-b333-341b200050b8">


**Performance**

Before:
<img width="516" alt="image"
src="https://github.com/user-attachments/assets/62747a1e-751a-4b89-bd62-adcd347d31a0">

After:
<img width="526" alt="image"
src="https://github.com/user-attachments/assets/be427969-c7cc-4bf4-a05d-43894163998b">
  • Loading branch information
lippertmarkus authored Nov 15, 2024
1 parent a399bbd commit c47d8ca
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
6 changes: 5 additions & 1 deletion NuGet/New-BcNuGetPackage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,11 @@ Function New-BcNuGetPackage {
if (Test-Path $nuPkgFile -PathType Leaf) {
Remove-Item $nupkgFile -Force
}
Compress-Archive -Path "$rootFolder\*" -DestinationPath "$nupkgFile.zip" -Force
if (Test-Path "$nupkgFile.zip" -PathType Leaf) {
Remove-Item "$nupkgFile.zip" -Force
}
Add-Type -AssemblyName System.IO.Compression.FileSystem
[System.IO.Compression.ZipFile]::CreateFromDirectory("$rootFolder", "$nupkgFile.zip", [System.IO.Compression.CompressionLevel]::Optimal, $false)
Rename-Item -Path "$nupkgFile.zip" -NewName $nuPkgFileName

$size = (Get-Item $nupkgFile).Length
Expand Down
20 changes: 11 additions & 9 deletions NuGet/NuGetFeedClass.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -353,17 +353,19 @@ class NuGetFeed {
"X-NuGet-ApiKey" = $this.token
"X-NuGet-Client-Version" = "6.3.0"
}
$FileContent = [System.IO.File]::ReadAllBytes($package)
$boundary = [System.Guid]::NewGuid().ToString();
$boundary = [System.Guid]::NewGuid().ToString();
$LF = "`r`n";

$body = [System.Text.Encoding]::UTF8.GetBytes("--$boundary$LF")
$body += [System.Text.Encoding]::UTF8.GetBytes("Content-Type: application/octet-stream$($LF)Content-Disposition: form-data; name=package; filename=""$([System.IO.Path]::GetFileName($package))""$($LF)$($LF)")
$body += $fileContent
$body += [System.Text.Encoding]::UTF8.GetBytes("$LF--$boundary--$LF")

$tmpFile = Join-Path ([System.IO.Path]::GetTempPath()) ([GUID]::NewGuid().ToString())
[System.IO.File]::WriteAllBytes($tmpFile, $body)
$fs = [System.IO.File]::OpenWrite($tmpFile)
try {
$fs.Write([System.Text.Encoding]::UTF8.GetBytes("--$boundary$LF"))
$fs.Write([System.Text.Encoding]::UTF8.GetBytes("Content-Type: application/octet-stream$($LF)Content-Disposition: form-data; name=package; filename=""$([System.IO.Path]::GetFileName($package))""$($LF)$($LF)"))
$fs.Write([System.IO.File]::ReadAllBytes($package))
$fs.Write([System.Text.Encoding]::UTF8.GetBytes("$LF--$boundary--$LF"))
} finally {
$fs.Close()
}

Write-Host "Submitting NuGet package"
try {
Invoke-RestMethod -UseBasicParsing -Uri $this.packagePublishUrl -ContentType "multipart/form-data; boundary=$boundary" -Method Put -Headers $headers -inFile $tmpFile | Out-Host
Expand Down
1 change: 1 addition & 0 deletions ReleaseNotes.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
6.0.29
Issue 3591 When using Publish-NAVApp to publish an app, which fails compilation in the service, the command might hang forever - the fix for this is a temporary hack put in place for the versions which doesn't work.
Improve performance and reduce memory consumption when creating and pushing NuGet packages

6.0.28
Set useApproximateVersion to true in settings to increase performance of Get-BcArtifactUrl when selecting latest artifact, by using an approximate filtering on blobs
Expand Down

0 comments on commit c47d8ca

Please sign in to comment.