-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShow-TraceStructure.ps1
More file actions
211 lines (176 loc) · 8.06 KB
/
Show-TraceStructure.ps1
File metadata and controls
211 lines (176 loc) · 8.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<#
.SYNOPSIS
High-Performance Firebird Trace Parser (Regex Block Mode)
Refined for correctness and robustness.
.DESCRIPTION
Parses Firebird trace logs by splitting blocks based on Timestamp.
Uses robust Regex look-aheads to separate fields.
Field Mapping:
- TransactionOptions: From TRA_ line (READ_COMMITTED...)
- Params: From param0 = ... lines
- SqlStatement: Stops at separators (^^^^, PLAN, param, records)
- SqlPlan: Includes PLAN (...) AND Table Stats logic.
.OUTPUTS
[System.Collections.Generic.List[PSObject]]
#>
param (
[Parameter(Mandatory = $true, Position = 0)]
[string]$Path
)
Write-Host "--- Fast Analysis Started (Legacy Mode) ---"
Write-Host "Reading (Memory): $Path ..."
if (-not (Test-Path $Path)) {
Write-Error "File not found: $Path"
return
}
$sw = [System.Diagnostics.Stopwatch]::StartNew()
# 1. Read Raw Content (Fastest single-threaded read)
$fileContent = Get-Content -Path $Path -Raw
Write-Host "File read in $($sw.Elapsed.TotalSeconds.ToString("N2"))s. Splitting..."
# 2. Split into Blocks
# Delimiter: Date at start of line
$delimiter = '(?m)(?=^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{4,})'
$logBlocks = $fileContent -split $delimiter | Where-Object {
-not [string]::IsNullOrWhiteSpace($_) -and
($_ -match '^\d{4}-\d{2}-\d{2}T')
}
$total = $logBlocks.Count
Write-Host "Processing $($total) blocks..."
# 3. Regex Definitions
# Header
$rxHeader = [System.Text.RegularExpressions.Regex]::new(
'^(?<Timestamp>\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{4,})\s+\((?<ProcessID>\d+):(?<SessionID>[0-9A-F]+)\)\s+(?<Action>\S+)',
[System.Text.RegularExpressions.RegexOptions]::Multiline -bor [System.Text.RegularExpressions.RegexOptions]::Compiled
)
# DB (Include IP/Port)
$rxDb = [System.Text.RegularExpressions.Regex]::new(
'^\s+(?<DatabasePath>.+?\.FDB)\s+\(ATT_(?<AttachID>\d+),\s+(?<User>.+?:NONE),\s+(?<Encoding>[^,]+),\s+(?<ProtocolInfo>(?<Protocol>TCPv[46]):(?<IPAddress>[^/]+)/(?<Port>\d+))\)',
[System.Text.RegularExpressions.RegexOptions]::Multiline -bor [System.Text.RegularExpressions.RegexOptions]::Compiled
)
# App (Exclude ATT match)
$rxApp = [System.Text.RegularExpressions.Regex]::new(
'(?im)^\s+(?!.*\(ATT_)(?<ApplicationPath>(?:[a-z]:|\\\\).+?):(?<ApplicationPID>\d+)\s*$',
[System.Text.RegularExpressions.RegexOptions]::Multiline -bor [System.Text.RegularExpressions.RegexOptions]::Compiled
)
# Transaction (TRA_ID, Init, Options)
$rxTx = [System.Text.RegularExpressions.Regex]::new(
'^\s+\(TRA_(?<TransactionID>\d+)(?:,\s+INIT_(?<InitID>\d+))?,\s+(?<TxOpts>.+?)\)',
[System.Text.RegularExpressions.RegexOptions]::Multiline -bor [System.Text.RegularExpressions.RegexOptions]::Compiled
)
# SQL Statement
# Stops at: ^^^^, PLAN (, paramX, records fetched, or X ms
$rxSql = [System.Text.RegularExpressions.Regex]::new(
'Statement\s+\d+:\s*[\r\n]+(?:-{3,}[\r\n]+)(?<SqlStatement>.+?)(?=(?m)[\r\n]+\s*(\^{4,}|PLAN \(|param\d+|[0-9]+\s+records? fetched|\d+\s+ms|Table\s+Natural))',
[System.Text.RegularExpressions.RegexOptions]::Singleline -bor [System.Text.RegularExpressions.RegexOptions]::Compiled
)
# SQL Plan (PLAN ...)
$rxPlan = [System.Text.RegularExpressions.Regex]::new(
'(?<SqlPlan>(?:^\s*PLAN\b.*[\r\n]?)+)',
[System.Text.RegularExpressions.RegexOptions]::Multiline -bor [System.Text.RegularExpressions.RegexOptions]::Compiled
)
# Table Stats (Treat as part of Plan or separate)
# Matches "Table ... Natural ... ******* ... rows"
$rxTableStats = [System.Text.RegularExpressions.Regex]::new(
'(?<TableStats>Table\s+Natural.+?[\r\n]+\*+[\r\n]+(?:.+[\r\n]?)+)',
[System.Text.RegularExpressions.RegexOptions]::Singleline -bor [System.Text.RegularExpressions.RegexOptions]::Compiled
)
# Fixed-Width data parsing logic is separate, but regex is needed for detection.
# SQL Params (param0 = ...)
$rxParams = [System.Text.RegularExpressions.Regex]::new(
'(?<Params>(?:^\s*param\d+\s*=\s*.+[\r\n]?)+)',
[System.Text.RegularExpressions.RegexOptions]::Multiline -bor [System.Text.RegularExpressions.RegexOptions]::Compiled
)
# Performance
$rxPerf = [System.Text.RegularExpressions.Regex]::new(
'^\s+(?<DurationMs>\d+)\s+ms(?:,\s+(?<Reads>\d+)\s+read\(s\))?(?:,\s+(?<Writes>\d+)\s+write\(s\))?(?:,\s+(?<Fetches>\d+)\s+fetch\(es\))?(?:,\s+(?<Marks>\d+)\s+mark\(s\))?',
[System.Text.RegularExpressions.RegexOptions]::Multiline -bor [System.Text.RegularExpressions.RegexOptions]::Compiled
)
# 4. Processing Loop
$results = [System.Collections.Generic.List[PSObject]]::new($total)
$i = 0
foreach ($block in $logBlocks) {
$i++
if ($i % 5000 -eq 0) { Write-Progress -Activity "Parsing Fast..." -Status "$i / $total" -PercentComplete (($i / $total) * 100) }
# Init Object
$entry = [ordered]@{
Timestamp = $null; Action = $null; ProcessID = $null; SessionID = $null;
DatabasePath = $null; AttachID = $null; User = $null; ProtocolInfo = $null;
ClientIP = $null; ClientPort = $null;
ApplicationPath = $null; ApplicationPID = $null;
TransactionID = $null; RootTxID = "NoTx"; TransactionOptions = $null;
Params = $null; # SQL Params
SqlStatement = $null; SqlPlan = $null;
TableStats = $null; # New field for Table Statistics
DurationMs = 0; Reads = 0; Writes = 0; Fetches = 0; Marks = 0;
}
# --- Header ---
$m = $rxHeader.Match($block)
if ($m.Success) {
$entry.Timestamp = $m.Groups['Timestamp'].Value
$entry.Action = $m.Groups['Action'].Value
$entry.ProcessID = $m.Groups['ProcessID'].Value
$entry.SessionID = $m.Groups['SessionID'].Value
}
# --- DB ---
$m = $rxDb.Match($block)
if ($m.Success) {
$entry.DatabasePath = $m.Groups['DatabasePath'].Value
$entry.AttachID = $m.Groups['AttachID'].Value
$entry.User = $m.Groups['User'].Value
$entry.ProtocolInfo = $m.Groups['ProtocolInfo'].Value
$entry.ClientIP = $m.Groups['IPAddress'].Value
$entry.ClientPort = $m.Groups['Port'].Value
}
# --- App ---
$m = $rxApp.Match($block)
if ($m.Success) {
$entry.ApplicationPath = $m.Groups['ApplicationPath'].Value
$entry.ApplicationPID = $m.Groups['ApplicationPID'].Value
}
# --- Tx ---
$m = $rxTx.Match($block)
if ($m.Success) {
$entry.TransactionID = $m.Groups['TransactionID'].Value
$initID = $m.Groups['InitID'].Value
$entry.TransactionOptions = $m.Groups['TxOpts'].Value
if (-not [string]::IsNullOrWhiteSpace($initID)) { $entry.RootTxID = $initID }
elseif (-not [string]::IsNullOrWhiteSpace($entry.TransactionID)) { $entry.RootTxID = $entry.TransactionID }
}
# --- SQL ---
$m = $rxSql.Match($block)
if ($m.Success) {
# Trim whitespace AND separator lines like --- if they slipped in
$entry.SqlStatement = $m.Groups['SqlStatement'].Value.Trim()
}
# --- Params ---
$m = $rxParams.Match($block)
if ($m.Success) {
$entry.Params = $m.Groups['Params'].Value.Trim()
}
# --- Plan ---
$m = $rxPlan.Match($block)
if ($m.Success) {
$entry.SqlPlan = $m.Groups['SqlPlan'].Value.Trim()
}
# --- Table Stats (Separate Column) ---
$m = $rxTableStats.Match($block)
if ($m.Success) {
$entry.TableStats = $m.Groups['TableStats'].Value # NO TRIM to preserve column alignment
}
# --- Perf ---
$m = $rxPerf.Match($block)
if ($m.Success) {
$entry.DurationMs = [int]$m.Groups['DurationMs'].Value
if ($m.Groups['Reads'].Success) { $entry.Reads = [int]$m.Groups['Reads'].Value }
if ($m.Groups['Writes'].Success) { $entry.Writes = [int]$m.Groups['Writes'].Value }
if ($m.Groups['Fetches'].Success) { $entry.Fetches = [int]$m.Groups['Fetches'].Value }
if ($m.Groups['Marks'].Success) { $entry.Marks = [int]$m.Groups['Marks'].Value }
}
$results.Add([PSCustomObject]$entry)
}
$sw.Stop()
Write-Progress -Activity "Parsing Fast..." -Completed
Write-Host "--- Processing Complete ---"
Write-Host "Parsed $($results.Count) records."
Write-Host "Duration: $($sw.Elapsed.TotalSeconds.ToString("N2")) seconds."
return $results