flightlesssomething/benchmark_data.go

428 lines
12 KiB
Go
Raw Normal View History

2024-07-11 18:33:15 +02:00
package flightlesssomething
import (
"archive/zip"
2024-07-11 18:33:15 +02:00
"bufio"
"bytes"
"encoding/csv"
2024-07-11 18:33:15 +02:00
"encoding/gob"
"errors"
"fmt"
2024-07-12 15:53:20 +02:00
"math"
2024-07-11 18:33:15 +02:00
"math/big"
"mime/multipart"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/dustin/go-humanize"
"github.com/klauspost/compress/zstd"
)
type BenchmarkData struct {
Label string
// Specs
SpecOS string
SpecGPU string
SpecCPU string
SpecRAM string
SpecLinuxKernel string
SpecLinuxScheduler string
// Data
DataFPS []float64
DataFrameTime []float64
DataCPULoad []float64
DataGPULoad []float64
DataCPUTemp []float64
DataGPUTemp []float64
DataGPUCoreClock []float64
DataGPUMemClock []float64
DataGPUVRAMUsed []float64
DataGPUPower []float64
DataRAMUsed []float64
DataSwapUsed []float64
}
const (
FileTypeUnknown = iota
FileTypeMangoHud
FileTypeAfterburner
)
2024-07-11 18:33:15 +02:00
func parseHeader(scanner *bufio.Scanner) (map[string]int, error) {
if !scanner.Scan() {
return nil, errors.New("failed to read file (header)")
}
line := strings.TrimRight(scanner.Text(), ", ")
line = strings.TrimSpace(line)
2024-07-11 18:33:15 +02:00
headerMap := make(map[string]int)
for i, field := range strings.Split(line, ",") {
headerMap[strings.TrimSpace(field)] = i
}
return headerMap, nil
}
2024-07-11 18:33:15 +02:00
func parseData(scanner *bufio.Scanner, headerMap map[string]int, benchmarkData *BenchmarkData, isAfterburner bool) error {
var counter uint
for scanner.Scan() {
record := strings.Split(scanner.Text(), ",")
if len(record) < len(headerMap) {
return errors.New("failed to read file (data)")
}
2024-07-11 18:33:15 +02:00
// Trim all values
for i := 0; i < len(record); i++ {
record[i] = strings.TrimSpace(record[i])
2024-07-11 18:33:15 +02:00
}
for key, index := range headerMap {
2024-07-14 18:46:49 +02:00
// Skip timestamp fields for Afterburner format
2024-07-14 18:55:25 +02:00
if isAfterburner && (index == 0 || index == 1) {
2024-07-14 18:46:49 +02:00
continue
}
val, err := strconv.ParseFloat(record[index], 64)
if err != nil {
return fmt.Errorf("failed to parse %s value '%s': %v", key, record[index], err)
}
2024-07-11 18:33:15 +02:00
switch key {
case "fps", "Framerate":
benchmarkData.DataFPS = append(benchmarkData.DataFPS, val)
case "frametime", "Frametime":
benchmarkData.DataFrameTime = append(benchmarkData.DataFrameTime, val)
case "cpu_load", "CPU usage":
benchmarkData.DataCPULoad = append(benchmarkData.DataCPULoad, val)
case "gpu_load", "GPU usage":
benchmarkData.DataGPULoad = append(benchmarkData.DataGPULoad, val)
case "cpu_temp", "CPU temperature":
benchmarkData.DataCPUTemp = append(benchmarkData.DataCPUTemp, val)
case "gpu_temp", "GPU temperature":
benchmarkData.DataGPUTemp = append(benchmarkData.DataGPUTemp, val)
case "gpu_core_clock", "Core clock":
benchmarkData.DataGPUCoreClock = append(benchmarkData.DataGPUCoreClock, val)
case "gpu_mem_clock", "Memory clock":
if isAfterburner {
val = math.Round(val/2*100000) / 100000 // divide by 2 and round to 5 decimal places
}
benchmarkData.DataGPUMemClock = append(benchmarkData.DataGPUMemClock, val)
case "gpu_vram_used", "Memory usage":
if isAfterburner {
val = math.Round(val/1024*100000) / 100000 // divide by 1024 and round to 5 decimal places
}
benchmarkData.DataGPUVRAMUsed = append(benchmarkData.DataGPUVRAMUsed, val)
case "gpu_power", "Power":
benchmarkData.DataGPUPower = append(benchmarkData.DataGPUPower, val)
case "ram_used", "RAM usage":
if isAfterburner {
val = math.Round(val/1024*100000) / 100000 // divide by 1024 and round to 5 decimal places
}
benchmarkData.DataRAMUsed = append(benchmarkData.DataRAMUsed, val)
case "swap_used":
benchmarkData.DataSwapUsed = append(benchmarkData.DataSwapUsed, val)
}
2024-07-11 18:33:15 +02:00
}
counter++
if counter == 100000 {
return errors.New("file cannot have more than 100000 data lines")
2024-07-11 18:33:15 +02:00
}
}
if err := scanner.Err(); err != nil {
return err
}
2024-07-11 18:33:15 +02:00
if len(benchmarkData.DataFPS) == 0 &&
len(benchmarkData.DataFrameTime) == 0 &&
len(benchmarkData.DataCPULoad) == 0 &&
len(benchmarkData.DataGPULoad) == 0 &&
len(benchmarkData.DataCPUTemp) == 0 &&
len(benchmarkData.DataGPUTemp) == 0 &&
len(benchmarkData.DataGPUCoreClock) == 0 &&
len(benchmarkData.DataGPUMemClock) == 0 &&
len(benchmarkData.DataGPUVRAMUsed) == 0 &&
len(benchmarkData.DataGPUPower) == 0 &&
len(benchmarkData.DataRAMUsed) == 0 &&
len(benchmarkData.DataSwapUsed) == 0 {
return errors.New("empty file")
2024-07-12 12:07:55 +02:00
}
return nil
2024-07-12 12:07:55 +02:00
}
func readBenchmarkFile(scanner *bufio.Scanner, fileType int) (*BenchmarkData, error) {
2024-07-12 12:07:55 +02:00
benchmarkData := &BenchmarkData{}
// Second line should contain specs
2024-07-12 12:07:55 +02:00
if !scanner.Scan() {
return nil, errors.New("failed to read file (err 1)")
2024-07-12 12:07:55 +02:00
}
record := strings.Split(scanner.Text(), ",")
switch fileType {
case FileTypeAfterburner:
if len(record) < 3 {
return nil, errors.New("failed to read file (err 2)")
}
benchmarkData.SpecOS = "Windows" // Hardcode
benchmarkData.SpecGPU = truncateString(strings.TrimSpace(record[2]))
case FileTypeMangoHud:
for i, v := range record {
switch i {
case 0:
benchmarkData.SpecOS = truncateString(strings.TrimSpace(v))
case 1:
benchmarkData.SpecCPU = truncateString(strings.TrimSpace(v))
case 2:
benchmarkData.SpecGPU = truncateString(strings.TrimSpace(v))
case 3:
kilobytes := new(big.Int)
_, ok := kilobytes.SetString(strings.TrimSpace(v), 10)
if ok {
// Contains number that represents kilobytes
bytes := new(big.Int).Mul(kilobytes, big.NewInt(1024))
benchmarkData.SpecRAM = humanize.Bytes(bytes.Uint64())
} else {
// Contains humanized (or invalid) value, so no conversion needed
benchmarkData.SpecRAM = truncateString(strings.TrimSpace(v))
}
case 4:
benchmarkData.SpecLinuxKernel = truncateString(strings.TrimSpace(v))
case 6:
benchmarkData.SpecLinuxScheduler = truncateString(strings.TrimSpace(v))
}
2024-07-12 12:07:55 +02:00
}
}
headerMap, err := parseHeader(scanner)
if err != nil {
return nil, err
}
2024-07-12 12:07:55 +02:00
if fileType == FileTypeAfterburner {
// Skip len(headerMap) amount of lines as this is not needed
for i := 0; i < len(headerMap); i++ {
if !scanner.Scan() {
return nil, errors.New("failed to read file (err 3)")
2024-07-12 12:07:55 +02:00
}
}
}
2024-07-12 12:07:55 +02:00
err = parseData(scanner, headerMap, benchmarkData, fileType == FileTypeAfterburner)
if err != nil {
return nil, err
}
2024-07-12 12:07:55 +02:00
return benchmarkData, nil
}
2024-07-12 12:07:55 +02:00
func detectFileType(firstLine string) int {
switch {
case firstLine == "os,cpu,gpu,ram,kernel,driver,cpuscheduler":
return FileTypeMangoHud
case strings.Contains(firstLine, ", Hardware monitoring log v"):
return FileTypeAfterburner
default:
return FileTypeUnknown
}
}
2024-07-12 12:07:55 +02:00
func readBenchmarkFiles(files []*multipart.FileHeader) ([]*BenchmarkData, error) {
benchmarkDatas := make([]*BenchmarkData, 0)
2024-07-12 12:07:55 +02:00
for _, fileHeader := range files {
file, err := fileHeader.Open()
if err != nil {
return nil, err
2024-07-12 12:07:55 +02:00
}
defer file.Close()
scanner := bufio.NewScanner(file)
2024-07-12 12:07:55 +02:00
// FirstLine identifies file format
if !scanner.Scan() {
return nil, errors.New("failed to read file (err 1)")
2024-07-12 12:07:55 +02:00
}
firstLine := scanner.Text()
firstLine = strings.TrimRight(firstLine, ", ")
firstLine = strings.TrimSpace(firstLine)
2024-07-12 12:07:55 +02:00
fileType := detectFileType(firstLine)
if fileType == FileTypeUnknown {
return nil, errors.New("unsupported file format")
2024-07-12 12:07:55 +02:00
}
benchmarkData, err := readBenchmarkFile(scanner, fileType)
if err != nil {
return nil, err
2024-07-12 12:07:55 +02:00
}
var suffix string
switch fileType {
case FileTypeMangoHud:
suffix = ".csv"
case FileTypeAfterburner:
suffix = ".hml"
2024-07-12 12:07:55 +02:00
}
benchmarkData.Label = strings.TrimSuffix(fileHeader.Filename, suffix)
benchmarkDatas = append(benchmarkDatas, benchmarkData)
2024-07-11 18:33:15 +02:00
}
return benchmarkDatas, nil
2024-07-11 18:33:15 +02:00
}
// truncateString truncates the input string to a maximum of 100 characters and appends "..." if it exceeds that length.
func truncateString(s string) string {
const maxLength = 100
if len(s) > maxLength {
return s[:maxLength] + "..."
}
return s
}
2024-07-12 12:07:55 +02:00
func storeBenchmarkData(benchmarkData []*BenchmarkData, benchmarkID uint) error {
2024-07-11 18:33:15 +02:00
// Store to disk
filePath := filepath.Join(benchmarksDir, fmt.Sprintf("%d.bin", benchmarkID))
file, err := os.Create(filePath)
if err != nil {
return err
}
defer file.Close()
// Convert to []byte
var buffer bytes.Buffer
gobEncoder := gob.NewEncoder(&buffer)
2024-07-12 12:07:55 +02:00
err = gobEncoder.Encode(benchmarkData)
2024-07-11 18:33:15 +02:00
if err != nil {
return err
}
// Compress and write to file
zstdEncoder, err := zstd.NewWriter(file, zstd.WithEncoderLevel(zstd.SpeedFastest))
if err != nil {
return err
}
defer zstdEncoder.Close()
_, err = zstdEncoder.Write(buffer.Bytes())
return err
}
2024-07-12 12:07:55 +02:00
func retrieveBenchmarkData(benchmarkID uint) (benchmarkData []*BenchmarkData, err error) {
2024-07-11 18:33:15 +02:00
filePath := filepath.Join(benchmarksDir, fmt.Sprintf("%d.bin", benchmarkID))
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()
// Decompress and read from file
zstdDecoder, err := zstd.NewReader(file)
if err != nil {
return nil, err
}
defer zstdDecoder.Close()
var buffer bytes.Buffer
_, err = buffer.ReadFrom(zstdDecoder)
if err != nil {
return nil, err
}
// Decode
gobDecoder := gob.NewDecoder(&buffer)
2024-07-12 12:07:55 +02:00
err = gobDecoder.Decode(&benchmarkData)
return benchmarkData, err
2024-07-11 18:33:15 +02:00
}
func deleteBenchmarkData(benchmarkID uint) error {
filePath := filepath.Join(benchmarksDir, fmt.Sprintf("%d.bin", benchmarkID))
return os.Remove(filePath)
}
func createZipFromBenchmarkData(benchmarkData []*BenchmarkData) (*bytes.Buffer, error) {
// Create a buffer to write our archive to.
buf := new(bytes.Buffer)
zipWriter := zip.NewWriter(buf)
for _, data := range benchmarkData {
// Create a new CSV file in the zip archive.
fileName := fmt.Sprintf("%s.csv", data.Label)
fileWriter, err := zipWriter.Create(fileName)
if err != nil {
return nil, fmt.Errorf("could not create file in zip: %v", err)
}
// Create a CSV writer.
csvWriter := csv.NewWriter(fileWriter)
// Write the header.
header := []string{"os", "cpu", "gpu", "ram", "kernel", "driver", "cpuscheduler"}
specs := []string{data.SpecOS, data.SpecCPU, data.SpecGPU, data.SpecRAM, data.SpecLinuxKernel, "", data.SpecLinuxScheduler}
2024-07-14 18:55:25 +02:00
csvWriter.Write(header)
csvWriter.Write(specs)
2024-07-14 18:55:25 +02:00
// Dynamically build the data header and rows based on available data.
dataHeader := []string{}
dataRows := [][]string{}
addColumn := func(headerName string, data []float64) {
if len(data) > 0 {
dataHeader = append(dataHeader, headerName)
for i := 0; i < len(data); i++ {
if len(dataRows) <= i {
dataRows = append(dataRows, make([]string, len(dataHeader)-1))
}
dataRows[i] = append(dataRows[i], formatFloatOrZero(data, i))
}
}
}
addColumn("fps", data.DataFPS)
addColumn("frametime", data.DataFrameTime)
addColumn("cpu_load", data.DataCPULoad)
addColumn("gpu_load", data.DataGPULoad)
addColumn("cpu_temp", data.DataCPUTemp)
addColumn("gpu_temp", data.DataGPUTemp)
addColumn("gpu_core_clock", data.DataGPUCoreClock)
addColumn("gpu_mem_clock", data.DataGPUMemClock)
addColumn("gpu_vram_used", data.DataGPUVRAMUsed)
addColumn("gpu_power", data.DataGPUPower)
addColumn("ram_used", data.DataRAMUsed)
addColumn("swap_used", data.DataSwapUsed)
// Write the data header.
csvWriter.Write(dataHeader)
// Write the data rows.
2024-07-14 18:55:25 +02:00
for _, row := range dataRows {
csvWriter.Write(row)
}
// Make sure to flush the writer.
csvWriter.Flush()
if err := csvWriter.Error(); err != nil {
return nil, fmt.Errorf("could not write CSV: %v", err)
}
}
// Close the zip writer to flush the buffer.
if err := zipWriter.Close(); err != nil {
return nil, fmt.Errorf("could not close zip writer: %v", err)
}
return buf, nil
}
2024-07-13 00:13:27 +02:00
// Helper function to format float or return "0.0000" if index is out of range.
func formatFloatOrZero(data []float64, index int) string {
if index < len(data) {
return strconv.FormatFloat(data[index], 'f', 4, 64)
}
return "0.0000"
}