package org.openbravo.test.dal; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import org.junit.Test; import org.openbravo.dal.service.OBDal; import org.openbravo.dal.service.OBQuery; import org.openbravo.model.ad.datamodel.Table; import org.openbravo.test.base.OBBaseTest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class QueryConstantContention extends OBBaseTest { private static final int LOOPS = 10_000; private static final int THREADS = 10; private static final Logger log = LoggerFactory.getLogger(QueryConstantContention.class); @Test public void multiThreadQuery() throws InterruptedException { ExecutorService executor = Executors.newFixedThreadPool(THREADS); List criterias = new ArrayList<>(); for (int i = 0; i < THREADS; i++) { criterias.add(new CallableQuery(LOOPS / THREADS, i * THREADS)); } log.info("*** Starting Query execution with {} threads ***", THREADS); long t = System.currentTimeMillis(); executor.invokeAll(criterias, 1, TimeUnit.HOURS); log.info("*** Query with {} threads done in {} ms ***", THREADS, System.currentTimeMillis() - t); } private class CallableQuery implements Callable { private int loops; private int id; public CallableQuery(int loops, int id) { this.loops = loops; } @Override public Void call() throws Exception { setSystemAdministratorContext(); String qry = "as t where t.id = '100' \n" + // "and t.active = true \n" + // "and t.client.id = '0' \n" + // "and t.organization.id = '0' \n" + // "and t.dBTableName='AD_Table' \n"; log.info("Starting {} OBQuery executions...", loops); long t = System.currentTimeMillis(); for (int i = 0; i < loops; i++) { int dummy = id + i; // to ensure no query is cached qry += "and " + (id + i) + "=" + (id + i); OBQuery q = OBDal.getInstance().createQuery(Table.class, qry); q.count(); } log.info("OBQuery: {} executions, {} ms", loops, System.currentTimeMillis() - t); return null; } } }