Search in sources :

Example 6 with OffHeapMemoryBuffer

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

the class BaseTests method checkExpiration.

@Test
public void checkExpiration() throws InterruptedException {
    OffHeapMemoryBuffer mem = OffHeapMemoryBuffer.createNew(10 * 1024 * 1024);
    assertNotNull(mem);
    int size = 400;
    int howMany = 5000;
    logger.info("off-heap capacity=" + Ram.inMb(mem.capacity()));
    logger.info("payload size=" + Ram.inKb(size));
    logger.info("entries=" + howMany);
    byte[] payload = new byte[size];
    for (int i = 0; i < howMany; i++) {
        mem.store(payload, 2000);
    }
    assertEquals(size * howMany, mem.used());
    logger.info("entries with relative expiration=" + (howMany / 2));
    for (int i = 0; i < howMany / 2; i++) {
        mem.store(payload, 100);
    }
    assertEquals(size * howMany + size * howMany / 2, mem.used());
    logger.info("entries with absolute expiration=" + (howMany / 2));
    for (int i = 0; i < howMany / 2; i++) {
        mem.store(payload, new Date());
    }
    assertEquals(size * howMany * 2, mem.used());
    logger.info("total used=" + Ram.inMb(mem.used()));
    Thread.sleep(500);
    logger.info("calling disposeExpiredAbsolute");
    mem.disposeExpiredAbsolute();
    logger.info("total used=" + Ram.inMb(mem.used()));
    assertEquals(size * howMany + size * howMany / 2, mem.used());
    logger.info("calling disposeExpiredRelative");
    mem.disposeExpiredRelative();
    logger.info("total used=" + Ram.inMb(mem.used()));
    assertEquals(size * howMany, mem.used());
    Thread.sleep(2000);
    logger.info("calling disposeExpiredRelative again");
    mem.disposeExpiredRelative();
    logger.info("total used=" + Ram.inMb(mem.used()));
    assertEquals(0, mem.used());
}
Also used : OffHeapMemoryBuffer(org.directmemory.memory.OffHeapMemoryBuffer) Date(java.util.Date) Test(org.junit.Test)

Example 7 with OffHeapMemoryBuffer

use of org.directmemory.memory.OffHeapMemoryBuffer 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 8 with OffHeapMemoryBuffer

use of org.directmemory.memory.OffHeapMemoryBuffer 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 9 with OffHeapMemoryBuffer

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

the class ConcurrentTests2 method dump.

@AfterClass
public static void dump() {
    for (OffHeapMemoryBuffer mem : MemoryManager.buffers) {
        dump(mem);
    }
    logger.info("************************************************");
    logger.info("entries: " + entries);
    logger.info("inserted: " + map.size());
    logger.info("reads: " + read);
    logger.info("count: " + count);
    logger.info("got: " + got);
    logger.info("missed: " + missed);
    logger.info("good: " + good);
    logger.info("bad: " + bad);
    logger.info("************************************************");
}
Also used : OffHeapMemoryBuffer(org.directmemory.memory.OffHeapMemoryBuffer) AfterClass(org.junit.AfterClass)

Aggregations

OffHeapMemoryBuffer (org.directmemory.memory.OffHeapMemoryBuffer)9 Test (org.junit.Test)5 Pointer (org.directmemory.memory.Pointer)4 AfterClass (org.junit.AfterClass)2 Date (java.util.Date)1 Random (java.util.Random)1