chore: simple traffic logger (#2378)

* feat: simple traffic logger

Controls: 
```
DEBUG TRAFFIC <base_path> | [STOP]
```
---------

Signed-off-by: Vladislav Oleshko <vlad@dragonflydb.io>
Signed-off-by: Roman Gershman <roman@dragonflydb.io>
Co-authored-by: Roman Gershman <roman@dragonflydb.io>
This commit is contained in:
Vladislav 2024-01-10 15:56:56 +03:00 committed by GitHub
parent e9453e62e4
commit f4ea42f2f6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 563 additions and 2 deletions

80
tools/replay/parsing.go Normal file
View file

@ -0,0 +1,80 @@
package main
import (
"bufio"
"encoding/binary"
"io"
"os"
)
var kBigEmptyBytes = make([]byte, 100_000)
func parseStrings(file io.Reader) (out []interface{}, err error) {
var num, strLen uint32
err = binary.Read(file, binary.LittleEndian, &num)
if err != nil {
return nil, err
}
out = make([]interface{}, num)
for i := range out {
err = binary.Read(file, binary.LittleEndian, &strLen)
if err != nil {
return nil, err
}
out[i] = strLen
}
for i := range out {
strLen = out[i].(uint32)
if strLen == 0 {
err = binary.Read(file, binary.LittleEndian, &strLen)
if err != nil {
return nil, err
}
out[i] = kBigEmptyBytes[:strLen]
continue
}
buf := make([]byte, strLen)
_, err := io.ReadFull(file, buf)
if err != nil {
return nil, err
}
out[i] = string(buf)
}
return
}
func parseRecords(filename string, cb func(Record) bool) error {
file, err := os.Open(filename)
if err != nil {
return err
}
defer file.Close()
reader := bufio.NewReader(file)
for {
var rec Record
err := binary.Read(reader, binary.LittleEndian, &rec.RecordHeader)
if err != nil {
if err == io.EOF {
break
}
return err
}
rec.values, err = parseStrings(reader)
if err != nil {
return err
}
if !cb(rec) {
return nil
}
}
return nil
}