You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sis.apache.org by de...@apache.org on 2012/11/28 18:01:49 UTC
svn commit: r1414813 - in /sis/branches/JDK7: ide-project/NetBeans/nbproject/
sis-utility/src/main/java/org/apache/sis/math/
sis-utility/src/test/java/org/apache/sis/test/
sis-utility/src/test/java/org/apache/sis/util/collection/
Author: desruisseaux
Date: Wed Nov 28 17:01:48 2012
New Revision: 1414813
URL: http://svn.apache.org/viewvc?rev=1414813&view=rev
Log:
Statistics in CacheTest while stressing the cache.
Modified:
sis/branches/JDK7/ide-project/NetBeans/nbproject/project.properties
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/math/StatisticsFormat.java
sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/TestUtilities.java
sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/collection/CacheTest.java
Modified: sis/branches/JDK7/ide-project/NetBeans/nbproject/project.properties
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/ide-project/NetBeans/nbproject/project.properties?rev=1414813&r1=1414812&r2=1414813&view=diff
==============================================================================
--- sis/branches/JDK7/ide-project/NetBeans/nbproject/project.properties (original)
+++ sis/branches/JDK7/ide-project/NetBeans/nbproject/project.properties Wed Nov 28 17:01:48 2012
@@ -18,7 +18,7 @@ source.encoding = UTF-8
javac.source = 1.7
javac.target = 1.7
platform.active = JDK_1.7
-run.jvmargs = -ea
+run.jvmargs = -ea -Dorg.apache.sis.test.verbose=true
junit.forkmode = once
#
Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/math/StatisticsFormat.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/math/StatisticsFormat.java?rev=1414813&r1=1414812&r2=1414813&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/math/StatisticsFormat.java (original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/math/StatisticsFormat.java Wed Nov 28 17:01:48 2012
@@ -53,6 +53,12 @@ final class StatisticsFormat extends Com
private static final int ADDITIONAL_DIGITS = 2;
/**
+ * The locale for row labels. This is usually the same than the format locale,
+ * but not necessarily.
+ */
+ private final Locale labelLocale;
+
+ /**
* {@code true} if the sample values given to {@code Statistics.add(â¦)} methods were the
* totality of the population under study, or {@code false} if they were only a sampling.
*
@@ -72,7 +78,22 @@ final class StatisticsFormat extends Com
* @return A statistics format instance for the current default locale.
*/
public static StatisticsFormat getInstance() {
- return new StatisticsFormat(Locale.getDefault(Locale.Category.FORMAT), null);
+ return new StatisticsFormat(
+ Locale.getDefault(Locale.Category.FORMAT),
+ Locale.getDefault(Locale.Category.DISPLAY), null);
+ }
+
+ /**
+ * Constructs a new format for the given locales.
+ * The timezone is used only if the values added to the {@link Statistics} are dates.
+ *
+ * @param locale The locale, or {@code null} for unlocalized format.
+ * @param labelLocale The locale for row labels. Usually, but not necessarily, same as {@code locale}.
+ * @param timezone The timezone, or {@code null} for UTC.
+ */
+ private StatisticsFormat(final Locale locale, final Locale labelLocale, final TimeZone timezone) {
+ super(locale, timezone);
+ this.labelLocale = labelLocale;
}
/**
@@ -84,6 +105,7 @@ final class StatisticsFormat extends Com
*/
public StatisticsFormat(final Locale locale, final TimeZone timezone) {
super(locale, timezone);
+ labelLocale = locale;
}
/**
@@ -159,7 +181,7 @@ final class StatisticsFormat extends Com
toAppendTo = table = new TableFormatter(toAppendTo, " ");
separator = '\t'; // Will be handled especially by TableFormatter.
}
- final Vocabulary resources = Vocabulary.getResources(getLocale());
+ final Vocabulary resources = Vocabulary.getResources(labelLocale);
/*
* Initialize the NumberFormat for formatting integers without scientific notation.
* This is necessary since the format may have been modified by a previous execution
Modified: sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/TestUtilities.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/TestUtilities.java?rev=1414813&r1=1414812&r2=1414813&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/TestUtilities.java (original)
+++ sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/TestUtilities.java Wed Nov 28 17:01:48 2012
@@ -21,6 +21,7 @@ import java.util.Locale;
import java.util.TimeZone;
import java.util.Iterator;
import java.util.concurrent.Callable;
+import java.io.PrintWriter;
import java.lang.reflect.UndeclaredThrowableException;
import java.text.Format;
import java.text.DateFormat;
@@ -28,6 +29,7 @@ import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.apache.sis.util.Static;
import org.apache.sis.util.ArgumentChecks;
+import org.apache.sis.internal.util.X364;
import static org.junit.Assert.*;
@@ -42,6 +44,11 @@ import static org.junit.Assert.*;
*/
public final strictfp class TestUtilities extends Static {
/**
+ * Width of the separator to print to {@link TestCase#out}, in number of characters.
+ */
+ private static final int SEPARATOR_WIDTH = 80;
+
+ /**
* Maximal time that {@code waitFoo()} methods can wait, in milliseconds.
*
* @see #waitForBlockedState(Thread)
@@ -67,6 +74,41 @@ public final strictfp class TestUtilitie
}
/**
+ * Prints the given title to {@link TestCase#out} in a box. This method is invoked for
+ * writing a clear visual separator between the verbose output of different test cases.
+ *
+ * @param title The title to write.
+ */
+ public static void printSeparator(final String title) {
+ final PrintWriter out = TestCase.out;
+ if (out != null) {
+ final boolean isAnsiSupported = X364.isAnsiSupported();
+ if (isAnsiSupported) {
+ out.print(X364.FOREGROUND_CYAN.sequence());
+ }
+ out.print('â');
+ for (int i=0; i<SEPARATOR_WIDTH-2; i++) {
+ out.print('â');
+ }
+ out.println('â');
+ out.print("â ");
+ out.print(title);
+ for (int i=title.codePointCount(0, title.length()); i<SEPARATOR_WIDTH-3; i++) {
+ out.print(' ');
+ }
+ out.println('â');
+ out.print('â');
+ for (int i=0; i<SEPARATOR_WIDTH-2; i++) {
+ out.print('â');
+ }
+ out.println('â');
+ if (isAnsiSupported) {
+ out.print(X364.FOREGROUND_DEFAULT.sequence());
+ }
+ }
+ }
+
+ /**
* Parses the date for the given string using the {@code "yyyy-MM-dd HH:mm:ss"} pattern
* in UTC timezone.
*
Modified: sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/collection/CacheTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/collection/CacheTest.java?rev=1414813&r1=1414812&r2=1414813&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/collection/CacheTest.java (original)
+++ sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/collection/CacheTest.java Wed Nov 28 17:01:48 2012
@@ -21,7 +21,11 @@ import java.util.HashMap;
import java.util.AbstractMap.SimpleEntry;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicReference;
+import java.io.IOException;
+import java.io.PrintWriter;
+import org.apache.sis.math.Statistics;
+import org.apache.sis.io.TableFormatter;
import org.apache.sis.util.CharSequences;
import org.apache.sis.test.TestUtilities;
import org.apache.sis.test.TestCase;
@@ -169,13 +173,19 @@ public final strictfp class CacheTest ex
/**
* Validates the entries created by the {@link #stress()} test. The check performed in
* this method shall obviously be consistent with the values created by {@code stress()}.
+ *
+ * @param cache The cache to validate.
+ * @return Statistics on the key values of the given map.
*/
- private static void validateStressEntries(final Map<Integer,Integer> cache) {
+ private static Statistics validateStressEntries(final Map<Integer,Integer> cache) {
+ final Statistics statistics = new Statistics();
for (final Map.Entry<Integer,Integer> entry : cache.entrySet()) {
final int key = entry.getKey();
final int value = entry.getValue();
assertEquals(key*key, value);
+ statistics.add(key);
}
+ return statistics;
}
/**
@@ -234,15 +244,20 @@ public final strictfp class CacheTest ex
/*
* Verifies the values.
*/
- validateStressEntries(cache);
+ final Statistics beforeGC = validateStressEntries(cache);
assertTrue("Should not have more entries than what we put in.", cache.size() <= count);
assertFalse("Some entries should be retained by strong references.", cache.isEmpty());
+ /*
+ * If verbose test output is enabled, report the number of cache hits.
+ * The numbers below are for tuning the test only. The output is somewhat
+ * random so we can not check it in a test suite. However if the test is
+ * properly tuned, most values should be non-zero.
+ */
+ final PrintWriter out = CacheTest.out;
if (out != null) {
- out.println();
- out.println("The numbers below are for tuning the test only. The output is somewhat");
- out.println("random so we can not check it in a test suite. However if the test is");
- out.println("properly tuned, most values should be non-zero.");
- out.println();
+ TestUtilities.printSeparator("CacheTest.stress() - testing concurrent accesses");
+ out.print("There is "); out.print(threads.length); out.print(" threads, each of them"
+ + " fetching or creating "); out.print(count); out.println(" values.");
out.println("Number of times a cached value has been reused, for each thread:");
for (int i=0; i<threads.length;) {
final String n = String.valueOf(threads[i++].addCount);
@@ -267,7 +282,32 @@ public final strictfp class CacheTest ex
out.println();
out.flush();
}
+ /*
+ * Gets the statistics of key values after garbage collection. The mean value should
+ * be higher, because oldest values (which should have been garbage collected first)
+ * have lower values. If verbose output is enabled, then we will print the statistics
+ * before to perform the actual check in order to allow the developer to have more
+ * information in case of failure.
+ */
System.gc();
- validateStressEntries(cache);
+ final Statistics afterGC = validateStressEntries(cache);
+ if (out != null) {
+ final TableFormatter table = new TableFormatter(out, " â ");
+ table.setMultiLinesCells(true);
+ table.append("Statistics on the keys before garbage collection:");
+ table.nextColumn();
+ table.append("Statistics on the keys after garbage collection.\n" +
+ "The minimum and the mean values should be greater.");
+ table.nextLine();
+ table.append(beforeGC.toString());
+ table.nextColumn();
+ table.append(afterGC.toString());
+ try {
+ table.flush();
+ } catch (IOException e) {
+ throw new AssertionError(e);
+ }
+ }
+ assertTrue("Mean key value should be greater after garbage collection.", afterGC.mean() >= beforeGC.mean());
}
}