Search in sources :

Example 6 with Pointer

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

the class MallocTests method withMap.

@Test
public void withMap() {
    ConcurrentMap<Long, Pointer> map = new MapMaker().concurrencyLevel(4).initialCapacity(500000).makeMap();
    String str = "This is the string to store into the off-heap memory";
    int size = str.length();
    int howMany = 1000000;
    byte[] payload = str.getBytes();
    logger.info("adding " + howMany + " strings of " + size + " bytes...");
    for (long i = 0; i < howMany; i++) {
        Pointer p = mem.store(payload);
        map.put(i, p);
    }
    logger.info("...done");
}
Also used : MapMaker(com.google.common.collect.MapMaker) Pointer(org.directmemory.memory.Pointer) Test(org.junit.Test)

Example 7 with Pointer

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

the class MallocTests method oneMillionEntriesWithRead.

@Test
public void oneMillionEntriesWithRead() {
    logger.info("total capacity=" + Ram.inMb(mem.capacity()));
    assertNotNull(mem);
    int size = 400;
    int howMany = 1000000;
    logger.info("payload size=" + Ram.inKb(size));
    logger.info("entries=" + howMany);
    String test = "this is a nicely crafted test";
    byte[] payload = test.getBytes();
    for (int i = 0; i < howMany; i++) {
        Pointer p = mem.store(payload);
        byte[] check = mem.retrieve(p);
        assertNotNull(check);
        assertEquals(test, new String(check));
    }
    logger.info("total used=" + Ram.inMb(mem.used()));
}
Also used : Pointer(org.directmemory.memory.Pointer) Test(org.junit.Test)

Example 8 with Pointer

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

the class MemoryManagerTests method smokeTest.

@Test
public void smokeTest() {
    Random rnd = new Random();
    int size = rnd.nextInt(10) * (int) MemoryManager.capacity() / 100;
    logger.info("payload size=" + Ram.inKb(size));
    Pointer p = MemoryManager.store(new byte[size]);
    logger.info("stored");
    assertNotNull(p);
    assertEquals(size, p.end);
    assertEquals(size, MemoryManager.activeBuffer.used());
    MemoryManager.free(p);
    assertEquals(0, MemoryManager.activeBuffer.used());
    logger.info("end");
}
Also used : Random(java.util.Random) Pointer(org.directmemory.memory.Pointer) Test(org.junit.Test)

Example 9 with Pointer

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

the class MemoryManagerTests method readTest.

@Test
public void readTest() {
    for (OffHeapMemoryBuffer buffer : MemoryManager.buffers) {
        for (Pointer ptr : buffer.pointers) {
            if (!ptr.free) {
                byte[] res = MemoryManager.retrieve(ptr);
                assertNotNull(res);
                assertEquals(new String(payload), new String(res));
            }
        }
    }
}
Also used : OffHeapMemoryBuffer(org.directmemory.memory.OffHeapMemoryBuffer) Pointer(org.directmemory.memory.Pointer) Test(org.junit.Test)

Example 10 with Pointer

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

the class ConcurrentTests2 method get.

private void get(String key) {
    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)

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