forked from kata-containers/tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
97 lines (79 loc) · 2.2 KB
/
json.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright (c) 2018 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
package main
import (
"bufio"
"bytes"
"io"
"os/exec"
"strconv"
log "github.com/sirupsen/logrus"
)
// jsonRecord has no data - the data is loaded and processed and stored
// back into the metrics structure passed in.
type jsonRecord struct {
}
// load reads in a JSON 'Metrics' results file from the file path given
// Parse out the actual results data using the 'jq' query found in the
// respective TOML entry.
func (c *jsonRecord) load(filepath string, metric *metrics) error {
var err error
log.Debugf("in json load of [%s]", filepath)
log.Debugf(" Run jq '%v' %s", metric.CheckVar, filepath)
out, err := exec.Command("jq", metric.CheckVar, filepath).Output()
if err != nil {
log.Warnf("Failed to run [jq %v %v][%v]", metric.CheckVar, filepath, err)
return err
}
log.Debugf(" Got result [%v]", out)
// Try to parse the results as floats first...
floats, err := readFloats(bytes.NewReader(out))
if err != nil {
// And if they are not floats, check if they are ints...
ints, err := readInts(bytes.NewReader(out))
if err != nil {
log.Warnf("Failed to decode [%v]", out)
return err
}
// Always store the internal data as floats
floats = []float64{}
for _, i := range ints {
floats = append(floats, float64(i))
}
}
log.Debugf(" and got output [%v]", floats)
// Store the results back 'up'
metric.stats.Results = floats
// And do the stats on them
metric.calculate()
return nil
}
// Parse a string of ascii ints into a slice of ints
func readInts(r io.Reader) ([]int, error) {
scanner := bufio.NewScanner(r)
scanner.Split(bufio.ScanWords)
var result []int
for scanner.Scan() {
i, err := strconv.Atoi(scanner.Text())
if err != nil {
return result, err
}
result = append(result, i)
}
return result, scanner.Err()
}
// Parse a string of ascii floats into a slice of floats
func readFloats(r io.Reader) ([]float64, error) {
scanner := bufio.NewScanner(r)
scanner.Split(bufio.ScanWords)
var result []float64
for scanner.Scan() {
f, err := strconv.ParseFloat(scanner.Text(), 64)
if err != nil {
return result, err
}
result = append(result, f)
}
return result, scanner.Err()
}