From 34dc4375dadd04fc28998462d6bad805f5f00adb Mon Sep 17 00:00:00 2001 From: rob Date: Wed, 22 Feb 2023 15:22:58 +1300 Subject: [PATCH 1/2] Tidy existing given that lineNumber in SpiProfileLocationFactory always comes in as 0 The history behind this is that originally there was a "profileId" concept where that was a int value that could be used as an "id" for a transactional method that we wanted to profile. That went away and then the idea was that bytecode enhancement would determine the line number and we would use that but that was a bad idea and we ended up with lineNumber always as 0. These days with StackWalker and DProfileLocation we can do this better that way so yeah. A followup to this is to add to the api a boolean flag as to whether a profile location should be with line numbers (which can change frequently due to refactoring etc) --- .../service/SpiProfileLocationFactory.java | 16 ++++++++++++-- .../server/profile/DProfileLocation.java | 22 +------------------ .../profile/DProfileLocationFactory.java | 9 +++----- .../server/profile/DTimedProfileLocation.java | 3 +-- .../server/profile/UtilLocation.java | 11 +++++++++- .../profile/BasicProfileLocationTest.java | 6 ++--- .../server/profile/UtilLocationTest.java | 12 +++++++--- 7 files changed, 41 insertions(+), 38 deletions(-) diff --git a/ebean-api/src/main/java/io/ebean/service/SpiProfileLocationFactory.java b/ebean-api/src/main/java/io/ebean/service/SpiProfileLocationFactory.java index e5626a69e..a2f269641 100644 --- a/ebean-api/src/main/java/io/ebean/service/SpiProfileLocationFactory.java +++ b/ebean-api/src/main/java/io/ebean/service/SpiProfileLocationFactory.java @@ -13,9 +13,21 @@ public interface SpiProfileLocationFactory { ProfileLocation create(); /** - * Create a profile location with a line number. + * Create with a given label - used only with {@code @Transaction}. + * + * @param label the label for the transaction */ - ProfileLocation create(int lineNumber, String label); + ProfileLocation create(String label); + + /** + * Create a profile location with a line number. + * + * @param lineNumber always 0 + * @param label the label for the transaction + */ + default ProfileLocation create(int lineNumber, String label) { + return create(label); + } /** * Create a known location. diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/profile/DProfileLocation.java b/ebean-core/src/main/java/io/ebeaninternal/server/profile/DProfileLocation.java index ed56cf89f..0f51e078a 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/profile/DProfileLocation.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/profile/DProfileLocation.java @@ -15,18 +15,9 @@ class DProfileLocation implements ProfileLocation { private String fullLocation; private String location; private String label; - private final int lineNumber; private int traceCount; DProfileLocation() { - this(0); - } - - /** - * Create with a given line number. - */ - DProfileLocation(int lineNumber) { - this.lineNumber = lineNumber; } @Override @@ -96,19 +87,8 @@ class DProfileLocation implements ProfileLocation { private String filter(Stream frames) { return frames.filter(StackWalkFilter.filter()) .findFirst() - .map(line -> withLineNumber(line.toString())) + .map(Object::toString) .orElse(UNKNOWN); } - private String withLineNumber(String traceLine) { - if (lineNumber == 0) { - return traceLine; - } else if (traceLine.endsWith(":1)")) { - return traceLine.substring(0, traceLine.length() - 3) + ":" + lineNumber + ")"; - } else if (traceLine.contains(":")) { - return traceLine; - } else { - return traceLine.substring(0, traceLine.length() - 1) + ":" + lineNumber + ")"; - } - } } diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/profile/DProfileLocationFactory.java b/ebean-core/src/main/java/io/ebeaninternal/server/profile/DProfileLocationFactory.java index cc6e19bd3..915a96163 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/profile/DProfileLocationFactory.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/profile/DProfileLocationFactory.java @@ -2,7 +2,6 @@ package io.ebeaninternal.server.profile; import io.ebean.ProfileLocation; import io.ebean.metric.MetricFactory; -import io.ebean.metric.TimedMetric; import io.ebean.service.SpiProfileLocationFactory; /** @@ -16,11 +15,9 @@ public final class DProfileLocationFactory implements SpiProfileLocationFactory } @Override - public ProfileLocation create(int lineNumber, String label) { - - TimedMetric timedMetric = MetricFactory.get().createTimedMetric("txn.named." + label); - - DTimedProfileLocation loc = new DTimedProfileLocation(lineNumber, label, timedMetric); + public ProfileLocation create(String label) { + final var timedMetric = MetricFactory.get().createTimedMetric("txn.named." + label); + final var loc = new DTimedProfileLocation(label, timedMetric); TimedProfileLocationRegistry.register(loc); return loc; } diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/profile/DTimedProfileLocation.java b/ebean-core/src/main/java/io/ebeaninternal/server/profile/DTimedProfileLocation.java index 9039a6751..cbbf3ad8d 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/profile/DTimedProfileLocation.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/profile/DTimedProfileLocation.java @@ -15,8 +15,7 @@ final class DTimedProfileLocation extends DProfileLocation implements TimedProfi private String fullName; private String reportName; - DTimedProfileLocation(int lineNumber, String label, TimedMetric timedMetric) { - super(lineNumber); + DTimedProfileLocation(String label, TimedMetric timedMetric) { this.label = label; this.timedMetric = timedMetric; this.overrideMetricName = "".equals(label); diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/profile/UtilLocation.java b/ebean-core/src/main/java/io/ebeaninternal/server/profile/UtilLocation.java index 65ab1ce4d..a5df881b3 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/profile/UtilLocation.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/profile/UtilLocation.java @@ -3,14 +3,23 @@ package io.ebeaninternal.server.profile; final class UtilLocation { static String loc(String full) { + return loc(full, false); + } + + static String loc(String full, boolean includeLineNumber) { final int pos = full.lastIndexOf('('); if (pos > -1) { - return full.substring(0, pos); + return !includeLineNumber ? full.substring(0, pos) : full.substring(0, pos) + lineNumberFrom(full); } else { return full; } } + private static String lineNumberFrom(String full) { + int pos = full.lastIndexOf(':'); + return pos == -1 ? "" : full.substring(pos, full.length() - 1); + } + static String label(String location) { return trimInit(shortDesc(location)); } diff --git a/ebean-core/src/test/java/io/ebeaninternal/server/profile/BasicProfileLocationTest.java b/ebean-core/src/test/java/io/ebeaninternal/server/profile/BasicProfileLocationTest.java index 7e89b08a8..c7ab83034 100644 --- a/ebean-core/src/test/java/io/ebeaninternal/server/profile/BasicProfileLocationTest.java +++ b/ebean-core/src/test/java/io/ebeaninternal/server/profile/BasicProfileLocationTest.java @@ -16,7 +16,7 @@ class BasicProfileLocationTest { @Test void metricNameFromOverride() { - DTimedProfileLocation loc = new DTimedProfileLocation(12, "", MetricFactory.get().createTimedMetric("a.b.c")); + DTimedProfileLocation loc = new DTimedProfileLocation("", MetricFactory.get().createTimedMetric("a.b.c")); loc.initWith("foo.label"); loc.add(42); @@ -39,7 +39,7 @@ class BasicProfileLocationTest { @Test void metricNameFromTimed() { - DTimedProfileLocation loc = new DTimedProfileLocation(12, "foo", MetricFactory.get().createTimedMetric("a.b.c")); + DTimedProfileLocation loc = new DTimedProfileLocation("foo", MetricFactory.get().createTimedMetric("a.b.c")); loc.add(42); BasicMetricVisitor visitor = new BasicMetricVisitor("v", naming); @@ -52,7 +52,7 @@ class BasicProfileLocationTest { @Test void obtain() { - DProfileLocation loc = new DTimedProfileLocation(12, "foo", MetricFactory.get().createTimedMetric("junk")); + DProfileLocation loc = new DTimedProfileLocation("foo", MetricFactory.get().createTimedMetric("junk")); assertThat(loc.obtain()).isTrue(); assertThat(loc.fullLocation()).endsWith("org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:725)"); diff --git a/ebean-core/src/test/java/io/ebeaninternal/server/profile/UtilLocationTest.java b/ebean-core/src/test/java/io/ebeaninternal/server/profile/UtilLocationTest.java index 778379c59..94eac9135 100644 --- a/ebean-core/src/test/java/io/ebeaninternal/server/profile/UtilLocationTest.java +++ b/ebean-core/src/test/java/io/ebeaninternal/server/profile/UtilLocationTest.java @@ -5,17 +5,23 @@ import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; -public class UtilLocationTest { +class UtilLocationTest { @Test - public void label() { + void label() { Assertions.assertThat(UtilLocation.label("foo")).isEqualTo("foo"); assertThat(UtilLocation.label("ProfileLocationTest$Other.")).isEqualTo("ProfileLocationTest$Other.init"); } @Test - public void loc() { + void loc() { assertThat(UtilLocation.loc("org.foo.MyFoo.doIt(MyFoo.java:12)")).isEqualTo("org.foo.MyFoo.doIt"); assertThat(UtilLocation.label("org.foo.MyFoo.doIt")).isEqualTo("MyFoo.doIt"); } + + @Test + void locWithLineNumber() { + assertThat(UtilLocation.loc("org.foo.MyFoo.doIt(MyFoo.java:12)", true)).isEqualTo("org.foo.MyFoo.doIt:12"); + assertThat(UtilLocation.label("org.foo.MyFoo.doIt:12")).isEqualTo("MyFoo.doIt:12"); + } } From 2e0dba3741c14dfdfc5eef98724b663c1846e4e6 Mon Sep 17 00:00:00 2001 From: rob Date: Wed, 22 Feb 2023 23:02:42 +1300 Subject: [PATCH 2/2] #2981 - ENH: Add profile-line-number-mode with options none | all | auto ... to control adding line numbers to profile locations --- .../main/java/io/ebean/ProfileLocation.java | 23 ++++++++++++++++--- .../service/SpiProfileLocationFactory.java | 15 ++++-------- .../main/resources/META-INF/ebean-version.mf | 2 +- .../server/profile/DProfileLocation.java | 6 +++-- .../profile/DProfileLocationFactory.java | 7 +++++- .../server/profile/DTimedProfileLocation.java | 1 + .../test/resources/META-INF/ebean-version.mf | 2 +- ebean-querybean/pom.xml | 2 +- .../test/java/org/querytest/QContactTest.java | 12 ++++++++++ .../java/org/querytest/QCustomerTest.java | 11 +++++++++ .../resources/application-test.properties | 2 ++ ebean-querybean/src/test/resources/ebean.mf | 4 +++- .../tests/profile/ProfileLocationTest.java | 16 +++++++++++-- .../test/resources/META-INF/ebean-version.mf | 2 +- pom.xml | 4 ++-- .../test/resources/META-INF/ebean-version.mf | 2 +- 16 files changed, 85 insertions(+), 26 deletions(-) diff --git a/ebean-api/src/main/java/io/ebean/ProfileLocation.java b/ebean-api/src/main/java/io/ebean/ProfileLocation.java index 33584a0b3..20a9a0d57 100644 --- a/ebean-api/src/main/java/io/ebean/ProfileLocation.java +++ b/ebean-api/src/main/java/io/ebean/ProfileLocation.java @@ -17,15 +17,32 @@ public interface ProfileLocation { } /** - * Create and return a new ProfileLocation with a given lineNumber and label. + * Create and return a new ProfileLocation with line number. */ - static ProfileLocation create(int lineNumber, String label) { - return XServiceProvider.profileLocationFactory().create(lineNumber, label); + static ProfileLocation createWithLine() { + return XServiceProvider.profileLocationFactory().createWithLine(); } /** + * Create and return a new ProfileLocation with a given lineNumber and label. + */ + static ProfileLocation create(String label) { + return XServiceProvider.profileLocationFactory().create(label); + } + + /** + * Deprecated in favor of {@link #create(String)}. + */ + @Deprecated + static ProfileLocation create(int lineNumber, String label) { + return create(label); + } + + /** + * Deprecated for removal - not used. * Create and return a new ProfileLocation with a given location. */ + @Deprecated static ProfileLocation createAt(String location) { return XServiceProvider.profileLocationFactory().createAt(location); } diff --git a/ebean-api/src/main/java/io/ebean/service/SpiProfileLocationFactory.java b/ebean-api/src/main/java/io/ebean/service/SpiProfileLocationFactory.java index a2f269641..7c78e37d7 100644 --- a/ebean-api/src/main/java/io/ebean/service/SpiProfileLocationFactory.java +++ b/ebean-api/src/main/java/io/ebean/service/SpiProfileLocationFactory.java @@ -12,6 +12,11 @@ public interface SpiProfileLocationFactory { */ ProfileLocation create(); + /** + * Create a profile location with line numbering. + */ + ProfileLocation createWithLine(); + /** * Create with a given label - used only with {@code @Transaction}. * @@ -19,16 +24,6 @@ public interface SpiProfileLocationFactory { */ ProfileLocation create(String label); - /** - * Create a profile location with a line number. - * - * @param lineNumber always 0 - * @param label the label for the transaction - */ - default ProfileLocation create(int lineNumber, String label) { - return create(label); - } - /** * Create a known location. */ diff --git a/ebean-api/src/main/resources/META-INF/ebean-version.mf b/ebean-api/src/main/resources/META-INF/ebean-version.mf index 45259b053..5b6b424fe 100644 --- a/ebean-api/src/main/resources/META-INF/ebean-version.mf +++ b/ebean-api/src/main/resources/META-INF/ebean-version.mf @@ -1 +1 @@ -ebean-version: 142 +ebean-version: 143 diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/profile/DProfileLocation.java b/ebean-core/src/main/java/io/ebeaninternal/server/profile/DProfileLocation.java index 0f51e078a..ae0ef13a5 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/profile/DProfileLocation.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/profile/DProfileLocation.java @@ -12,12 +12,14 @@ class DProfileLocation implements ProfileLocation { private static final String UNKNOWN = "unknown"; + private final boolean withLine; private String fullLocation; private String location; private String label; private int traceCount; - DProfileLocation() { + DProfileLocation(boolean withLine) { + this.withLine = withLine; } @Override @@ -37,7 +39,7 @@ class DProfileLocation implements ProfileLocation { return false; } final String loc = create(); - final String location = UtilLocation.loc(loc); + final String location = UtilLocation.loc(loc, withLine); this.label = UtilLocation.label(location); this.location = location; this.fullLocation = loc; diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/profile/DProfileLocationFactory.java b/ebean-core/src/main/java/io/ebeaninternal/server/profile/DProfileLocationFactory.java index 915a96163..9b4253624 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/profile/DProfileLocationFactory.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/profile/DProfileLocationFactory.java @@ -11,7 +11,12 @@ public final class DProfileLocationFactory implements SpiProfileLocationFactory @Override public ProfileLocation create() { - return new DProfileLocation(); + return new DProfileLocation(false); + } + + @Override + public ProfileLocation createWithLine() { + return new DProfileLocation(true); } @Override diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/profile/DTimedProfileLocation.java b/ebean-core/src/main/java/io/ebeaninternal/server/profile/DTimedProfileLocation.java index cbbf3ad8d..fcb7c2f82 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/profile/DTimedProfileLocation.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/profile/DTimedProfileLocation.java @@ -16,6 +16,7 @@ final class DTimedProfileLocation extends DProfileLocation implements TimedProfi private String reportName; DTimedProfileLocation(String label, TimedMetric timedMetric) { + super(false); this.label = label; this.timedMetric = timedMetric; this.overrideMetricName = "".equals(label); diff --git a/ebean-externalmapping-xml/src/test/resources/META-INF/ebean-version.mf b/ebean-externalmapping-xml/src/test/resources/META-INF/ebean-version.mf index 45259b053..5b6b424fe 100644 --- a/ebean-externalmapping-xml/src/test/resources/META-INF/ebean-version.mf +++ b/ebean-externalmapping-xml/src/test/resources/META-INF/ebean-version.mf @@ -1 +1 @@ -ebean-version: 142 +ebean-version: 143 diff --git a/ebean-querybean/pom.xml b/ebean-querybean/pom.xml index 085e7a884..3d693e4fe 100644 --- a/ebean-querybean/pom.xml +++ b/ebean-querybean/pom.xml @@ -107,7 +107,7 @@ true - io.ebean.tile:enhancement:13.12.0 + io.ebean.tile:enhancement:13.13.1 diff --git a/ebean-querybean/src/test/java/org/querytest/QContactTest.java b/ebean-querybean/src/test/java/org/querytest/QContactTest.java index 296aa5be6..e2cec545e 100644 --- a/ebean-querybean/src/test/java/org/querytest/QContactTest.java +++ b/ebean-querybean/src/test/java/org/querytest/QContactTest.java @@ -9,9 +9,21 @@ public class QContactTest { @Test public void test_oneToManyMap() { + findThem(); + findThem(ZonedDateTime.now()); + } + + void findThem() { new QContact() .others.fetch() .zoneDateTime.before(ZonedDateTime.now()) .findList(); } + + void findThem(ZonedDateTime dateTime) { + new QContact() + .others.fetch() + .zoneDateTime.before(dateTime) + .findList(); + } } diff --git a/ebean-querybean/src/test/java/org/querytest/QCustomerTest.java b/ebean-querybean/src/test/java/org/querytest/QCustomerTest.java index 0375ace45..d8f0dff38 100644 --- a/ebean-querybean/src/test/java/org/querytest/QCustomerTest.java +++ b/ebean-querybean/src/test/java/org/querytest/QCustomerTest.java @@ -368,6 +368,17 @@ public class QCustomerTest { .findList(); } + @Test + public void testTwoDiffQueryTypes_expect_NoLineNumbers() { + new QCustomer() + .id.isIn(34L) + .findList(); + + new QContact() + .email.isNull() + .findList(); + } + @Test public void testQueryBoolean() { diff --git a/ebean-querybean/src/test/resources/application-test.properties b/ebean-querybean/src/test/resources/application-test.properties index d477b74c8..9c1b1a919 100644 --- a/ebean-querybean/src/test/resources/application-test.properties +++ b/ebean-querybean/src/test/resources/application-test.properties @@ -1,6 +1,8 @@ ebean.ddl.generate=true ebean.ddl.run=true ebean.ddl.initSql=init-db.sql +ebean.dumpMetricsOnShutdown=true +ebean.dumpMetricsOptions=sql,hash,loc datasource.default=h2 diff --git a/ebean-querybean/src/test/resources/ebean.mf b/ebean-querybean/src/test/resources/ebean.mf index 9f7856f61..6c540c433 100644 --- a/ebean-querybean/src/test/resources/ebean.mf +++ b/ebean-querybean/src/test/resources/ebean.mf @@ -1,5 +1,7 @@ entity-packages: org.example.domain querybean-packages: org.example.domain,org.querytest debug: 0 -synthetic: false +synthetic: true entity-field-access: true +profile-line-number-mode: auto + diff --git a/ebean-test/src/test/java/org/tests/profile/ProfileLocationTest.java b/ebean-test/src/test/java/org/tests/profile/ProfileLocationTest.java index b14b35963..38597e004 100644 --- a/ebean-test/src/test/java/org/tests/profile/ProfileLocationTest.java +++ b/ebean-test/src/test/java/org/tests/profile/ProfileLocationTest.java @@ -10,10 +10,12 @@ class ProfileLocationTest { private static final ProfileLocation loc = ProfileLocation.create(12, "foo"); private static final ProfileLocation locB = ProfileLocation.create(); private static final ProfileLocation loc2 = ProfileLocation.create(); - + private static final ProfileLocation locWithLine = ProfileLocation.createWithLine(); private boolean doIt() { locB.obtain(); // simulate a location moving by line number only - return loc.obtain(); + boolean result = loc.obtain(); + locWithLine.obtain(); + return result; } @Test @@ -29,6 +31,16 @@ class ProfileLocationTest { assertThat(locB.label()).isEqualTo("ProfileLocationTest.doIt"); } + @Test + void test_obtainWithLine() { + doIt(); + + // same hash even when the line number has changed + assertThat(locWithLine.fullLocation()).isEqualTo("org.tests.profile.ProfileLocationTest.doIt(ProfileLocationTest.java:17)"); + assertThat(locWithLine.location()).isEqualTo("org.tests.profile.ProfileLocationTest.doIt:17"); + assertThat(locWithLine.label()).isEqualTo("ProfileLocationTest.doIt:17"); + } + @Test void test_add() { loc.add(100); diff --git a/kotlin-querybean-generator/src/test/resources/META-INF/ebean-version.mf b/kotlin-querybean-generator/src/test/resources/META-INF/ebean-version.mf index 45259b053..5b6b424fe 100644 --- a/kotlin-querybean-generator/src/test/resources/META-INF/ebean-version.mf +++ b/kotlin-querybean-generator/src/test/resources/META-INF/ebean-version.mf @@ -1 +1 @@ -ebean-version: 142 +ebean-version: 143 diff --git a/pom.xml b/pom.xml index 4aa62b5c8..750cc81ae 100644 --- a/pom.xml +++ b/pom.xml @@ -50,8 +50,8 @@ 13.7.0 6.3 8.5 - 13.13.0 - 13.13.0 + 13.13.1 + 13.13.1 false diff --git a/tests/test-kotlin/src/test/resources/META-INF/ebean-version.mf b/tests/test-kotlin/src/test/resources/META-INF/ebean-version.mf index 45259b053..5b6b424fe 100644 --- a/tests/test-kotlin/src/test/resources/META-INF/ebean-version.mf +++ b/tests/test-kotlin/src/test/resources/META-INF/ebean-version.mf @@ -1 +1 @@ -ebean-version: 142 +ebean-version: 143