KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > benchmark > ring > RingStatImpl


1 /**
2  * Dream
3  * Copyright (C) 2003-2004 INRIA Rhone-Alpes
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: dream@objectweb.org
20  *
21  * Initial developer(s): Matthieu Leclercq
22  * Contributor(s):
23  */

24
25 package benchmark.ring;
26
27 import java.text.DecimalFormat JavaDoc;
28 import java.text.NumberFormat JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.Map JavaDoc;
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 /**
47  *
48  */

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 JavaDoc rateFormatter = new DecimalFormat JavaDoc(".00"); ;
67
68   // ---------------------------------------------------------------------------
69
// Attribute fields
70
// ---------------------------------------------------------------------------
71

72   protected int payLoadSize = -1;
73
74   // ---------------------------------------------------------------------------
75
// Client interfaces
76
// ---------------------------------------------------------------------------
77

78   protected Push outPushItf;
79   protected MessageManager messageManagerItf;
80
81   // ---------------------------------------------------------------------------
82
// Inner task class implementation
83
// ---------------------------------------------------------------------------
84

85   protected class StatPrinterTask extends AbstractTask
86   {
87
88     /** Constructor */
89     public StatPrinterTask()
90     {
91       super("StatPrinterTask");
92     }
93
94     /**
95      * @see org.objectweb.deployment.scheduling.core.api.Task#execute(Object)
96      */

97     public Object JavaDoc execute(Object JavaDoc hints) throws InterruptedException JavaDoc
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         // print header.
124
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 JavaDoc buffer = new StringBuffer JavaDoc("| ");
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   // -------------------------------------------------------------------------
197
// Overriden methods
198
// -------------------------------------------------------------------------
199

200   /**
201    * @see org.objectweb.dream.AbstractComponent#beforeFirstStart(org.objectweb.fractal.api.Component)
202    */

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 JavaDoc e)
211     {
212       throw new IllegalLifeCycleException("Can't add task");
213     }
214   }
215
216   // ---------------------------------------------------------------------------
217
// Implementation of the Push interface
218
// ---------------------------------------------------------------------------
219

220   /**
221    * @see Push#push(Message, Map)
222    */

223   public void push(Message message, Map JavaDoc 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   // ---------------------------------------------------------------------------
237
// Implementation of the AttributeController interface
238
// ---------------------------------------------------------------------------
239

240   /**
241    * @see PayLoadSizeAttributeController#getPayLoadSize()
242    */

243   public int getPayLoadSize()
244   {
245     return payLoadSize;
246   }
247
248   /**
249    * @see PayLoadSizeAttributeController#setPayLoadSize(int)
250    */

251   public void setPayLoadSize(int nbByte)
252   {
253     payLoadSize = nbByte;
254   }
255
256   // ---------------------------------------------------------------------------
257
// Implementation of the BindingController interface
258
// ---------------------------------------------------------------------------
259

260   /**
261    * @see org.objectweb.fractal.api.control.BindingController#listFc()
262    */

263   public String JavaDoc[] listFc()
264   {
265     return new String JavaDoc[]{MessageManager.ITF_NAME, Push.OUT_PUSH_ITF_NAME};
266   }
267
268   /**
269    * @see org.objectweb.fractal.api.control.BindingController#bindFc(String,
270    * Object)
271    */

272   public void bindFc(String JavaDoc clientItfName, Object JavaDoc 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   // ---------------------------------------------------------------------------
288
// Utility methods
289
// ---------------------------------------------------------------------------
290

291   private StringBuffer JavaDoc formatLong(StringBuffer JavaDoc buffer, long value, int nbChar)
292   {
293     String JavaDoc 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 JavaDoc formatDouble(StringBuffer JavaDoc buffer, double value,
302       int nbChar)
303   {
304     String JavaDoc 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