perfmonger 0.10.2 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -11,23 +11,30 @@ import (
11
11
  "io"
12
12
  "io/ioutil"
13
13
  "os"
14
+ "regexp"
14
15
  "sort"
15
16
 
16
17
  ss "github.com/hayamiz/perfmonger/core/subsystem"
17
18
  )
18
19
 
19
20
  type CmdOption struct {
20
- DiskFile string
21
- CpuFile string
22
- PerfmongerFile string
21
+ DiskFile string
22
+ CpuFile string
23
+ PerfmongerFile string
24
+ disk_only string
25
+ disk_only_regex *regexp.Regexp
23
26
  }
24
27
 
25
28
  func parseArgs() *CmdOption {
29
+ var err error
30
+
26
31
  opt := new(CmdOption)
27
32
 
28
33
  flag.StringVar(&opt.DiskFile, "diskfile", "./disk.dat", "Disk performance data file")
29
34
  flag.StringVar(&opt.CpuFile, "cpufile", "./cpu.dat", "CPU performance data file")
30
35
  flag.StringVar(&opt.PerfmongerFile, "perfmonger", "", "Perfmonger log file")
36
+ flag.StringVar(&opt.disk_only, "disk-only",
37
+ "", "Select disk devices by regex")
31
38
 
32
39
  flag.Parse()
33
40
 
@@ -36,6 +43,11 @@ func parseArgs() *CmdOption {
36
43
  os.Exit(1)
37
44
  }
38
45
 
46
+ opt.disk_only_regex, err = regexp.Compile(opt.disk_only)
47
+ if err != nil {
48
+ panic(err)
49
+ }
50
+
39
51
  return opt
40
52
  }
41
53
 
@@ -192,7 +204,9 @@ func main() {
192
204
  }
193
205
 
194
206
  // Disk usage
195
- dusage, err := ss.GetDiskUsage(prev_rec.Time, prev_rec.Disk, cur_rec.Time, cur_rec.Disk)
207
+ dusage, err := ss.GetDiskUsage1(prev_rec.Time, prev_rec.Disk,
208
+ cur_rec.Time, cur_rec.Disk,
209
+ opt.disk_only_regex)
196
210
  if err != nil {
197
211
  panic(err)
198
212
  }
@@ -43,6 +43,8 @@ type RecorderOption struct {
43
43
  targetDisks *map[string]bool
44
44
  background bool
45
45
  gzip bool
46
+ color bool
47
+ pretty bool
46
48
  }
47
49
 
48
50
  var option RecorderOption
@@ -86,6 +88,10 @@ func parseArgs() {
86
88
  "", "Run perfmonger-player to show JSON output")
87
89
  flag.BoolVar(&option.gzip, "gzip",
88
90
  false, "Save a logfile in gzipped format")
91
+ flag.BoolVar(&option.color, "color",
92
+ false, "Colored output (for live subcmd)")
93
+ flag.BoolVar(&option.pretty, "pretty",
94
+ false, "Pretty output (for live subcmd)")
89
95
 
90
96
  flag.Parse()
91
97
 
@@ -190,7 +196,7 @@ func main() {
190
196
  parseArgs()
191
197
 
192
198
  hostname, _ := os.Hostname()
193
- cheader := &ss.CommonHeader{ss.Linux, hostname, time.Now()}
199
+ cheader := &ss.CommonHeader{Platform: ss.Linux, Hostname: hostname, StartTime: time.Now()}
194
200
 
195
201
  platform_header := ss.NewPlatformHeader()
196
202
 
@@ -206,7 +212,15 @@ func main() {
206
212
  var player_stdout io.ReadCloser = nil
207
213
 
208
214
  if option.player_bin != "" {
209
- player_cmd = exec.Command(option.player_bin)
215
+ if option.color {
216
+ if option.pretty {
217
+ player_cmd = exec.Command(option.player_bin, "-color", "-pretty")
218
+ } else {
219
+ player_cmd = exec.Command(option.player_bin, "-color")
220
+ }
221
+ } else {
222
+ player_cmd = exec.Command(option.player_bin)
223
+ }
210
224
  player_stdin, err = player_cmd.StdinPipe()
211
225
  if err != nil {
212
226
  fmt.Fprintf(os.Stderr, "Failed to get stdin of %s", option.player_bin)
@@ -3,7 +3,6 @@
3
3
  package main
4
4
 
5
5
  import (
6
- "bytes"
7
6
  "encoding/gob"
8
7
  "flag"
9
8
  "fmt"
@@ -12,6 +11,7 @@ import (
12
11
  "regexp"
13
12
  "sort"
14
13
 
14
+ projson "github.com/hayamiz/go-projson"
15
15
  ss "github.com/hayamiz/perfmonger/core/subsystem"
16
16
  )
17
17
 
@@ -137,32 +137,38 @@ func main() {
137
137
  interval := lst_record.Time.Sub(fst_record.Time)
138
138
 
139
139
  if option.json {
140
- buf := bytes.NewBuffer([]byte{})
140
+ printer := projson.NewPrinter()
141
141
 
142
- buf.WriteString(fmt.Sprintf(`{"exectime":%.3f`, interval.Seconds()))
142
+ printer.BeginObject()
143
+ printer.PutKey("exectime")
144
+ printer.PutFloatFmt(interval.Seconds(), "%.3f")
143
145
  if cpu_usage != nil {
144
- buf.WriteString(`,"cpu":`)
145
- cpu_usage.WriteJsonTo(buf)
146
+ printer.PutKey("cpu")
147
+ cpu_usage.WriteJsonTo(printer)
146
148
  }
147
149
 
148
150
  if intr_usage != nil {
149
- buf.WriteString(`,"intr":`)
150
- intr_usage.WriteJsonTo(buf)
151
+ printer.PutKey("intr")
152
+ intr_usage.WriteJsonTo(printer)
151
153
  }
152
154
 
153
155
  if disk_usage != nil {
154
- buf.WriteString(`,"disk":`)
155
- disk_usage.WriteJsonTo(buf)
156
+ printer.PutKey("disk")
157
+ disk_usage.WriteJsonTo(printer)
156
158
  }
157
159
 
158
160
  if net_usage != nil {
159
- buf.WriteString(`,"net":`)
160
- net_usage.WriteJsonTo(buf)
161
+ printer.PutKey("net")
162
+ net_usage.WriteJsonTo(printer)
161
163
  }
162
164
 
163
- buf.WriteByte('}')
165
+ printer.FinishObject()
164
166
 
165
- fmt.Println(buf.String())
167
+ if str, err := printer.String(); err != nil {
168
+ fmt.Println("skip by err")
169
+ } else {
170
+ fmt.Println(str)
171
+ }
166
172
  } else {
167
173
  if option.title == "" {
168
174
  fmt.Println("== performance summary ==")
@@ -2,12 +2,13 @@ package subsystem
2
2
 
3
3
  import (
4
4
  "bytes"
5
- "encoding/json"
6
5
  "errors"
7
6
  "fmt"
8
7
  "regexp"
9
8
  "sort"
10
9
  "time"
10
+
11
+ projson "github.com/hayamiz/go-projson"
11
12
  )
12
13
 
13
14
  type CpuCoreUsage struct {
@@ -84,25 +85,52 @@ type NetUsageEntry struct {
84
85
 
85
86
  type NetUsage map[string]*NetUsageEntry
86
87
 
87
- func (ccusage *CpuCoreUsage) WriteJsonTo(buf *bytes.Buffer) {
88
- buf.WriteString(
89
- fmt.Sprintf(`{"usr":%.2f,"nice":%.2f,"sys":%.2f,"idle":%.2f,"iowait":%.2f,"hardirq":%.2f,"softirq":%.2f,"steal":%.2f,"guest":%.2f,"guestnice":%.2f}`,
90
- ccusage.User, ccusage.Nice, ccusage.Sys, ccusage.Idle, ccusage.Iowait,
91
- ccusage.Hardirq, ccusage.Softirq, ccusage.Steal, ccusage.Guest, ccusage.GuestNice))
88
+ var UseColor = false
89
+
90
+ func SetUseColor(use_color bool) {
91
+ UseColor = use_color
92
92
  }
93
93
 
94
- func (cusage *CpuUsage) WriteJsonTo(buf *bytes.Buffer) {
95
- buf.WriteString(
96
- fmt.Sprintf(`{"num_core":%d,"all":`, cusage.NumCore))
97
- cusage.All.WriteJsonTo(buf)
98
- buf.WriteString(`,"cores":[`)
99
- for idx, ccusage := range cusage.CoreUsages {
100
- if idx > 0 {
101
- buf.WriteString(",")
102
- }
103
- ccusage.WriteJsonTo(buf)
94
+ func (ccusage *CpuCoreUsage) WriteJsonTo(printer *projson.JsonPrinter) {
95
+ printer.BeginObject()
96
+ printer.PutKey("usr")
97
+ printer.PutFloatFmt(ccusage.User, "%.2f")
98
+ printer.PutKey("nice")
99
+ printer.PutFloatFmt(ccusage.Nice, "%.2f")
100
+ printer.PutKey("sys")
101
+ printer.PutFloatFmt(ccusage.Sys, "%.2f")
102
+ printer.PutKey("idle")
103
+ printer.PutFloatFmt(ccusage.Idle, "%.2f")
104
+ printer.PutKey("iowait")
105
+ printer.PutFloatFmt(ccusage.Iowait, "%.2f")
106
+ printer.PutKey("hardirq")
107
+ printer.PutFloatFmt(ccusage.Hardirq, "%.2f")
108
+ printer.PutKey("softirq")
109
+ printer.PutFloatFmt(ccusage.Softirq, "%.2f")
110
+ printer.PutKey("steal")
111
+ printer.PutFloatFmt(ccusage.Steal, "%.2f")
112
+ printer.PutKey("guest")
113
+ printer.PutFloatFmt(ccusage.Guest, "%.2f")
114
+ printer.PutKey("guestnice")
115
+ printer.PutFloatFmt(ccusage.GuestNice, "%.2f")
116
+ printer.FinishObject()
117
+ }
118
+
119
+ func (cusage *CpuUsage) WriteJsonTo(printer *projson.JsonPrinter) {
120
+ printer.BeginObject()
121
+ printer.PutKey("num_core")
122
+ printer.PutInt(cusage.NumCore)
123
+ printer.PutKey("all")
124
+
125
+ cusage.All.WriteJsonTo(printer)
126
+
127
+ printer.PutKey("cores")
128
+ printer.BeginArray()
129
+ for _, ccusage := range cusage.CoreUsages {
130
+ ccusage.WriteJsonTo(printer)
104
131
  }
105
- buf.WriteString(`]}`)
132
+ printer.FinishArray()
133
+ printer.FinishObject()
106
134
  }
107
135
 
108
136
  func GetCpuCoreUsage(c1 *CpuCoreStat, c2 *CpuCoreStat) (*CpuCoreUsage, error) {
@@ -225,35 +253,68 @@ func GetInterruptUsage(t1 time.Time, i1 *InterruptStat, t2 time.Time, i2 *Interr
225
253
  return usage, nil
226
254
  }
227
255
 
228
- func (intr_usage *InterruptUsage) WriteJsonTo(buf *bytes.Buffer) {
229
- buf.WriteString("{")
230
- buf.WriteString(`"core_dev_intr":[`)
231
- for idx, core_usage := range intr_usage.CoreIntrUsages {
232
- if idx > 0 {
233
- buf.WriteString(",")
234
- }
235
- fmt.Fprintf(buf, "%.2f", core_usage.Device)
256
+ func (intr_usage *InterruptUsage) WriteJsonTo(printer *projson.JsonPrinter) {
257
+ printer.BeginObject()
258
+ printer.PutKey("core_dev_intr")
259
+ printer.BeginArray()
260
+ for _, core_usage := range intr_usage.CoreIntrUsages {
261
+ printer.PutFloatFmt(core_usage.Device, "%.2f")
236
262
  }
237
- buf.WriteString(`],"core_sys_intr":[`)
238
- for idx, core_usage := range intr_usage.CoreIntrUsages {
239
- if idx > 0 {
240
- buf.WriteString(",")
241
- }
242
- fmt.Fprintf(buf, "%.2f", core_usage.System)
263
+ printer.FinishArray()
264
+
265
+ printer.PutKey("core_sys_intr")
266
+ printer.BeginArray()
267
+ for _, core_usage := range intr_usage.CoreIntrUsages {
268
+ printer.PutFloatFmt(core_usage.System, "%.2f")
243
269
  }
244
- buf.WriteString("]")
245
- buf.WriteString("}")
270
+ printer.FinishArray()
271
+ printer.FinishObject()
272
+ }
273
+
274
+ func (duentry *DiskUsageEntry) WriteJsonTo(printer *projson.JsonPrinter) {
275
+ printer.BeginObject()
276
+ printer.PutKey("riops")
277
+ printer.PutFloatFmt(duentry.RdIops, "%.2f")
278
+ printer.PutKey("wiops")
279
+ printer.PutFloatFmt(duentry.WrIops, "%.2f")
280
+ printer.PutKey("rkbyteps")
281
+ printer.PutFloatFmt(duentry.RdSecps/2.0, "%.2f")
282
+ printer.PutKey("wkbyteps")
283
+ printer.PutFloatFmt(duentry.WrSecps/2.0, "%.2f")
284
+ printer.PutKey("rlatency")
285
+ printer.PutFloatFmt(duentry.RdLatency, "%.3f")
286
+ printer.PutKey("wlatency")
287
+ printer.PutFloatFmt(duentry.WrLatency, "%.3f")
288
+ printer.PutKey("rsize")
289
+ printer.PutFloatFmt(duentry.AvgRdSize, "%.2f")
290
+ printer.PutKey("wsize")
291
+ printer.PutFloatFmt(duentry.AvgWrSize, "%.2f")
292
+ printer.PutKey("qlen")
293
+ printer.PutFloatFmt(duentry.ReqQlen, "%.2f")
294
+ printer.FinishObject()
246
295
  }
247
296
 
248
- func (duentry *DiskUsageEntry) WriteJsonTo(buf *bytes.Buffer) {
249
- fmt.Fprintf(buf,
250
- `{"riops":%.2f,"wiops":%.2f,"rkbyteps":%.2f,"wkbyteps":%.2f,"rlatency":%.3f,"wlatency":%.3f,"rsize":%.2f,"wsize":%.2f,"qlen":%.2f}`,
251
- duentry.RdIops, duentry.WrIops, duentry.RdSecps/2.0, duentry.WrSecps/2.0,
252
- duentry.RdLatency, duentry.WrLatency,
253
- duentry.AvgRdSize, duentry.AvgWrSize, duentry.ReqQlen)
297
+ func strarrayToString(arr []string) string {
298
+ buf := bytes.NewBuffer([]byte{})
299
+
300
+ fmt.Fprintf(buf, "[")
301
+ for i, elem := range arr {
302
+ if i > 0 {
303
+ fmt.Fprintf(buf, ",")
304
+ }
305
+
306
+ if UseColor {
307
+ fmt.Fprintf(buf, "\033[35m\"%s\"\033[0m", elem)
308
+ } else {
309
+ fmt.Fprintf(buf, "\"%s\"", elem)
310
+ }
311
+ }
312
+ fmt.Fprintf(buf, "]")
313
+
314
+ return buf.String()
254
315
  }
255
316
 
256
- func (dusage *DiskUsage) WriteJsonTo(buf *bytes.Buffer) {
317
+ func (dusage *DiskUsage) WriteJsonTo(printer *projson.JsonPrinter) {
257
318
  var devices []string
258
319
 
259
320
  for device, _ := range *dusage {
@@ -263,23 +324,24 @@ func (dusage *DiskUsage) WriteJsonTo(buf *bytes.Buffer) {
263
324
  }
264
325
  sort.Strings(devices)
265
326
 
266
- bytes, err := json.Marshal(devices)
267
- if err != nil {
268
- panic(err)
327
+ printer.BeginObject()
328
+ printer.PutKey("devices")
329
+ printer.BeginArray()
330
+ for _, device := range devices {
331
+ printer.PutString(device)
269
332
  }
270
- fmt.Fprintf(buf, `{"devices":%s`, string(bytes))
333
+ printer.FinishArray()
271
334
 
272
335
  devices = append(devices, "total")
273
336
 
274
337
  for _, device := range devices {
275
338
  usage := (*dusage)[device]
276
- buf.WriteString(`,"`)
277
- buf.WriteString(device)
278
- buf.WriteString(`":`)
279
- usage.WriteJsonTo(buf)
339
+
340
+ printer.PutKey(device)
341
+ usage.WriteJsonTo(printer)
280
342
  }
281
343
 
282
- buf.WriteByte('}')
344
+ printer.FinishObject()
283
345
  }
284
346
 
285
347
  func avgDelta(v int64, w int64, interval float64) float64 {
@@ -470,7 +532,7 @@ func GetNetUsage(t1 time.Time, d1 *NetStat, t2 time.Time, d2 *NetStat) (*NetUsag
470
532
  return net_usage, nil
471
533
  }
472
534
 
473
- func (nusage *NetUsage) WriteJsonTo(buf *bytes.Buffer) {
535
+ func (nusage *NetUsage) WriteJsonTo(printer *projson.JsonPrinter) {
474
536
  var devices []string
475
537
 
476
538
  for device, _ := range *nusage {
@@ -480,30 +542,45 @@ func (nusage *NetUsage) WriteJsonTo(buf *bytes.Buffer) {
480
542
  }
481
543
  sort.Strings(devices)
482
544
 
483
- bytes, err := json.Marshal(devices)
484
- if err != nil {
485
- panic(err)
545
+ printer.BeginObject()
546
+ printer.PutKey("devices")
547
+ printer.BeginArray()
548
+ for _, device := range devices {
549
+ printer.PutString(device)
486
550
  }
487
- fmt.Fprintf(buf, `{"devices":%s`, string(bytes))
551
+ printer.FinishArray()
488
552
 
489
553
  devices = append(devices, "total")
490
554
 
491
555
  for _, device := range devices {
492
556
  usage := (*nusage)[device]
493
- buf.WriteString(`,"`)
494
- buf.WriteString(device)
495
- buf.WriteString(`":`)
496
- usage.WriteJsonTo(buf)
557
+
558
+ printer.PutKey(device)
559
+ usage.WriteJsonTo(printer)
497
560
  }
498
561
 
499
- buf.WriteByte('}')
562
+ printer.FinishObject()
500
563
  }
501
564
 
502
- func (entry *NetUsageEntry) WriteJsonTo(buf *bytes.Buffer) {
503
- buf.WriteString(
504
- fmt.Sprintf(`{"rxkbyteps":%.2f,"rxpktps":%.2f,"rxerrps":%.2f,"rxdropps":%.2f,"txkbyteps":%.2f,"txpktps":%.2f,"txerrps":%.2f,"txdropps":%.2f}`,
505
- entry.RxBytesPerSec/1024.0, entry.RxPacketsPerSec,
506
- entry.RxErrorsPerSec, entry.RxDropsPerSec,
507
- entry.TxBytesPerSec/1024.0, entry.TxPacketsPerSec,
508
- entry.TxErrorsPerSec, entry.TxDropsPerSec))
565
+ func (entry *NetUsageEntry) WriteJsonTo(printer *projson.JsonPrinter) {
566
+ printer.BeginObject()
567
+
568
+ printer.PutKey("rxkbyteps")
569
+ printer.PutFloatFmt(entry.RxBytesPerSec/1024.0, "%.2f")
570
+ printer.PutKey("rxpktps")
571
+ printer.PutFloatFmt(entry.RxPacketsPerSec, "%.2f")
572
+ printer.PutKey("rxerrps")
573
+ printer.PutFloatFmt(entry.RxErrorsPerSec, "%.2f")
574
+ printer.PutKey("rxdropps")
575
+ printer.PutFloatFmt(entry.RxDropsPerSec, "%.2f")
576
+ printer.PutKey("txkbyteps")
577
+ printer.PutFloatFmt(entry.TxBytesPerSec/1024.0, "%.2f")
578
+ printer.PutKey("txpktps")
579
+ printer.PutFloatFmt(entry.TxPacketsPerSec, "%.2f")
580
+ printer.PutKey("txerrps")
581
+ printer.PutFloatFmt(entry.TxErrorsPerSec, "%.2f")
582
+ printer.PutKey("txdropps")
583
+ printer.PutFloatFmt(entry.TxDropsPerSec, "%.2f")
584
+
585
+ printer.FinishObject()
509
586
  }
@@ -1,7 +1,6 @@
1
1
  package subsystem
2
2
 
3
3
  import (
4
- "bytes"
5
4
  "encoding/json"
6
5
  "math"
7
6
  "regexp"
@@ -9,6 +8,8 @@ import (
9
8
  "strings"
10
9
  "testing"
11
10
  "time"
11
+
12
+ projson "github.com/hayamiz/go-projson"
12
13
  )
13
14
 
14
15
  func isValidJson(byt []byte) bool {
@@ -89,7 +90,7 @@ func TestGetCoreUsage(t *testing.T) {
89
90
  usage.Steal == 0 &&
90
91
  usage.Guest == 0 &&
91
92
  usage.GuestNice == 0) {
92
- t.Error("usage is not 0%, want 0%")
93
+ t.Error("usage is not 0 percent, want 0 percent")
93
94
  }
94
95
 
95
96
  // should return error if c1.Uptime() is larger than c2.Uptime()
@@ -120,10 +121,14 @@ func TestGetCoreUsage(t *testing.T) {
120
121
  t.Errorf("usage.Sys = %v, want 25.0", usage.User)
121
122
  }
122
123
 
123
- buf := bytes.NewBuffer([]byte{})
124
- usage.WriteJsonTo(buf)
125
- if !isValidJson(buf.Bytes()) {
126
- t.Errorf("Invalid JSON: %s", buf.String())
124
+ printer := projson.NewPrinter()
125
+ usage.WriteJsonTo(printer)
126
+ if str, err := printer.String(); err != nil {
127
+ t.Errorf("failed to print JSON")
128
+ } else {
129
+ if !isValidJson([]byte(str)) {
130
+ t.Errorf("Invalid JSON: %s", str)
131
+ }
127
132
  }
128
133
  }
129
134
 
@@ -207,15 +212,20 @@ func TestGetCpuUsage(t *testing.T) {
207
212
  t.Errorf("usage.Sys = %v, want 25.0", usage.CoreUsages[1].User)
208
213
  }
209
214
 
210
- buf := bytes.NewBuffer([]byte{})
211
- usage.WriteJsonTo(buf)
212
- if !isValidJson(buf.Bytes()) {
213
- t.Errorf("Invalid JSON: %s", buf.String())
215
+ printer := projson.NewPrinter()
216
+ usage.WriteJsonTo(printer)
217
+ str, err := printer.String()
218
+ if err != nil {
219
+ t.Errorf("failed printing JSON")
220
+ } else {
221
+ if !isValidJson([]byte(str)) {
222
+ t.Errorf("Invalid JSON: %s", str)
223
+ }
214
224
  }
215
225
 
216
226
  assertHasKey := func(key_path string) {
217
- if !jsonHasKey(buf.Bytes(), key_path) {
218
- t.Errorf("%v is not present in JSON:\n%v", key_path, buf.String())
227
+ if !jsonHasKey([]byte(str), key_path) {
228
+ t.Errorf("%v is not present in JSON:\n%v", key_path, str)
219
229
  }
220
230
  }
221
231
  assertHasKey("num_core")
@@ -291,10 +301,15 @@ func TestDiskUsage(t *testing.T) {
291
301
  t.Errorf("sda.RdSectors = %v, want %v", (*usage)["sda"].RdSectors, 350)
292
302
  }
293
303
 
294
- buf := bytes.NewBuffer([]byte{})
295
- usage.WriteJsonTo(buf)
296
- if !isValidJson(buf.Bytes()) {
297
- t.Errorf("invalid json: %s", buf.String())
304
+ printer := projson.NewPrinter()
305
+ usage.WriteJsonTo(printer)
306
+ str, err := printer.String()
307
+ if err != nil {
308
+ t.Errorf("failed printing JSON")
309
+ } else {
310
+ if !isValidJson([]byte(str)) {
311
+ t.Errorf("invalid json: %s", str)
312
+ }
298
313
  }
299
314
 
300
315
  d1.Entries = append(d1.Entries, NewDiskStatEntry())
@@ -347,15 +362,20 @@ func TestDiskUsage(t *testing.T) {
347
362
  t.Errorf("total.RdSectors = %v, want %v", (*usage)["total"].RdSectors, 350)
348
363
  }
349
364
 
350
- buf = bytes.NewBuffer([]byte{})
351
- usage.WriteJsonTo(buf)
352
- if !isValidJson(buf.Bytes()) {
353
- t.Errorf("invalid json: %s", buf.String())
365
+ printer = projson.NewPrinter()
366
+ usage.WriteJsonTo(printer)
367
+ str, err = printer.String()
368
+ if err != nil {
369
+ t.Errorf("failed printing JSON")
370
+ } else {
371
+ if !isValidJson([]byte(str)) {
372
+ t.Errorf("invalid json: %s", str)
373
+ }
354
374
  }
355
375
 
356
376
  assertHasKey := func(key_path string) {
357
- if !jsonHasKey(buf.Bytes(), key_path) {
358
- t.Errorf("%v is not present in JSON:\n%v", key_path, buf.String())
377
+ if !jsonHasKey([]byte(str), key_path) {
378
+ t.Errorf("%v is not present in JSON:\n%v", key_path, str)
359
379
  }
360
380
  }
361
381
  assertHasKey("devices")
@@ -428,10 +448,15 @@ func TestGetNetUsage(t *testing.T) {
428
448
  t.Errorf("lo.RxPacketsPerSec = %v, want %v", (*usage)["lo"].RxPacketsPerSec, 100.0/interval)
429
449
  }
430
450
 
431
- buf := bytes.NewBuffer([]byte{})
432
- usage.WriteJsonTo(buf)
433
- if !isValidJson(buf.Bytes()) {
434
- t.Errorf("invalid json: %s", buf.String())
451
+ printer := projson.NewPrinter()
452
+ usage.WriteJsonTo(printer)
453
+ str, err := printer.String()
454
+ if err != nil {
455
+ t.Errorf("failed printing JSON")
456
+ } else {
457
+ if !isValidJson([]byte(str)) {
458
+ t.Errorf("invalid json: %s", str)
459
+ }
435
460
  }
436
461
 
437
462
  n1.Entries = append(n1.Entries, NewNetStatEntry())
@@ -483,15 +508,20 @@ func TestGetNetUsage(t *testing.T) {
483
508
  (100.0+150.0)/interval)
484
509
  }
485
510
 
486
- buf = bytes.NewBuffer([]byte{})
487
- usage.WriteJsonTo(buf)
488
- if !isValidJson(buf.Bytes()) {
489
- t.Errorf("invalid json: %s", buf.String())
511
+ printer = projson.NewPrinter()
512
+ usage.WriteJsonTo(printer)
513
+ str, err = printer.String()
514
+ if err != nil {
515
+ t.Errorf("failed printing JSON")
516
+ } else {
517
+ if !isValidJson([]byte(str)) {
518
+ t.Errorf("invalid json: %s", str)
519
+ }
490
520
  }
491
521
 
492
522
  assertHasKey := func(key_path string) {
493
- if !jsonHasKey(buf.Bytes(), key_path) {
494
- t.Errorf("%v is not present in JSON:\n%v", key_path, buf.String())
523
+ if !jsonHasKey([]byte(str), key_path) {
524
+ t.Errorf("%v is not present in JSON:\n%v", key_path, str)
495
525
  }
496
526
  }
497
527
  assertHasKey("devices")