2 * ============LICENSE_START====================================================
\r
4 * ===========================================================================
\r
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
\r
6 * ===========================================================================
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
18 * ============LICENSE_END====================================================
\r
22 package org.onap.aaf.misc.env;
\r
25 * <h1>TimeTaken</h1>
\r
26 * This simple interface allows for many different kinds of
\r
27 * Audit Logs to be accomplished, by assuming that the creation
\r
28 * of this object indicates "start", and the calling of "done"
\r
31 * The implementor of this class can easily be stored in efficient
\r
32 * mechanisms to minimize impact of Auditing on performance.
\r
37 public abstract class TimeTaken {
\r
38 public final long start;
\r
39 protected long end, size;
\r
40 public final int flag;
\r
41 public final String name;
\r
44 * The name is as it will appear when written to output (abstract method)
\r
46 * The flag is an integer which can be System type (XML, REMOTE, etc), or End User defined for reporting purposes
\r
51 public TimeTaken(String name, int flag) {
\r
52 start = System.nanoTime();
\r
60 * Call this when process is done to state ending time.<p>
\r
62 * It is <i>exceedingly prudent</i> to wrap the process called with a try-finally:<p>
\r
65 * TimeTaken tt = env.startSubTime();
\r
67 * process.me(); // code to be timed.
\r
73 public void done() {
\r
74 end = System.nanoTime();
\r
79 * For sizable contents, set the size. Implementations can simply write a no-op if they don't wish to
\r
84 public void size(long theSize) {
\r
89 * Give readonly access to End, which isn't final
\r
97 * Time is taken in NanoSeconds. This method converts to decimals of Milliseconds
\r
100 public float millis() {
\r
101 return (end-start)/1000000f;
\r
104 * Write self to a String Builder (for making Audits)
\r
107 public abstract void output(StringBuilder sb);
\r
112 public String toString() {
\r
113 return name + ' ' + millis() + "ms " + (size>0?Long.toString(size):"");
\r