Search in sources :

Example 16 with Pointer

use of org.directmemory.memory.Pointer in project DirectMemory by raffaeleguidi.

the class BaseTests method aFewEntriesWithCheck.

@Test
public void aFewEntriesWithCheck() {
    OffHeapMemoryBuffer mem = OffHeapMemoryBuffer.createNew(10 * 1024 * 1024);
    logger.info("total capacity=" + Ram.inMb(mem.capacity()));
    assertNotNull(mem);
    int howMany = 10;
    logger.info("payload size is variable");
    logger.info("entries=" + howMany);
    String test = "this is a nicely crafted test";
    Pointer lastP = null;
    for (int i = 0; i < howMany; i++) {
        byte[] payload = (test + " - " + i).getBytes();
        Pointer p = mem.store(payload);
        logger.info("p.start=" + p.start);
        logger.info("p.end=" + p.end);
        if (lastP != null) {
            assertEquals(lastP.end + 1, p.start);
        }
        assertEquals(p.end - p.start, payload.length);
        lastP = p;
        logger.info("---");
    }
    logger.info("total used=" + Ram.inMb(mem.used()));
}
Also used : OffHeapMemoryBuffer(org.directmemory.memory.OffHeapMemoryBuffer) Pointer(org.directmemory.memory.Pointer) Test(org.junit.Test)

Example 17 with Pointer

use of org.directmemory.memory.Pointer in project DirectMemory by raffaeleguidi.

the class BaseTests method aFewEntriesWithRead.

@Test
public void aFewEntriesWithRead() {
    OffHeapMemoryBuffer mem = OffHeapMemoryBuffer.createNew(100 * 1024 * 1024);
    logger.info("total capacity=" + Ram.inMb(mem.capacity()));
    assertNotNull(mem);
    int howMany = 10000;
    logger.info("payload size is variable");
    logger.info("entries=" + howMany);
    String test = "this is a nicely crafted test";
    for (int i = 0; i < howMany; i++) {
        final byte[] payload = (test + " - " + i).getBytes();
        Pointer p = mem.store(payload);
        final byte[] check = mem.retrieve(p);
        assertNotNull(check);
        assertEquals(test + " - " + i, new String(check));
        long crc1 = OffHeapMemoryBuffer.crc32(payload);
        long crc2 = OffHeapMemoryBuffer.crc32(check);
        assertEquals(crc1, crc2);
    }
    logger.info("total used=" + Ram.inMb(mem.used()));
}
Also used : OffHeapMemoryBuffer(org.directmemory.memory.OffHeapMemoryBuffer) Pointer(org.directmemory.memory.Pointer) Test(org.junit.Test)

Example 18 with Pointer

use of org.directmemory.memory.Pointer in project DirectMemory by raffaeleguidi.

the class ConcurrentTests method get.

private void get(String key) {
    Pointer p = map.get(key);
    read.incrementAndGet();
    if (p != null) {
        got.incrementAndGet();
        byte[] payload = mem.retrieve(p);
        if (key.equals(new String(payload)))
            good.incrementAndGet();
        else
            bad.incrementAndGet();
    } else {
        missed.incrementAndGet();
    }
}
Also used : Pointer(org.directmemory.memory.Pointer)

Example 19 with Pointer

use of org.directmemory.memory.Pointer in project DirectMemory by raffaeleguidi.

the class ConcurrentTests method retrieveCatchHalfOfThem.

@BenchmarkOptions(benchmarkRounds = 1000000, warmupRounds = 0, concurrency = 100)
@Test
public void retrieveCatchHalfOfThem() {
    String key = "test-" + (rndGen.nextInt(entries * 2) + 1);
    Pointer p = map.get(key);
    read.incrementAndGet();
    if (p != null) {
        got.incrementAndGet();
        byte[] payload = mem.retrieve(p);
        if (key.equals(new String(payload)))
            good.incrementAndGet();
        else
            bad.incrementAndGet();
    } else {
        missed.incrementAndGet();
    }
}
Also used : Pointer(org.directmemory.memory.Pointer) Test(org.junit.Test) BenchmarkOptions(com.carrotsearch.junitbenchmarks.BenchmarkOptions)

Example 20 with Pointer

use of org.directmemory.memory.Pointer in project DirectMemory by raffaeleguidi.

the class ConcurrentTests2 method retrieveCatchHalfOfThem.

@BenchmarkOptions(benchmarkRounds = 1000000, warmupRounds = 0, concurrency = 100)
@Test
public void retrieveCatchHalfOfThem() {
    String key = "test-" + (rndGen.nextInt(entries * 2) + 1);
    Pointer p = map.get(key);
    read.incrementAndGet();
    if (p != null) {
        got.incrementAndGet();
        byte[] payload = MemoryManager.retrieve(p);
        if (key.equals(new String(payload)))
            good.incrementAndGet();
        else
            bad.incrementAndGet();
    } else {
        missed.incrementAndGet();
    }
}
Also used : Pointer(org.directmemory.memory.Pointer) Test(org.junit.Test) BenchmarkOptions(com.carrotsearch.junitbenchmarks.BenchmarkOptions)

Aggregations

Pointer (org.directmemory.memory.Pointer)21 Test (org.junit.Test)12 BenchmarkOptions (com.carrotsearch.junitbenchmarks.BenchmarkOptions)4 OffHeapMemoryBuffer (org.directmemory.memory.OffHeapMemoryBuffer)4 IOException (java.io.IOException)2 Random (java.util.Random)2 MapMaker (com.google.common.collect.MapMaker)1