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, version 2.0,
6    as published by the Free Software Foundation.
7 
8    This program is also distributed with certain software (including
9    but not limited to OpenSSL) that is licensed under separate terms,
10    as designated in a particular file or component or in included license
11    documentation.  The authors of MySQL hereby grant you an additional
12    permission to link the program and your derivative works with the
13    separately licensed software that they have included with MySQL.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License, version 2.0, for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
23 */
24 
25 package testsuite.clusterj;
26 
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.Random;
30 
31 import com.mysql.clusterj.Session;
32 import com.mysql.clusterj.annotation.Index;
33 import com.mysql.clusterj.annotation.PersistenceCapable;
34 import com.mysql.clusterj.annotation.PrimaryKey;
35 
36 public class MultithreadedFindTest extends AbstractClusterJModelTest {
37 
38     @Override
getDebug()39     protected boolean getDebug() {
40         return false;
41     }
42 
43     private int numberOfThreads = 6;
44     private int numberOfIterations = 5000;
45     private ThreadGroup threadGroup;
46 
47     @Override
localSetUp()48     public void localSetUp() {
49         createSessionFactory();
50     }
51 
52     /** The test method creates numberOfThreads threads and starts them.
53      * Once the threads are started, the main thread waits until all threads complete.
54      */
test()55     public void test() {
56         List<Thread> threads = new ArrayList<Thread>();
57         List<Finder> finders = new ArrayList<Finder>();
58         // create thread group
59         threadGroup = new ThreadGroup("Finder");
60         // create uncaught exception handler
61         MyUncaughtExceptionHandler uncaughtExceptionHandler = new MyUncaughtExceptionHandler();
62         Thread.setDefaultUncaughtExceptionHandler(uncaughtExceptionHandler);
63         // create all threads
64         for (int i = 0; i < numberOfThreads ; ++i) {
65             Finder finder = new Finder();
66             Thread thread = new Thread(threadGroup, finder);
67             threads.add(thread);
68             finders.add(finder);
69             thread.start();
70         }
71         // wait until all threads have finished
72         for (Thread t: threads) {
73             try {
74                 t.join();
75 
76             } catch (InterruptedException e) {
77                 throw new RuntimeException("Interrupted while joining threads.");
78             }
79         }
80         for (Throwable thrown: uncaughtExceptionHandler.getUncaughtExceptions()) {
81             error("Caught exception: " + thrown.getClass().getName() + ": " + thrown.getMessage());
82             StackTraceElement[] elements = thrown.getStackTrace();
83             for (StackTraceElement element: elements) {
84                 error("        at " + element.toString());
85             }
86         }
87         failOnError();
88     }
89 
90     /** This class implements the logic per thread. For each thread created,
91      * the run method is invoked.
92      * Each thread uses its own session.
93      */
94     class Finder implements Runnable {
95 
96         private Random myRandom = new Random();
97         private Session session;
98         private long time;
99 
getTime()100         public long getTime() {
101             return time;
102         }
103 
run()104         public void run() {
105             // get my own session
106             if (getDebug()) System.out.println("Getting session.");
107             session = sessionFactory.getSession();
108             long start = System.nanoTime();
109             if (getDebug()) System.out.println("Finding " + numberOfIterations + " subscribers.");
110             for(int i = 0; i < numberOfIterations; i++ ) {
111                int r = (int) (myRandom.nextInt(4000));
112                find(r);
113             }
114             long stop = System.nanoTime();
115             time = stop - start;
116             if (getDebug()) System.out.println("Elapsed time for " + numberOfIterations + " find operations: " + (time/1000000) + "(ms.)");
117         }
118 
find(int id)119         public void find(int id)
120         {
121             Subscriber subscriber = session.find(Subscriber.class, String.valueOf(id));
122             if (subscriber == null) {
123                 //System.out.print(".");
124             } else {
125                 String aImsi = subscriber.getImsi();
126                 //System.out.print("-" + aImsi);
127             }
128         }
129 
130     }
131 
132     @PersistenceCapable(table="subscriber")
133     public interface Subscriber {
134 
135         @PrimaryKey
getImsi()136         String getImsi();
setImsi(String imsi)137         void setImsi(String imsi);
138 
139         @Index(name="guti_UNIQUE")
getGuti()140         String getGuti();
setGuti(String guti)141         void setGuti(String guti);
142 
143         @Index(name="mme_s1ap_id_UNIQUE")
getMme_s1ap_id()144         int getMme_s1ap_id();
setMme_s1ap_id(int mme_s1ap_id)145         void setMme_s1ap_id(int mme_s1ap_id);
146 
getEnb_s1ap_id()147         int getEnb_s1ap_id();
setEnb_s1ap_id(int enb_s1ap_id)148         void setEnb_s1ap_id(int enb_s1ap_id);
149 
getMme_teid()150         int getMme_teid();
setMme_teid(int mme_teid)151         void setMme_teid(int mme_teid);
152 
getSgw_teid()153         String getSgw_teid();
setSgw_teid(String sgw_teid)154         void setSgw_teid(String sgw_teid);
155 
getPgw_teid()156         String getPgw_teid();
setPgw_teid(String pgw_teid)157         void setPgw_teid(String pgw_teid);
158 
159         @Index(name="imei_UNIQUE")
getImei()160         String getImei();
setImei(String imei)161         void setImei(String imei);
162 
163         @Index(name="msisdn_UNIQUE")
getMsisdn()164         String getMsisdn();
setMsisdn(String msisdn)165         void setMsisdn(String msisdn);
166 
getEcm_state()167         String getEcm_state();
setEcm_state(String ecm_state)168         void setEcm_state(String ecm_state);
169 
getEmm_state()170         String getEmm_state();
setEmm_state(String emm_state)171         void setEmm_state(String emm_state);
172 
getEps_cgi()173         String getEps_cgi();
setEps_cgi(String eps_cgi)174         void setEps_cgi(String eps_cgi);
175 
getGlobal_enb_id()176         String getGlobal_enb_id();
setGlobal_enb_id(String global_enb_id)177         void setGlobal_enb_id(String global_enb_id);
178 
getBearer_id()179         String getBearer_id();
setBearer_id(String bearer_id)180         void setBearer_id(String bearer_id);
181 
getSgw_ip_addr()182         String getSgw_ip_addr();
setSgw_ip_addr(String sgw_ip_addr)183         void setSgw_ip_addr(String sgw_ip_addr);
184   }
185 
186 }
187