diff --git a/hawkbit-repository/hawkbit-repository-jpa-eclipselink/src/main/java/org/eclipse/hawkbit/repository/jpa/Statistics.java b/hawkbit-repository/hawkbit-repository-jpa-eclipselink/src/main/java/org/eclipse/hawkbit/repository/jpa/Statistics.java index 36bd7460c..7fbbd3c92 100644 --- a/hawkbit-repository/hawkbit-repository-jpa-eclipselink/src/main/java/org/eclipse/hawkbit/repository/jpa/Statistics.java +++ b/hawkbit-repository/hawkbit-repository-jpa-eclipselink/src/main/java/org/eclipse/hawkbit/repository/jpa/Statistics.java @@ -40,7 +40,7 @@ *
    *
  1. The Spring property spring.jpa.properties.eclipselink.profiler=PerformanceMonitor shall be set - enables Eclipselink statistics * collecting
  2. - *
  3. By default the stdout log is disabled by setting hawkbit.jpa.statistics.dumpPeriodMS=9223372036854775807 (Long.MAX_VALUE) - + *
  4. By default the periodic stdout log is disabled by setting hawkbit.jpa.statistics.dumpPeriodMS=9223372036854775807 (Long.MAX_VALUE) - * i.e. effectively never. If log is required it should be set to the required period
  5. *
  6. The MeterRegistry shall be registered available - e.g. include org.springframework.boot:spring-boot-actuator-autoconfigure
  7. *
  8. (?) When using in test the metrics MAYBE shall be enabled with @AutoConfigureObservability(tracing = false)
  9. diff --git a/hawkbit-repository/hawkbit-repository-jpa-hibernate/src/main/java/org/eclipse/hawkbit/repository/jpa/Statistics.java b/hawkbit-repository/hawkbit-repository-jpa-hibernate/src/main/java/org/eclipse/hawkbit/repository/jpa/Statistics.java index 0d3733333..a0f1e06d6 100644 --- a/hawkbit-repository/hawkbit-repository-jpa-hibernate/src/main/java/org/eclipse/hawkbit/repository/jpa/Statistics.java +++ b/hawkbit-repository/hawkbit-repository-jpa-hibernate/src/main/java/org/eclipse/hawkbit/repository/jpa/Statistics.java @@ -25,7 +25,7 @@ *
      *
    1. The Spring property spring.jpa.properties.hibernate.generate_statistics=true shall be set - enables Hibernate statistics * collecting
    2. - *
    3. If don't need log in the stdout set logging.level.org.hibernate.engine.internal.StatisticalLoggingSessionEventListener=WARN
    4. + *
    5. If don't need periodic log (Slf4J) set logging.level.org.hibernate.engine.internal.StatisticalLoggingSessionEventListener=WARN
    6. *
    7. The MeterRegistry shall be registered available - e.g. include org.springframework.boot:spring-boot-actuator-autoconfigure
    8. *
    9. Hibernate reporting to micrometer shall be enabled - include org.hibernate.orm:hibernate-micrometer
    10. *
    11. (?) When using in test the metrics MAYBE shall be enabled with @AutoConfigureObservability(tracing = false)
    12. diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/utils/StatisticsUtils.java b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/utils/StatisticsUtils.java index a6ee0072e..30dfafce2 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/utils/StatisticsUtils.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/utils/StatisticsUtils.java @@ -26,11 +26,12 @@ /** * (Experimental) Utility class to get some statistics. + * It's main purpose is to be used for debugging / testing (performance) purposes. */ @NoArgsConstructor(access = AccessLevel.PRIVATE) public class StatisticsUtils { - private static final ThreadLocal> LAST_COUNTERS = ThreadLocal.withInitial(HashMap::new); + private static final ThreadLocal> LAST_COUNTERS = ThreadLocal.withInitial(MapUFToString::new); // for test purposes we may want to flush the statistics and to get diff from the last get int THIS thread public static Map diff() { @@ -60,7 +61,7 @@ public static Map counters() { Statistics.flush(); - final Map counters = new HashMap<>(); + final Map counters = new MapUFToString(); meterRegistry.forEachMeter(m -> { final Meter.Id id = m.getId(); if (id.getName().startsWith(Statistics.METER_PREFIX)) { @@ -78,7 +79,7 @@ public static Map counters() { if (!ObjectUtils.isEmpty(tags)) { key.append(" ["); tags.forEach(tag -> key.append(tag.getKey()).append('=').append(tag.getValue()).append(", ")); - key.setLength(key.length() - 2); + key.setLength(key.length() - 2); // remove the last ", " key.append(']'); } counters.put(key.toString(), value); @@ -88,4 +89,24 @@ public static Map counters() { LAST_COUNTERS.set(counters); return counters; } + + // Map with user-friendly toString, sorted and without the last ", " + private static class MapUFToString extends HashMap { + + @Override + public String toString() { + if (isEmpty()) { + return "{}"; + } + + final StringBuilder sb = new StringBuilder("{"); + entrySet().stream() + .sorted() + .forEach(e -> sb.append(e.getKey()).append(": ").append(e.getValue()).append(", ")); + sb.setLength(sb.length() - 2); // remove the last ", " + sb.append("}"); + + return sb.toString(); + } + } } \ No newline at end of file diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/test/resources/jpa-test.properties b/hawkbit-repository/hawkbit-repository-jpa/src/test/resources/jpa-test.properties index 6cda0a4fa..b10c73419 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/test/resources/jpa-test.properties +++ b/hawkbit-repository/hawkbit-repository-jpa/src/test/resources/jpa-test.properties @@ -31,7 +31,7 @@ logging.level.org.eclipse.persistence=ERROR ## Enable Hibernate statistics #spring.jpa.properties.hibernate.generate_statistics=true ## Disables info log messages from Hibernate statistics -logging.level.org.hibernate.engine.internal.StatisticalLoggingSessionEventListener=WARN +#logging.level.org.hibernate.engine.internal.StatisticalLoggingSessionEventListener=WARN # Debug & Monitor Hibernate - END #logging.level.org.springframework.security=TRACE