// sample usage public static void main(String[] args) { StopWatch s = new StopWatch(); s.start(); // code you want to time goes here s.stop(); System.out.println("elapsed time in milliseconds: " + s.getElapsedTime()); }
@Test(dataProvider = "composeAllPermutationsOfSamInputResource") public void queryInputResourcePermutation(final SamInputResource resource) throws IOException { final SamReader reader = SamReaderFactory.makeDefault().open(resource); LOG.info(String.format("Query from %s ...", resource)); if (reader.hasIndex()) { final StopWatch stopWatch = new StopWatch(); stopWatch.start(); final SAMRecordIterator q1 = reader.query("chr1", 500000, 100000000, true); observedRecordOrdering1.add(Iterables.slurp(q1)); q1.close(); final SAMRecordIterator q20 = reader.query("chr20", 1, 1000000, true); observedRecordOrdering20.add(Iterables.slurp(q20)); q20.close(); final SAMRecordIterator q3 = reader.query("chr3", 1, 10000000, true); observedRecordOrdering3.add(Iterables.slurp(q3)); q3.close(); stopWatch.stop(); LOG.info(String.format("Finished queries in %sms", stopWatch.getElapsedTime())); Assert.assertEquals( observedRecordOrdering1.size(), 1, "read different records for chromosome 1"); Assert.assertEquals( observedRecordOrdering20.size(), 1, "read different records for chromosome 20"); Assert.assertEquals( observedRecordOrdering3.size(), 1, "read different records for chromosome 3"); } else if (resource.indexMaybe() != null) { LOG.warn("Resource has an index source, but is not indexed: " + resource); } else { LOG.info("Skipping query operation: no index."); } reader.close(); }
/** Tests bulk load. */ @Test public void testBulkLoad() throws Exception { final Random random = new Random(); StopWatch stopWatch = new StopWatch(); // Add a bunch of entries for (int i = 0; i < 500; i++) { // Use a random length value final String key = "key" + i; final String value = "value" + random.nextInt(1000); // Add an element, and make sure it is present Element element = new Element(key, value); store.put(element); element = store.get(key); assertNotNull(element); // Remove the element store.remove(key); element = store.get(key); assertNull(element); element = new Element(key, value); store.put(element); element = store.get(key); assertNotNull(element); } long time = stopWatch.getElapsedTime(); LOG.info("Time for Bulk Load: " + time); }
public static void main(String[] args) { StopWatch stopWatch1 = new StopWatch(); final int LENGTH_OF_LIST = 100000; int[] list = new int[LENGTH_OF_LIST]; stopWatch1.start(); for (int i = 0; i < LENGTH_OF_LIST; i++) { list[i] = (int) (Math.random() * list.length); } for (int i = 0; i < LENGTH_OF_LIST; i++) { int currentMin = list[i]; int currentMinIndex = i; for (int j = i + 1; j < LENGTH_OF_LIST; j++) { if (currentMin > list[j]) { currentMin = list[j]; currentMinIndex = j; } } if (currentMinIndex != i) { list[currentMinIndex] = list[i]; list[i] = currentMin; } } stopWatch1.stop(); System.out.println( "The execution time of sorting 10000 numbers using selection sort is " + stopWatch1.getElapsedTime()); }
/** Benchmark to test speed. */ @Test public void testBenchmarkPutGet() throws Exception { final String key = "key"; byte[] value = new byte[500]; StopWatch stopWatch = new StopWatch(); // Add a bunch of entries for (int i = 0; i < 50000; i++) { Element element = new Element(key, value); store.put(element); } for (int i = 0; i < 50000; i++) { store.get(key + i); } long time = stopWatch.getElapsedTime(); LOG.info("Time for benchmarkPutGet: " + time); assertTrue("Too slow. Time was " + time, time < 300); }
/** Benchmark to test speed. Original implementation 12seconds This implementation 9 seconds */ public void benchmarkPutGetSuryaTest(long allowedTime) throws Exception { Random random = new Random(); byte[] value = new byte[500]; StopWatch stopWatch = new StopWatch(); // Add a bunch of entries for (int i = 0; i < 50000; i++) { String key = "key" + i; Element element = new Element(key, value); store.put(element); // Access each element random number of times, min:0 maximum:9 int accesses = random.nextInt(5); for (int j = 0; j <= accesses; j++) { store.get(key); } } long time = stopWatch.getElapsedTime(); LOG.info("Time for benchmarkPutGetSurya: " + time); assertTrue("Too slow. Time was " + time, time < allowedTime); }
public void run() { StopWatch st = null; if (GPResourceHome.perfProfile.PROFILE) { st = new StopWatch(false, false, true); st.start(); } StopWatch ct = new StopWatch(); int numWorkers = 0; try { if (GPResourceHome.commonState == null) { GPResourceHome.load_common(); } } catch (Exception e) { e.printStackTrace(); } ct.start(); while (true) { if (GPResourceHome.perfProfile.PROFILE) { st.stop(); GPResourceHome.perfProfile.addSample("notification_engine_worker", st.getElapsedTimeMX()); st.reset(); st.start(); } GPResource gpResource = null; try { // logger.debug("gpResource.notificationWorkerQ.remove()..."); gpResource = (GPResource) GPResourceHome.commonState.getNextResource(); // gpResource = // (GPResource)GPResourceHome.commonState.notificationWorkerQ.remove();//GPResourceHome.commonState.notificationQ.waitUntilNotEmpty(); boolean sentOK = false; if (gpResource != null && gpResource.taskQ != null && gpResource.taskPendQ != null && (gpResource.taskQ.size() + gpResource.taskPendQ.size()) > 0) // if (gpResource != null) { Task task = null; // if (gpResource.DATA_AWARE_SCHEDULER && !gpResource.MAX_CACHE_HIT) if (gpResource.DATA_AWARE_SCHEDULER) { // synchronized(gpResource) // { // task = (Task)gpResource.taskQ.remove(); task = (Task) gpResource.taskQ.removeNoWait(); if (task == null) { task = (Task) gpResource.taskPendQ.remove(); } logger.debug( "GPService:NotificationEngine:sendNotification(): removed task " + task.getExecutable().getId() + " from gpResource.taskQ... gpResource.taskQ.size() = " + gpResource.taskQ.size()); gpResource.taskPendQ.insert(task); logger.debug( "GPService:NotificationEngine:sendNotification(): inserted task " + task.getExecutable().getId() + " into gpResource.taskPendQ... gpResource.taskPendQ.size() = " + gpResource.taskPendQ.size()); // } } logger.debug( "GPService:NotificationEngine:sendNotification(): key = " + String.valueOf(gpResource.resourceKey) + " ..."); // if (gpResourceHome.sendNotification(gpResource.resourceKey)) boolean sendNotificationTest = false; if (gpResourceHome.useTCPCore) { sendNotificationTest = gpResourceHome.sendNotificationTCPCore(gpResource); } // else if (gpResource.DATA_AWARE_SCHEDULER && !gpResource.MAX_CACHE_HIT) else if (gpResource.DATA_AWARE_SCHEDULER) { sendNotificationTest = gpResourceHome.sendNotification(gpResource, task); } /* else if (gpResourceHome.localFork) { sendNotificationTest = gpResourceHome.sendNotificationLocal(gpResource); } */ else { sendNotificationTest = gpResourceHome.sendNotification(gpResource); } if (sendNotificationTest) { setNotificationsSent(); logger.debug( "Notification for key " + String.valueOf(gpResource.resourceKey) + " sent successfully!"); // if (gpResource.DATA_AWARE_SCHEDULER) // { // gpResource.taskPendQ.insert(task); // logger.debug("GPService:NotificationEngine:sendNotification(): inserted task into // gpResource.taskPendQ... gpResource.taskPendQ.size() = " + // gpResource.taskPendQ.size()); // } // insert back in to try again later... this is a hack to ensure that we never run out // of notification messages :) // if (!gpResourceHome.useTCPCore) // if (gpResource != null && gpResource.taskQ != null && (gpResource.taskQ.size() + // gpResource.taskPendQ.size()) > 0 && (gpResource.taskQ.size() + // gpResource.taskPendQ.size()) <= GPResourceHome.maxNumNotificationWorkerThreads*2) // { // logger.debug("there is still work to be collected and we have dipped below the // number of notification threads, insert back into the notification queue..."); // GPResourceHome.commonState.notificationWorkerQ.insert(gpResource); // } } else { logger.debug( "Notification for key " + String.valueOf(gpResource.resourceKey) + " failed!"); // if (gpResource.DATA_AWARE_SCHEDULER && !gpResource.MAX_CACHE_HIT) if (gpResource.DATA_AWARE_SCHEDULER) { // synchronized(gpResource) // { // gpResource.taskPendQ.removeTask(task); // should insert back at the front... fix this // gpResource.taskQ.insertFront(task); // } // logger.debug("GPService:NotificationEngine:sendNotification(): inserted task " + // task.getExecutable().getId() + " into gpResource.taskQ... gpResource.taskQ.size() = // " + gpResource.taskQ.size()); } // insert back in to try again later... // if ((gpResource.taskQ.size() + gpResource.taskPendQ.size()) > 0) // { // logger.debug("there is still work to be collected, insert back into the // notification queue..."); // GPResourceHome.commonState.notificationWorkerQ.insert(gpResource); // used for throtling notifications... /* try { System.currentTimeMillis(); Thread.sleep(20); } catch (Exception sss) { } */ // } } } if (MAX_NOT_PER_SEC > 0 && getNotificationsSent() >= MAX_NOT_PER_SEC) { long timeLeft = NOT_TIME_QUANTA - ct.getElapsedTime(); if (timeLeft > 20) try { logger.debug( getNotificationsSent() + " notifications sent in " + ct.getElapsedTime() + " ms, sleeping for " + timeLeft + " ms"); // System.currentTimeMillis(); Thread.sleep(timeLeft); } catch (Exception sss) { } } if (ct.getElapsedTime() >= NOT_TIME_QUANTA) { if (getNotificationsSent() > 0) { logger.debug( "***NotificationEngineWorker(): " + getNotificationsSent() + " notifications sent to workers in " + ct.getElapsedTime() + " ms, maximum allowed notifications " + MAX_NOT_PER_SEC + " per " + NOT_TIME_QUANTA + " ms..."); } ct.reset(); resetNotificationsSent(); ct.start(); } // if (GPResourceHome.commonState.resourceQ.size() > 0 && // GPResourceHome.commonState.getWaitingTasks() <= 0) if (GPResourceHome.commonState.getWaitingTasks() <= 0) { try { Thread.sleep(100); } catch (Exception e) { if (logger.isDebugEnabled()) e.printStackTrace(); } } } catch (Exception eee) { logger.debug("Error in NotificationEngineWorker thread: " + eee); if (logger.isDebugEnabled()) eee.printStackTrace(); // continue; // if (gpResource != null && gpResource.taskQ != null && gpResource.taskQ.size() + // gpResource.taskPendQ.size() > 0) // if (gpResource != null && gpResource.taskQ != null) // { // logger.debug("there is still work to be collected, insert back into the notification // queue..."); // GPResourceHome.commonState.notificationWorkerQ.insert(gpResource); // } } } }