1 11 12 package org.eclipse.pde.internal.core.util; 13 14 import java.io.IOException ; 15 import java.io.Writer ; 16 17 21 public class PDEPerfTesterUtil { 22 23 private String fTag; 24 25 private long fDuration; 26 27 private long fStart; 28 29 private long fEnd; 30 31 private long fIteration; 32 33 private long fTotalDuration; 34 35 private long fAverageDuration; 36 37 private final static long F_SECOND_IN_MS = 1000; 38 39 private final static long F_MINUTE_IN_MS = 60000; 40 41 private final static long F_HOUR_IN_MS = 3600000; 42 43 46 public PDEPerfTesterUtil(String tag) { 47 fTag = tag; 48 reset(); 49 } 50 51 54 public void reset() { 55 fDuration = 0; 56 fStart = 0; 57 fEnd = 0; 58 fIteration = 0; 59 fTotalDuration = 0; 60 fAverageDuration = 0; 61 } 62 63 66 public void start() { 67 fIteration++; 68 fStart = System.currentTimeMillis(); 69 } 70 71 74 public void stop() { 75 fEnd = System.currentTimeMillis(); 76 calculateDuration(); 77 } 78 79 82 private void calculateDuration() { 83 fDuration = (fEnd - fStart); 84 fTotalDuration = fTotalDuration + fDuration; 85 if (fIteration > 0) { 86 fAverageDuration = fTotalDuration / fIteration; 87 } 88 } 89 90 94 private String formatDuration(long duration) { 95 96 String output = null; 97 int hours = 0; 98 int minutes = 0; 99 int seconds = 0; 100 long milliseconds = 0; 101 long timeDifference = duration; 102 103 hours = (int)Math.rint(timeDifference / F_HOUR_IN_MS); 104 if (hours > 0) { 105 timeDifference = timeDifference - (hours * F_HOUR_IN_MS); 106 } 107 108 minutes = (int)Math.rint(timeDifference / F_MINUTE_IN_MS); 109 if (minutes > 0) { 110 timeDifference = timeDifference - (minutes * F_MINUTE_IN_MS); 111 } 112 113 seconds = (int)Math.rint(timeDifference / F_SECOND_IN_MS); 114 if (seconds > 0) { 115 timeDifference = timeDifference - (seconds * F_SECOND_IN_MS); 116 } 117 118 milliseconds = timeDifference; 119 120 output = hours + " h " + minutes + " m " + seconds + " s " + milliseconds + " ms"; 125 return output; 126 } 127 128 131 public void printDuration(Writer writer) { 132 String output = formatTag() + 133 "(" + fIteration + 135 "): " + formatDuration(fDuration) + 137 "\n"; try { 139 writer.write(output); 140 writer.flush(); 141 } catch (IOException e) { 142 } 144 } 145 146 149 public void printTotalDuration(Writer writer) { 150 String output = formatTag() + 151 "(TOTAL " + fIteration + 153 "): " + formatDuration(fTotalDuration) + 155 "\n"; try { 157 writer.write(output); 158 writer.flush(); 159 } catch (IOException e) { 160 } 162 } 163 164 167 public void printAverageDuration(Writer writer) { 168 String output = formatTag() + 169 "(AVERAGE " + fIteration + 171 "): " + formatDuration(fAverageDuration) + 173 "\n"; try { 175 writer.write(output); 176 writer.flush(); 177 } catch (IOException e) { 178 } 180 } 181 182 185 private String formatTag() { 186 return "[" + fTag + "]: "; } 188 189 } 190 | Popular Tags |