Source Code Cross Referenced for T_MultiThreadedIterations.java in  » Database-DBMS » db-derby-10.2 » org » apache » derbyTesting » unitTests » harness » 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 » Database DBMS » db derby 10.2 » org.apache.derbyTesting.unitTests.harness 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:
003:           Derby - Class org.apache.derbyTesting.unitTests.harness.T_MultiThreadedIterations
004:
005:           Licensed to the Apache Software Foundation (ASF) under one or more
006:           contributor license agreements.  See the NOTICE file distributed with
007:           this work for additional information regarding copyright ownership.
008:           The ASF licenses this file to You under the Apache License, Version 2.0
009:           (the "License"); you may not use this file except in compliance with
010:           the License.  You may obtain a copy of the License at
011:
012:              http://www.apache.org/licenses/LICENSE-2.0
013:
014:           Unless required by applicable law or agreed to in writing, software
015:           distributed under the License is distributed on an "AS IS" BASIS,
016:           WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017:           See the License for the specific language governing permissions and
018:           limitations under the License.
019:
020:         */
021:
022:        package org.apache.derbyTesting.unitTests.harness;
023:
024:        import org.apache.derby.iapi.services.monitor.Monitor;
025:        import org.apache.derby.iapi.services.property.PropertyUtil;
026:
027:        import java.util.Properties;
028:
029:        /**
030:         Abstract class which executes T_MultiIterations. This allows multiple
031:         threads running T_MultiIterations.
032:
033:         This allows the setup to be performed once, and then the
034:         test itself to be run with multiple threads for a number of iterations. 
035:         The number of threads and iterations are set by the property 
036:         derby.unittests.numThreads and derby.unittests.iterations
037:         and default to 1.
038:         <P>
039:         Statistics are provided about each iteration in the error log. The statistics
040:         are time for each iteration, used and total memory changes per iteration.
041:
042:         @see T_Generic
043:         */
044:        public abstract class T_MultiThreadedIterations extends
045:                T_MultiIterations implements  Runnable {
046:            protected int threadNumber = 0;
047:
048:            static volatile boolean inError = false;
049:
050:            static int numThreads = 1;
051:            static int iterations = 1;
052:
053:            Throwable error = null;
054:            static Thread[] TestThreads;
055:            static T_MultiThreadedIterations[] TestObjects;
056:
057:            protected T_MultiThreadedIterations() {
058:                super ();
059:            }
060:
061:            /**
062:              Run the test. The test should raise an exception if it
063:              fails. runTests should return if the tests pass.
064:
065:              @exception T_Fail Test code throws these
066:             */
067:            protected void runTests() throws T_Fail {
068:                /*
069:                 ** The property name for the number of iterations is
070:                 ** derby.className.numThreads.  For example, if the test
071:                 ** class is derby.com.package.to.test.T_Tester,
072:                 ** the property name is derby.T_Tester.numThreads.
073:                 */
074:                String myClass = this .getClass().getName();
075:                String noPackage = myClass
076:                        .substring(myClass.lastIndexOf('.') + 1);
077:                String propertyName = "derby." + noPackage + ".numThreads";
078:
079:                String nthread = PropertyUtil.getSystemProperty(propertyName);
080:                if (nthread != null) {
081:                    try {
082:                        numThreads = Integer.parseInt(nthread);
083:                    } catch (NumberFormatException nfe) {
084:                        numThreads = 1;
085:                    }
086:                    if (numThreads <= 0)
087:                        numThreads = 1;
088:                }
089:
090:                if (numThreads == 1) // just use this thread
091:                    super .runTests(); // use T_MultiIterations runtest
092:                else {
093:                    // start numThreads new threads, each with its own test object
094:                    TestThreads = new Thread[numThreads];
095:                    TestObjects = new T_MultiThreadedIterations[numThreads];
096:
097:                    inError = false;
098:
099:                    for (int i = 0; i < numThreads; i++) {
100:                        TestObjects[i] = newTestObject();
101:                        TestObjects[i].out = this .out;
102:
103:                        TestThreads[i] = new Thread(TestObjects[i], "Thread_"
104:                                + i);
105:                    }
106:
107:                    // use the first test object to setup the test
108:                    TestObjects[0].setupTest();
109:                    TestObjects[0].threadNumber = 0;
110:
111:                    // make the other test objects to join in the setup
112:                    for (int i = 1; i < numThreads; i++) {
113:                        TestObjects[i].threadNumber = i;
114:                        TestObjects[i].joinSetupTest();
115:                    }
116:
117:                    // now run them 
118:                    propertyName = "derby." + noPackage + ".iterations";
119:
120:                    String iter = PropertyUtil.getSystemProperty(propertyName);
121:                    if (iter != null) {
122:                        try {
123:                            iterations = Integer.parseInt(iter);
124:                        } catch (NumberFormatException nfe) {
125:                            // leave at one
126:                        }
127:                        if (iterations <= 0)
128:                            iterations = 1;
129:                    }
130:
131:                    for (int i = 0; i < numThreads; i++) {
132:                        TestThreads[i].start();
133:                    }
134:
135:                    // wait for the threads to end
136:                    try {
137:                        for (int i = 0; i < numThreads; i++) {
138:                            TestThreads[i].join();
139:                        }
140:                    } catch (InterruptedException ie) {
141:                        throw T_Fail.exceptionFail(ie);
142:                    }
143:
144:                    // report error
145:                    for (int i = 0; i < numThreads; i++) {
146:                        if (TestObjects[i].error != null)
147:                            throw T_Fail.exceptionFail(TestObjects[i].error);
148:                    }
149:                }
150:            }
151:
152:            /*
153:             * run each worker test thread
154:             */
155:            public void run() {
156:                String threadName = "[" + Thread.currentThread().getName()
157:                        + "] ";
158:
159:                out.println(threadName + "started");
160:
161:                try {
162:
163:                    for (int i = 0; i < iterations; i++) {
164:                        Runtime.getRuntime().gc();
165:                        long btm = Runtime.getRuntime().totalMemory();
166:                        long bfm = Runtime.getRuntime().freeMemory();
167:                        long bum = btm - bfm;
168:
169:                        long start = System.currentTimeMillis();
170:
171:                        runTestSet();
172:                        long end = System.currentTimeMillis();
173:
174:                        Runtime.getRuntime().gc();
175:                        long atm = Runtime.getRuntime().totalMemory();
176:                        long afm = Runtime.getRuntime().freeMemory();
177:                        long aum = atm - afm;
178:
179:                        out.println(threadName + "Iteration " + i + " took "
180:                                + (end - start) + "ms");
181:                        out.println(threadName + "Total memory increased by "
182:                                + (atm - btm) + " is " + atm);
183:                        out.println(threadName + "Used  memory increased by "
184:                                + (aum - bum) + " is " + aum);
185:                    }
186:                } catch (ThreadDeath death) // some other thread has died and want to see my stack 
187:                {
188:                    out.println(threadName
189:                            + "caught thread death, printing stack");
190:                    death.printStackTrace(out.getPrintWriter());
191:                    Thread.dumpStack();
192:
193:                    throw death;
194:                } catch (Throwable t) {
195:                    error = t;
196:                }
197:
198:                if (error == null)
199:                    out.println(threadName + "finished with no error");
200:                else if (!inError) {
201:                    inError = true;
202:
203:                    error.printStackTrace(out.getPrintWriter());
204:                    for (int i = 0; i < numThreads; i++) {
205:                        if (this  != TestObjects[i]) // don't kill myself again
206:                            TestThreads[i].interrupt();
207:                    }
208:                }
209:            }
210:
211:            /*
212:             * multi threaded test abstract methods
213:             */
214:
215:            /* 
216:             * joins an existing setup - do whatever remaining setup the test may need
217:             * to do given that setupTest has already been run by another test object
218:             *
219:             * This call will be executed in the main (parent) thread
220:             */
221:            protected abstract void joinSetupTest() throws T_Fail;
222:
223:            /*
224:             * make a new test object instance
225:             */
226:            protected abstract T_MultiThreadedIterations newTestObject();
227:
228:            /*
229:             * class specific method
230:             */
231:            protected int getNumThreads() {
232:                return numThreads;
233:            }
234:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.