Last active
August 29, 2015 14:17
-
-
Save danielshaya/0fe2f369b86123583160 to your computer and use it in GitHub Desktop.
Chronicle implementation for creating 10m objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package zeroalloc; | |
import net.openhft.chronicle.map.ChronicleMap; | |
import net.openhft.chronicle.map.ChronicleMapBuilder; | |
import net.openhft.lang.values.IntValue; | |
import org.junit.Assert; | |
import org.junit.Test; | |
import java.io.File; | |
import java.io.IOException; | |
/** | |
* Class to demonstrate zero garbage creation. | |
* Run with -verbose:gc | |
* To run in JFR use these options for best results | |
* -XX:+UnlockCommercialFeatures -XX:+FlightRecorder | |
*/ | |
public class CreateChronicleTest { | |
private static final int ITERATIONS = 10_000_000; | |
@Test | |
public void demoChronicleMap() throws IOException, InterruptedException { | |
System.out.println("----- CHRONICLE MAP ------------------------"); | |
File file = new File("/tmp/chronicle-map-" + System.nanoTime() + ".map"); | |
file.deleteOnExit(); | |
ChronicleMapBuilder<IntValue, BondVOInterface> builder = | |
ChronicleMapBuilder.of(IntValue.class, BondVOInterface.class) | |
.entries(ITERATIONS); | |
try (ChronicleMap<IntValue, BondVOInterface> map = | |
builder.createPersistedTo(file)) { | |
final BondVOInterface value = map.newValueInstance(); | |
final IntValue key = map.newKeyInstance(); | |
long actualQuantity = 0; | |
long expectedQuantity = 0; | |
long time = System.currentTimeMillis(); | |
System.out.println("*** Entering critical section ***"); | |
for (int i = 0; i < ITERATIONS; i++) { | |
value.setQuantity(i); | |
key.setValue(i); | |
map.put(key, value); | |
expectedQuantity += i; | |
} | |
long putTime = (System.currentTimeMillis()-time); | |
time = System.currentTimeMillis(); | |
for (int i = 0; i < ITERATIONS; i++) { | |
key.setValue(i); | |
actualQuantity += map.getUsing(key, value).getQuantity(); | |
} | |
System.out.println("*** Exiting critical section ***"); | |
System.out.println("Time for putting " + putTime); | |
System.out.println("Time for getting " + (System.currentTimeMillis()-time)); | |
Assert.assertEquals(expectedQuantity, actualQuantity); | |
printMemUsage(); | |
} finally { | |
file.delete(); | |
} | |
} | |
public static void printMemUsage(){ | |
System.gc(); | |
System.gc(); | |
System.out.println("Memory(heap) used " + humanReadableByteCount(Runtime.getRuntime().totalMemory() | |
- Runtime.getRuntime().freeMemory(), true)); | |
} | |
public static String humanReadableByteCount(long bytes, boolean si) { | |
int unit = si ? 1000 : 1024; | |
if (bytes < unit) return bytes + " B"; | |
int exp = (int) (Math.log(bytes) / Math.log(unit)); | |
String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp-1) + (si ? "" : "i"); | |
return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment