From 9c463e09ba8d77823fd8dd47d00d068c223a4c50 Mon Sep 17 00:00:00 2001 From: gmungoc Date: Sun, 8 Sep 2019 20:00:14 +0100 Subject: [PATCH] JAL-3397 timing comparisons for (add then query) IntervalStore --- test/intervalstore/nonc/IntervalStoreTest.java | 88 +++++++++++++++++++++++- 1 file changed, 87 insertions(+), 1 deletion(-) diff --git a/test/intervalstore/nonc/IntervalStoreTest.java b/test/intervalstore/nonc/IntervalStoreTest.java index 7b96e39..7c1a921 100644 --- a/test/intervalstore/nonc/IntervalStoreTest.java +++ b/test/intervalstore/nonc/IntervalStoreTest.java @@ -38,9 +38,12 @@ import static org.testng.Assert.assertTrue; import java.util.ArrayList; import java.util.List; +import java.util.Random; import org.testng.annotations.Test; +import intervalstore.api.IntervalStoreI; + public class IntervalStoreTest { @Test(groups = "Functional") @@ -580,4 +583,87 @@ public class IntervalStoreTest assertTrue(store.findOverlaps(36, 100, overlaps).isEmpty()); assertTrue(store.findOverlaps(1, 9, overlaps).isEmpty()); } -} + + /** + * Compares alternative IntervalStoreI implementations for time to add an + * interval then query, at a range of different scales of N (the number of + * stored features). + */ + @Test(groups = "Timing") + public void testAddAndQueryTiming() + { + IntervalStoreI store = new intervalstore.impl.IntervalStore<>(); + System.out.println("IntervalStoreJ"); + testAddAndQueryTiming(store); + + System.out.println("\nnonc.IntervalStore"); + store = new intervalstore.nonc.IntervalStore<>(); + testAddAndQueryTiming(store); + } + + /** + * Populates the store with intervals, then logs the time taken to add an + * interval then query, at a range of different scales of N (the number of + * stored features). + * + * @param store + */ + private void testAddAndQueryTiming(IntervalStoreI store) + { + final int seqlen = 100000; // e.g. length of gene sequence + final int repeats = 20; + final int K = 1000; + final int warmups = 5; + final int[] scales = new int[] { 10 * K, 100 * K, K * K }; + + Random r = new Random(732); // fixed seed = repeatable test + int counter = 0; // to ensure a distinct description for each feature + + System.out.println("Scale\titeration\tmicrosecs"); + + for (int scale : scales) + { + /* + * add 'scale' features to the store + */ + for (int i = 0; i < scale; i++) + { + SimpleFeature sf = makeFeature(seqlen, r, counter); + counter++; + store.add(sf); + } + + /* + * do "add then query" and log the time taken for the query + * we ignore the first 5 repetitions as 'warmups' + */ + long total = 0L; + for (int i = 0; i < repeats + warmups; i++) + { + SimpleFeature sf = makeFeature(seqlen, r, counter); + store.add(sf); + long t1 = System.nanoTime(); + store.findOverlaps(10, 20); + long elapsed = System.nanoTime() - t1; + if (i >= warmups) + { + total += elapsed; + System.out.println( + String.format("%d\t%d\t%d", scale, i - warmups, + elapsed / K)); + } + } + System.out.println("average " + (total / (K * repeats))); + } + } + + SimpleFeature makeFeature(final int seqlen, Random r, int counter) + { + int i1 = r.nextInt(seqlen); + int i2 = r.nextInt(seqlen); + int from = Math.min(i1, i2); + int to = Math.max(i1, i2); + SimpleFeature sf = new SimpleFeature(from, to, "desc" + counter); + return sf; + } +} \ No newline at end of file -- 1.7.10.2