18 package testsuite.clusterj;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Comparator;
23 import java.util.List;
24 import java.util.Random;
26 import java.util.TreeSet;
28 import com.mysql.clusterj.Query;
29 import com.mysql.clusterj.Session;
30 import com.mysql.clusterj.query.QueryDomainType;
32 import testsuite.clusterj.model.Customer;
33 import testsuite.clusterj.model.Order;
34 import testsuite.clusterj.model.OrderLine;
43 private int numberOfThreads = 50;
44 private int numberOfNewCustomersPerThread = 5;
45 private int numberOfNewOrdersPerNewCustomer = 5;
46 private int numberOfUpdatesPerThread = 2;
48 private int maximumOrderLinesPerOrder = 5;
49 private int maximumQuantityPerOrderLine = 100;
50 private int maximumUnitPrice = 100;
53 private int numberOfInitialCustomers = 10;
54 private int nextCustomerId = numberOfInitialCustomers;
55 private int nextOrderId = 0;
56 private int nextOrderLineId = 0;
58 private int numberOfUpdatedOrderLines = 0;
59 private int numberOfDeletedOrders = 0;
60 private int numberOfDeletedOrderLines = 0;
62 private ThreadGroup threadGroup;
65 List<Customer> customers =
new ArrayList<Customer>();
68 List<Order> orders =
new ArrayList<Order>();
71 Set<OrderLine> orderlines =
new TreeSet<OrderLine>(
72 new Comparator<OrderLine>() {
74 return o1.getId() - o2.getId();
81 createSessionFactory();
91 createCustomerInstances(nextCustomerId);
98 addTearDownClasses(
Order.class);
102 private void createCustomerInstances(
int numberToCreate) {
103 for (
int i = 0;
i < numberToCreate; ++
i) {
106 customer.setName(
"Customer number " +
i);
107 customer.setMagic(
i * 100);
108 customers.add(customer);
120 List<Thread> threads =
new ArrayList<Thread>();
122 threadGroup =
new ThreadGroup(
"Stuff");
124 MyUncaughtExceptionHandler uncaughtExceptionHandler =
new MyUncaughtExceptionHandler();
125 Thread.setDefaultUncaughtExceptionHandler(uncaughtExceptionHandler);
127 for (
int i = 0;
i < numberOfThreads ; ++
i) {
128 Thread thread =
new Thread(threadGroup,
new StuffToDo());
133 for (Thread t: threads) {
136 }
catch (InterruptedException e) {
137 throw new RuntimeException(
"Interrupted while joining threads.");
141 for (Throwable thrown: uncaughtExceptionHandler.getUncaughtExceptions()) {
142 error(
"Caught exception: " + thrown.getClass().getName() +
": " + thrown.getMessage());
143 StackTraceElement[] elements = thrown.getStackTrace();
144 for (StackTraceElement element: elements) {
145 error(
" at " + element.toString());
150 System.out.println(
"Number of threads: " + numberOfThreads +
151 "; number of new customers per thread: " + numberOfNewCustomersPerThread +
152 "; number of orders per new customer: " + numberOfNewOrdersPerNewCustomer);
153 System.out.println(
"Created " + nextCustomerId +
" customers; " +
154 nextOrderId +
" orders; and " + nextOrderLineId +
" order lines.");
155 System.out.println(
"Deleted " + numberOfDeletedOrders +
" orders; and " +
156 numberOfDeletedOrderLines +
" order lines.");
157 System.out.println(
"Updated " + numberOfUpdatedOrderLines +
" order lines.");
159 errorIfNotEqual(
"Failed to create customers.",
160 numberOfThreads * numberOfNewCustomersPerThread + numberOfInitialCustomers, nextCustomerId);
161 errorIfNotEqual(
"Failed to create orders. ",
162 numberOfThreads * numberOfNewCustomersPerThread * numberOfNewOrdersPerNewCustomer, nextOrderId);
166 queryOrderType.where(queryOrderType.get(
"orderId").equal(queryOrderType.param(
"orderId")));
168 for (
Order order: orders) {
169 int orderId = order.getId();
171 order = session.
find(
Order.class, orderId);
172 double expectedTotal = order.getValue();
173 double actualTotal = 0.0d;
174 for (
OrderLine orderLine: getOrderLines(session, query, orderId)) {
175 actualTotal += orderLine.getTotalValue();
177 errorIfNotEqual(
"For order " + orderId +
", order value does not equal sum of order line values.",
178 expectedTotal, actualTotal);
194 class StuffToDo
implements Runnable {
196 private Random myRandom =
new Random();
200 Session session = sessionFactory.getSession();
202 queryOrderType.where(queryOrderType.get(
"orderId").equal(queryOrderType.param(
"orderId")));
204 for (
int i = 0;
i < numberOfNewCustomersPerThread; ++
i) {
206 createCustomer(session,
String.valueOf(Thread.currentThread().getId()));
207 for (
int j = 0; j < numberOfNewOrdersPerNewCustomer ; ++j) {
213 for (
int j = 0; j < numberOfUpdatesPerThread; ++j) {
227 private void createCustomer(Session session,
String threadId) {
228 Customer customer = session.newInstance(Customer.class);
229 int id = getNextCustomerId();
231 customer.setName(
"Customer number " +
id +
" thread " + threadId);
232 customer.setMagic(
id * 10000);
233 session.makePersistent(customer);
234 addCustomer(customer);
246 int orderid = getNextOrderId();
248 order.setId(orderid);
250 int customerId = random .nextInt(nextCustomerId);
251 order.setCustomerId(customerId);
252 order.setDescription(
"Order " + orderid +
" for Customer " + customerId);
253 Double orderValue = 0.0d;
255 int numberOfOrderLines = random.nextInt(maximumOrderLinesPerOrder);
256 for (
int i = 0;
i < numberOfOrderLines; ++
i) {
257 int orderLineNumber = getNextOrderLineId();
259 orderLine.setId(orderLineNumber);
260 orderLine.setOrderId(orderid);
261 long quantity = random.nextInt(maximumQuantityPerOrderLine);
262 orderLine.setQuantity(quantity);
263 float unitPrice = ((float)random.nextInt(maximumUnitPrice)) / 4;
264 orderLine.setUnitPrice(unitPrice);
265 double orderLineValue = unitPrice * quantity;
266 orderValue += orderLineValue;
267 if (
getDebug()) System.out.println(
"For order " + orderid +
" orderline " + orderLineNumber +
268 " order line value " + orderLineValue +
" order value " + orderValue);
269 orderLine.setTotalValue(orderLineValue);
270 addOrderLine(orderLine);
273 order.setValue(orderValue);
289 order = removeOrderFromOrdersCollection(random);
293 int orderId = order.getId();
295 order = session.
find(
Order.class, orderId);
296 List<OrderLine> orderLines = getOrderLines(session, query, orderId);
297 int numberOfOrderLines = orderLines.size();
299 double orderValue = order.getValue();
300 if (numberOfOrderLines > 0) {
301 int index = random.nextInt(numberOfOrderLines);
302 orderLine = orderLines.get(index);
303 orderValue -= orderLine.getTotalValue();
304 updateOrderLine(orderLine, random);
305 orderValue += orderLine.getTotalValue();
307 order.setValue(orderValue);
320 private void updateOrderLine(
OrderLine orderLine, Random random) {
321 int orderid = orderLine.getOrderId();
322 int orderLineNumber = orderLine.getId();
323 double previousValue = orderLine.getTotalValue();
324 long quantity = random.nextInt(maximumQuantityPerOrderLine );
325 orderLine.setQuantity(quantity);
326 float unitPrice = ((float)random.nextInt(maximumUnitPrice)) / 4;
327 orderLine.setUnitPrice(unitPrice);
328 double orderLineValue = unitPrice * quantity;
329 orderLine.setTotalValue(orderLineValue);
330 if (
getDebug()) System.out.println(
"For order " + orderid +
" orderline " + orderLineNumber +
331 " previous order line value " + previousValue +
" new order line value " + orderLineValue);
332 synchronized (orderlines) {
333 ++numberOfUpdatedOrderLines;
347 order = removeOrderFromOrdersCollection(random);
351 int orderId = order.getId();
352 List<OrderLine> orderLines = getOrderLines(session, query, orderId);
353 removeOrderLinesFromOrderLinesCollection(orderLines);
359 private List<OrderLine> getOrderLines(
Session session, Query<OrderLine> query,
int orderId) {
360 query.setParameter(
"orderId", orderId);
361 return query.getResultList();
364 private Order removeOrderFromOrdersCollection(Random random) {
365 synchronized(orders) {
366 int numberOfOrders = orders.size();
367 if (numberOfOrders < 10) {
370 int orderId = random.nextInt(numberOfOrders);
371 ++numberOfDeletedOrders;
372 return orders.remove(orderId);
376 private void removeOrderLinesFromOrderLinesCollection(Collection<OrderLine> orderLinesToRemove) {
377 synchronized(orderlines) {
378 orderlines.removeAll(orderLinesToRemove);
379 numberOfDeletedOrderLines += orderLinesToRemove.size();
386 private void addCustomer(Customer customer) {
387 synchronized(customers) {
388 customers.add(customer);
395 private int getNextCustomerId() {
396 synchronized(customers) {
397 return nextCustomerId++;
404 private int getNextOrderId() {
405 synchronized(orders) {
406 return nextOrderId++;
413 private int getNextOrderLineId() {
414 synchronized(orderlines) {
415 return nextOrderLineId++;
423 private void addOrder(Order order) {
424 synchronized(orders) {
433 private void addOrderLine(OrderLine orderLine) {
434 synchronized(orderlines) {
435 orderlines.add(orderLine);