Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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 | |
| 17 | package org.onosproject.drivers.server.impl.stats; |
| 18 | |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame^] | 19 | import org.onosproject.drivers.server.stats.MonitoringUnit; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 20 | import org.onosproject.drivers.server.stats.TimingStatistics; |
| 21 | |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame^] | 22 | import com.google.common.base.Strings; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 23 | import com.google.common.base.MoreObjects; |
| 24 | |
| 25 | import static com.google.common.base.Preconditions.checkArgument; |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame^] | 26 | import static com.google.common.base.Preconditions.checkNotNull; |
| 27 | import static org.onosproject.drivers.server.stats.MonitoringUnit.LatencyUnit; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * Default implementation for timing statistics. |
| 31 | */ |
| 32 | public final class DefaultTimingStatistics implements TimingStatistics { |
| 33 | |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame^] | 34 | private static final LatencyUnit DEF_UNIT = LatencyUnit.NANO_SECOND; |
| 35 | |
| 36 | private final MonitoringUnit unit; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 37 | private final long deployCommandParsingTime; |
| 38 | private final long deployCommandLaunchingTime; |
| 39 | private long autoscaleTime; |
| 40 | |
| 41 | private DefaultTimingStatistics( |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame^] | 42 | MonitoringUnit unit, |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 43 | long parsingTime, |
| 44 | long launchingTime, |
| 45 | long autoscaleTime) { |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame^] | 46 | checkNotNull(unit, "Time statistics unit is null"); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 47 | checkArgument(parsingTime >= 0, "Parsing time is negative"); |
| 48 | checkArgument(launchingTime >= 0, "Launching time is negative"); |
| 49 | checkArgument(autoscaleTime >= 0, "Autoscale time is negative"); |
| 50 | |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame^] | 51 | this.unit = unit; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 52 | this.deployCommandParsingTime = parsingTime; |
| 53 | this.deployCommandLaunchingTime = launchingTime; |
| 54 | this.autoscaleTime = autoscaleTime; |
| 55 | } |
| 56 | |
| 57 | // Constructor for serializer |
| 58 | private DefaultTimingStatistics() { |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame^] | 59 | this.unit = null; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 60 | this.deployCommandParsingTime = 0; |
| 61 | this.deployCommandLaunchingTime = 0; |
| 62 | this.autoscaleTime = 0; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Creates a builder for DefaultTimingStatistics object. |
| 67 | * |
| 68 | * @return builder object for DefaultTimingStatistics object |
| 69 | */ |
| 70 | public static DefaultTimingStatistics.Builder builder() { |
| 71 | return new Builder(); |
| 72 | } |
| 73 | |
| 74 | @Override |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame^] | 75 | public MonitoringUnit unit() { |
| 76 | return this.unit; |
| 77 | } |
| 78 | |
| 79 | @Override |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 80 | public long deployCommandParsingTime() { |
| 81 | return this.deployCommandParsingTime; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public long deployCommandLaunchingTime() { |
| 86 | return this.deployCommandLaunchingTime; |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public long totalDeploymentTime() { |
| 91 | return this.deployCommandParsingTime + this.deployCommandLaunchingTime; |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public long autoscaleTime() { |
| 96 | return this.autoscaleTime; |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public String toString() { |
| 101 | return MoreObjects.toStringHelper(this) |
| 102 | .omitNullValues() |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame^] | 103 | .add("unit", this.unit().toString()) |
| 104 | .add("parsingTime", this.deployCommandParsingTime()) |
| 105 | .add("launchingTime", this.deployCommandLaunchingTime()) |
| 106 | .add("deploymentTime", this.totalDeploymentTime()) |
| 107 | .add("autoScaleTime", this.autoscaleTime()) |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 108 | .toString(); |
| 109 | } |
| 110 | |
| 111 | public static final class Builder { |
| 112 | |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame^] | 113 | MonitoringUnit unit = DEF_UNIT; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 114 | long deployCommandParsingTime; |
| 115 | long deployCommandLaunchingTime; |
| 116 | long autoscaleTime; |
| 117 | |
| 118 | private Builder() { |
| 119 | |
| 120 | } |
| 121 | |
| 122 | /** |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame^] | 123 | * Sets time statistics unit. |
| 124 | * |
| 125 | * @param unitStr time statistics unit as a string |
| 126 | * @return builder object |
| 127 | */ |
| 128 | public Builder setUnit(String unitStr) { |
| 129 | if (!Strings.isNullOrEmpty(unitStr)) { |
| 130 | this.unit = LatencyUnit.getByName(unitStr); |
| 131 | } |
| 132 | |
| 133 | return this; |
| 134 | } |
| 135 | |
| 136 | /** |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 137 | * Sets parsing time. |
| 138 | * |
| 139 | * @param parsingTime parsing time |
| 140 | * @return builder object |
| 141 | */ |
| 142 | public Builder setParsingTime(long parsingTime) { |
| 143 | this.deployCommandParsingTime = parsingTime; |
| 144 | |
| 145 | return this; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Sets launching time. |
| 150 | * |
| 151 | * @param launchingTime launching time |
| 152 | * @return builder object |
| 153 | */ |
| 154 | public Builder setLaunchingTime(long launchingTime) { |
| 155 | this.deployCommandLaunchingTime = launchingTime; |
| 156 | |
| 157 | return this; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Sets autoscale time. |
| 162 | * |
| 163 | * @param autoscaleTime time required to autoscale |
| 164 | * @return builder object |
| 165 | */ |
| 166 | public Builder setAutoscaleTime(long autoscaleTime) { |
| 167 | this.autoscaleTime = autoscaleTime; |
| 168 | |
| 169 | return this; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Creates a DefaultTimingStatistics object. |
| 174 | * |
| 175 | * @return DefaultTimingStatistics object |
| 176 | */ |
| 177 | public DefaultTimingStatistics build() { |
| 178 | return new DefaultTimingStatistics( |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame^] | 179 | unit, |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 180 | deployCommandParsingTime, |
| 181 | deployCommandLaunchingTime, |
| 182 | autoscaleTime |
| 183 | ); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | } |