-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser_test.go
More file actions
179 lines (144 loc) · 4.4 KB
/
parser_test.go
File metadata and controls
179 lines (144 loc) · 4.4 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
package main
import (
"regexp"
"strconv"
"strings"
"testing"
)
const sampleWALLine = `rmgr: Heap len (rec/tot): 54/ 54, tx: 1234, lsn: 0/01531F38, prev 0/01531F00, desc: INSERT off 8 flags 0x00, blkref #0: rel 1663/12345/16384 blk 42`
// OldParser is the regex-based parser for benchmarking comparison
type OldParser struct {
lsnPattern *regexp.Regexp
descPattern *regexp.Regexp
relPattern *regexp.Regexp
blkPattern *regexp.Regexp
}
func NewOldParser() *OldParser {
return &OldParser{
lsnPattern: regexp.MustCompile(`rmgr:\s+(\S+)\s+len\s+\(rec/tot\):\s*(\d+)/\s*(\d+),\s+tx:\s*(\d+),\s+lsn:\s+([0-9A-F/]+),\s+prev\s+([0-9A-F/]+)`),
descPattern: regexp.MustCompile(`desc:\s+(.+)`),
relPattern: regexp.MustCompile(`rel\s+(\d+)/(\d+)/(\d+)(?:\s+(\w+))?`),
blkPattern: regexp.MustCompile(`blk\s+(\d+)`),
}
}
func (p *OldParser) ParseLine(line string) (*WALRecord, error) {
if strings.TrimSpace(line) == "" {
return nil, nil
}
matches := p.lsnPattern.FindStringSubmatch(line)
if matches == nil {
return nil, nil
}
recLen, _ := strconv.Atoi(matches[2])
totLen, _ := strconv.Atoi(matches[3])
xid, _ := strconv.ParseInt(matches[4], 10, 64)
record := &WALRecord{
ResourceMgr: matches[1],
Length: recLen,
TotalLength: totLen,
XID: xid,
LSN: matches[5],
PrevLSN: matches[6],
}
descMatches := p.descPattern.FindStringSubmatch(line)
if descMatches != nil {
record.Description = descMatches[1]
parts := strings.Fields(record.Description)
if len(parts) > 0 {
record.RecordType = parts[0]
}
relMatches := p.relPattern.FindStringSubmatch(record.Description)
if relMatches != nil {
if ts, err := strconv.Atoi(relMatches[1]); err == nil {
record.Tablespace = &ts
}
if db, err := strconv.Atoi(relMatches[2]); err == nil {
record.Database = &db
}
if rel, err := strconv.Atoi(relMatches[3]); err == nil {
record.Relfilenode = &rel
}
}
blkMatches := p.blkPattern.FindStringSubmatch(record.Description)
if blkMatches != nil {
if blk, err := strconv.Atoi(blkMatches[1]); err == nil {
record.BlockNumber = &blk
}
}
}
return record, nil
}
func TestParser(t *testing.T) {
parser := NewParser()
record, err := parser.ParseLine(sampleWALLine)
if err != nil {
t.Fatalf("Failed to parse line: %v", err)
}
if record == nil {
t.Fatal("Expected record, got nil")
}
// Verify parsed fields
if record.ResourceMgr != "Heap" {
t.Errorf("Expected ResourceMgr 'Heap', got '%s'", record.ResourceMgr)
}
if record.Length != 54 {
t.Errorf("Expected Length 54, got %d", record.Length)
}
if record.TotalLength != 54 {
t.Errorf("Expected TotalLength 54, got %d", record.TotalLength)
}
if record.XID != 1234 {
t.Errorf("Expected XID 1234, got %d", record.XID)
}
if record.LSN != "0/01531F38" {
t.Errorf("Expected LSN '0/01531F38', got '%s'", record.LSN)
}
if record.PrevLSN != "0/01531F00" {
t.Errorf("Expected PrevLSN '0/01531F00', got '%s'", record.PrevLSN)
}
if record.RecordType != "INSERT" {
t.Errorf("Expected RecordType 'INSERT', got '%s'", record.RecordType)
}
if record.Tablespace == nil || *record.Tablespace != 1663 {
t.Error("Expected Tablespace 1663")
}
if record.Database == nil || *record.Database != 12345 {
t.Error("Expected Database 12345")
}
if record.Relfilenode == nil || *record.Relfilenode != 16384 {
t.Error("Expected Relfilenode 16384")
}
if record.BlockNumber == nil || *record.BlockNumber != 42 {
t.Error("Expected BlockNumber 42")
}
}
func BenchmarkStateMachineParser(b *testing.B) {
parser := NewParser()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = parser.ParseLine(sampleWALLine)
}
}
func BenchmarkRegexParser(b *testing.B) {
parser := NewOldParser()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = parser.ParseLine(sampleWALLine)
}
}
func BenchmarkStateMachineParserNoRelation(b *testing.B) {
line := `rmgr: Heap len (rec/tot): 54/ 54, tx: 1234, lsn: 0/01531F38, prev 0/01531F00, desc: INSERT off 8 flags 0x00`
parser := NewParser()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = parser.ParseLine(line)
}
}
func BenchmarkRegexParserNoRelation(b *testing.B) {
line := `rmgr: Heap len (rec/tot): 54/ 54, tx: 1234, lsn: 0/01531F38, prev 0/01531F00, desc: INSERT off 8 flags 0x00`
parser := NewOldParser()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = parser.ParseLine(line)
}
}