All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mentalog.test.GCTest Maven / Gradle / Ivy

There is a newer version: 2.1.2
Show newest version
package org.mentalog.test;

import static org.mentalog.Log.*;

import org.mentalog.Log;
import org.mentalog.util.Benchmarker;

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) {

		// java -verbosegc -cp target/classes:lib/mentalog-license.jar org.mentalog.test.GCTest 100000

		if (args.length != 1) {
			System.out.println("format HeapTest ");
			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();
		
		Log.setFile(true);

		Warn.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++) {
			long freeMemory = Runtime.getRuntime().freeMemory();
			bench.mark();
			Warn.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++;
			}
		}
		
		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) + "%";
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy