MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MultithreadedFindTest.java
1 /*
2  Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
3 
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.
7 
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.
12 
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
16 */
17 
18 package testsuite.clusterj;
19 
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.Random;
23 
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;
28 
30 
31  @Override
32  protected boolean getDebug() {
33  return false;
34  }
35 
36  private int numberOfThreads = 6;
37  private int numberOfIterations = 5000;
38  private ThreadGroup threadGroup;
39 
40  @Override
41  public void localSetUp() {
42  createSessionFactory();
43  }
44 
48  public void test() {
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);
56  // create all threads
57  for (int i = 0; i < numberOfThreads ; ++i) {
58  Finder finder = new Finder();
59  Thread thread = new Thread(threadGroup, finder);
60  threads.add(thread);
61  finders.add(finder);
62  thread.start();
63  }
64  // wait until all threads have finished
65  for (Thread t: threads) {
66  try {
67  t.join();
68 
69  } catch (InterruptedException e) {
70  throw new RuntimeException("Interrupted while joining threads.");
71  }
72  }
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());
78  }
79  }
80  failOnError();
81  }
82 
87  class Finder implements Runnable {
88 
89  private Random myRandom = new Random();
90  private Session session;
91  private long time;
92 
93  public long getTime() {
94  return time;
95  }
96 
97  public void run() {
98  // get my own session
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));
105  find(r);
106  }
107  long stop = System.nanoTime();
108  time = stop - start;
109  if (getDebug()) System.out.println("Elapsed time for " + numberOfIterations + " find operations: " + (time/1000000) + "(ms.)");
110  }
111 
112  public void find(int id)
113  {
114  Subscriber subscriber = session.find(Subscriber.class, String.valueOf(id));
115  if (subscriber == null) {
116  //System.out.print(".");
117  } else {
118  String aImsi = subscriber.getImsi();
119  //System.out.print("-" + aImsi);
120  }
121  }
122 
123  }
124 
125  @PersistenceCapable(table="subscriber")
126  public interface Subscriber {
127 
128  @PrimaryKey
129  String getImsi();
130  void setImsi(String imsi);
131 
132  @Index(name="guti_UNIQUE")
133  String getGuti();
134  void setGuti(String guti);
135 
136  @Index(name="mme_s1ap_id_UNIQUE")
137  int getMme_s1ap_id();
138  void setMme_s1ap_id(int mme_s1ap_id);
139 
140  int getEnb_s1ap_id();
141  void setEnb_s1ap_id(int enb_s1ap_id);
142 
143  int getMme_teid();
144  void setMme_teid(int mme_teid);
145 
146  String getSgw_teid();
147  void setSgw_teid(String sgw_teid);
148 
149  String getPgw_teid();
150  void setPgw_teid(String pgw_teid);
151 
152  @Index(name="imei_UNIQUE")
153  String getImei();
154  void setImei(String imei);
155 
156  @Index(name="msisdn_UNIQUE")
157  String getMsisdn();
158  void setMsisdn(String msisdn);
159 
160  String getEcm_state();
161  void setEcm_state(String ecm_state);
162 
163  String getEmm_state();
164  void setEmm_state(String emm_state);
165 
166  String getEps_cgi();
167  void setEps_cgi(String eps_cgi);
168 
169  String getGlobal_enb_id();
170  void setGlobal_enb_id(String global_enb_id);
171 
172  String getBearer_id();
173  void setBearer_id(String bearer_id);
174 
175  String getSgw_ip_addr();
176  void setSgw_ip_addr(String sgw_ip_addr);
177  }
178 
179 }