1 24 25 package benchmark.ring; 26 27 import java.text.DecimalFormat ; 28 import java.text.NumberFormat ; 29 import java.util.Collections ; 30 import java.util.Map ; 31 32 import org.objectweb.dream.AbstractComponent; 33 import org.objectweb.dream.Push; 34 import org.objectweb.dream.PushException; 35 import org.objectweb.dream.control.activity.Util; 36 import org.objectweb.dream.control.activity.task.AbstractTask; 37 import org.objectweb.dream.message.Message; 38 import org.objectweb.dream.message.MessageTypeImpl; 39 import org.objectweb.dream.message.manager.MessageManager; 40 import org.objectweb.fractal.api.Component; 41 import org.objectweb.fractal.api.NoSuchInterfaceException; 42 import org.objectweb.fractal.api.control.IllegalBindingException; 43 import org.objectweb.fractal.api.control.IllegalLifeCycleException; 44 import org.objectweb.util.monolog.api.BasicLevel; 45 46 49 public class RingStatImpl extends AbstractComponent 50 implements 51 Push, 52 PayLoadSizeAttributeController 53 { 54 55 protected boolean messageSent = false; 56 protected long nbRing = 0; 57 protected long nbRingSinceLast = 0; 58 protected int nbDisplay = 0; 59 protected long lastTimestamp = 0; 60 protected long firstTimestamp = 0; 61 protected double last5sum = 0; 62 protected double last10sum = 0; 63 protected double last15sum = 0; 64 protected double[] last15Rates = new double[15]; 65 protected int last15RatesIndex = 0; 66 protected NumberFormat rateFormatter = new DecimalFormat (".00"); ; 67 68 72 protected int payLoadSize = -1; 73 74 78 protected Push outPushItf; 79 protected MessageManager messageManagerItf; 80 81 85 protected class StatPrinterTask extends AbstractTask 86 { 87 88 89 public StatPrinterTask() 90 { 91 super("StatPrinterTask"); 92 } 93 94 97 public Object execute(Object hints) throws InterruptedException 98 { 99 if (!messageSent) 100 { 101 Message message = messageManagerItf.createMessage(new MessageTypeImpl( 102 PayLoadChunk.DEFAULT_NAME, PayLoadChunk.TYPE)); 103 PayLoadChunk chunk = (PayLoadChunk) message 104 .getChunk(PayLoadChunk.DEFAULT_NAME); 105 chunk.setPayLoadSize(payLoadSize); 106 synchronized (RingStatImpl.this) 107 { 108 try 109 { 110 outPushItf.push(message, null); 111 } 112 catch (PushException e) 113 { 114 logger.log(BasicLevel.ERROR, "Unable to push message", e); 115 } 116 } 117 messageSent = true; 118 } 119 Thread.sleep(5000); 120 121 if (nbDisplay % 50 == 0) 122 { 123 System.out 125 .println("|-------|------------|------------|------------|------------|------------|-------------|"); 126 System.out 127 .println("| | last | last 5 | last 10 | last 15 | global | nb ring |"); 128 System.out 129 .println("|-------|------------|------------|------------|------------|------------|-------------|"); 130 } 131 nbDisplay++; 132 long nextTimestamp = System.currentTimeMillis(); 133 if (lastTimestamp == 0) 134 { 135 lastTimestamp = firstTimestamp; 136 } 137 138 nbRing += nbRingSinceLast; 139 double rate = nbRingSinceLast * 1000.0 / (nextTimestamp - lastTimestamp); 140 double globalRate = nbRing * 1000.0 / (nextTimestamp - firstTimestamp); 141 last5sum += rate; 142 last10sum += rate; 143 last15sum += rate; 144 if (nbDisplay >= 5) 145 { 146 last5sum -= last15Rates[(last15RatesIndex + 10) % 15]; 147 if (nbDisplay >= 10) 148 { 149 last10sum -= last15Rates[(last15RatesIndex + 5) % 15]; 150 if (nbDisplay >= 15) 151 { 152 last15sum -= last15Rates[last15RatesIndex]; 153 } 154 } 155 } 156 last15Rates[last15RatesIndex] = rate; 157 last15RatesIndex = (last15RatesIndex + 1) % 15; 158 nbRingSinceLast = 0; 159 lastTimestamp = nextTimestamp; 160 161 StringBuffer buffer = new StringBuffer ("| "); 162 formatLong(buffer, nbDisplay, 5).append(" | "); 163 formatDouble(buffer, rate, 10).append(" | "); 164 if (nbDisplay < 5) 165 { 166 buffer.append(" | | | "); 167 } 168 else 169 { 170 formatDouble(buffer, last5sum / 5, 10).append(" | "); 171 if (nbDisplay < 10) 172 { 173 buffer.append(" | | "); 174 } 175 else 176 { 177 formatDouble(buffer, last10sum / 10, 10).append(" | "); 178 if (nbDisplay < 15) 179 { 180 buffer.append(" | "); 181 } 182 else 183 { 184 formatDouble(buffer, last15sum / 15, 10).append(" | "); 185 } 186 } 187 } 188 formatDouble(buffer, globalRate, 10).append(" | "); 189 formatLong(buffer, nbRing, 11).append(" |"); 190 System.out.println(buffer); 191 192 return EXECUTE_AGAIN; 193 } 194 } 195 196 200 203 protected void beforeFirstStart(Component componentItf) 204 throws IllegalLifeCycleException 205 { 206 try 207 { 208 Util.addTask(componentItf, new StatPrinterTask(), Collections.EMPTY_MAP); 209 } 210 catch (Exception e) 211 { 212 throw new IllegalLifeCycleException("Can't add task"); 213 } 214 } 215 216 220 223 public void push(Message message, Map context) throws PushException 224 { 225 synchronized (this) 226 { 227 if (firstTimestamp == 0) 228 { 229 firstTimestamp = System.currentTimeMillis(); 230 } 231 nbRingSinceLast++; 232 outPushItf.push(message, context); 233 } 234 } 235 236 240 243 public int getPayLoadSize() 244 { 245 return payLoadSize; 246 } 247 248 251 public void setPayLoadSize(int nbByte) 252 { 253 payLoadSize = nbByte; 254 } 255 256 260 263 public String [] listFc() 264 { 265 return new String []{MessageManager.ITF_NAME, Push.OUT_PUSH_ITF_NAME}; 266 } 267 268 272 public void bindFc(String clientItfName, Object serverItf) 273 throws NoSuchInterfaceException, IllegalBindingException, 274 IllegalLifeCycleException 275 { 276 super.bindFc(clientItfName, serverItf); 277 if (clientItfName.equals(MessageManager.ITF_NAME)) 278 { 279 messageManagerItf = (MessageManager) serverItf; 280 } 281 else if (clientItfName.equals(Push.OUT_PUSH_ITF_NAME)) 282 { 283 outPushItf = (Push) serverItf; 284 } 285 } 286 287 291 private StringBuffer formatLong(StringBuffer buffer, long value, int nbChar) 292 { 293 String s = Long.toString(value); 294 for (int i = s.length(); i < nbChar; i++) 295 { 296 buffer.append(' '); 297 } 298 return buffer.append(s); 299 } 300 301 private StringBuffer formatDouble(StringBuffer buffer, double value, 302 int nbChar) 303 { 304 String s = rateFormatter.format(value); 305 for (int i = s.length(); i < nbChar; i++) 306 { 307 buffer.append(' '); 308 } 309 return buffer.append(s); 310 } 311 312 } | Popular Tags |