Skip to content

Commit e35b7d2

Browse files
committed
Address PR feedback, adjust string builder size based on log file data
1 parent ef04cd6 commit e35b7d2

File tree

7 files changed

+87
-30
lines changed

7 files changed

+87
-30
lines changed

src/PowerShellEditorServices.Protocol/MessageProtocol/MessageReader.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,11 @@ public async Task<Message> ReadMessage()
114114
Message parsedMessage = this.messageSerializer.DeserializeMessage(messageObject);
115115

116116
// Log message info - initial capacity for StringBuilder varies depending on whether
117-
// the log level is Diagnostic where JsonRpc message payloads are logged and
118-
// vary from 1K up edited file size chars. When not logging message payloads the
119-
// typical request log message is under 250 chars.
117+
// the log level is Diagnostic where JsonRpc message payloads are logged and vary in size
118+
// from 1K up to the edited file size. When not logging message payloads, the typical
119+
// request log message size is under 256 chars.
120120
var logStrBld =
121-
new StringBuilder(this.logger.MinimumConfiguredLogLevel == LogLevel.Diagnostic ? 4096 : 250)
121+
new StringBuilder(this.logger.MinimumConfiguredLogLevel == LogLevel.Diagnostic ? 4096 : 256)
122122
.Append("Received ")
123123
.Append(parsedMessage.MessageType)
124124
.Append(" '").Append(parsedMessage.Method).Append("'");

src/PowerShellEditorServices.Protocol/MessageProtocol/MessageWriter.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ public async Task WriteMessage(Message messageToWrite)
5959
messageToWrite);
6060

6161
// Log message info - initial capacity for StringBuilder varies depending on whether
62-
// the log level is Diagnostic where JsonRpc message payloads are logged and
63-
// vary from 1K up 225K chars. When not logging message payloads the typical response
64-
// log message is under 250 chars.
62+
// the log level is Diagnostic where JsonRpc message payloads are logged and vary
63+
// in size from 1K up to 225K chars. When not logging message payloads, the typical
64+
// response log message size is under 256 chars.
6565
var logStrBld =
66-
new StringBuilder(this.logger.MinimumConfiguredLogLevel == LogLevel.Diagnostic ? 4096 : 250)
66+
new StringBuilder(this.logger.MinimumConfiguredLogLevel == LogLevel.Diagnostic ? 4096 : 256)
6767
.Append("Writing ")
6868
.Append(messageToWrite.MessageType)
6969
.Append(" '").Append(messageToWrite.Method).Append("'");

tools/PsesLogAnalyzer/Analyze.ps1

+49-1
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,59 @@ function Get-PsesIntelliSenseCompletionTime {
155155
}
156156

157157
foreach ($entry in $logEntries) {
158-
# IntelliSense completed in 320ms.
158+
# IntelliSense completed in 320ms.
159159
if (($entry.LogMessageType -eq 'Log') -and ($entry.Message.Data -match '^\s*IntelliSense completed in\s+(?<ms>\d+)ms.\s*$')) {
160160
$elapsedMilliseconds = [int]$matches["ms"]
161161
[PsesLogEntryElapsed]::new($entry, $elapsedMilliseconds)
162162
}
163163
}
164164
}
165165
}
166+
167+
function Get-PsesMessage {
168+
[CmdletBinding(DefaultParameterSetName = "PsesLogEntry")]
169+
param(
170+
# Specifies a path to one or more PSES EditorServices log files.
171+
[Parameter(Mandatory = $true, Position = 0, ParameterSetName = "Path")]
172+
[Alias("PSPath")]
173+
[ValidateNotNullOrEmpty()]
174+
[string]
175+
$Path,
176+
177+
# Specifies PsesLogEntry objects to analyze.
178+
[Parameter(Mandatory = $true, Position = 0, ParameterSetName = "PsesLogEntry", ValueFromPipeline = $true)]
179+
[ValidateNotNull()]
180+
[psobject[]]
181+
$LogEntry,
182+
183+
# Specifies the log level entries to return. Default returns Normal and above.
184+
# Use StrictMatch to return only the specified log level entries.
185+
[Parameter()]
186+
[PsesLogLevel]
187+
$LogLevel = $([PsesLogLevel]::Normal),
188+
189+
# Use StrictMatch to return only the specified log level entries.
190+
[Parameter()]
191+
[switch]
192+
$StrictMatch
193+
)
194+
195+
begin {
196+
if ($PSCmdlet.ParameterSetName -eq "Path") {
197+
$logEntries = Parse-PsesLog $Path
198+
}
199+
}
200+
201+
process {
202+
if ($PSCmdlet.ParameterSetName -eq "PsesLogEntry") {
203+
$logEntries = $LogEntry
204+
}
205+
206+
foreach ($entry in $logEntries) {
207+
if (($StrictMatch -and ($entry.LogLevel -eq $LogLevel)) -or
208+
(!$StrictMatch -and ($entry.LogLevel -ge $LogLevel))) {
209+
$entry
210+
}
211+
}
212+
}
213+
}

tools/PsesLogAnalyzer/PsesLogAnalyzer.format.ps1xml

+9-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
<Width>15</Width>
2828
<Alignment>left</Alignment>
2929
</TableColumnHeader>
30+
<TableColumnHeader>
31+
<Label>LogLevel</Label>
32+
<Width>11</Width>
33+
<Alignment>left</Alignment>
34+
</TableColumnHeader>
3035
<TableColumnHeader>
3136
<Label>Message</Label>
3237
<Alignment>left</Alignment>
@@ -37,7 +42,7 @@
3742
<TableColumnItems>
3843
<TableColumnItem>
3944
<PropertyName>Index</PropertyName>
40-
</TableColumnItem>
45+
</TableColumnItem>
4146
<TableColumnItem>
4247
<PropertyName>TimestampStr</PropertyName>
4348
</TableColumnItem>
@@ -47,6 +52,9 @@
4752
<TableColumnItem>
4853
<PropertyName>LogMessageType</PropertyName>
4954
</TableColumnItem>
55+
<TableColumnItem>
56+
<PropertyName>LogLevel</PropertyName>
57+
</TableColumnItem>
5058
<TableColumnItem>
5159
<ScriptBlock>($_.Message -split "`r`n")[0]</ScriptBlock>
5260
</TableColumnItem>

tools/PsesLogAnalyzer/PsesLogAnalyzer.psd1

+5-4
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ Author = 'Microsoft'
2727
CompanyName = 'Microsoft'
2828

2929
# Copyright statement for this module
30-
Copyright = '(c) 2017 Microsoft. All rights reserved.'
30+
Copyright = '(c) 2018 Microsoft. All rights reserved.'
3131

3232
# Description of the functionality provided by this module
33-
# Description = ''
33+
Description = 'PowerShellEditorServices log file parser and analysis commands.'
3434

3535
# Minimum version of the PowerShell engine required by this module
36-
# PowerShellVersion = ''
36+
PowerShellVersion = '5.1'
3737

3838
# Name of the PowerShell host required by this module
3939
# PowerShellHostName = ''
@@ -74,7 +74,8 @@ FunctionsToExport = @(
7474
'Get-PsesRpcMessageResponseTime',
7575
'Get-PsesRpcNotificationMessage',
7676
'Get-PsesScriptAnalysisCompletionTime',
77-
'Get-PsesIntelliSenseCompletionTime'
77+
'Get-PsesIntelliSenseCompletionTime',
78+
'Get-PsesMessage'
7879
)
7980

8081
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
. $PSScriptRoot\Types.ps1
22
. $PSScriptRoot\Parse-PsesLog.ps1
33
. $PSScriptRoot\Analyze.ps1
4-
5-
Export-ModuleMember -Function *-*

tools/PsesLogAnalyzer/Types.ps1

+16-14
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
11
enum PsesLogLevel {
2-
Diagnostic;
3-
Verbose;
4-
Normal;
5-
Warning;
6-
Error;
2+
Diagnostic
3+
Verbose
4+
Normal
5+
Warning
6+
Error
77
}
88

99
enum PsesLogMessageType {
10-
Log;
11-
Exception;
12-
HandledException;
13-
Request;
14-
Response;
15-
Notification;
10+
Log
11+
Exception
12+
HandledException
13+
Request
14+
Response
15+
Notification
1616
}
1717

1818
enum PsesNotificationSource {
19-
Unknown;
20-
Client;
21-
Server;
19+
Unknown
20+
Client
21+
Server
2222
}
2323

2424
class PsesLogMessage {
2525
[string]$Data
26+
[int]$DataSize
2627

2728
PsesLogMessage([string]$Data) {
2829
$this.Data = $Data
30+
$this.DataSize = $Data.Length
2931
}
3032

3133
[string] ToString() {

0 commit comments

Comments
 (0)