From 212a8ea49e4d1d95f45cf1d609834c90b7419794 Mon Sep 17 00:00:00 2001 From: codistmonk Date: Sun, 3 Feb 2013 16:32:11 +0000 Subject: [PATCH] [Aprog] Added another gc() in Tools + test; added manual timer TicToc + test. git-svn-id: https://aprog.svn.sourceforge.net/svnroot/aprog/trunk@207 7cbf5e2b-b55d-4b93-acdd-c0d7b961df51 --- Aprog/src/net/sourceforge/aprog/tools/TicToc.java | 47 +++++++++++++++++++ Aprog/src/net/sourceforge/aprog/tools/Tools.java | 54 ++++++++++++---------- .../net/sourceforge/aprog/tools/TicTocTest.java | 36 +++++++++++++++ .../net/sourceforge/aprog/tools/ToolsTest.java | 22 ++++++++- 4 files changed, 134 insertions(+), 25 deletions(-) create mode 100644 Aprog/src/net/sourceforge/aprog/tools/TicToc.java create mode 100644 Aprog/test/net/sourceforge/aprog/tools/TicTocTest.java diff --git a/Aprog/src/net/sourceforge/aprog/tools/TicToc.java b/Aprog/src/net/sourceforge/aprog/tools/TicToc.java new file mode 100644 index 0000000..20eb5b7 --- /dev/null +++ b/Aprog/src/net/sourceforge/aprog/tools/TicToc.java @@ -0,0 +1,47 @@ +package net.sourceforge.aprog.tools; + +import static java.lang.System.currentTimeMillis; + +/** + * Simple time measuring class to manually instrument blocks of code. + *
Call {@link #tic()} at the beginning and {@link #toc()} at the end. + *
{@link #getTotalTime()} gives the total time (cumulative) spent in {@link #tic()} {@link #toc()} intervals. + * + * @author codistmonk (creation 2013-01-25) + */ +public final class TicToc { + + private long totalTime; + + private long ticTocTime; + + private long t0; + + /** + * Starts a time interval. + * + * @return The current time in milliseconds + */ + public final long tic() { + this.totalTime += this.ticTocTime; + this.ticTocTime = 0L; + return this.t0 = currentTimeMillis(); + } + + /** + * @return The time in milliseconds since the last call to {@link #tic()} + */ + public final long toc() { + this.ticTocTime = currentTimeMillis() - this.t0; + + return this.ticTocTime; + } + + /** + * @return The cumulative time of this timer (sum of tic toc intervals) + */ + public final long getTotalTime() { + return this.totalTime + this.ticTocTime; + } + +} diff --git a/Aprog/src/net/sourceforge/aprog/tools/Tools.java b/Aprog/src/net/sourceforge/aprog/tools/Tools.java index d67847a..b06b0ba 100644 --- a/Aprog/src/net/sourceforge/aprog/tools/Tools.java +++ b/Aprog/src/net/sourceforge/aprog/tools/Tools.java @@ -24,8 +24,6 @@ package net.sourceforge.aprog.tools; -import static net.sourceforge.aprog.tools.Tools.unchecked; - import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; @@ -52,16 +50,16 @@ import java.util.logging.Logger; * @author codistmonk (creation 2010-06-11) */ public final class Tools { - + /** * @throws IllegalInstantiationException To prevent instantiation */ private Tools() { throw new IllegalInstantiationException(); } - + public static final int DEBUG_STACK_OFFSET = getDebugStackOffset(); - + public static final String LINE_SEPARATOR = System.getProperty("line.separator"); /** @@ -81,6 +79,14 @@ public final class Tools { } /** + * Runs the garbage collector twice. + */ + public static final void gc() { + System.gc(); + System.gc(); + } + + /** * @return *
Range: [0L .. Long.MAX_VALUE] */ @@ -89,7 +95,7 @@ public final class Tools { return runtime.totalMemory() - runtime.freeMemory(); } - + /** * Does nothing, but prevents the IDE from displaying "unused" warning. * @@ -103,7 +109,7 @@ public final class Tools { ignore(object); } } - + /** * This method can be used to determine if a particular compiler performs tail-recursion elimination. *
If this particular derecursifiation isn't performed, calling this method will result in an {@link StackOverflowError}. @@ -116,7 +122,7 @@ public final class Tools { tailRecursiveInfiniteLoop(); } } - + /** * Tries to create a temporary file and initialize it using {@code contents}. * {@code contents} is closed at the end of this method. @@ -162,7 +168,7 @@ public final class Tools { close(contents); } } - + /** * Writes {@code input} to {@code output}; this method does not close the streams when it terminates. * @@ -186,7 +192,7 @@ public final class Tools { throw unchecked(exception); } } - + /** * Tries to close {@code closable} using reflection and without throwing an exception if it fails. * If an exception occurs, it is logged in the caller's logger. @@ -230,7 +236,7 @@ public final class Tools { throw unchecked(exception); } } - + /** * Retrieves the application URL (it could be a folder, a compiled * class file or a jar, depending on the packaging). @@ -242,7 +248,7 @@ public final class Tools { public static final URL getApplicationURL() { return getClassRootURL(getCallerClass()); } - + /** * Retrieves the local file associated with the application URL (it could be a folder, a compiled * class file or a jar, depending on the packaging). @@ -254,7 +260,7 @@ public final class Tools { public static final File getApplicationFile() { return getClassRoot(getCallerClass()); } - + /** * * @param The type of the elements @@ -266,14 +272,14 @@ public final class Tools { */ public static final ArrayList list(final Iterable iterable) { final ArrayList result = new ArrayList(); - + for (final T element : iterable) { result.add(element); } - + return result; } - + /** * * @param The type of the elements @@ -287,32 +293,32 @@ public final class Tools { */ public static final Iterable iterable(final Enumeration enumeration) { return new Iterable() { - + @Override public final Iterator iterator() { return new Iterator() { - + @Override public final boolean hasNext() { return enumeration.hasMoreElements(); } - + @Override public final T next() { return enumeration.nextElement(); } - + @Override public final void remove() { throw new UnsupportedOperationException(); } - + }; } - + }; } - + /** * * @param The common type of the elements @@ -325,7 +331,7 @@ public final class Tools { public static final T[] array(final T... array) { return array; } - + /** * * @param The common type of the elements diff --git a/Aprog/test/net/sourceforge/aprog/tools/TicTocTest.java b/Aprog/test/net/sourceforge/aprog/tools/TicTocTest.java new file mode 100644 index 0000000..c5f9e15 --- /dev/null +++ b/Aprog/test/net/sourceforge/aprog/tools/TicTocTest.java @@ -0,0 +1,36 @@ +package net.sourceforge.aprog.tools; + +import static org.junit.Assert.*; + +import org.junit.Test; + +/** + * Automated tests using JUnit 4 for {@link TicToc}. + * + * @author codistmonk (creation 2013-02-03) + */ +public class TicTocTest { + + @Test + public final void testGetTotalTime() { + final TicToc timer = new TicToc(); + long time; + + timer.tic(); + Tools.gc(500L); + time = timer.toc(); + + assertTrue(500L <= time && time < 600L); + assertTrue(500L <= timer.getTotalTime() && timer.getTotalTime() < 600L); + + Tools.gc(500L); + + timer.tic(); + Tools.gc(500L); + time = timer.toc(); + + assertTrue(500L <= time && time < 600L); + assertTrue(1000L <= timer.getTotalTime() && timer.getTotalTime() < 1100L); + } + +} diff --git a/Aprog/test/net/sourceforge/aprog/tools/ToolsTest.java b/Aprog/test/net/sourceforge/aprog/tools/ToolsTest.java index 81721a1..05d6fea 100644 --- a/Aprog/test/net/sourceforge/aprog/tools/ToolsTest.java +++ b/Aprog/test/net/sourceforge/aprog/tools/ToolsTest.java @@ -53,7 +53,7 @@ import org.junit.Test; public final class ToolsTest { @Test - public final void testGCAndUsedMemory() { + public final void testGCAndUsedMemory1() { final long usedMemoryBeforeAllocation = Tools.usedMemory(); Object object = new int[100000]; final long usedMemoryAfterAllocation = Tools.usedMemory(); @@ -71,6 +71,26 @@ public final class ToolsTest { assertTrue(usedMemoryBeforeAllocation < usedMemoryAfterAllocation); assertTrue(usedMemoryAfterGC < usedMemoryAfterAllocation); } + + @Test + public final void testGCAndUsedMemory2() { + final long usedMemoryBeforeAllocation = Tools.usedMemory(); + Object object = new int[100000]; + final long usedMemoryAfterAllocation = Tools.usedMemory(); + final WeakReference weakReference = new WeakReference(object); + + assertNotNull(weakReference.get()); + + object = null; + + Tools.gc(); + + final long usedMemoryAfterGC = Tools.usedMemory(); + + assertNull(weakReference.get()); + assertTrue(usedMemoryBeforeAllocation < usedMemoryAfterAllocation); + assertTrue(usedMemoryAfterGC < usedMemoryAfterAllocation); + } /** * This creates a temporary folder with a name containing a space "tmp dirXXXXXX" (XXXXXX is a random number generated by {@link File#createTempFile(String, String)}. -- 2.11.4.GIT