/** 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); }
/** Tests the functionality of getTimeDiffString() */ public void testGetTimeDiffString() { StopWatch watch = new StopWatch(); watch.stop(); // just check that we don't get an empty string assertTrue("" != watch.getTimeDiffString()); }
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()); }
/** * Time a multi-threaded access to a cache. * * @return the timing stopwatch */ private <V> StopWatch timeMultiThreaded( String id, final Map<Integer, V> map, ValueFactory<V> factory) throws InterruptedException { StopWatch stopWatch = new StopWatch(id); for (int i = 0; i < 500; i++) { map.put(i, factory.newValue(i)); } Thread[] threads = new Thread[30]; stopWatch.start("Running threads"); for (int threadIndex = 0; threadIndex < threads.length; threadIndex++) { threads[threadIndex] = new Thread("Cache access thread " + threadIndex) { @Override public void run() { for (int j = 0; j < 1000; j++) { for (int i = 0; i < 1000; i++) { map.get(i); } } } }; } for (Thread thread : threads) { thread.start(); } for (Thread thread : threads) { if (thread.isAlive()) { thread.join(2000); } } stopWatch.stop(); return stopWatch; }
@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(); }
// 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()); }
/** Finds shortcuts, does not change the underlying graph. */ void findShortcuts(ShortcutHandler sch) { long tmpDegreeCounter = 0; EdgeIterator incomingEdges = vehicleInExplorer.setBaseNode(sch.getNode()); // collect outgoing nodes (goal-nodes) only once while (incomingEdges.next()) { int u_fromNode = incomingEdges.getAdjNode(); // accept only uncontracted nodes if (g.getLevel(u_fromNode) != 0) continue; double v_u_weight = incomingEdges.getDistance(); int skippedEdge1 = incomingEdges.getEdge(); int incomingEdgeOrigCount = getOrigEdgeCount(skippedEdge1); // collect outgoing nodes (goal-nodes) only once EdgeIterator outgoingEdges = vehicleOutExplorer.setBaseNode(sch.getNode()); // force fresh maps etc as this cannot be determined by from node alone (e.g. same from node // but different avoidNode) algo.clear(); tmpDegreeCounter++; while (outgoingEdges.next()) { int w_toNode = outgoingEdges.getAdjNode(); // add only uncontracted nodes if (g.getLevel(w_toNode) != 0 || u_fromNode == w_toNode) { continue; } // Limit weight as ferries or forbidden edges can increase local search too much. // If we decrease the correct weight we only explore less and introduce more shortcuts. // I.e. no change to accuracy is made. double existingDirectWeight = v_u_weight + outgoingEdges.getDistance(); algo.setLimitWeight(existingDirectWeight) .setLimitVisitedNodes((int) meanDegree * 100) .setEdgeFilter(levelEdgeFilter.setAvoidNode(sch.getNode())); dijkstraSW.start(); dijkstraCount++; int endNode = algo.findEndNode(u_fromNode, w_toNode); dijkstraSW.stop(); // compare end node as the limit could force dijkstra to finish earlier if (endNode == w_toNode && algo.getWeight(endNode) <= existingDirectWeight) // FOUND witness path, so do not add shortcut continue; sch.foundShortcut( u_fromNode, w_toNode, existingDirectWeight, outgoingEdges, skippedEdge1, incomingEdgeOrigCount); } } if (sch instanceof AddShortcutHandler) { // sliding mean value when using "*2" => slower changes meanDegree = (meanDegree * 2 + tmpDegreeCounter) / 3; // meanDegree = (meanDegree + tmpDegreeCounter) / 2; } }
private Set<List<Integer>> distributions(int bound, List<GrammarProductionElement> elems) { distroTimer.start(); int sumSize = elems.size(); List<Integer> lowerBounds = lowerBounds(elems, bound); List<Integer> upperBounds = upperBounds(elems, bound, lowerBounds); Set<List<Integer>> result = distributor.distribute2(sumSize, bound, lowerBounds, upperBounds); distroTimer.stop(); return result; }
/** Tests the functionality of getTimeDiff */ public void testGetTimeDiff() { StopWatch watch = new StopWatch(); long startTime; long endTime; startTime = watch.getStartTime(); watch.stop(); endTime = watch.getEndTime(); assertEquals(endTime - startTime, watch.getTimeDiff()); }
/** Tests the functionality of toString() */ public void testToString() { StopWatch watch = new StopWatch(); try { Thread.sleep(10); } catch (InterruptedException e) { } watch.stop(); assertEquals(watch.getTimeDiffString(), watch.toString()); }
@Test public void testConstructorMilli() { StopWatch s = new StopWatch(0); assertEquals(s.toString(), "0:00:000"); s = new StopWatch(1); assertEquals(s.toString(), "0:00:001"); s = new StopWatch(999); assertEquals(s.toString(), "0:00:999"); }
@Test public void testConstructorString() { StopWatch s = new StopWatch("0:0:0"); assertEquals(s.toString(), "0:00:000"); s = new StopWatch("1:1:1"); assertEquals(s.toString(), "1:01:001"); s = new StopWatch("9999:59:999"); assertEquals(s.toString(), "9999:59:999"); }
public static void main(String[] args) { try { // Create the table DbDAO.createTable(); // Save 1000 names with nonprepared statements double nonPreparedSave; StopWatch sw = new StopWatch(); for (int i = 0; i < 1000; i++) saveNameNonPrepared("person" + i); nonPreparedSave = sw.elapsedTime(); // Reset the table dropTable(); DbDAO.createTable(); // Save 1000 names with prepared statements double preparedSave; ArrayList<String> names = new ArrayList<String>(); for (int i = 0; i < 1000; i++) names.add("person" + i); sw = new StopWatch(); saveNamePrepared(names); preparedSave = sw.elapsedTime(); // Print af test af 1000 saves System.out.println( "1000 names saved! \nNonprepared: " + nonPreparedSave + "\nPrepared: " + preparedSave); // Load 1000 names nonprepared ( We use that the table consists of the 1000 names from the // arraylist names) double nonPreparedLoad; sw = new StopWatch(); for (String s : names) loadNameNonPrepared(s); nonPreparedLoad = sw.elapsedTime(); // Load 1000 names prepared ( We use the arraylist names again ) double preparedLoad; sw = new StopWatch(); loadNamesPrepared(names); preparedLoad = sw.elapsedTime(); // Print af test af 1000 saves System.out.println( "1000 names loaded! \nNonprepared: " + nonPreparedLoad + "\nPrepared: " + preparedLoad); // Reset table again dropTable(); } catch (SQLException e) { e.printStackTrace(); } }
/** * Stress Test of selectionRectangleChanged method.<br> * Print the time interval for recording. */ public void testSelectionRectangleChanged() { StopWatch watch = new StopWatch(); watch.start(); for (int i = 0; i < StressHelper.TEST_LOOP_COUNT; i++) { handler.selectionRectangleChanged(event); } System.out.println( String.format( "Running SelectionHandler#selectionRectangleChanged() method for" + " %d times consumes %d milliseconds.", StressHelper.TEST_LOOP_COUNT, watch.stop())); }
/* This succeeds if it passes quietly. */ @Test public void test() throws InterruptedException { StopWatchFactory swf = StopWatchFactory.getDefault(); int iterations = 120; for (int i = 0; i < iterations; i++) { StopWatch sw = swf.getStopWatch("foo"); Thread.sleep(10 + (long) (Math.random() * 10)); sw.stop("iteration:success"); } }
@Test public void testConstructor() { StopWatch s = new StopWatch(5, 10, 300); assertEquals(s.toString(), "5:10:300"); s = new StopWatch("20:10:8"); assertEquals(s.toString(), "20:10:008"); s = new StopWatch("20:8"); assertEquals(s.toString(), "0:20:008"); s = new StopWatch("8"); assertEquals(s.toString(), "0:00:008"); }
@Test public void testConstructorSec() { StopWatch s = new StopWatch(0, 0); assertEquals(s.toString(), "0:00:000"); s = new StopWatch(1, 0); assertEquals(s.toString(), "0:01:000"); s = new StopWatch(1, 1); assertEquals(s.toString(), "0:01:001"); s = new StopWatch(59, 999); assertEquals(s.toString(), "0:59:999"); }
/** * vrátí zbývající čas v milisekundách. * * @return zbývající čas */ public long getRemainingMillis() { long remain = timeToCount - stopWatch.getElapsedMillis(); if (remain < 0) { remain = 0; } return remain; }
@Test public void testSlf4jLog() throws InterruptedException { Slf4jLog log = new Slf4jLog(); log.setSlf4jLogname("foo"); StopWatchFactory swf = StopWatchFactory.getInstance(log); int iterations = 100; for (int i = 0; i < iterations; i++) { StopWatch sw = swf.getStopWatch("foo"); Thread.sleep(10 + (long) (Math.random() * 10)); sw.stop("iteration:success"); } }
/** Tests the functionality for getEndTime */ public void testGetEndTime() { StopWatch watch = new StopWatch(); long endTime; try { Thread.sleep(10); } catch (InterruptedException e) { } // check if stop time is different from start watch.stop(); endTime = watch.getEndTime(); assertTrue(0 != endTime); // check if end time now is the same (we haven't started the timer agaiin assertEquals(endTime, watch.getEndTime()); }
@Test public void testToString() { StopWatch s1 = new StopWatch(); assertEquals(s1.toString(), "0:00:000"); StopWatch s2 = new StopWatch(1234, 53, 867); assertEquals(s2.toString(), "1234:53:867"); StopWatch s3 = new StopWatch(0, 0, 0); assertEquals(s3.toString(), "0:00:000"); StopWatch s4 = new StopWatch(1, 1, 1); assertEquals(s4.toString(), "1:01:001"); StopWatch s5 = new StopWatch(22, 30, 900); assertEquals(s5.toString(), "22:30:900"); }
public void end(boolean success) { m_dispatchTime = Math.max(m_timeAuthority.getTimeInMilliseconds() - m_startTime, 0); if (m_pauseTimer.isRunning()) { m_pauseTimer.stop(); } if (!success && m_statisticsForTest != null) { // Always mark as an error if the test threw an exception. m_testStatisticsHelper.setSuccess(m_statisticsForTest.getStatistics(), false); // We don't log the exception. If the script doesn't handle the // exception it will be logged when the run is aborted, // otherwise we assume the script writer knows what they're // doing. } }
/** 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); }
/** * TODO Vermijden dubbel berekenen u en var => miss omschrijven classes TODO BitVector ipv BitSet * TODO Score berekenen i.p.v. score TODO DataSet matching? */ public static void main(String[] args) { /*Tuple[] tuples = new Tuple[]{ new Tuple("0 0 1 0 0", "1 3", ' '), new Tuple("1 0 0 1 1", "1 2", ' '), new Tuple("0 0 0 1 1", "1 1", ' '), new Tuple("1 1 0 0 1", "2 1", ' '), new Tuple("1 1 0 0 1", "3 1", ' '), new Tuple("1 1 0 0 1", "3 2", ' '), new Tuple("0 0 0 1 0", "3 3", ' '), new Tuple("0 1 0 1 1", "2 3", ' ') }; data = new DataSet(tuples);*/ // data = Parser.parseAttributes("Corel5k-train.arff", 374); data = Parser.parseShortNotation("diabetes.txt", 1, new NumericalItemSet(new int[] {})); s = data.s(); n = data.getTuples().length; m = data.getTuples()[0].getClassValues().length; /*double u = 0; long l = 0; for(int j = 0; j < 10; j++) { double t = System.currentTimeMillis(); for(int i = 0; i < 10000; i++) u = data.getBluePrint().getOneItemSet(2, 48).ub(); l += (System.currentTimeMillis() - t); } System.out.println(((double)l/10));*/ System.out.println( "Data loaded, " + data.getTuples().length + " tuples, " + data.getTuples()[0].getItemSet().getLength() + " items, " + data.getTuples()[0].getClassValues().length + " class values"); StopWatch.tic("Full time"); System.out.println(icc()); StopWatch.printToc("Full time"); }
@Test @Ignore("Intended for use during development only") public void shouldBeFasterThanSynchronizedMap() throws Exception { Map<Integer, WeakReference<String>> synchronizedMap = Collections.synchronizedMap(new WeakHashMap<Integer, WeakReference<String>>()); StopWatch mapTime = timeMultiThreaded( "SynchronizedMap", synchronizedMap, new ValueFactory<WeakReference<String>>() { @Override public WeakReference<String> newValue(int v) { return new WeakReference<String>(String.valueOf(v)); } }); System.out.println(mapTime.prettyPrint()); this.map.setDisableTestHooks(true); StopWatch cacheTime = timeMultiThreaded( "WeakConcurrentCache", this.map, new ValueFactory<String>() { @Override public String newValue(int v) { return String.valueOf(v); } }); System.out.println(cacheTime.prettyPrint()); // We should be at least 4 time faster assertThat(cacheTime.getTotalTimeSeconds(), is(lessThan(mapTime.getTotalTimeSeconds() / 4.0))); }
@Test public void testCompareTo() { StopWatch s1 = new StopWatch(5, 59, 300); StopWatch s2 = new StopWatch(6, 01, 200); StopWatch s3 = new StopWatch(5, 50, 200); StopWatch s4 = new StopWatch(5, 59, 300); assertFalse(s1.equals(s2)); assertTrue(s1.equals(s4)); assertTrue(s2.compareTo(s1) > 0); assertTrue(s3.compareTo(s1) < 0); assertTrue(s1.compareTo(s4) == 0); }
public static void main(String[] args) { int[] array = ArrayUtils.randomIntArray(10000, 100); System.out.println(Arrays.toString(array)); StopWatch stopwatch = new StopWatch(); stopwatch.start(); System.out.println( Arrays.toString(DuplicateRemover.removeDuplicates(Arrays.copyOf(array, array.length)))); stopwatch.stop(); stopwatch.showElapsedTime(); stopwatch.reset(); stopwatch.start(); System.out.println( Arrays.toString(DuplicateRemover.removeDuplicatesBest(Arrays.copyOf(array, array.length)))); stopwatch.stop(); stopwatch.showElapsedTime(); stopwatch.reset(); }
@Test public void testPeriodicalLog() throws InterruptedException { PeriodicalLog log = new PeriodicalLog(); log.setSlf4jLogname("foo"); log.setPeriod(5); log.setName("testLog"); log.setJmx("iteration:1,iteration:2,iteration:3,iteration:4,iteration:N"); StopWatchFactory swf = StopWatchFactory.getInstance(log); int iterations = 1000; for (int i = 0; i < iterations; i++) { StopWatch sw = swf.getStopWatch("foo"); long waitPeriod = (long) (Math.random() * 10); Thread.sleep(10 + waitPeriod); sw.stop("iteration:" + waitPeriod); } }
/** 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); }
@Test public void testMutate() { StopWatch s1 = new StopWatch(5, 59, 300); StopWatch s2 = new StopWatch(5, 59, 300); StopWatch.setMutate(false); s1.add(1000); s1.setMilliseconds(100); s1.setSeconds(50); s1.setMinutes(2); s1.add(s2); assertTrue(s1.equals(s2)); }