Search in sources :

Example 1 with RandomString

use of herddb.utils.RandomString in project herddb by diennea.

the class ConcurrentUpdatesBLinkTest method concurrentUpdatesTests.

@Test
public void concurrentUpdatesTests() throws Exception {
    int DATA_SIZE = 50000;
    int THREADS = 4;
    int KEY_SIZE = 25;
    int ITERATIONS = 100000;
    BLinkTest.DummyBLinkIndexDataStorage<Sized<String>, Long> storage = new BLinkTest.DummyBLinkIndexDataStorage<>();
    try (BLink<Sized<String>, Long> blink = new BLink<>(2048L, new BLinkTest.StringSizeEvaluator(), new ClockProPolicy(30), storage)) {
        Random random = new Random();
        RandomString rs = new RandomString(random);
        ConcurrentHashMap<String, Long> expectedValues = new ConcurrentHashMap<>();
        List<String> keys = new ArrayList<>();
        for (int i = 0; i < DATA_SIZE; ++i) {
            String key = rs.nextString(KEY_SIZE);
            keys.add(key);
            long value = random.nextInt(Integer.MAX_VALUE) + 1;
            // zero means null
            assertTrue(value > 0);
            blink.insert(Sized.valueOf(key), value);
            expectedValues.put(key, value);
        }
        int numKeys = keys.size();
        ExecutorService threadpool = Executors.newFixedThreadPool(THREADS);
        System.out.println("generated " + numKeys + " keys");
        AtomicLong updates = new AtomicLong();
        AtomicLong inserts = new AtomicLong();
        AtomicLong skipped = new AtomicLong();
        AtomicLong deletes = new AtomicLong();
        List<Future<?>> futures = new ArrayList<>();
        for (int i = 0; i < ITERATIONS; i++) {
            String key = keys.get(random.nextInt(numKeys));
            long value = random.nextLong();
            // 10 % deletes
            boolean delete = random.nextInt(100) < 10;
            futures.add(threadpool.submit(new Runnable() {

                @Override
                public void run() {
                    Long current = expectedValues.remove(key);
                    if (current == null) {
                        skipped.incrementAndGet();
                        return;
                    }
                    if (delete) {
                        blink.delete(Sized.valueOf(key));
                        expectedValues.put(key, 0L);
                        deletes.incrementAndGet();
                    } else {
                        blink.insert(Sized.valueOf(key), value);
                        if (current == 0L) {
                            inserts.incrementAndGet();
                        } else {
                            updates.incrementAndGet();
                        }
                        expectedValues.put(key, value);
                    }
                }
            }));
        }
        int progress = 0;
        for (Future f : futures) {
            f.get();
            if (++progress % 10000 == 0) {
                System.out.println("done " + progress + "/" + ITERATIONS);
            }
        }
        int nulls = 0;
        for (String key : keys) {
            Long value = blink.search(Sized.valueOf(key));
            Long expected = expectedValues.get(key);
            if (expected == 0) {
                assertNull(value);
                nulls++;
            } else {
                assertEquals(expected, value);
            }
        }
        System.out.println("total swapin " + storage.swapIn);
        System.out.println("inserts " + inserts);
        System.out.println("updates " + updates);
        System.out.println("skipped " + skipped);
        System.out.println("deletes " + deletes);
        System.out.println("iterations " + ITERATIONS + " (" + (inserts.intValue() + updates.intValue() + deletes.intValue() + skipped.intValue()) + ")");
        System.out.println("nulls " + nulls);
        threadpool.shutdown();
    }
}
Also used : Sized(herddb.utils.Sized) RandomString(herddb.utils.RandomString) ArrayList(java.util.ArrayList) RandomString(herddb.utils.RandomString) AtomicLong(java.util.concurrent.atomic.AtomicLong) Random(java.util.Random) AtomicLong(java.util.concurrent.atomic.AtomicLong) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) ClockProPolicy(herddb.core.ClockProPolicy) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.Test)

Aggregations

ClockProPolicy (herddb.core.ClockProPolicy)1 RandomString (herddb.utils.RandomString)1 Sized (herddb.utils.Sized)1 ArrayList (java.util.ArrayList)1 Random (java.util.Random)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Test (org.junit.Test)1