2 Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; version 2 of the License.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 package testsuite.clusterj;
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.Random;
24 import com.mysql.clusterj.Session;
25 import com.mysql.clusterj.annotation.Index;
26 import com.mysql.clusterj.annotation.PersistenceCapable;
27 import com.mysql.clusterj.annotation.PrimaryKey;
29 public class MultithreadedFindTest extends AbstractClusterJModelTest {
32 protected boolean getDebug() {
36 private int numberOfThreads = 6;
37 private int numberOfIterations = 5000;
38 private ThreadGroup threadGroup;
41 public void localSetUp() {
42 createSessionFactory();
45 /** The test method creates numberOfThreads threads and starts them.
46 * Once the threads are started, the main thread waits until all threads complete.
49 List<Thread> threads = new ArrayList<Thread>();
50 List<Finder> finders = new ArrayList<Finder>();
51 // create thread group
52 threadGroup = new ThreadGroup("Finder");
53 // create uncaught exception handler
54 MyUncaughtExceptionHandler uncaughtExceptionHandler = new MyUncaughtExceptionHandler();
55 Thread.setDefaultUncaughtExceptionHandler(uncaughtExceptionHandler);
57 for (int i = 0; i < numberOfThreads ; ++i) {
58 Finder finder = new Finder();
59 Thread thread = new Thread(threadGroup, finder);
64 // wait until all threads have finished
65 for (Thread t: threads) {
69 } catch (InterruptedException e) {
70 throw new RuntimeException("Interrupted while joining threads.");
73 for (Throwable thrown: uncaughtExceptionHandler.getUncaughtExceptions()) {
74 error("Caught exception: " + thrown.getClass().getName() + ": " + thrown.getMessage());
75 StackTraceElement[] elements = thrown.getStackTrace();
76 for (StackTraceElement element: elements) {
77 error(" at " + element.toString());
83 /** This class implements the logic per thread. For each thread created,
84 * the run method is invoked.
85 * Each thread uses its own session.
87 class Finder implements Runnable {
89 private Random myRandom = new Random();
90 private Session session;
93 public long getTime() {
99 if (getDebug()) System.out.println("Getting session.");
100 session = sessionFactory.getSession();
101 long start = System.nanoTime();
102 if (getDebug()) System.out.println("Finding " + numberOfIterations + " subscribers.");
103 for(int i = 0; i < numberOfIterations; i++ ) {
104 int r = (int) (myRandom.nextInt(4000));
107 long stop = System.nanoTime();
109 if (getDebug()) System.out.println("Elapsed time for " + numberOfIterations + " find operations: " + (time/1000000) + "(ms.)");
112 public void find(int id)
114 Subscriber subscriber = session.find(Subscriber.class, String.valueOf(id));
115 if (subscriber == null) {
116 //System.out.print(".");
118 String aImsi = subscriber.getImsi();
119 //System.out.print("-" + aImsi);
125 @PersistenceCapable(table="subscriber")
126 public interface Subscriber {
130 void setImsi(String imsi);
132 @Index(name="guti_UNIQUE")
134 void setGuti(String guti);
136 @Index(name="mme_s1ap_id_UNIQUE")
137 int getMme_s1ap_id();
138 void setMme_s1ap_id(int mme_s1ap_id);
140 int getEnb_s1ap_id();
141 void setEnb_s1ap_id(int enb_s1ap_id);
144 void setMme_teid(int mme_teid);
146 String getSgw_teid();
147 void setSgw_teid(String sgw_teid);
149 String getPgw_teid();
150 void setPgw_teid(String pgw_teid);
152 @Index(name="imei_UNIQUE")
154 void setImei(String imei);
156 @Index(name="msisdn_UNIQUE")
158 void setMsisdn(String msisdn);
160 String getEcm_state();
161 void setEcm_state(String ecm_state);
163 String getEmm_state();
164 void setEmm_state(String emm_state);
167 void setEps_cgi(String eps_cgi);
169 String getGlobal_enb_id();
170 void setGlobal_enb_id(String global_enb_id);
172 String getBearer_id();
173 void setBearer_id(String bearer_id);
175 String getSgw_ip_addr();
176 void setSgw_ip_addr(String sgw_ip_addr);