From 5dc9a8d165062784b834578d5c02e2f95b8fbf15 Mon Sep 17 00:00:00 2001 From: bryanhuhta Date: Mon, 16 Dec 2024 14:38:48 -0700 Subject: [PATCH 1/4] Add .vscode to .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 40a2003..696f13e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /.idea/ +/.vscode/ # Ignore Gradle project-specific cache directory .gradle From 58da7a026dbb0850e5be06b956e839348627a81d Mon Sep 17 00:00:00 2001 From: bryanhuhta Date: Mon, 16 Dec 2024 14:40:26 -0700 Subject: [PATCH 2/4] Remove unused imports and retvals --- .../java/io/pyroscope/javaagent/impl/PyroscopeExporter.java | 1 - .../labels/io/pyroscope/PyroscopeAsyncProfiler.java | 6 ------ .../src/test/java/io/pyroscope/labels/LabelsTest.java | 2 +- demo/src/main/java/Fib.java | 2 -- 4 files changed, 1 insertion(+), 10 deletions(-) diff --git a/agent/src/main/java/io/pyroscope/javaagent/impl/PyroscopeExporter.java b/agent/src/main/java/io/pyroscope/javaagent/impl/PyroscopeExporter.java index 6c00843..64fc554 100644 --- a/agent/src/main/java/io/pyroscope/javaagent/impl/PyroscopeExporter.java +++ b/agent/src/main/java/io/pyroscope/javaagent/impl/PyroscopeExporter.java @@ -1,6 +1,5 @@ package io.pyroscope.javaagent.impl; -import io.pyroscope.http.Format; import io.pyroscope.javaagent.EventType; import io.pyroscope.javaagent.Snapshot; import io.pyroscope.javaagent.api.Exporter; diff --git a/async-profiler-context/src/main/java/io/pyroscope/labels/io/pyroscope/PyroscopeAsyncProfiler.java b/async-profiler-context/src/main/java/io/pyroscope/labels/io/pyroscope/PyroscopeAsyncProfiler.java index 053a3c1..b05e93c 100644 --- a/async-profiler-context/src/main/java/io/pyroscope/labels/io/pyroscope/PyroscopeAsyncProfiler.java +++ b/async-profiler-context/src/main/java/io/pyroscope/labels/io/pyroscope/PyroscopeAsyncProfiler.java @@ -2,20 +2,14 @@ import one.profiler.AsyncProfiler; -import java.io.BufferedReader; -import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; public class PyroscopeAsyncProfiler { static final String libraryPath; diff --git a/async-profiler-context/src/test/java/io/pyroscope/labels/LabelsTest.java b/async-profiler-context/src/test/java/io/pyroscope/labels/LabelsTest.java index 46516e5..48abe67 100644 --- a/async-profiler-context/src/test/java/io/pyroscope/labels/LabelsTest.java +++ b/async-profiler-context/src/test/java/io/pyroscope/labels/LabelsTest.java @@ -225,7 +225,7 @@ void stressTest() throws InterruptedException { } e.shutdown(); e.awaitTermination(100, TimeUnit.SECONDS); - Snapshot res = Pyroscope.LabelsWrapper.dump(); + Pyroscope.LabelsWrapper.dump(); assertEquals(0, ScopedContext.context.get().id); assertEquals(0, RefCounted.strings.valueToRef.size()); assertEquals(0, RefCounted.contexts.valueToRef.size()); diff --git a/demo/src/main/java/Fib.java b/demo/src/main/java/Fib.java index 7686294..729ef61 100644 --- a/demo/src/main/java/Fib.java +++ b/demo/src/main/java/Fib.java @@ -1,6 +1,4 @@ -import java.util.HashMap; -import java.util.Map; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; From f477f44a686d64fd93445025925f9e6be0df5259 Mon Sep 17 00:00:00 2001 From: bryanhuhta Date: Mon, 16 Dec 2024 14:46:53 -0700 Subject: [PATCH 3/4] Update equals() method to account for the type of T Also reflects the equals() change to hashCode() and adds tests for both implementations. --- .../main/java/io/pyroscope/labels/Ref.java | 10 ++++-- .../java/io/pyroscope/labels/RefTest.java | 33 +++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 async-profiler-context/src/test/java/io/pyroscope/labels/RefTest.java diff --git a/async-profiler-context/src/main/java/io/pyroscope/labels/Ref.java b/async-profiler-context/src/main/java/io/pyroscope/labels/Ref.java index 4dc0777..0ff901f 100644 --- a/async-profiler-context/src/main/java/io/pyroscope/labels/Ref.java +++ b/async-profiler-context/src/main/java/io/pyroscope/labels/Ref.java @@ -1,5 +1,6 @@ package io.pyroscope.labels; +import java.util.Objects; import java.util.concurrent.atomic.AtomicLong; class Ref { @@ -16,13 +17,16 @@ public Ref(T val, Long id) { public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - Ref valueRef = (Ref) o; - return id.equals(valueRef.id); + + Ref valueRef = (Ref) o; + if (val.getClass() != o.getClass()) return false; + + return id.equals(valueRef.id) && val.equals(valueRef.val); } @Override public int hashCode() { - return id.hashCode(); + return Objects.hash(id, val); } @Override diff --git a/async-profiler-context/src/test/java/io/pyroscope/labels/RefTest.java b/async-profiler-context/src/test/java/io/pyroscope/labels/RefTest.java new file mode 100644 index 0000000..bde8747 --- /dev/null +++ b/async-profiler-context/src/test/java/io/pyroscope/labels/RefTest.java @@ -0,0 +1,33 @@ +package io.pyroscope.labels; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class RefTest { + @Test + void testCreateRef() { + Ref ref = new Ref<>("test", 1L); + assertEquals("test", ref.val); + assertEquals(1L, ref.id); + assertEquals(1L, ref.refCount.get()); + } + + @Test + @SuppressWarnings("unlikely-arg-type") + void testEquals() { + Ref ref1 = new Ref<>("test", 1L); + assertTrue(ref1.equals(ref1)); + assertFalse(ref1.equals(null)); + assertFalse(ref1.equals(new Integer(3))); + assertFalse(ref1.equals(new Ref(3, 1L))); + } + + @Test + void testHashCode() { + Ref ref1 = new Ref<>("test", 1L); + assertEquals(3557490, ref1.hashCode()); + } +} From 32f2f7771b61edd3e06f92f9b7fff672b1d09dac Mon Sep 17 00:00:00 2001 From: bryanhuhta Date: Tue, 17 Dec 2024 16:37:30 -0700 Subject: [PATCH 4/4] Revert the equals() and hashCode() changes --- .../src/main/java/io/pyroscope/labels/Ref.java | 7 ++----- .../src/test/java/io/pyroscope/labels/RefTest.java | 4 ++-- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/async-profiler-context/src/main/java/io/pyroscope/labels/Ref.java b/async-profiler-context/src/main/java/io/pyroscope/labels/Ref.java index 0ff901f..8fe85b1 100644 --- a/async-profiler-context/src/main/java/io/pyroscope/labels/Ref.java +++ b/async-profiler-context/src/main/java/io/pyroscope/labels/Ref.java @@ -1,6 +1,5 @@ package io.pyroscope.labels; -import java.util.Objects; import java.util.concurrent.atomic.AtomicLong; class Ref { @@ -19,14 +18,12 @@ public boolean equals(Object o) { if (o == null || getClass() != o.getClass()) return false; Ref valueRef = (Ref) o; - if (val.getClass() != o.getClass()) return false; - - return id.equals(valueRef.id) && val.equals(valueRef.val); + return id.equals(valueRef.id); } @Override public int hashCode() { - return Objects.hash(id, val); + return id.hashCode(); } @Override diff --git a/async-profiler-context/src/test/java/io/pyroscope/labels/RefTest.java b/async-profiler-context/src/test/java/io/pyroscope/labels/RefTest.java index bde8747..8fe7d65 100644 --- a/async-profiler-context/src/test/java/io/pyroscope/labels/RefTest.java +++ b/async-profiler-context/src/test/java/io/pyroscope/labels/RefTest.java @@ -22,12 +22,12 @@ void testEquals() { assertTrue(ref1.equals(ref1)); assertFalse(ref1.equals(null)); assertFalse(ref1.equals(new Integer(3))); - assertFalse(ref1.equals(new Ref(3, 1L))); + assertTrue(ref1.equals(new Ref(3, 1L))); // We don't compare the value types. } @Test void testHashCode() { Ref ref1 = new Ref<>("test", 1L); - assertEquals(3557490, ref1.hashCode()); + assertEquals(1, ref1.hashCode()); } }