001: /*******************************************************************************
002: * Copyright (c) 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.core.util;
011:
012: import java.io.IOException;
013: import java.io.Writer;
014:
015: /**
016: * PDEPerformanceTester
017: *
018: */
019: public class PDEPerfTesterUtil {
020:
021: private String fTag;
022:
023: private long fDuration;
024:
025: private long fStart;
026:
027: private long fEnd;
028:
029: private long fIteration;
030:
031: private long fTotalDuration;
032:
033: private long fAverageDuration;
034:
035: private final static long F_SECOND_IN_MS = 1000;
036:
037: private final static long F_MINUTE_IN_MS = 60000;
038:
039: private final static long F_HOUR_IN_MS = 3600000;
040:
041: /**
042: * @param tag
043: */
044: public PDEPerfTesterUtil(String tag) {
045: fTag = tag;
046: reset();
047: }
048:
049: /**
050: *
051: */
052: public void reset() {
053: fDuration = 0;
054: fStart = 0;
055: fEnd = 0;
056: fIteration = 0;
057: fTotalDuration = 0;
058: fAverageDuration = 0;
059: }
060:
061: /**
062: *
063: */
064: public void start() {
065: fIteration++;
066: fStart = System.currentTimeMillis();
067: }
068:
069: /**
070: *
071: */
072: public void stop() {
073: fEnd = System.currentTimeMillis();
074: calculateDuration();
075: }
076:
077: /**
078: *
079: */
080: private void calculateDuration() {
081: fDuration = (fEnd - fStart);
082: fTotalDuration = fTotalDuration + fDuration;
083: if (fIteration > 0) {
084: fAverageDuration = fTotalDuration / fIteration;
085: }
086: }
087:
088: /**
089: * @param duration
090: * @return
091: */
092: private String formatDuration(long duration) {
093:
094: String output = null;
095: int hours = 0;
096: int minutes = 0;
097: int seconds = 0;
098: long milliseconds = 0;
099: long timeDifference = duration;
100:
101: hours = (int) Math.rint(timeDifference / F_HOUR_IN_MS);
102: if (hours > 0) {
103: timeDifference = timeDifference - (hours * F_HOUR_IN_MS);
104: }
105:
106: minutes = (int) Math.rint(timeDifference / F_MINUTE_IN_MS);
107: if (minutes > 0) {
108: timeDifference = timeDifference
109: - (minutes * F_MINUTE_IN_MS);
110: }
111:
112: seconds = (int) Math.rint(timeDifference / F_SECOND_IN_MS);
113: if (seconds > 0) {
114: timeDifference = timeDifference
115: - (seconds * F_SECOND_IN_MS);
116: }
117:
118: milliseconds = timeDifference;
119:
120: output = hours + " h " + //$NON-NLS-1$
121: minutes + " m " + //$NON-NLS-1$
122: seconds + " s " + //$NON-NLS-1$
123: milliseconds + " ms"; //$NON-NLS-1$
124:
125: return output;
126: }
127:
128: /**
129: * @param writer
130: */
131: public void printDuration(Writer writer) {
132: String output = formatTag() + "(" + //$NON-NLS-1$
133: fIteration + "): " + //$NON-NLS-1$
134: formatDuration(fDuration) + "\n"; //$NON-NLS-1$
135: try {
136: writer.write(output);
137: writer.flush();
138: } catch (IOException e) {
139: // Ignore
140: }
141: }
142:
143: /**
144: * @param writer
145: */
146: public void printTotalDuration(Writer writer) {
147: String output = formatTag() + "(TOTAL " + //$NON-NLS-1$
148: fIteration + "): " + //$NON-NLS-1$
149: formatDuration(fTotalDuration) + "\n"; //$NON-NLS-1$
150: try {
151: writer.write(output);
152: writer.flush();
153: } catch (IOException e) {
154: // Ignore
155: }
156: }
157:
158: /**
159: * @param writer
160: */
161: public void printAverageDuration(Writer writer) {
162: String output = formatTag() + "(AVERAGE " + //$NON-NLS-1$
163: fIteration + "): " + //$NON-NLS-1$
164: formatDuration(fAverageDuration) + "\n"; //$NON-NLS-1$
165: try {
166: writer.write(output);
167: writer.flush();
168: } catch (IOException e) {
169: // Ignore
170: }
171: }
172:
173: /**
174: * @return
175: */
176: private String formatTag() {
177: return "[" + fTag + "]: "; //$NON-NLS-1$ //$NON-NLS-2$
178: }
179:
180: }
|