Title: PSAMSIMethodInvocationLogging: Stream operation is extremely slow when buffer is created with New-Object · Issue #18033 · PowerShell/PowerShell · GitHub
Open Graph Title: PSAMSIMethodInvocationLogging: Stream operation is extremely slow when buffer is created with New-Object · Issue #18033 · PowerShell/PowerShell
X Title: PSAMSIMethodInvocationLogging: Stream operation is extremely slow when buffer is created with New-Object · Issue #18033 · PowerShell/PowerShell
Description: Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerShell 5.1 and PowerShell. Steps to reprodu...
Open Graph Description: Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerSh...
X Description: Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerSh...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/18033
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"PSAMSIMethodInvocationLogging: Stream operation is extremely slow when buffer is created with New-Object","articleBody":"### Prerequisites\r\n\r\n- [X] Write a descriptive title.\r\n- [X] Make sure you are able to repro it on the [latest released version](https://github.com/PowerShell/PowerShell/releases)\r\n- [X] Search the existing issues.\r\n- [X] Refer to the [FAQ](https://github.com/PowerShell/PowerShell/blob/master/docs/FAQ.md).\r\n- [X] Refer to [Differences between Windows PowerShell 5.1 and PowerShell](https://docs.microsoft.com/powershell/scripting/whats-new/differences-from-windows-powershell).\r\n\r\n### Steps to reproduce\r\n\r\n### Preparation\r\nCreate test script (copytest.ps1)\r\n``` PowerShell\r\n[CmdletBinding()]\r\nparam (\r\n [switch]$UseNewObject\r\n)\r\n\r\n$bufLen = 1MB\r\nif ($UseNewObject) {\r\n $buffer = New-Object -TypeName byte[] -ArgumentList $bufLen\r\n}\r\nelse {\r\n $buffer = [byte[]]::new($bufLen)\r\n}\r\n\r\n$srcFile = Join-Path -Path $PSScriptRoot -ChildPath 'srcFile'\r\n$dstFile = Join-Path -Path $PSScriptRoot -ChildPath 'dstFile'\r\nif (Test-Path -LiteralPath $dstFile) {\r\n Remove-Item -LiteralPath $dstFile\r\n}\r\n\r\ntry {\r\n $inputStream = [System.IO.File]::Open($srcFile, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::Read)\r\n $outputStream = [System.IO.File]::Open($dstFile, [System.IO.FileMode]::Create, [System.IO.FileAccess]::Write, [System.IO.FileShare]::Read)\r\n $stopwatch = [System.Diagnostics.Stopwatch]::new()\r\n $stopwatch.Start()\r\n while ($true) {\r\n $ts = Measure-Command -Expression { $readCount = $inputStream.Read($buffer, 0, $buffer.Length) }\r\n [PSCustomObject]@{\r\n Operation = 'Read'\r\n Count = $readCount\r\n Time = $ts.TotalMilliseconds\r\n }\r\n\r\n if ($readCount -eq 0) {\r\n break\r\n }\r\n\r\n $ts = Measure-Command -Expression { $outputStream.Write($buffer, 0, $readCount) }\r\n [PSCustomObject]@{\r\n Operation = 'Write'\r\n Count = $readCount\r\n Time = $ts.TotalMilliseconds\r\n }\r\n }\r\n $stopwatch.Stop()\r\n \"----------\"\r\n \"Time Total: $($stopwatch.Elapsed.TotalMilliseconds)\"\r\n}\r\nfinally {\r\n $outputStream.Dispose()\r\n $inputStream.Dispose()\r\n}\r\n```\r\nThis script simply copies srcFile to dstFile using Stream read/write operation with byte[] buffer. With no parameter, buffer would be created using `[byte[]]::new()` method. With `-UseNewObject` parameter, buffer would be created using `New-Object` cmdlet.\r\n\r\ncreate srcFile (10MB) in the same directory as copytest.ps1.\r\n``` PowerShell\r\nPS> fsutil file createnew srcFile (10MB)\r\n```\r\n\r\n### Execution\r\n\r\n``` PowerShell\r\nPS> Enable-ExperimentalFeature PSAMSIMethodInvocationLogging\r\n```\r\nRestart pwsh\r\n\r\n#### Case1 PSAMSIMethodInvocationLogging ON, new() method\r\n``` PowerShell\r\nPS> .\\copytest.ps1\r\n\r\nOperation Count Time\r\n--------- ----- ----\r\nRead 1048576 0.26\r\nWrite 1048576 10.06\r\nRead 1048576 0.27\r\nWrite 1048576 3.84\r\nRead 1048576 0.24\r\nWrite 1048576 3.71\r\nRead 1048576 0.31\r\nWrite 1048576 3.87\r\nRead 1048576 0.27\r\nWrite 1048576 4.15\r\nRead 1048576 0.29\r\nWrite 1048576 3.79\r\nRead 1048576 0.26\r\nWrite 1048576 3.78\r\nRead 1048576 0.35\r\nWrite 1048576 3.98\r\nRead 1048576 0.29\r\nWrite 1048576 3.92\r\nRead 1048576 0.18\r\nWrite 1048576 3.51\r\nRead 0 0.05\r\n----------\r\nTime Total: 51.1335\r\n```\r\n\r\n#### Case2 PSAMSIMethodInvocationLogging ON, New-Object cmdlet\r\n``` PowerShell\r\nPS> .\\copytest.ps1 -UseNewObject\r\n\r\nOperation Count Time\r\n--------- ----- ----\r\nRead 1048576 435.67\r\nWrite 1048576 531.13\r\nRead 1048576 448.98\r\nWrite 1048576 458.24\r\nRead 1048576 454.25\r\nWrite 1048576 448.41\r\nRead 1048576 434.75\r\nWrite 1048576 474.36\r\nRead 1048576 436.77\r\nWrite 1048576 461.22\r\nRead 1048576 450.79\r\nWrite 1048576 465.32\r\nRead 1048576 453.66\r\nWrite 1048576 466.68\r\nRead 1048576 456.89\r\nWrite 1048576 459.88\r\nRead 1048576 454.27\r\nWrite 1048576 446.89\r\nRead 1048576 465.88\r\nWrite 1048576 457.08\r\nRead 0 452.79\r\n----------\r\nTime Total: 9625.9351\r\n```\r\n\r\n``` PowerShell\r\nPS> Disable-ExperimentalFeature PSAMSIMethodInvocationLogging\r\n```\r\nRestart pwsh\r\n\r\n#### Case3 PSAMSIMethodInvocationLogging OFF, new() method\r\n``` PowerShell\r\nPS> .\\copytest.ps1\r\n\r\nOperation Count Time\r\n--------- ----- ----\r\nRead 1048576 0.48\r\nWrite 1048576 4.15\r\nRead 1048576 0.35\r\nWrite 1048576 3.78\r\nRead 1048576 0.27\r\nWrite 1048576 6.58\r\nRead 1048576 0.28\r\nWrite 1048576 3.85\r\nRead 1048576 0.38\r\nWrite 1048576 3.53\r\nRead 1048576 0.26\r\nWrite 1048576 3.42\r\nRead 1048576 0.25\r\nWrite 1048576 3.48\r\nRead 1048576 0.24\r\nWrite 1048576 3.89\r\nRead 1048576 0.32\r\nWrite 1048576 3.66\r\nRead 1048576 0.19\r\nWrite 1048576 3.90\r\nRead 0 0.09\r\n----------\r\nTime Total: 47.3111\r\n```\r\n#### Case4 PSAMSIMethodInvocationLogging OFF, New-Object cmdlet\r\n``` PowerShell\r\nPS> .\\copytest.ps1 -UseNewObject\r\n\r\nOperation Count Time\r\n--------- ----- ----\r\nRead 1048576 0.50\r\nWrite 1048576 4.22\r\nRead 1048576 0.27\r\nWrite 1048576 3.96\r\nRead 1048576 0.28\r\nWrite 1048576 3.94\r\nRead 1048576 0.27\r\nWrite 1048576 4.52\r\nRead 1048576 0.27\r\nWrite 1048576 4.02\r\nRead 1048576 0.27\r\nWrite 1048576 3.66\r\nRead 1048576 0.30\r\nWrite 1048576 3.65\r\nRead 1048576 0.34\r\nWrite 1048576 3.93\r\nRead 1048576 0.26\r\nWrite 1048576 3.80\r\nRead 1048576 0.26\r\nWrite 1048576 3.60\r\nRead 0 0.05\r\n----------\r\nTime Total: 46.4007\r\n```\r\n\r\nStrangely, only in Case2 (PSAMSIMethodInvocationLogging ON, and buffer created with New-Object cmdlet), Stream read/write operation become extreamly slow (more than 188 times).\r\nPSAMSIMethodInvocationLogging will be stable in PS7.3 (#17962), so some scripts may slow down at that time.\r\n\r\n### Expected behavior\r\n\r\n```console\r\nAll 1-4 Cases take roughly the same time\r\n```\r\n\r\n\r\n### Actual behavior\r\n\r\n```console\r\nStream read/write operation become extreamly slow in Case2\r\n```\r\n\r\n\r\n### Error details\r\n\r\n```console\r\nNo error\r\n```\r\n\r\n\r\n### Environment data\r\n\r\n```powershell\r\nName Value\r\n---- -----\r\nPSVersion 7.3.0-preview.7\r\nPSEdition Core\r\nGitCommitId 7.3.0-preview.7\r\nOS Microsoft Windows 10.0.19044\r\nPlatform Win32NT\r\nPSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}\r\nPSRemotingProtocolVersion 2.3\r\nSerializationVersion 1.1.0.1\r\nWSManStackVersion 3.0\r\n```\r\n\r\n\r\n### Visuals\r\n\r\n_No response_","author":{"url":"https://github.com/hazychill","@type":"Person","name":"hazychill"},"datePublished":"2022-09-06T15:11:20Z","url":"https://github.com/PowerShell/PowerShell/issues/18033"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:23e5deda-4ee4-ad4a-589f-736084c1249d |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AE4A:1EF581:CFDD0D:12DA698:6AFDA3FC |
| html-safe-nonce | 810c369f1924b967787960ff55b2339a179d56a182ce97b9dca70352c3db827c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBRTRBOjFFRjU4MTpDRkREMEQ6MTJEQTY5ODo2QUZEQTNGQyIsInZpc2l0b3JfaWQiOiI1Njk5NDg1NTkwMDA5NzgzMjkyIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 12cbeb8afbaa123cde4b82073165a59602d21440790fe1bfbc6adf8a7ce0659e |
| hovercard-subject-tag | issue:1363440556 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/PowerShell/18033/issue_layout |
| twitter:image | https://opengraph.githubassets.com/200cacd3f0c902db32bb5395b357fcfff3085bc085573d199bdcdc08a950578e/PowerShell/PowerShell/issues/18033 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/200cacd3f0c902db32bb5395b357fcfff3085bc085573d199bdcdc08a950578e/PowerShell/PowerShell/issues/18033 |
| og:image:alt | Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerSh... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | hazychill |
| hostname | github.com |
| expected-hostname | github.com |
| None | bc86424ef34a73b386237872800e922d149d65ba907f63d6f150a2dcb8433bea |
| turbo-cache-control | no-preview |
| go-import | github.com/PowerShell/PowerShell git https://github.com/PowerShell/PowerShell.git |
| octolytics-dimension-user_id | 11524380 |
| octolytics-dimension-user_login | PowerShell |
| octolytics-dimension-repository_id | 49609581 |
| octolytics-dimension-repository_nwo | PowerShell/PowerShell |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 49609581 |
| octolytics-dimension-repository_network_root_nwo | PowerShell/PowerShell |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 0ddf53eb35c87f1f8df13dbf75c3bdadbfc6e969 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width