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.hibernate.criterion.Restrictions; import org.junit.Test; import org.openbravo.dal.service.OBCriteria; 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 CriteriaVsQuery extends OBBaseTest { private static final int LOOPS = 50_000; private static final Logger log = LoggerFactory.getLogger(CriteriaVsQuery.class); @Test public void multiThreadQuery() throws InterruptedException { for (int threads = 1; threads <= 8; threads++) { ExecutorService executor = Executors.newFixedThreadPool(threads); List criterias = new ArrayList<>(); for (int i = 0; i < threads; i++) { criterias.add(new CallableQuery(LOOPS / 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); } } @Test public void multiThreadCriteria() throws InterruptedException { for (int threads = 1; threads <= 8; threads++) { ExecutorService executor = Executors.newFixedThreadPool(threads); List criterias = new ArrayList<>(); for (int i = 0; i < threads; i++) { criterias.add(new CallableCriteria(LOOPS / threads)); } log.info("*** Starting Criteria execution with {} threads ***", threads); long t = System.currentTimeMillis(); executor.invokeAll(criterias, 1, TimeUnit.HOURS); log.info("*** Criteria with {} threads done in {} ms ***", threads, System.currentTimeMillis() - t); } } private class CallableQuery implements Callable { private int loops; public CallableQuery(int loops) { this.loops = loops; } @Override public Void call() throws Exception { setSystemAdministratorContext(); log.info("Starting {} OBQuery executions...", loops); long t = System.currentTimeMillis(); for (int i = 0; i < loops; i++) { OBQuery q = OBDal.getInstance().createQuery(Table.class, "id = :id"); q.setNamedParameter("id", "100"); q.count(); } log.info("OBQuery: {} executions, {} ms", loops, System.currentTimeMillis() - t); return null; } } private class CallableCriteria implements Callable { private int loops; public CallableCriteria(int loops) { this.loops = loops; } @Override public Void call() throws Exception { setSystemAdministratorContext(); log.info("Starting {} OBCriteria executions...", loops); long t = System.currentTimeMillis(); for (int i = 0; i < loops; i++) { OBCriteria
c = OBDal.getInstance().createCriteria(Table.class); c.add(Restrictions.eq(Table.PROPERTY_ID, "100")); c.count(); } log.info("OBCriteria: {} executions, {} ms", loops, System.currentTimeMillis() - t); return null; } } }