feat: optimize dump methods to use bufio.Writer for improved performance

This commit is contained in:
SunBK201 2025-12-09 13:30:15 +08:00
parent beaaf678e3
commit 8132085c8c
3 changed files with 54 additions and 29 deletions

View File

@ -1,6 +1,7 @@
package statistics
import (
"bufio"
"fmt"
"log/slog"
"os"
@ -96,24 +97,31 @@ func (l *ConnectionRecordList) Dump() {
}()
l.mu.RLock()
var statList []ConnectionRecord
for _, record := range l.records {
statList = append(statList, *record)
records := make([]*ConnectionRecord, 0, len(l.records))
for _, r := range l.records {
records = append(records, r)
}
l.mu.RUnlock()
// Sort by start time (newest first)
sort.SliceStable(statList, func(i, j int) bool {
return statList[i].StartTime.After(statList[j].StartTime)
sort.SliceStable(records, func(i, j int) bool {
return records[i].StartTime.After(records[j].StartTime)
})
for _, record := range statList {
duration := time.Since(record.StartTime)
line := fmt.Sprintf("%s %s %s %d\n",
w := bufio.NewWriter(f)
defer func() {
if err := w.Flush(); err != nil {
slog.Error("bufio.Writer.Flush", slog.Any("error", err))
}
}()
now := time.Now()
for _, record := range records {
duration := now.Sub(record.StartTime)
_, err := fmt.Fprintf(w, "%s %s %s %d\n",
record.Protocol, record.SrcAddr, record.DestAddr, int(duration.Seconds()))
if _, err := f.WriteString(line); err != nil {
slog.Error("os.File.WriteString", slog.Any("error", err))
return
if err != nil {
slog.Error("Dump fmt.Fprintf", slog.Any("error", err))
}
}
}

View File

@ -1,6 +1,7 @@
package statistics
import (
"bufio"
"fmt"
"log/slog"
"os"
@ -84,21 +85,28 @@ func (l *PassThroughRecordList) Dump() {
}()
l.mu.RLock()
var statList []PassThroughRecord
for _, record := range l.records {
statList = append(statList, *record)
records := make([]*PassThroughRecord, 0, len(l.records))
for _, r := range l.records {
records = append(records, r)
}
l.mu.RUnlock()
sort.SliceStable(statList, func(i, j int) bool {
return statList[i].Count > statList[j].Count
sort.SliceStable(records, func(i, j int) bool {
return records[i].Count > records[j].Count
})
for _, record := range statList {
line := fmt.Sprintf("%s %s %d %s\n", record.SrcAddr, record.DestAddr, record.Count, record.UA)
if _, err := f.WriteString(line); err != nil {
slog.Error("os.File.WriteString", slog.Any("error", err))
return
w := bufio.NewWriter(f)
defer func() {
if err := w.Flush(); err != nil {
slog.Error("bufio.Writer.Flush", slog.Any("error", err))
}
}()
for _, record := range records {
_, err := fmt.Fprintf(w, "%s %s %d %s\n",
record.SrcAddr, record.DestAddr, record.Count, record.UA)
if err != nil {
slog.Error("Dump fmt.Fprintf", slog.Any("error", err))
}
}
}

View File

@ -1,6 +1,7 @@
package statistics
import (
"bufio"
"fmt"
"log/slog"
"os"
@ -79,20 +80,28 @@ func (l *RewriteRecordList) Dump() {
}()
l.mu.RLock()
var statList []RewriteRecord
records := make([]*RewriteRecord, 0, len(l.records))
for _, record := range l.records {
statList = append(statList, *record)
records = append(records, record)
}
l.mu.RUnlock()
sort.SliceStable(statList, func(i, j int) bool {
return statList[i].Count > statList[j].Count
sort.SliceStable(records, func(i, j int) bool {
return records[i].Count > records[j].Count
})
for _, record := range statList {
line := fmt.Sprintf("%s %d %sSEQSEQ%s\n", record.Host, record.Count, record.OriginalUA, record.MockedUA)
if _, err := f.WriteString(line); err != nil {
slog.Error("os.File.WriteString", slog.Any("error", err))
w := bufio.NewWriter(f)
defer func() {
if err := w.Flush(); err != nil {
slog.Error("bufio.Writer.Flush", slog.Any("error", err))
}
}()
for _, record := range records {
_, err := fmt.Fprintf(w, "%s %d %sSEQSEQ%s\n",
record.Host, record.Count, record.OriginalUA, record.MockedUA)
if err != nil {
slog.Error("Dump fmt.Fprintf", slog.Any("error", err))
}
}
}