Skip to content
Snippets Groups Projects
benchmark.go 1.17 KiB
Newer Older
  • Learn to ignore specific revisions
  • S.H.'s avatar
    S.H. committed
    package rtdt_benchmark
    
    import (
    	"encoding/json"
    	"fmt"
    	"os"
    	"time"
    )
    
    type Rtdt_Benchmark struct {
    	CtlRcvPathRequest   time.Time
    	CtlRcvCommitRequest time.Time
    	CtlSendGnmi         time.Time
    	CtlRcvGnmi          time.Time
    }
    
    var CurrentBenchmark Rtdt_Benchmark
    
    func AppendToJsonFile(r *Rtdt_Benchmark) {
    	var benchmarks []Rtdt_Benchmark
    	if _, err := os.Stat("/app/gosdn_share/measure_controller.json"); err == nil {
    		data, err := os.ReadFile("/app/gosdn_share/measure_controller.json")
    		if err != nil {
    			fmt.Println("Error reading file:", err)
    			return
    		}
    		if len(data) > 0 {
    			err = json.Unmarshal(data, &benchmarks)
    			if err != nil {
    				fmt.Println("Error unmarshaling JSON:", err)
    				return
    			}
    		}
    	} else if !os.IsNotExist(err) {
    		fmt.Println("Error checking file status:", err)
    		return
    	}
    
    	// Append the new benchmark
    	benchmarks = append(benchmarks, *r)
    
    	// Marshal and write back to file
    	outData, err := json.MarshalIndent(benchmarks, "", "  ")
    	if err != nil {
    		fmt.Println("Error marshaling JSON:", err)
    		return
    	}
    
    	err = os.WriteFile("/app/gosdn_share/measure_controller.json", outData, 0644)
    	if err != nil {
    		fmt.Println("Error writing to file:", err)
    	}
    }