backtest: avoid writing same record into the file

This commit is contained in:
c9s 2022-07-12 18:46:09 +08:00
parent e4bfc2bc52
commit 24e009f333
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -27,6 +27,7 @@ type StateRecorder struct {
outputDirectory string outputDirectory string
strategies []Instance strategies []Instance
writers map[types.CsvFormatter]*tsv.Writer writers map[types.CsvFormatter]*tsv.Writer
lastLines map[types.CsvFormatter][]string
manifests Manifests manifests Manifests
} }
@ -34,6 +35,7 @@ func NewStateRecorder(outputDir string) *StateRecorder {
return &StateRecorder{ return &StateRecorder{
outputDirectory: outputDir, outputDirectory: outputDir,
writers: make(map[types.CsvFormatter]*tsv.Writer), writers: make(map[types.CsvFormatter]*tsv.Writer),
lastLines: make(map[types.CsvFormatter][]string),
manifests: make(Manifests), manifests: make(Manifests),
} }
} }
@ -42,11 +44,18 @@ func (r *StateRecorder) Snapshot() (int, error) {
var c int var c int
for obj, writer := range r.writers { for obj, writer := range r.writers {
records := obj.CsvRecords() records := obj.CsvRecords()
lastLine, hasLastLine := r.lastLines[obj]
for _, record := range records { for _, record := range records {
if hasLastLine && equalStringSlice(lastLine, record) {
continue
}
if err := writer.Write(record); err != nil { if err := writer.Write(record); err != nil {
return c, err return c, err
} }
c++ c++
r.lastLines[obj] = record
} }
writer.Flush() writer.Flush()
@ -129,3 +138,19 @@ func (r *StateRecorder) Close() error {
return err return err
} }
func equalStringSlice(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := 0; i < len(a); i++ {
ad := a[i]
bd := b[i]
if ad != bd {
return false
}
}
return true
}