Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f569f46499 | ||
|
|
c185289c74 | ||
|
|
34f14e8dc0 |
@@ -15,6 +15,7 @@ To start replaying:
|
|||||||
`BeamNGMockOg replay [--loop] -a 127.0.0.1 -p 4443 -i sunburstManual.bin`
|
`BeamNGMockOg replay [--loop] -a 127.0.0.1 -p 4443 -i sunburstManual.bin`
|
||||||
- `loop` allows the replay functionality to keep replaying the same data file
|
- `loop` allows the replay functionality to keep replaying the same data file
|
||||||
|
|
||||||
|
|
||||||
To start recording:
|
To start recording:
|
||||||
`BeamNGMockOg record -a 127.0.0.1 -p 4443 -o sunburstDCT.bin`
|
`BeamNGMockOg record -a 127.0.0.1 -p 4443 -o sunburstDCT.bin`
|
||||||
|
|
||||||
@@ -22,6 +23,7 @@ To start recording:
|
|||||||
Use `tcpdump` to listen to the socket and check if data is coming through:
|
Use `tcpdump` to listen to the socket and check if data is coming through:
|
||||||
`tcpdump -i any udp port <port> -X`
|
`tcpdump -i any udp port <port> -X`
|
||||||
|
|
||||||
|
|
||||||
### Benchmark
|
### Benchmark
|
||||||
Run with:
|
Run with:
|
||||||
`go test ./mockserver -bench=BenchmarkReplayAsync -benchmem -benchtime=1x -memprofile=mem.pprof`
|
`go test ./mockserver -bench=BenchmarkReplayAsync -benchmem -benchtime=1x -memprofile=mem.pprof`
|
||||||
@@ -33,16 +35,10 @@ Analyze the output with:
|
|||||||
or
|
or
|
||||||
`go tool pprof -http=:8080 mem.pprof`
|
`go tool pprof -http=:8080 mem.pprof`
|
||||||
|
|
||||||
#### Previous results:
|
|
||||||
goos: linux
|
|
||||||
goarch: amd64
|
|
||||||
pkg: github.com/ESilva15/BeamNGMockOg/mockserver
|
|
||||||
cpu: AMD Ryzen 5 5600G with Radeon Graphics
|
|
||||||
BenchmarkReplayAsync-12 1 109433574741 ns/op 4533008 B/op 66059 allocs/op
|
|
||||||
PASS
|
|
||||||
ok github.com/ESilva15/BeamNGMockOg/mockserver 109.438s
|
|
||||||
|
|
||||||
#### Current results
|
#### Previous results:
|
||||||
|
Note: this were made before introducing the real time visualizer
|
||||||
|
```
|
||||||
goos: linux
|
goos: linux
|
||||||
goarch: amd64
|
goarch: amd64
|
||||||
pkg: github.com/ESilva15/BeamNGMockOg/mockserver
|
pkg: github.com/ESilva15/BeamNGMockOg/mockserver
|
||||||
@@ -50,3 +46,16 @@ cpu: AMD Ryzen 5 5600G with Radeon Graphics
|
|||||||
BenchmarkReplayAsync-12 1 109433931841 ns/op 356088 B/op 13272 allocs/op
|
BenchmarkReplayAsync-12 1 109433931841 ns/op 356088 B/op 13272 allocs/op
|
||||||
PASS
|
PASS
|
||||||
ok github.com/ESilva15/BeamNGMockOg/mockserver 109.440s
|
ok github.com/ESilva15/BeamNGMockOg/mockserver 109.440s
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
#### Current results
|
||||||
|
```
|
||||||
|
goos: linux
|
||||||
|
goarch: amd64
|
||||||
|
pkg: github.com/ESilva15/BeamNGMockOg/mockserver
|
||||||
|
cpu: AMD Ryzen 7 5800X3D 8-Core Processor
|
||||||
|
BenchmarkReplayAsync-16 1 109433890158 ns/op 1673128 B/op 112986 allocs/op
|
||||||
|
PASS
|
||||||
|
ok github.com/ESilva15/BeamNGMockOg/mockserver 109.439s
|
||||||
|
```
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ module github.com/ESilva15/BeamNGMockOg
|
|||||||
go 1.23.2
|
go 1.23.2
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/ESilva15/gobngsdk v1.1.1
|
github.com/ESilva15/gobngsdk v1.1.3
|
||||||
github.com/spf13/cobra v1.10.1
|
github.com/spf13/cobra v1.10.1
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
github.com/ESilva15/gobngsdk v1.1.1 h1:MyKUjIqG77lYcUY6cnZpnnxPoxS/FdAYwGoi6InXan0=
|
github.com/ESilva15/gobngsdk v1.1.3 h1:CFaz2KjHKnBLrQuEN1QHOd1761cWM/rmTwLpSLrgbe4=
|
||||||
github.com/ESilva15/gobngsdk v1.1.1/go.mod h1:cKLaZRgM0tGGXDvosaSjHnxeyC5Y6rg7zCLqEUJnOzw=
|
github.com/ESilva15/gobngsdk v1.1.3/go.mod h1:cKLaZRgM0tGGXDvosaSjHnxeyC5Y6rg7zCLqEUJnOzw=
|
||||||
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
|
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
|
||||||
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
||||||
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
||||||
|
|||||||
+17
-12
@@ -1,11 +1,11 @@
|
|||||||
package mockserver
|
package mockserver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -64,10 +64,10 @@ func (r *Recorder) record(ctx context.Context) {
|
|||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return
|
return
|
||||||
case data := <-r.recorderCh:
|
case data := <-r.recorderCh:
|
||||||
r.mut.RLock()
|
r.mut.Lock()
|
||||||
err := binary.Write(r.OutputFile, binary.LittleEndian, data)
|
err := binary.Write(r.OutputFile, binary.LittleEndian, r.SDK.Data)
|
||||||
r.TotalBytes += len(data)
|
r.TotalBytes += len(data)
|
||||||
r.mut.RUnlock()
|
r.mut.Unlock()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// NOTE: find a way of logging this somehow
|
// NOTE: find a way of logging this somehow
|
||||||
@@ -77,26 +77,31 @@ func (r *Recorder) record(ctx context.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *Recorder) view(ctx context.Context) {
|
func (r *Recorder) view(ctx context.Context) {
|
||||||
var s strings.Builder
|
var buf bytes.Buffer
|
||||||
var nBytes int
|
var nBytes int
|
||||||
|
|
||||||
|
buf.Grow(2048)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return
|
return
|
||||||
case viewData := <-r.viewCh:
|
case viewData := <-r.viewCh:
|
||||||
s.Reset()
|
buf.Reset()
|
||||||
fmt.Fprintf(os.Stdout, "\x1b[2J\x1b[H")
|
buf.WriteString("\x1b[2J\x1b[H")
|
||||||
|
fmt.Fprintf(&buf, "\x1b]0;%s - Recording ", ProgramName)
|
||||||
|
stringifyRecordingProgress(&buf, nBytes)
|
||||||
|
fmt.Fprintf(&buf, "\x07")
|
||||||
|
|
||||||
stringifyRecordingProgress(&s, nBytes)
|
stringifyRecordingProgress(&buf, nBytes)
|
||||||
fmt.Fprintf(&s, "\n\n")
|
fmt.Fprintf(&buf, "\n\n")
|
||||||
|
|
||||||
r.viewDataMut.RLock()
|
r.viewDataMut.RLock()
|
||||||
nBytes = viewData.TotalBytes
|
nBytes = viewData.TotalBytes
|
||||||
stringifyOutgaugeData(&s, &r.SDK)
|
stringifyOutgaugeData(&buf, &r.SDK)
|
||||||
r.viewDataMut.RUnlock()
|
r.viewDataMut.RUnlock()
|
||||||
|
|
||||||
fmt.Fprint(os.Stdout, s.String())
|
_, _ = buf.WriteTo(os.Stdout)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -129,7 +134,7 @@ func (r *Recorder) Record(ctx context.Context) error {
|
|||||||
// Dropped the frame!
|
// Dropped the frame!
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send the data to the UDP socket
|
// Write the data to the file
|
||||||
select {
|
select {
|
||||||
case r.recorderCh <- r.SDK.Buffer:
|
case r.recorderCh <- r.SDK.Buffer:
|
||||||
// Sent the data
|
// Sent the data
|
||||||
|
|||||||
+15
-13
@@ -9,7 +9,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
@@ -49,7 +48,7 @@ func NewReplayer(address string, port int, fp string) (*Replayer, error) {
|
|||||||
replayer := &Replayer{
|
replayer := &Replayer{
|
||||||
DataSourcePath: fp,
|
DataSourcePath: fp,
|
||||||
SDK: bngsdk.BeamNGSDK{
|
SDK: bngsdk.BeamNGSDK{
|
||||||
Data: &bngsdk.Outgauge{},
|
Data: bngsdk.Outgauge{},
|
||||||
Buffer: make([]byte, unsafe.Sizeof(bngsdk.Outgauge{})),
|
Buffer: make([]byte, unsafe.Sizeof(bngsdk.Outgauge{})),
|
||||||
},
|
},
|
||||||
Socket: udp,
|
Socket: udp,
|
||||||
@@ -69,33 +68,36 @@ func (r *Replayer) renderToTerminal(ctx context.Context) {
|
|||||||
// return fmt.Errorf("error stating file: %v", err)
|
// return fmt.Errorf("error stating file: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE: temporary until I make a better view
|
var buf bytes.Buffer
|
||||||
var s strings.Builder
|
|
||||||
var bytesReader bytes.Reader
|
var bytesReader bytes.Reader
|
||||||
|
|
||||||
|
buf.Grow(2048)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return
|
return
|
||||||
case data := <-r.dataViewCh:
|
case data := <-r.dataViewCh:
|
||||||
// Reset to the start of the terminal
|
// Reset to the start of the terminal
|
||||||
s.Reset()
|
percent := int(float64(data.SizeRead) / float64(fileInfo.Size()) * 100)
|
||||||
fmt.Fprintf(os.Stdout, "\x1b[2J\x1b[H")
|
|
||||||
|
buf.Reset()
|
||||||
|
buf.WriteString("\x1b[2J\x1b[H")
|
||||||
|
fmt.Fprintf(&buf, "\x1b]0;%s - Replaying %d%%\x07", ProgramName, percent)
|
||||||
|
|
||||||
bytesReader.Reset(data.SDK.Buffer)
|
bytesReader.Reset(data.SDK.Buffer)
|
||||||
err := binary.Read(&bytesReader, binary.LittleEndian, r.SDK.Data)
|
err := binary.Read(&bytesReader, binary.LittleEndian, &r.SDK.Data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(&s, "FAILED TO PARSE DATA\nError: %+v", err)
|
fmt.Fprintf(&buf, "FAILED TO PARSE DATA\nError: %+v", err)
|
||||||
fmt.Fprint(os.Stdout, s.String())
|
_, _ = buf.WriteTo(os.Stdout)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
percent := int(float64(data.SizeRead) / float64(fileInfo.Size()) * 100)
|
fmt.Fprintf(&buf, "Replayed: %d%%\n", percent)
|
||||||
fmt.Fprintf(&s, "Replayed: %d%%\n", percent)
|
|
||||||
|
|
||||||
stringifyOutgaugeData(&s, data.SDK)
|
stringifyOutgaugeData(&buf, data.SDK)
|
||||||
|
|
||||||
fmt.Fprint(os.Stdout, s.String())
|
buf.WriteTo(os.Stdout)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const ProgramName = "TelemetryMockerserver"
|
||||||
|
|
||||||
type Transport interface {
|
type Transport interface {
|
||||||
Send(data []byte) error
|
Send(data []byte) error
|
||||||
Close() error
|
Close() error
|
||||||
|
|||||||
+3
-3
@@ -1,8 +1,8 @@
|
|||||||
package mockserver
|
package mockserver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
|
|
||||||
bngsdk "github.com/ESilva15/gobngsdk"
|
bngsdk "github.com/ESilva15/gobngsdk"
|
||||||
)
|
)
|
||||||
@@ -14,7 +14,7 @@ const (
|
|||||||
GiB
|
GiB
|
||||||
)
|
)
|
||||||
|
|
||||||
func stringifyRecordingProgress(s *strings.Builder, nBytes int) {
|
func stringifyRecordingProgress(s *bytes.Buffer, nBytes int) {
|
||||||
if nBytes < KiB {
|
if nBytes < KiB {
|
||||||
fmt.Fprintf(s, "%d B", nBytes)
|
fmt.Fprintf(s, "%d B", nBytes)
|
||||||
} else if nBytes < MiB {
|
} else if nBytes < MiB {
|
||||||
@@ -26,7 +26,7 @@ func stringifyRecordingProgress(s *strings.Builder, nBytes int) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func stringifyOutgaugeData(s *strings.Builder, sdk *bngsdk.BeamNGSDK) {
|
func stringifyOutgaugeData(s *bytes.Buffer, sdk *bngsdk.BeamNGSDK) {
|
||||||
// NOTE: write a string serialization function on the SDK itself
|
// NOTE: write a string serialization function on the SDK itself
|
||||||
fmt.Fprint(s, "Outgauge {\n")
|
fmt.Fprint(s, "Outgauge {\n")
|
||||||
fmt.Fprintf(s, " Time: %d ms\n", sdk.Data.Time)
|
fmt.Fprintf(s, " Time: %d ms\n", sdk.Data.Time)
|
||||||
|
|||||||
Reference in New Issue
Block a user