Console
Console copied to clipboard
Question: Remoting, serialization optimization?
I run 2 scripts
- In Sitecore Powershell ISE
$criteria = @(
@{Filter = "Equals"; Field = "_haslayout_b"; Value = $True}
)
$props = @{
Index = "sitecore_master_index"
Criteria = $criteria
}
Find-Item @props
- In Powershell ISE on the same machine
Import-Module -Name SPE
$session = New-ScriptSession -Username admin -Password b -ConnectionUri https://xmcloudcm.localhost/
Invoke-RemoteScript -ScriptBlock {
$criteria = @(
@{Filter = "Equals"; Field = "_haslayout_b"; Value = $True}
)
$props = @{
Index = "sitecore_master_index"
Criteria = $criteria
}
Find-Item @props
} -Session $session
The first script executes immediately. It returns 400 records. The second script runs for a very long time, or forever. After finishing, Sitecore starts to work slowly. Even the first script executes much more slowly.
When I optimize the script with Select-Object and select only the fields that I need, the issue disappears.
But the serialization of 400 search results doesn't sound like something crazy. Additionally, Sitecore is starting to work slowly after performing the serialization, so there may be an issue with the serialization process.
Question: Is Sitecore PowerShell Remoting rely on Microsoft CLIXML binaries? Or is it something inside this repository that probably could be optimized?
It seems that for this command, the complexity is not O(N). It works fine for 10(fast), slow for 20(in 3-5 times slower than for 10), much slower for 40(in 10 times slower than for 10), and extremely slow for 100+.
I think it's serializing each record with this call:
session.ExecuteScriptPart("ConvertTo-CliXml -InputObject $results");
Can you try using the Raw parameter?
Import-Module -Name SPE
$session = New-ScriptSession -Username admin -Password b -ConnectionUri https://xmcloudcm.localhost/
Invoke-RemoteScript -ScriptBlock {
$criteria = @(
@{Filter = "Equals"; Field = "_haslayout_b"; Value = $True}
)
$props = @{
Index = "sitecore_master_index"
Criteria = $criteria
}
Find-Item @props
} -Session $session -Raw
Raw parameter makes it immediate!
However, the output is not useful... It is impossible to map raw strings with comma-separated values back into items and fields.
It seems the problem is inside ConvertTo-CliXml. But it doesn't do a lot, it relies on System.Management.Automation.Serializer, System.Xml.XmlWriter, System.IO.StringWriter, and System.Xml.XmlTextWriter. And I guess the performance problem is in System.Management.Automation.Serializer, because all other classes are used too often and that should not be possible that they have bad performance. Someone would notice it before and report to Microsoft.
So, the options that I have:
- Use
Select-Object - Write my own serializer, serialize to CSV/JSON/XML, and return it as text. And use it together with
-Rawparameter.
Or any other options?