Jordan Halterman | 29718e6 | 2017-07-20 13:24:33 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Jordan Halterman | 29718e6 | 2017-07-20 13:24:33 -0700 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | package org.onosproject.primitiveperf; |
| 17 | |
| 18 | import java.text.SimpleDateFormat; |
| 19 | import java.util.Date; |
| 20 | import java.util.List; |
| 21 | |
| 22 | import org.apache.karaf.shell.commands.Command; |
| 23 | import org.apache.karaf.shell.commands.Option; |
| 24 | import org.onosproject.cli.AbstractShellCommand; |
| 25 | import org.onosproject.primitiveperf.PrimitivePerfCollector.Sample; |
| 26 | |
| 27 | /** |
| 28 | * Displays accumulated performance metrics. |
| 29 | */ |
| 30 | @Command(scope = "onos", name = "primitive-perf", |
| 31 | description = "Displays accumulated performance metrics") |
| 32 | public class PrimitivePerfListCommand extends AbstractShellCommand { |
| 33 | |
| 34 | @Option(name = "-s", aliases = "--summary", description = "Output just summary", |
| 35 | required = false, multiValued = false) |
| 36 | private boolean summary = false; |
| 37 | |
| 38 | @Override |
| 39 | protected void execute() { |
| 40 | if (summary) { |
| 41 | printSummary(); |
| 42 | } else { |
| 43 | printSamples(); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | private void printSummary() { |
| 48 | PrimitivePerfCollector collector = get(PrimitivePerfCollector.class); |
| 49 | List<String> headers = collector.getSampleHeaders(); |
| 50 | Sample overall = collector.getOverall(); |
| 51 | double total = 0; |
| 52 | print("%12s: %14s", "Node ID", "Overall Rate"); |
| 53 | for (int i = 0; i < overall.data.length; i++) { |
| 54 | if (overall.data[i] >= 0) { |
| 55 | print("%12s: %14.2f", headers.get(i), overall.data[i]); |
| 56 | total += overall.data[i]; |
| 57 | } else { |
| 58 | print("%12s: %14s", headers.get(i), " "); |
| 59 | } |
| 60 | } |
| 61 | print("%12s: %14.2f", "total", total); |
| 62 | } |
| 63 | |
| 64 | private void printSamples() { |
| 65 | PrimitivePerfCollector collector = get(PrimitivePerfCollector.class); |
| 66 | List<String> headers = collector.getSampleHeaders(); |
| 67 | List<Sample> samples = collector.getSamples(); |
| 68 | |
| 69 | System.out.print(String.format("%10s ", "Time")); |
| 70 | for (String header : headers) { |
| 71 | System.out.print(String.format("%12s ", header)); |
| 72 | } |
| 73 | System.out.println(String.format("%12s", "Total")); |
| 74 | |
| 75 | SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); |
| 76 | for (Sample sample : samples) { |
| 77 | double total = 0; |
| 78 | System.out.print(String.format("%10s ", sdf.format(new Date(sample.time)))); |
| 79 | for (int i = 0; i < sample.data.length; i++) { |
| 80 | if (sample.data[i] >= 0) { |
| 81 | System.out.print(String.format("%12.2f ", sample.data[i])); |
| 82 | total += sample.data[i]; |
| 83 | } else { |
| 84 | System.out.print(String.format("%12s ", " ")); |
| 85 | } |
| 86 | } |
| 87 | System.out.println(String.format("%12.2f", total)); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | } |