@Test public void serializeStringAsAsciiBytes() throws Exception { final String data = "abc123"; final Charset asciiCharset = Charset.forName("US-ASCII"); final int loops = 1000; ByteArrayOutputStream out = new ByteArrayOutputStream(10); byte[] expecteds = new byte[] {'a', 'b', 'c', '1', '2', '3'}; long start = 0; start = System.nanoTime(); for (int i = 0; i < loops; i++) { AsciiUtils.writeStringAsAsciiBytes(data, out); out.flush(); assertArrayEquals(expecteds, out.toByteArray()); out.reset(); } System.out.println( "AsciiUtils#writeStringAsAsciiBytes() elapsed time: " + ((System.nanoTime() - start) / 1000000) + " ms"); start = System.nanoTime(); for (int i = 0; i < loops; i++) { out.write(data.getBytes(asciiCharset)); out.flush(); assertArrayEquals(expecteds, out.toByteArray()); out.reset(); } System.out.println( "String#getBytes() elapsed time: " + ((System.nanoTime() - start) / 1000000) + " ms"); }
// @Test public void graphBubbleSort() { System.out.println("size, time"); for (int i = 0; i < 255; i++) { int size = Sort.randBoundInt(r, 100, 10000); Random pop = new Random(42); ArrayList<Integer> arr = new ArrayList<Integer>(size); for (int j = 0; j < size; j++) { arr.add(pop.nextInt()); } Integer[] a = arr.toArray(new Integer[0]); long startTime = System.nanoTime(); Sort.<Integer>bubblesort(a); long stopTime = System.nanoTime(); long elapsedTime = stopTime - startTime; System.out.println(size + ", " + elapsedTime); } }
private double getReferenceTime(int n) { final long t0 = System.nanoTime(); for (int i = 0; i < n * 100; i++) { // Jacobi is less than 100 times slower Math.acos(1.0e-5 * Math.random()); } final long t1 = System.nanoTime(); return (t1 - t0) / 1.0e9; }
@Test public void get_shouldJoinTheCreatedThread_givenAFutureReturnedByExecute() throws Exception { long startTime = System.nanoTime(); Future<?> result = asyncExecutor.execute(suppressCheckedExceptions(() -> Thread.sleep(250L))); result.get(); long endTime = System.nanoTime(); assertThat(endTime - startTime, isGreaterThan(100 * 1000000L)); }
@Test public void testChained() throws IOException { ChronicleTools.deleteOnExit(TMP + "/chronicle1"); Chronicle chronicle1 = new IndexedChronicle(TMP + "/chronicle1"); InProcessChronicleSource source1 = new InProcessChronicleSource(chronicle1, 61111); ChronicleTools.deleteOnExit(TMP + "/chronicle2"); Chronicle chronicle2 = new IndexedChronicle(TMP + "/chronicle2"); InProcessChronicleSource source2 = new InProcessChronicleSource(chronicle2, 62222); InProcessChronicleSink sink2 = new InProcessChronicleSink(source2, "localhost", 61111); ChronicleTools.deleteOnExit(TMP + "/chronicle3"); Chronicle chronicle3 = new IndexedChronicle(TMP + "/chronicle3"); InProcessChronicleSink sink3 = new InProcessChronicleSink(chronicle3, "localhost", 62222); ExcerptAppender excerpt1 = source1.createAppender(); ExcerptTailer excerpt2 = sink2.createTailer(); ExcerptTailer excerpt3 = sink3.createTailer(); for (int i = 1; i < 20; i++) { excerpt1.startExcerpt(); excerpt1.writeLong(System.nanoTime()); excerpt1.finish(); while (excerpt2.size() < i) excerpt2.nextIndex(); while (excerpt3.size() < i) excerpt3.nextIndex(); } sink3.close(); sink2.close(); source1.close(); }
void work_helper( NonBlockingIdentityHashMap<String, String> nbhm, String thrd, int d, String[] keys) { final int ITERS = 20000; for (int j = 0; j < 10; j++) { long start = System.nanoTime(); for (int i = d; i < ITERS; i += 2) assertThat( "this key not in there, so putIfAbsent must work", nbhm.putIfAbsent(keys[i], thrd), is((String) null)); for (int i = d; i < ITERS; i += 2) assertTrue(nbhm.remove(keys[i], thrd)); double delta_nanos = System.nanoTime() - start; double delta_secs = delta_nanos / 1000000000.0; double ops = ITERS * 2; // System.out.println("Thrd"+thrd+" "+(ops/delta_secs)+" ops/sec size="+nbhm.size()); } }
public static void concurrentBenchmark(List<BloomFilter<String>> bfs, final int opsPerThread) { ExecutorService pool = Executors.newFixedThreadPool(bfs.size()); List<Runnable> threads = new ArrayList<>(bfs.size()); final List<String> items = new ArrayList<>(opsPerThread); for (int i = 0; i < opsPerThread; i++) { items.add(String.valueOf(i)); } for (final BloomFilter<String> bf : bfs) { threads.add( new Runnable() { @Override public void run() { for (int i = 0; i < opsPerThread; i++) { bf.add(String.valueOf(i)); } for (int i = 0; i < opsPerThread; i++) { bf.contains(String.valueOf(i)); } bf.addAll(items); } }); } long start = System.nanoTime(); for (Runnable r : threads) { pool.execute(r); } pool.shutdown(); try { pool.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); long end = System.nanoTime(); System.out.println( "Concurrent Benchmark, " + opsPerThread + " ops * " + bfs.size() + " threads = " + opsPerThread * bfs.size() + " total ops: " + ((double) (end - start)) / 1000000 + " ms"); } catch (InterruptedException e) { // ... } }
@Test public void testGrafo() { for (int i = 0; i < 100000000; i++) { Math.random(); } GrafoPredicados grafo = generateStar(10, 10, 8); ArrayList<Tuple<Integer, Integer>> aristas = generateAristas(10, 10); int cantAristas = aristas.size(); for (int i = 0; i < cantAristas; i++) { for (int j = 0; j < 100; j++) { Tuple<Integer, Integer> tuple = aristas.get(0); aristas.remove(0); grafo.connectMateria(tuple.getX(), tuple.getY()); } double tiempoFinal = 0; Ejercicio3 ej; int conflictos1 = Integer.MAX_VALUE; for (int j = 0; j < 3; j++) { ej = new Ejercicio3(grafo); double tiempo = System.nanoTime(); conflictos1 = Math.min(conflictos1, ej.checkColoreoFinal()); tiempoFinal += (System.nanoTime() - tiempo) / 1000; } System.out.print(tiempoFinal + ";" + conflictos1 + ";"); conflictos1 = 0; for (int j = 0; j < 3; j++) { Ejercicio2 ej2 = new Ejercicio2(grafo); double tiempo = System.nanoTime(); if (ej2.solverWithBacktrack()) { conflictos1 = 0; } else { conflictos1 = 1; } tiempoFinal += (System.nanoTime() - tiempo) / 1000; } System.out.print(tiempoFinal + ";" + conflictos1 + ";"); System.out.println(); } }
// @Test public void testEstrella() { for (int i = 0; i < 100000000; i++) { Math.random(); } for (int i = 0; i < 1000; i++) { double tiempoFinal = 0; Ejercicio3 ej; GrafoPredicados grafo = generateStar(i, i, 100); int conflictos1 = Integer.MAX_VALUE; for (int j = 0; j < 3; j++) { ej = new Ejercicio3(grafo); double tiempo = System.nanoTime(); conflictos1 = Math.min(conflictos1, ej.checkColoreo()); tiempoFinal += (System.nanoTime() - tiempo) / 1000; } System.out.println(tiempoFinal + ";" + conflictos1); } }
@Test public void testCalcPerformance() { final NNffbpAlphaTabFast tab = loadTestNet(); final double[] nnInput = new double[] {1.0, 3.4, 6.988, 4.4, 7.0, 16.21}; final long t1 = System.nanoTime(); final int N = 100000; for (int i = 0; i < N; i++) { final double[] doubles = nnInput.clone(); for (int j = 0; j < doubles.length; j++) { doubles[j] += 1.0e-5 * Math.random(); } final double[] nnCalc = tab.calc(doubles); assertTrue(nnCalc[0] != 0.0); } final long t2 = System.nanoTime(); final double seconds = (t2 - t1) / 1.0e9; System.out.println("testCalcPerformance: " + seconds + " seconds"); assertTrue(seconds < 1.0); }
public static void benchIO(LargeByteList target, String name, int unit, int iteration) { Random ra = new Random(); byte[] buf_set = new byte[unit]; byte[] buf_get = new byte[unit]; long start, elapse_set, elapse_get; int i; elapse_set = 0; elapse_get = 0; for (i = 0; i < iteration; i++) { // init for (int j = 0; j < buf_set.length; j++) { buf_set[j] = (byte) ra.nextInt(255); } // set start = System.nanoTime(); for (int j = 0; j < target.length(); j += buf_set.length) { target.set(j, buf_set, 0, buf_set.length); } elapse_set += (System.nanoTime() - start); // get start = System.nanoTime(); for (int j = 0; j < target.length(); j += buf_get.length) { target.get(j, buf_get, 0, buf_get.length); } elapse_get += (System.nanoTime() - start); // verify for (int j = 0; j < buf_get.length; j++) { if (buf_get[j] != buf_set[j]) { assertEquals("failed to verify at " + j, buf_set[j], buf_get[j]); } } } LargeByteListTest.printBenchResult(name + "(rd)", iteration * target.length(), elapse_get); LargeByteListTest.printBenchResult(name + "(wr)", iteration * target.length(), elapse_set); }
@Test public void testFastMultiply() throws Exception { DenseMatrix a = new DenseMatrix(N, M, generateRandomIntArray(N, M)); DenseMatrix b = new DenseMatrix(M, N, generateRandomIntArray(M, N)); long startTime = System.nanoTime(); DenseMatrix result = a.fastMultiply(b); long estimatedTime = System.nanoTime() - startTime; System.out.println("Execution time(ms) fast multiply " + (estimatedTime / 1000000)); int[][] correct = new int[N][N]; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { for (int k = 0; k < M; k++) { correct[i][j] += a.get(i, k) * b.get(k, j); } } } for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { assertTrue(result.get(i, j) == correct[i][j]); } } }
private void _benchmark(Lookup lookup, Map<String, Integer> ref, boolean estimate, Bench bench) throws Exception { long start = System.currentTimeMillis(); lookup.build(getTFIT()); long buildTime = System.currentTimeMillis() - start; TermFreqIterator tfit = getTFIT(); long elapsed = 0; while (tfit.hasNext()) { String key = tfit.next(); // take only the first part of the key int len = key.length() > 4 ? key.length() / 3 : 2; String prefix = key.substring(0, len); start = System.nanoTime(); List<LookupResult> res = lookup.lookup(prefix, true, 10); elapsed += System.nanoTime() - start; assertTrue(res.size() > 0); for (LookupResult lr : res) { assertTrue(lr.key.startsWith(prefix)); } if (ref != null) { // verify the counts Integer Cnt = ref.get(key); if (Cnt == null) { // first pass ref.put(key, res.size()); } else { assertEquals(key + ", prefix: " + prefix, Cnt.intValue(), res.size()); } } } if (estimate) { RamUsageEstimator rue = new RamUsageEstimator(); long size = rue.estimateRamUsage(lookup); System.err.println(lookup.getClass().getSimpleName() + " - size=" + size); } if (bench != null) { bench.buildTime += buildTime; bench.lookupTime += elapsed; } }
/** Test that new records can be continuously added without hitting {@link OutOfMemoryError}. */ @Test @Ignore("Start this test with a small heap size (e.g. -Xmx64m)") public void testPerformance() { final Random random = new Random(System.nanoTime()); while (true) { final byte[] key = new byte[100]; random.nextBytes(key); final int size = random.nextInt(); final long pointer = random.nextLong(); cache.add(key, size, pointer); } }
@Test public void testGrafoBipartito() { for (int i = 0; i < 100000000; i++) { Math.random(); } for (int i = 1; i < 1000; i++) { GrafoPredicados grafo = generateBiPartitoCompleto(i, i, 2); double tiempoFinal = 0; Ejercicio3 ej; int conflictos1 = Integer.MAX_VALUE; for (int j = 0; j < 3; j++) { ej = new Ejercicio3(grafo); double tiempo = System.nanoTime(); conflictos1 = Math.min(conflictos1, ej.checkColoreoFinal()); tiempoFinal += (System.nanoTime() - tiempo) / 1000; } System.out.print(tiempoFinal + ";" + conflictos1 + ";"); conflictos1 = 0; for (int j = 0; j < 3; j++) { Ejercicio2 ej2 = new Ejercicio2(grafo); double tiempo = System.nanoTime(); if (ej2.solverWithBacktrack()) { conflictos1 = 0; } else { conflictos1 = 1; } tiempoFinal += (System.nanoTime() - tiempo) / 1000; } System.out.print(tiempoFinal + ";" + conflictos1 + ";"); System.out.println(); } }
@SuppressWarnings({"CallToSystemGC"}) // deactivated this test; it fails sometimes public void testCalcJacobiPerformance() { final NNffbpAlphaTabFast tab = loadTestNet(); final double[] nnInput = new double[] {1.0, 3.4, 6.988, 4.4, 7.0, 16.21}; System.gc(); // call gc in order to prevent garbage collection during performance test final long t1 = System.nanoTime(); final int N = 100000; for (int i = 0; i < N; i++) { final double[] doubles = nnInput.clone(); for (int j = 0; j < doubles.length; j++) { doubles[j] += 1.0e-5 * Math.random(); } final NNCalc nnCalc = tab.calcJacobi(doubles); assertTrue(nnCalc.getNnOutput()[0] != 0.0); } final long t2 = System.nanoTime(); final double duration = (t2 - t1) / 1.0e9; // using reference time in order to get rid of a constant time the duration is compared to System.gc(); // call gc in order to prevent garbage collection during performance test final double refTime = getReferenceTime(N); assertTrue(duration < refTime); }
/** Create a new TestData instance. */ public TestData() { attribute3 = new Date(); attribute2 = System.nanoTime(); attribute1 = String.valueOf(attribute3) + " and " + String.valueOf(attribute2); }
@After public void after() throws Exception { long now = System.nanoTime(); System.out.println("elapsed time " + (now - starttime) / 1000000000 + "secs"); }
@Before public void before() throws Exception { starttime = System.nanoTime(); }