-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathreadmetrics.go
327 lines (275 loc) · 9.77 KB
/
readmetrics.go
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package readmetrics
import (
"bufio"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"time"
)
// LogEntry represents the structure of a log entry parsed from the raw log.
type LogEntry struct {
MessageType string `json:"message_type"` // Type of message (e.g., "D", "8").
Timestamp string `json:"timestamp"` // Timestamp of the log entry.
Fields map[string]string `json:"fields"` // Additional fields in the log.
}
// LogMetricsEntry stores parsed information for latency and throughput calculations.
type LogMetricsEntry struct {
timestamp time.Time // Timestamp of the message.
msgType string // Type of message (e.g., "D", "8").
clOrdID string // Client Order ID.
}
// Execute processes the log file, calculates metrics, and saves them to output files.
func Execute(logFilePath, outputFilePath, tmpDir string) error {
// Get the current working directory
dir, err := os.Getwd()
if err != nil {
return fmt.Errorf("error getting working directory: %v", err)
}
// Open the log file
logFile, err := os.Open(filepath.Join(dir, logFilePath))
if err != nil {
return fmt.Errorf("error opening log file: %v", err)
}
defer logFile.Close()
// Prepare a scanner to read the log file line by line
scanner := bufio.NewScanner(logFile)
entries := make([]LogEntry, 0)
// Read each line in the log file and parse relevant entries
for scanner.Scan() {
line := scanner.Text()
// Filter lines that are message type "D" or "8"
if strings.Contains(line, "35=D") || strings.Contains(line, "35=8") {
entry := LogEntry{
Fields: make(map[string]string),
}
// Split the line by spaces and process the parts
parts := strings.Split(line, " ")
if len(parts) > 2 {
// Extract message type and timestamp
entry.MessageType = strings.Split(parts[2], "\u0001")[0]
entry.Timestamp = parts[1]
// Extract fields (key-value pairs) from the log
for _, part := range parts {
if strings.Contains(part, "=") {
keyValue := strings.SplitN(part, "=", 2)
if len(keyValue) == 2 {
entry.Fields[keyValue[0]] = keyValue[1]
}
}
}
}
entries = append(entries, entry) // Add the entry to the list
}
}
if err := scanner.Err(); err != nil {
return fmt.Errorf("error reading log file: %v", err)
}
// Save the parsed entries to a JSON file
if err := saveToJSON(entries, outputFilePath); err != nil {
return fmt.Errorf("error saving to JSON: %v", err)
}
// Calculate latencies and save them
if err := CalculateLatenciesToFile(logFilePath, tmpDir); err != nil {
return fmt.Errorf("error calculating latencies: %v", err)
}
// Calculate success rates for orders
filledCount, newOrderCount, successRate, err := countFilledOrders(logFilePath)
if err != nil {
return fmt.Errorf("error calculating success percentages: %v", err)
}
// Write the calculated metrics to file
if err := writeMetricsToFile(tmpDir, filledCount, newOrderCount, successRate); err != nil {
return fmt.Errorf("error writing metrics to file: %v", err)
}
fmt.Printf("Raw Data saved to %s\n", outputFilePath)
return nil
}
// saveToJSON saves the parsed log entries to a JSON file.
func saveToJSON(entries []LogEntry, outputFilePath string) error {
// Marshal entries into JSON format
jsonData, err := json.MarshalIndent(entries, "", " ")
if err != nil {
return fmt.Errorf("error converting to JSON: %v", err)
}
// Get the current working directory to create the output file path
dir, err := os.Getwd()
if err != nil {
return fmt.Errorf("error getting working directory: %v", err)
}
// Create and open the output file
outputFile, err := os.Create(filepath.Join(dir, outputFilePath))
if err != nil {
return fmt.Errorf("error creating output file: %v", err)
}
defer outputFile.Close()
// Write the JSON data to the file
_, err = outputFile.Write(jsonData)
if err != nil {
return fmt.Errorf("error writing to output file: %v", err)
}
return nil
}
// parseFIXMessage parses a single FIX message and returns the relevant data.
func parseFIXMessage(line string) (LogMetricsEntry, error) {
// Split the line by the FIX field delimiter
fields := strings.Split(line, "")
msg := LogMetricsEntry{}
// Parse the timestamp from the first 26 characters of the line
timestampStr := line[:26]
timestamp, err := time.Parse("2006/01/02 15:04:05.000000", timestampStr)
if err != nil {
return msg, err
}
msg.timestamp = timestamp
// Extract message type and client order ID
for _, field := range fields {
if strings.HasPrefix(field, "35=") {
msg.msgType = strings.TrimPrefix(field, "35=")
} else if strings.HasPrefix(field, "11=") {
msg.clOrdID = strings.TrimPrefix(field, "11=")
}
}
return msg, nil
}
// CalculateLatenciesToFile calculates the latencies between orders and saves the results to files.
func CalculateLatenciesToFile(logFilePath, tmpDir string) error {
// Open the log file for reading
file, err := os.Open(logFilePath)
if err != nil {
return fmt.Errorf("error opening file: %v", err)
}
defer file.Close()
// Initialize variables for storing latency data
dMessages := make(map[string]LogMetricsEntry)
latencies := []int64{}
throughputCounts := make(map[time.Time]int)
// Read each line of the log and calculate latencies
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
msg, err := parseFIXMessage(line)
if err != nil {
fmt.Println("Error parsing line:", err)
continue
}
// Track order creation ("D") messages and calculate latency for execution ("8") messages
if msg.msgType == "D" {
dMessages[msg.clOrdID] = msg
minute := msg.timestamp.Truncate(time.Minute)
throughputCounts[minute]++
} else if msg.msgType == "8" && msg.clOrdID != "" {
if dMsg, found := dMessages[msg.clOrdID]; found {
latency := msg.timestamp.Sub(dMsg.timestamp).Milliseconds()
latencies = append(latencies, latency)
delete(dMessages, msg.clOrdID)
}
}
}
// Handle any errors encountered during scanning
if err := scanner.Err(); err != nil {
return fmt.Errorf("error reading file: %v", err)
}
// Save latencies to a file
latencyFile, err := os.Create(filepath.Join(tmpDir, "latencies.txt"))
if err != nil {
return fmt.Errorf("error creating latencies file: %v", err)
}
defer latencyFile.Close()
writer := bufio.NewWriter(latencyFile)
for index, latency := range latencies {
_, err := writer.WriteString(fmt.Sprintf("Latency %d: %d ms\n", index+1, latency))
if err != nil {
return fmt.Errorf("error writing to latencies file: %v", err)
}
}
// Calculate average latency
averageLatency := float64(0)
if len(latencies) > 0 {
for _, latency := range latencies {
averageLatency += float64(latency)
}
averageLatency /= float64(len(latencies))
}
// Save the metrics (average latency, throughput) to file
metricsFile, err := os.Create(filepath.Join(tmpDir, "metrics.txt"))
if err != nil {
return fmt.Errorf("error creating metrics file: %v", err)
}
defer metricsFile.Close()
metricsWriter := bufio.NewWriter(metricsFile)
_, err = metricsWriter.WriteString(fmt.Sprintf("Average Latency: %.2f ms\n", averageLatency))
if err != nil {
return fmt.Errorf("error writing average latency to metrics file: %v", err)
}
// Write throughput data
for minute, count := range throughputCounts {
throughputStr := fmt.Sprintf("Minute: %s, Throughput: %d orders/min\n", minute.Format("2006-01-02 15:04"), count)
_, err := metricsWriter.WriteString(throughputStr)
if err != nil {
return fmt.Errorf("error writing throughput to metrics file: %v", err)
}
}
writer.Flush()
metricsWriter.Flush()
return nil
}
// countFilledOrders counts the number of filled and new orders and calculates the success rate.
func countFilledOrders(logFilePath string) (int, int, float64, error) {
// Open the log file for scanning
file, err := os.Open(logFilePath)
if err != nil {
return 0, 0, 0, fmt.Errorf("failed to open log file: %v", err)
}
defer file.Close()
var filledCount, newOrderCount int
// Scan the file line by line
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
// Count new orders (type "D")
if strings.Contains(line, "35=D") {
newOrderCount++
}
// Count filled orders (150=F)
if strings.Contains(line, "150=F") {
filledCount++
}
}
// Handle any errors encountered during scanning
if err := scanner.Err(); err != nil {
return 0, 0, 0, fmt.Errorf("failed to scan log file: %v", err)
}
// Calculate the success rate
var successRate float64
if newOrderCount > 0 {
successRate = float64(filledCount) / float64(newOrderCount) * 100
}
return filledCount, newOrderCount, successRate, nil
}
// writeMetricsToFile appends metrics data (new orders, filled orders, success rate) to a file.
func writeMetricsToFile(tmpDir string, filledCount, newOrderCount int, successRate float64) error {
// Open the metrics file for appending
metricsFile, err := os.OpenFile(filepath.Join(tmpDir, "metrics.txt"), os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf("error opening metrics file: %v", err)
}
defer metricsFile.Close()
metricsWriter := bufio.NewWriter(metricsFile)
// Write the metrics data (new orders, filled orders, success rate) to the file
_, err = metricsWriter.WriteString(fmt.Sprintf("Total New Orders: %v\n", newOrderCount))
if err != nil {
return fmt.Errorf("error writing new orders count to metrics file: %v", err)
}
_, err = metricsWriter.WriteString(fmt.Sprintf("Total Orders Successfully Filled: %v\n", filledCount))
if err != nil {
return fmt.Errorf("error writing filled orders count to metrics file: %v", err)
}
_, err = metricsWriter.WriteString(fmt.Sprintf("Success Rate: %.2f%%\n", successRate))
if err != nil {
return fmt.Errorf("error writing success rate to metrics file: %v", err)
}
// Flush the buffered writer to ensure data is written to file
return metricsWriter.Flush()
}