Search in sources :

Example 1 with AsyncLogger

use of org.apache.logging.log4j.core.async.AsyncLogger in project logging-log4j2 by apache.

the class SimplePerfTest method main.

public static void main(final String[] args) throws Exception {
    System.setProperty("Log4jContextSelector", AsyncLoggerContextSelector.class.getName());
    final Logger logger = LogManager.getLogger();
    if (!(logger instanceof AsyncLogger)) {
        throw new IllegalStateException();
    }
    // work around a bug in Log4j-2.5
    workAroundLog4j2_5Bug();
    logger.error("Starting...");
    System.out.println("Starting...");
    Thread.sleep(100);
    final RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
    final long testStartNanos = System.nanoTime();
    final long[] UPTIMES = new long[1024];
    final long[] DURATIONS = new long[1024];
    // warmup
    final long startMs = System.currentTimeMillis();
    final long end = startMs + TimeUnit.SECONDS.toMillis(10);
    int warmupCount = 0;
    do {
        runTest(logger, runtimeMXBean, UPTIMES, DURATIONS, warmupCount);
        warmupCount++;
    // Thread.sleep(1000);// drain buffer
    } while (System.currentTimeMillis() < end);
    final int COUNT = 10;
    for (int i = 0; i < COUNT; i++) {
        final int count = warmupCount + i;
        runTest(logger, runtimeMXBean, UPTIMES, DURATIONS, count);
    // Thread.sleep(1000);// drain buffer
    }
    final double testDurationNanos = System.nanoTime() - testStartNanos;
    System.out.println("Done. Calculating stats...");
    printReport("Warmup", UPTIMES, DURATIONS, 0, warmupCount);
    printReport("Test", UPTIMES, DURATIONS, warmupCount, COUNT);
    final StringBuilder sb = new StringBuilder(512);
    sb.append("Test took: ").append(testDurationNanos / (1000.0 * 1000.0 * 1000.0)).append(" sec");
    System.out.println(sb);
    final List<GarbageCollectorMXBean> gcBeans = ManagementFactory.getGarbageCollectorMXBeans();
    for (int i = 0; i < gcBeans.size(); i++) {
        final GarbageCollectorMXBean gcBean = gcBeans.get(i);
        sb.setLength(0);
        sb.append("GC[").append(gcBean.getName()).append("] ");
        sb.append(gcBean.getCollectionCount()).append(" collections, collection time=");
        sb.append(gcBean.getCollectionTime()).append(" millis.");
        System.out.println(sb);
    }
}
Also used : AsyncLogger(org.apache.logging.log4j.core.async.AsyncLogger) GarbageCollectorMXBean(java.lang.management.GarbageCollectorMXBean) RuntimeMXBean(java.lang.management.RuntimeMXBean) AsyncLoggerContextSelector(org.apache.logging.log4j.core.async.AsyncLoggerContextSelector) Logger(org.apache.logging.log4j.Logger) AsyncLogger(org.apache.logging.log4j.core.async.AsyncLogger)

Aggregations

GarbageCollectorMXBean (java.lang.management.GarbageCollectorMXBean)1 RuntimeMXBean (java.lang.management.RuntimeMXBean)1 Logger (org.apache.logging.log4j.Logger)1 AsyncLogger (org.apache.logging.log4j.core.async.AsyncLogger)1 AsyncLoggerContextSelector (org.apache.logging.log4j.core.async.AsyncLoggerContextSelector)1