blob: d443865eadeb27a0d8ff63d9e7d9c7b44a252bf7 [file] [log] [blame]
Jordan Halterman29718e62017-07-20 13:24:33 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jordan Halterman29718e62017-07-20 13:24:33 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.primitiveperf;
17
18import java.text.SimpleDateFormat;
19import java.util.Date;
20import java.util.List;
21
Ray Milkey86ad7bb2018-09-27 12:32:28 -070022import org.apache.karaf.shell.api.action.Command;
23import org.apache.karaf.shell.api.action.Option;
Ray Milkey7a2dee52018-09-28 10:58:28 -070024import org.apache.karaf.shell.api.action.lifecycle.Service;
Jordan Halterman29718e62017-07-20 13:24:33 -070025import org.onosproject.cli.AbstractShellCommand;
26import org.onosproject.primitiveperf.PrimitivePerfCollector.Sample;
27
28/**
29 * Displays accumulated performance metrics.
30 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070031@Service
Jordan Halterman29718e62017-07-20 13:24:33 -070032@Command(scope = "onos", name = "primitive-perf",
33 description = "Displays accumulated performance metrics")
34public class PrimitivePerfListCommand extends AbstractShellCommand {
35
36 @Option(name = "-s", aliases = "--summary", description = "Output just summary",
37 required = false, multiValued = false)
38 private boolean summary = false;
39
40 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070041 protected void doExecute() {
Jordan Halterman29718e62017-07-20 13:24:33 -070042 if (summary) {
43 printSummary();
44 } else {
45 printSamples();
46 }
47 }
48
49 private void printSummary() {
50 PrimitivePerfCollector collector = get(PrimitivePerfCollector.class);
51 List<String> headers = collector.getSampleHeaders();
52 Sample overall = collector.getOverall();
53 double total = 0;
54 print("%12s: %14s", "Node ID", "Overall Rate");
55 for (int i = 0; i < overall.data.length; i++) {
56 if (overall.data[i] >= 0) {
57 print("%12s: %14.2f", headers.get(i), overall.data[i]);
58 total += overall.data[i];
59 } else {
60 print("%12s: %14s", headers.get(i), " ");
61 }
62 }
63 print("%12s: %14.2f", "total", total);
64 }
65
66 private void printSamples() {
67 PrimitivePerfCollector collector = get(PrimitivePerfCollector.class);
68 List<String> headers = collector.getSampleHeaders();
69 List<Sample> samples = collector.getSamples();
70
71 System.out.print(String.format("%10s ", "Time"));
72 for (String header : headers) {
73 System.out.print(String.format("%12s ", header));
74 }
75 System.out.println(String.format("%12s", "Total"));
76
77 SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
78 for (Sample sample : samples) {
79 double total = 0;
80 System.out.print(String.format("%10s ", sdf.format(new Date(sample.time))));
81 for (int i = 0; i < sample.data.length; i++) {
82 if (sample.data[i] >= 0) {
83 System.out.print(String.format("%12.2f ", sample.data[i]));
84 total += sample.data[i];
85 } else {
86 System.out.print(String.format("%12s ", " "));
87 }
88 }
89 System.out.println(String.format("%12.2f", total));
90 }
91 }
92
93}