1 /*******************************************************************************
2  * Copyright (c) 2009, 2015 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 
15 package org.eclipse.pde.ui.tests.util;
16 
17 import java.io.IOException;
18 import java.io.Writer;
19 
20 public class PDEPerfTesterUtil {
21 
22 	private String fTag;
23 
24 	private long fDuration;
25 
26 	private long fStart;
27 
28 	private long fEnd;
29 
30 	private long fIteration;
31 
32 	private long fTotalDuration;
33 
34 	private long fAverageDuration;
35 
36 	private final static long F_SECOND_IN_MS = 1000;
37 
38 	private final static long F_MINUTE_IN_MS = 60000;
39 
40 	private final static long F_HOUR_IN_MS = 3600000;
41 
42 	/**
43 	 * @param tag
44 	 */
PDEPerfTesterUtil(String tag)45 	public PDEPerfTesterUtil(String tag) {
46 		fTag = tag;
47 		reset();
48 	}
49 
50 	/**
51 	 *
52 	 */
reset()53 	public void reset() {
54 		fDuration = 0;
55 		fStart = 0;
56 		fEnd = 0;
57 		fIteration = 0;
58 		fTotalDuration = 0;
59 		fAverageDuration = 0;
60 	}
61 
62 	/**
63 	 *
64 	 */
start()65 	public void start() {
66 		fIteration++;
67 		fStart = System.currentTimeMillis();
68 	}
69 
70 	/**
71 	 *
72 	 */
stop()73 	public void stop() {
74 		fEnd = System.currentTimeMillis();
75 		calculateDuration();
76 	}
77 
78 	/**
79 	 *
80 	 */
calculateDuration()81 	private void calculateDuration() {
82 		fDuration = (fEnd - fStart);
83 		fTotalDuration = fTotalDuration + fDuration;
84 		if (fIteration > 0) {
85 			fAverageDuration = fTotalDuration / fIteration;
86 		}
87 	}
88 
89 	/**
90 	 * @param duration
91 	 * @return
92 	 */
formatDuration(long duration)93 	private String formatDuration(long duration) {
94 
95 		String output = null;
96 		int hours = 0;
97 		int minutes = 0;
98 		int seconds = 0;
99 		long milliseconds = 0;
100 		long timeDifference = duration;
101 
102 		hours = (int) Math.rint(timeDifference / F_HOUR_IN_MS);
103 		if (hours > 0) {
104 			timeDifference = timeDifference - (hours * F_HOUR_IN_MS);
105 		}
106 
107 		minutes = (int) Math.rint(timeDifference / F_MINUTE_IN_MS);
108 		if (minutes > 0) {
109 			timeDifference = timeDifference - (minutes * F_MINUTE_IN_MS);
110 		}
111 
112 		seconds = (int) Math.rint(timeDifference / F_SECOND_IN_MS);
113 		if (seconds > 0) {
114 			timeDifference = timeDifference - (seconds * F_SECOND_IN_MS);
115 		}
116 
117 		milliseconds = timeDifference;
118 
119 		output = hours + " h " + //$NON-NLS-1$
120 				minutes + " m " + //$NON-NLS-1$
121 				seconds + " s " + //$NON-NLS-1$
122 				milliseconds + " ms"; //$NON-NLS-1$
123 
124 		return output;
125 	}
126 
127 	/**
128 	 * @param writer
129 	 */
printDuration(Writer writer)130 	public void printDuration(Writer writer) {
131 		String output = formatTag() + "(" + //$NON-NLS-1$
132 				fIteration + "): " + //$NON-NLS-1$
133 				formatDuration(fDuration) + "\n"; //$NON-NLS-1$
134 		try {
135 			writer.write(output);
136 			writer.flush();
137 		} catch (IOException e) {
138 			// Ignore
139 		}
140 	}
141 
142 	/**
143 	 * @param writer
144 	 */
printTotalDuration(Writer writer)145 	public void printTotalDuration(Writer writer) {
146 		String output = formatTag() + "(TOTAL " + //$NON-NLS-1$
147 				fIteration + "): " + //$NON-NLS-1$
148 				formatDuration(fTotalDuration) + "\n"; //$NON-NLS-1$
149 		try {
150 			writer.write(output);
151 			writer.flush();
152 		} catch (IOException e) {
153 			// Ignore
154 		}
155 	}
156 
157 	/**
158 	 * @param writer
159 	 */
printAverageDuration(Writer writer)160 	public void printAverageDuration(Writer writer) {
161 		String output = formatTag() + "(AVERAGE " + //$NON-NLS-1$
162 				fIteration + "): " + //$NON-NLS-1$
163 				formatDuration(fAverageDuration) + "\n"; //$NON-NLS-1$
164 		try {
165 			writer.write(output);
166 			writer.flush();
167 		} catch (IOException e) {
168 			// Ignore
169 		}
170 	}
171 
172 	/**
173 	 * @return
174 	 */
formatTag()175 	private String formatTag() {
176 		return "[" + fTag + "]: "; //$NON-NLS-1$ //$NON-NLS-2$
177 	}
178 
179 }
180