-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#9254] Add Snapshot manager & Testcase
- Loading branch information
Showing
7 changed files
with
180 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
profiler/src/main/java/com/navercorp/pinpoint/profiler/context/storage/Snapshot.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.navercorp.pinpoint.profiler.context.storage; | ||
|
||
import java.util.Objects; | ||
import java.util.function.LongFunction; | ||
import java.util.function.ToLongFunction; | ||
|
||
/** | ||
* @author Woonduk Kang(emeroad) | ||
*/ | ||
public class Snapshot<T> { | ||
|
||
private T currentImage; | ||
private final LongFunction<T> instanceFactory; | ||
|
||
public Snapshot(LongFunction<T> instanceFactory) { | ||
this.instanceFactory = Objects.requireNonNull(instanceFactory, "instanceFactory"); | ||
} | ||
|
||
public T takeSnapshot(long currentBaseTimestamp, ToLongFunction<T> currentImageTimestamp) { | ||
if (currentImage == null) { | ||
return null; | ||
} | ||
|
||
if (currentBaseTimestamp > currentImageTimestamp.applyAsLong(currentImage)) { | ||
T snapshot = this.currentImage; | ||
this.currentImage = null; | ||
return snapshot; | ||
} | ||
return null; | ||
} | ||
|
||
public T getCurrent(long currentBaseTimestamp) { | ||
if (currentImage == null) { | ||
currentImage = instanceFactory.apply(currentBaseTimestamp); | ||
} | ||
return currentImage; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
profiler/src/main/java/com/navercorp/pinpoint/profiler/monitor/metric/uri/URIKey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.navercorp.pinpoint.profiler.monitor.metric.uri; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* @author Woonduk Kang(emeroad) | ||
*/ | ||
public class URIKey { | ||
private final String uri; | ||
private final long timestamp; | ||
|
||
public URIKey(String uri, long timestamp) { | ||
this.uri = uri; | ||
this.timestamp = timestamp; | ||
} | ||
|
||
public String getUri() { | ||
return uri; | ||
} | ||
|
||
public long getTimestamp() { | ||
return timestamp; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
|
||
URIKey uriKey = (URIKey) o; | ||
|
||
if (timestamp != uriKey.timestamp) return false; | ||
return Objects.equals(uri, uriKey.uri); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int result = uri != null ? uri.hashCode() : 0; | ||
result = 31 * result + (int) (timestamp ^ (timestamp >>> 32)); | ||
return result; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "URIKey{" + | ||
"uri='" + uri + '\'' + | ||
", timestamp=" + timestamp + | ||
'}'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...rc/test/java/com/navercorp/pinpoint/profiler/monitor/metric/uri/AgentUriStatDataTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.navercorp.pinpoint.profiler.monitor.metric.uri; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
/** | ||
* @author Woonduk Kang(emeroad) | ||
*/ | ||
public class AgentUriStatDataTest { | ||
|
||
@Test | ||
public void add_capacity() { | ||
long baseTimestamp = System.currentTimeMillis(); | ||
AgentUriStatData metric = new AgentUriStatData(baseTimestamp, 1); | ||
|
||
UriStatInfo test = new UriStatInfo("test", true, 10, baseTimestamp); | ||
|
||
Assertions.assertTrue(metric.add(test)); | ||
Assertions.assertFalse(metric.add(test)); | ||
} | ||
|
||
@Test | ||
public void add_pattern() { | ||
long baseTimestamp = System.currentTimeMillis(); | ||
AgentUriStatData metric = new AgentUriStatData(baseTimestamp, 10); | ||
|
||
UriStatInfo pattern1 = new UriStatInfo("pattern1", true, 10, baseTimestamp); | ||
metric.add(pattern1); | ||
UriStatInfo pattern1_1 = new UriStatInfo("pattern1", true, 10, baseTimestamp); | ||
metric.add(pattern1_1); | ||
|
||
UriStatInfo pattern2 = new UriStatInfo("pattern2", true, 10, baseTimestamp); | ||
metric.add(pattern2); | ||
|
||
Assertions.assertEquals(2, metric.getAllUriStatData().size()); | ||
|
||
Optional<EachUriStatData> findPattern1 = metric.getAllUriStatData() | ||
.stream() | ||
.map(Map.Entry::getValue) | ||
.filter(eachUriStatData -> eachUriStatData.getUri().equals("pattern1")) | ||
.findFirst(); | ||
Assertions.assertEquals(2, findPattern1.get().getTotalHistogram().getCount()); | ||
|
||
} | ||
} |