sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject [sis] 01/03: Simplify the logging levels provided by `PerformanceLevel`. https://issues.apache.org/jira/browse/SIS-504
Date Tue, 24 Nov 2020 22:25:14 GMT
This is an automated email from the ASF dual-hosted git repository.

desruisseaux pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git

commit f67bd79ca7ffa3e4d53d757cc56eb3949af65a8d
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Tue Nov 24 12:34:39 2020 +0100

    Simplify the logging levels provided by `PerformanceLevel`.
    https://issues.apache.org/jira/browse/SIS-504
---
 .../java/org/apache/sis/gui/dataset/LogViewer.java |  2 +-
 .../factory/ConcurrentAuthorityFactory.java        |  2 +-
 .../apache/sis/util/logging/MonolineFormatter.java |  2 +-
 .../apache/sis/util/logging/PerformanceLevel.java  | 90 ++++++++++------------
 .../sis/util/logging/PerformanceLevelTest.java     | 46 ++++++-----
 5 files changed, 67 insertions(+), 75 deletions(-)

diff --git a/application/sis-javafx/src/main/java/org/apache/sis/gui/dataset/LogViewer.java
b/application/sis-javafx/src/main/java/org/apache/sis/gui/dataset/LogViewer.java
index 41b42be..bbe782a 100644
--- a/application/sis-javafx/src/main/java/org/apache/sis/gui/dataset/LogViewer.java
+++ b/application/sis-javafx/src/main/java/org/apache/sis/gui/dataset/LogViewer.java
@@ -260,7 +260,7 @@ public class LogViewer extends Widget {
             VBox.setVgrow(table, Priority.ALWAYS);
 
             levels.getItems().setAll(Level.SEVERE, Level.WARNING, Level.INFO, Level.CONFIG,
-                        PerformanceLevel.PERFORMANCE, Level.FINE, Level.FINER, Level.ALL);
+                                     PerformanceLevel.SLOW, Level.FINE, Level.FINER, Level.ALL);
             levels.setConverter(Converter.INSTANCE);
             levels.getSelectionModel().select(Level.ALL);
             levels.getSelectionModel().selectedItemProperty().addListener((p,o,n) -> setFilter(n));
diff --git a/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/ConcurrentAuthorityFactory.java
b/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/ConcurrentAuthorityFactory.java
index 421b7a9..841dfcf 100644
--- a/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/ConcurrentAuthorityFactory.java
+++ b/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/ConcurrentAuthorityFactory.java
@@ -455,7 +455,7 @@ public abstract class ConcurrentAuthorityFactory<DAO extends GeodeticAuthorityFa
                 if (caller == null) {
                     caller = "create".concat(type.getSimpleName());
                 }
-                final PerformanceLevel level = PerformanceLevel.forDuration(time, TimeUnit.NANOSECONDS);
+                final Level level = PerformanceLevel.forDuration(time, TimeUnit.NANOSECONDS);
                 final Double duration = time / (double) StandardDateFormat.NANOS_PER_SECOND;
                 final Messages resources = Messages.getResources(null);
                 final LogRecord record;
diff --git a/core/sis-utility/src/main/java/org/apache/sis/util/logging/MonolineFormatter.java
b/core/sis-utility/src/main/java/org/apache/sis/util/logging/MonolineFormatter.java
index f972303..ec6ffd4 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/util/logging/MonolineFormatter.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/util/logging/MonolineFormatter.java
@@ -598,7 +598,7 @@ loop:   for (int i=0; ; i++) {
         colors.put(Level.INFO,    X364.BACKGROUND_GREEN);
         colors.put(Level.WARNING, X364.BACKGROUND_YELLOW);
         colors.put(Level.SEVERE,  X364.BACKGROUND_RED);
-        colors.put(PerformanceLevel.PERFORMANCE, X364.BACKGROUND_CYAN);
+        colors.put(PerformanceLevel.SLOW, X364.BACKGROUND_CYAN);
     }
 
     /**
diff --git a/core/sis-utility/src/main/java/org/apache/sis/util/logging/PerformanceLevel.java
b/core/sis-utility/src/main/java/org/apache/sis/util/logging/PerformanceLevel.java
index cd6a3b9..a13392c 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/util/logging/PerformanceLevel.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/util/logging/PerformanceLevel.java
@@ -20,15 +20,16 @@ import java.util.logging.Level;
 import java.util.logging.Logger;
 import java.util.concurrent.TimeUnit;
 import org.apache.sis.util.Configuration;
-
-import static org.apache.sis.util.ArgumentChecks.ensurePositive;
+import org.apache.sis.util.ArgumentChecks;
 
 
 /**
- * Logging levels for measurements of execution time. Different logging levels - {@link #SLOW},
- * {@link #SLOWER} and {@link #SLOWEST} - are provided in order to log only the events taking
- * more than some time duration. For example the console could log only the slowest events,
- * while a file could log all events considered slow.
+ * Logging levels for data processing with execution time measurements.
+ * Those levels are used for events that would normally be logged at {@link Level#FINE},
+ * but with the possibility to use a slightly higher level if execution time was long.
+ * Different logging levels - {@link #SLOW} and {@link #SLOWER} - are provided for logging
+ * only the events taking more time than some thresholds. For example the console could log
+ * only the slowest events, while a file could log all events considered slow.
  *
  * <p>Every levels defined in this class have a {@linkplain #intValue() value} between
the
  * {@link Level#FINE} and {@link Level#CONFIG} values. Consequently performance logging are
@@ -49,7 +50,7 @@ import static org.apache.sis.util.ArgumentChecks.ensurePositive;
  * </ul>
  *
  * @author  Martin Desruisseaux (Geomatys)
- * @version 0.3
+ * @version 1.1
  * @since   0.3
  * @module
  */
@@ -59,39 +60,41 @@ public final class PerformanceLevel extends Level {
      */
     private static final long serialVersionUID = -6547125008284983701L;
 
-    /*
-     * IMPLEMENTATION NOTE: The level values used in the constants below are also used
-     * in the 'switch' statements of the 'setMinDuration(...)' method. If those values
-     * are modified, don't forget to update also the switch statements!!
-     */
-
     /**
      * The level for logging all time measurements, regardless of their duration.
      * The {@linkplain #intValue() value} of this level is 600.
+     *
+     * @deprecated Replaced by {@link Level#FINE}.
+     *
+     * @see <a href="https://issues.apache.org/jira/browse/SIS-504">SIS-504</a>
      */
+    @Deprecated
     public static final PerformanceLevel PERFORMANCE = new PerformanceLevel("PERFORMANCE",
600, 0);
 
     /**
      * The level for logging relatively slow events. By default, only events having an execution
-     * time equals or greater than 0.1 second are logged at this level. However this threshold
-     * can be changed by a call to <code>SLOW.{@linkplain #setMinDuration(long, TimeUnit)}</code>.
+     * time equals or greater than 1 second are logged at this level. However this threshold
can
+     * be changed by a call to <code>SLOW.{@linkplain #setMinDuration(long, TimeUnit)}</code>.
      */
-    public static final PerformanceLevel SLOW = new PerformanceLevel("SLOW", 610, 100000000L);
+    public static final PerformanceLevel SLOW = new PerformanceLevel("SLOW", 620, 1000_000_000L);
 
     /**
      * The level for logging only events slower than the ones logged at the {@link #SLOW}
level.
-     * By default, only events having an execution time equals or greater than 1 second are
+     * By default, only events having an execution time equals or greater than 10 seconds
are
      * logged at this level. However this threshold can be changed by a call to
      * <code>SLOWER.{@linkplain #setMinDuration(long, TimeUnit)}</code>.
      */
-    public static final PerformanceLevel SLOWER = new PerformanceLevel("SLOWER", 620, 1000000000L);
+    public static final PerformanceLevel SLOWER = new PerformanceLevel("SLOWER", 630, 10_000_000_000L);
 
     /**
-     * The level for logging only slowest events. By default, only events having an execution
-     * time equals or greater than 5 seconds are logged at this level. However this threshold
-     * can be changed by a call to <code>SLOWEST.{@linkplain #setMinDuration(long,
TimeUnit)}</code>.
+     * The level for logging only slowest events.
+     *
+     * @deprecated Removed for simplification.
+     *
+     * @see <a href="https://issues.apache.org/jira/browse/SIS-504">SIS-504</a>
      */
-    public static final PerformanceLevel SLOWEST = new PerformanceLevel("SLOWEST", 630, 5000000000L);
+    @Deprecated
+    public static final PerformanceLevel SLOWEST = SLOWER;
 
     /**
      * The minimal duration (in nanoseconds) for logging the record.
@@ -112,18 +115,19 @@ public final class PerformanceLevel extends Level {
 
     /**
      * Returns the level to use for logging an event of the given duration.
+     * The method may return {@link Level#FINE}, {@link #SLOW} or {@link #SLOWER}
+     * depending on the duration.
      *
      * @param  duration  the event duration.
      * @param  unit      the unit of the given duration value.
      * @return the level to use for logging an event of the given duration.
      */
-    public static PerformanceLevel forDuration(long duration, final TimeUnit unit) {
+    public static Level forDuration(long duration, final TimeUnit unit) {
         duration = unit.toNanos(duration);
-        if (duration >= SLOWER.minDuration) {
-            return (duration >= SLOWEST.minDuration) ? SLOWEST : SLOWER;
-        } else {
-            return (duration >= SLOW.minDuration) ? SLOW : PERFORMANCE;
+        if (duration < SLOW.minDuration) {
+            return Level.FINE;              // Most common case.
         }
+        return (duration >= SLOWER.minDuration) ? SLOWER : SLOW;
     }
 
     /**
@@ -147,37 +151,27 @@ public final class PerformanceLevel extends Level {
      *       are also set to the given duration.</li>
      * </ul>
      *
-     * <div class="note"><b>Usage note:</b>
-     * The duration of the {@link #PERFORMANCE} level can not be modified: it is always zero.
-     * However invoking this method on the {@code PERFORMANCE} field will ensure that every
-     * {@code SLOW*} levels will have at least the given duration.</div>
-     *
      * @param  duration  the minimal duration.
      * @param  unit      the unit of the given duration value.
-     * @throws IllegalArgumentException if the given duration is negative.
+     * @throws IllegalArgumentException if the given duration is zero or negative.
      */
     @Configuration
     @SuppressWarnings("fallthrough")
     public void setMinDuration(long duration, final TimeUnit unit) throws IllegalArgumentException
{
-        ensurePositive("duration", duration);
+        if (this == PERFORMANCE) {
+            SLOW.setMinDuration(duration, unit);
+            return;
+        }
+        ArgumentChecks.ensureStrictlyPositive("duration", duration);
         duration = unit.toNanos(duration);
         final int value = intValue();
         synchronized (PerformanceLevel.class) {
-            // Check the value of slower levels.
-            switch (value) {
-                default:  throw new AssertionError(this);
-                case 600: if (duration > SLOW   .minDuration) SLOW   .minDuration = duration;
-                case 610: if (duration > SLOWER .minDuration) SLOWER .minDuration = duration;
-                case 620: if (duration > SLOWEST.minDuration) SLOWEST.minDuration = duration;
-                case 630: // Do nothing since there is no level slower than 'SLOWEST'.
+            if (value >= SLOWER.intValue() && duration < SLOW.minDuration)
{
+                SLOW.minDuration = duration;
             }
-            // Check the value of faster levels.
-            switch (value) {
-                default:  throw new AssertionError(this);
-                case 630: if (duration < SLOWER .minDuration) SLOWER .minDuration = duration;
-                case 620: if (duration < SLOW   .minDuration) SLOW   .minDuration = duration;
-                case 610: minDuration = duration;
-                case 600: // Do nothing, since we don't allow modification of PERFORMANCE
level.
+            minDuration = duration;
+            if (value <= SLOW.intValue() && duration > SLOWER.minDuration)
{
+                SLOWER.minDuration = duration;
             }
         }
     }
diff --git a/core/sis-utility/src/test/java/org/apache/sis/util/logging/PerformanceLevelTest.java
b/core/sis-utility/src/test/java/org/apache/sis/util/logging/PerformanceLevelTest.java
index 32adbb1..6b1da85 100644
--- a/core/sis-utility/src/test/java/org/apache/sis/util/logging/PerformanceLevelTest.java
+++ b/core/sis-utility/src/test/java/org/apache/sis/util/logging/PerformanceLevelTest.java
@@ -21,6 +21,7 @@ import org.apache.sis.test.TestCase;
 import org.junit.Test;
 
 import static org.junit.Assert.*;
+import static java.util.logging.Level.FINE;
 import static org.apache.sis.util.logging.PerformanceLevel.*;
 
 
@@ -28,7 +29,7 @@ import static org.apache.sis.util.logging.PerformanceLevel.*;
  * Tests the {@link PerformanceLevel} class.
  *
  * @author  Martin Desruisseaux (Geomatys)
- * @version 0.3
+ * @version 1.1
  * @since   0.3
  * @module
  */
@@ -38,10 +39,9 @@ public final strictfp class PerformanceLevelTest extends TestCase {
      */
     @Test
     public void testGetMinDuration() {
-        assertEquals(0,   PERFORMANCE.getMinDuration(TimeUnit.NANOSECONDS));
-        assertEquals(100, SLOW       .getMinDuration(TimeUnit.MILLISECONDS));
-        assertEquals(1,   SLOWER     .getMinDuration(TimeUnit.SECONDS));
-        assertEquals(5,   SLOWEST    .getMinDuration(TimeUnit.SECONDS));
+        assertEquals(0,  PERFORMANCE.getMinDuration(TimeUnit.NANOSECONDS));
+        assertEquals(1,  SLOW  .getMinDuration(TimeUnit.SECONDS));
+        assertEquals(10, SLOWER.getMinDuration(TimeUnit.SECONDS));
     }
 
     /**
@@ -49,28 +49,26 @@ public final strictfp class PerformanceLevelTest extends TestCase {
      */
     @Test
     public void testSetMinDuration() {
+        final long t1 = SLOW  .getMinDuration(TimeUnit.SECONDS);
+        final long t2 = SLOWER.getMinDuration(TimeUnit.SECONDS);
         try {
-            SLOW.setMinDuration(2, TimeUnit.SECONDS);
-            assertEquals(0, PERFORMANCE.getMinDuration(TimeUnit.SECONDS));
-            assertEquals(2, SLOW       .getMinDuration(TimeUnit.SECONDS));
-            assertEquals(2, SLOWER     .getMinDuration(TimeUnit.SECONDS));
-            assertEquals(5, SLOWEST    .getMinDuration(TimeUnit.SECONDS));
+            SLOW.setMinDuration(80, TimeUnit.SECONDS);
+            assertEquals( 0, PERFORMANCE.getMinDuration(TimeUnit.SECONDS));
+            assertEquals(80, SLOW  .getMinDuration(TimeUnit.SECONDS));
+            assertEquals(80, SLOWER.getMinDuration(TimeUnit.SECONDS));
 
-            SLOWEST.setMinDuration(1, TimeUnit.SECONDS);
+            SLOWER.setMinDuration(4, TimeUnit.SECONDS);
             assertEquals(0, PERFORMANCE.getMinDuration(TimeUnit.SECONDS));
-            assertEquals(1, SLOW       .getMinDuration(TimeUnit.SECONDS));
-            assertEquals(1, SLOWER     .getMinDuration(TimeUnit.SECONDS));
-            assertEquals(1, SLOWEST    .getMinDuration(TimeUnit.SECONDS));
+            assertEquals(4, SLOW  .getMinDuration(TimeUnit.SECONDS));
+            assertEquals(4, SLOWER.getMinDuration(TimeUnit.SECONDS));
 
             PERFORMANCE.setMinDuration(6, TimeUnit.SECONDS);
             assertEquals(0, PERFORMANCE.getMinDuration(TimeUnit.SECONDS));
-            assertEquals(6, SLOW       .getMinDuration(TimeUnit.SECONDS));
-            assertEquals(6, SLOWER     .getMinDuration(TimeUnit.SECONDS));
-            assertEquals(6, SLOWEST    .getMinDuration(TimeUnit.SECONDS));
+            assertEquals(6, SLOW  .getMinDuration(TimeUnit.SECONDS));
+            assertEquals(6, SLOWER.getMinDuration(TimeUnit.SECONDS));
         } finally {
-            SLOW   .setMinDuration(100, TimeUnit.MILLISECONDS);
-            SLOWER .setMinDuration(1,   TimeUnit.SECONDS);
-            SLOWEST.setMinDuration(5,   TimeUnit.SECONDS);
+            SLOWER .setMinDuration(t1, TimeUnit.SECONDS);
+            SLOWER .setMinDuration(t2, TimeUnit.SECONDS);
         }
     }
 
@@ -79,9 +77,9 @@ public final strictfp class PerformanceLevelTest extends TestCase {
      */
     @Test
     public void testForDuration() {
-        assertSame(SLOW,        forDuration(500, TimeUnit.MILLISECONDS));
-        assertSame(SLOWER,      forDuration(2,   TimeUnit.SECONDS));
-        assertSame(SLOWEST,     forDuration(6,   TimeUnit.SECONDS));
-        assertSame(PERFORMANCE, forDuration(50,  TimeUnit.MILLISECONDS));
+        assertSame(SLOW,    forDuration( 2, TimeUnit.SECONDS));
+        assertSame(SLOWER,  forDuration(10, TimeUnit.SECONDS));
+        assertSame(SLOWER , forDuration(20, TimeUnit.SECONDS));
+        assertSame(FINE,    forDuration(50, TimeUnit.MILLISECONDS));
     }
 }


Mime
View raw message