Source Code Cross Referenced for TimerTortureTestCase.java in  » EJB-Server-JBoss-4.2.1 » jmx » test » performance » timer » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » EJB Server JBoss 4.2.1 » jmx » test.performance.timer 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * JBoss, Home of Professional Open Source.
003:         * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004:         * as indicated by the @author tags. See the copyright.txt file in the
005:         * distribution for a full listing of individual contributors.
006:         *
007:         * This is free software; you can redistribute it and/or modify it
008:         * under the terms of the GNU Lesser General Public License as
009:         * published by the Free Software Foundation; either version 2.1 of
010:         * the License, or (at your option) any later version.
011:         *
012:         * This software is distributed in the hope that it will be useful,
013:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
014:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015:         * Lesser General Public License for more details.
016:         *
017:         * You should have received a copy of the GNU Lesser General Public
018:         * License along with this software; if not, write to the Free
019:         * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020:         * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021:         */
022:        package test.performance.timer;
023:
024:        import java.util.Date;
025:
026:        import javax.management.MBeanServer;
027:        import javax.management.MBeanServerFactory;
028:        import javax.management.Notification;
029:        import javax.management.NotificationListener;
030:        import javax.management.ObjectName;
031:        import javax.management.timer.Timer;
032:        import javax.management.timer.TimerNotification;
033:
034:        import junit.framework.TestCase;
035:        import test.performance.PerformanceSUITE;
036:
037:        /**
038:         * Timer Performance Tests
039:         *
040:         * @author  <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
041:         */
042:        public class TimerTortureTestCase extends TestCase implements 
043:                NotificationListener {
044:            // Constants ---------------------------------------------------------------
045:
046:            String TIMER_TYPE = "TimerType";
047:            String MESSAGE = "Message";
048:            String USER_DATA = "UserData";
049:
050:            // Attributes --------------------------------------------------------------
051:
052:            /**
053:             * The MBeanServer
054:             */
055:            MBeanServer server;
056:
057:            /**
058:             * The object name of the timer
059:             */
060:            ObjectName timerName;
061:
062:            /**
063:             * The timer
064:             */
065:            Timer timer;
066:
067:            /**
068:             * The timer notification id
069:             */
070:            Integer id;
071:
072:            /**
073:             * The target notifications
074:             */
075:            int target = 0;
076:
077:            /**
078:             * The number of notifications
079:             */
080:            int notifications = 0;
081:
082:            // Constructor -------------------------------------------------------------
083:
084:            /**
085:             * Construct the test
086:             */
087:            public TimerTortureTestCase(String s) {
088:                super (s);
089:            }
090:
091:            // Tests -------------------------------------------------------------------
092:
093:            /**
094:             * See how long to do many notifications one notification, 
095:             * this tests the overhead
096:             */
097:            public void testTortureOne() {
098:                System.err.println("\nTimer iterations "
099:                        + PerformanceSUITE.TIMER_ITERATION_COUNT);
100:                System.err
101:                        .println("One notification at 1 millsecond intervals.");
102:                initTest();
103:                try {
104:                    initTimer();
105:                    startTimer();
106:                    target = PerformanceSUITE.TIMER_ITERATION_COUNT;
107:                    long start = timeOffset(0).getTime();
108:                    addNotification(1000, 1,
109:                            PerformanceSUITE.TIMER_ITERATION_COUNT);
110:                    sync();
111:                    sleep(1000);
112:                    long end = timeOffset(0).getTime();
113:                    stopTimer();
114:
115:                    System.err.println("Time (ms): " + (end - start));
116:                } finally {
117:                    MBeanServerFactory.releaseMBeanServer(server);
118:                }
119:            }
120:
121:            /**
122:             * See how long to many notifications one notification, this tests the overhead
123:             */
124:            public void testTortureTen() {
125:                System.err.println("\nTimer iterations "
126:                        + PerformanceSUITE.TIMER_ITERATION_COUNT);
127:                System.err
128:                        .println("Ten notifications at 1 millsecond intervals.");
129:                initTest();
130:                try {
131:                    initTimer();
132:                    startTimer();
133:                    target = 10 * PerformanceSUITE.TIMER_ITERATION_COUNT;
134:                    long start = timeOffset(0).getTime();
135:                    for (int i = 0; i < 10; i++)
136:                        addNotification(1000, 1,
137:                                PerformanceSUITE.TIMER_ITERATION_COUNT);
138:                    sync();
139:                    sleep(1000);
140:                    long end = timeOffset(0).getTime();
141:                    stopTimer();
142:
143:                    System.err.println("Time (ms): " + (end - start));
144:                } finally {
145:                    MBeanServerFactory.releaseMBeanServer(server);
146:                }
147:            }
148:
149:            // Support -----------------------------------------------------------------
150:
151:            /**
152:             * Start a new test
153:             */
154:            private void initTest() {
155:                notifications = 0;
156:                server = MBeanServerFactory.createMBeanServer();
157:            }
158:
159:            /**
160:             * Create a timer and register ourselves as a listener
161:             */
162:            private void initTimer() {
163:                try {
164:                    timer = new Timer();
165:                    timerName = new ObjectName("test:type=timer");
166:                    server.registerMBean(timer, timerName);
167:                    server.addNotificationListener(timerName, this , null, null);
168:                } catch (Exception e) {
169:                    fail(e.toString());
170:                }
171:            }
172:
173:            /**
174:             * Start the timer
175:             */
176:            private void startTimer() {
177:                timer.start();
178:            }
179:
180:            /**
181:             * Stop the timer, does a small wait to avoid problems with the RI
182:             */
183:            private void stopTimer() {
184:                timer.stop();
185:            }
186:
187:            /**
188:             * Add a notification
189:             */
190:            private void addNotification(long offset, long period, long occurs) {
191:                id = timer.addNotification(TIMER_TYPE, MESSAGE, USER_DATA,
192:                        timeOffset(offset), period, occurs);
193:            }
194:
195:            /**
196:             * Handle the notification, just add it to the list
197:             */
198:            public void handleNotification(Notification n, Object ignored) {
199:                notifications++;
200:                TimerNotification tn = (TimerNotification) n;
201:                if (timer.getNbOccurences(tn.getNotificationID()).longValue() == 1)
202:                    synchronized (timerName) {
203:                        timerName.notifyAll();
204:                    }
205:            }
206:
207:            /**
208:             * Sync with the notification handler
209:             */
210:            private void sync() {
211:                synchronized (timerName) {
212:                    try {
213:                        timerName.wait(60000);
214:                    } catch (InterruptedException ignored) {
215:                    }
216:                }
217:            }
218:
219:            /**
220:             * Get the time using and offset
221:             */
222:            private Date timeOffset(long offset) {
223:                return new Date(System.currentTimeMillis() + offset);
224:            }
225:
226:            /**
227:             * Sleep for a bit
228:             */
229:            private void sleep(long time) {
230:                try {
231:                    Thread.sleep(time);
232:                } catch (InterruptedException ignored) {
233:                }
234:            }
235:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.