66 lines
1.3 KiB
Go
66 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
type input struct {
|
|
Source json.RawMessage `json:"source"`
|
|
Update bool `json:"update"`
|
|
Ticks string `json:"ticks"`
|
|
}
|
|
|
|
type sample struct {
|
|
Elapsed int64 `json:"elapsed_ns"`
|
|
Checksum int `json:"checksum"`
|
|
Output string `json:"output"`
|
|
}
|
|
|
|
func transform(source []byte, update bool) []byte {
|
|
decoder := json.NewDecoder(bytes.NewReader(source))
|
|
decoder.UseNumber()
|
|
var object map[string]any
|
|
if err := decoder.Decode(&object); err != nil {
|
|
panic(err)
|
|
}
|
|
var selected any = object["data"]
|
|
if update {
|
|
object["active"] = true
|
|
selected = object
|
|
}
|
|
output, err := json.Marshal(selected)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return output
|
|
}
|
|
|
|
func main() {
|
|
var arguments input
|
|
if len(os.Args) != 2 {
|
|
panic("expected one JSON input")
|
|
}
|
|
if err := json.Unmarshal([]byte(os.Args[1]), &arguments); err != nil {
|
|
panic(err)
|
|
}
|
|
if !json.Valid(arguments.Source) {
|
|
panic("invalid source")
|
|
}
|
|
output := transform(arguments.Source, arguments.Update)
|
|
checksum := 0
|
|
begin := time.Now()
|
|
for range []byte(arguments.Ticks) {
|
|
checksum += len(transform(arguments.Source, arguments.Update))
|
|
}
|
|
result := sample{time.Since(begin).Nanoseconds(), checksum, string(output)}
|
|
encoded, err := json.Marshal(result)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
fmt.Println(string(encoded))
|
|
}
|