org.mentalog.test.GCTest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of menta-log Show documentation
Show all versions of menta-log Show documentation
A log library that embraces the kiss principle.
package org.mentalog.test;
import static org.mentalog.Log.*;
import java.util.concurrent.locks.LockSupport;
import org.mentaaffinity.Affinity;
import org.mentalog.AsyncThread;
import org.mentalog.ExtendedDefaultLogger;
import org.mentalog.Log;
import org.mentalog.Logger;
import org.mentalog.config.BytesConfigParam;
import org.mentalog.util.Benchmarker;
import org.mentalog.util.SystemUtils;
import org.mentaqueue.wait.SpinWaitStrategy;
/**
* java -verbose:gc -cp ../MentaLogExt/target/mentalogext.jar:target/classes:lib/slf4j-api-1.6.2.jar:lib/logback-core-0.9.30.jar:lib/logback-classic-0.9.30.jar:lib/log4j-1.2.16.jar:lib/menta-queue-0.9.4.jar:lib/menta-affinity-0.9.7.jar:/usr/share/java/jna.jar:src/main/java/org/mentalog/test -DmemoryMappedFile=true -DmemoryMappedBufferSize=150m -Dasynchronous=true -Dlogback.configurationFile=./src/main/java/org/mentalog/test/logback.xml -DdetailedBenchmark=true -Ddelay=20 -DprocToBindProducer=2 -DlogProcToBindConsumer=3 -Xms1g -Xmx4g -XX:NewSize=512m -XX:MaxNewSize=1024m org.mentalog.test.GCTest 1000000
*
* 09:45:45.364-INFO Log created: ExtendedDefaultLogger=[file=./mentalog_gc_test.log, isSynchronized=false, isMemoryMappedFile=true, isAsynchronous=true, isExtended=true]
* Wrote 1000000 log lines to a file!
* Logs that allocated memory: 0 (0.0%)
* Logs that did not allocate any memory: 1000000 (100.0%)
* Logs that triggered GC: 0 (0.0%)
* Memory allocated: 0 bytes
* Benchmark: Iterations: 1000000 | Avg Time: 336.68 nanos | Min Time: 224 nanos | Max Time: 57275 nanos
*
* @author soliveira
*/
public class GCTest {
private static final String MSG = "This is a log message not so small that you can use to test. I hope you have fun and it works well!";
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("format GCTest ");
return;
}
int max = Integer.parseInt(args[0]);
long totalMemory = 0;
long logsWithNoMemory = 0;
long logsWithMemory = 0;
long logsTotal = 0;
long logsGC = 0;
Benchmarker bench = new Benchmarker();
int logDelay = Integer.parseInt(SystemUtils.getString("delay", "-1"));
boolean isMemoryMapped = SystemUtils.getBoolean("memoryMappedFile", false);
BytesConfigParam bcp = new BytesConfigParam("memoryMappedBufferSize", -1);
int memoryMappedSize = bcp.value();
boolean async = SystemUtils.getBoolean("asynchronous", false);
if (ExtendedDefaultLogger.isExtension()) {
AsyncThread.setConsumerWaitStrategy(new SpinWaitStrategy());
}
// set producer thread affinity...
int procToBindProducer = Integer.parseInt(SystemUtils.getString("procToBindProducer", "-1"));
if (procToBindProducer > 0) {
if (!Affinity.isAvailable()) {
System.err.println("MentaAffinity is not available!");
} else {
Affinity.assignToProcessor(procToBindProducer, Thread.currentThread());
Affinity.bind(); // we are already running so bind now and be happy...
}
}
Logger logger_mentalog = Log.createLogger(".", "mentalog_gc_test.log", false, isMemoryMapped, memoryMappedSize, async);
Info.log("Log created:", logger_mentalog);
logger_mentalog.log(MSG); // force the log to open to create the file and get ready to log (that of course creates memory!)
for (int i = 1; i <= max; i++) {
if (logDelay != -1) LockSupport.parkNanos(logDelay); // be realistic instead of overflowing the other thread with logs...
long freeMemory = Runtime.getRuntime().freeMemory();
bench.mark();
logger_mentalog.log(MSG);
bench.measure();
long mem = freeMemory - Runtime.getRuntime().freeMemory();
logsTotal++;
if (mem > 0) {
System.out.println("Created " + mem + " bytes after " + i + " log calls");
totalMemory += mem;
logsWithMemory++;
} else if (mem == 0) {
logsWithNoMemory++;
} else {
System.out.println("GC called!");
logsGC++;
}
}
logger_mentalog.close();
Log.stop();
System.out.println("Wrote " + logsTotal + " log lines to a file!");
System.out.println("Logs that allocated memory: " + logsWithMemory + " (" + p(logsWithMemory, logsTotal) + ")");
System.out.println("Logs that did not allocate any memory: " + logsWithNoMemory + " (" + p(logsWithNoMemory, logsTotal) + ")");
System.out.println("Logs that triggered GC: " + logsGC + " (" + p(logsGC, logsTotal) + ")");
System.out.println("Memory allocated: " + totalMemory + " bytes");
System.out.println("Benchmark: " + bench.results());
System.out.println();
}
private static String p(long l, long total) {
double d = ((double) l) / ((double) total) * 100D;
double rounded = Math.round(d * 100) / 100D;
return String.valueOf(rounded) + "%";
}
}