-
Notifications
You must be signed in to change notification settings - Fork 468
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6489162
commit 8f7f778
Showing
8 changed files
with
263 additions
and
19 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
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
110 changes: 110 additions & 0 deletions
110
dotCMS/src/main/java/com/dotcms/metrics/timing/TimeMetric.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,110 @@ | ||
package com.dotcms.metrics.timing; | ||
|
||
import com.dotmarketing.util.Logger; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import java.util.UUID; | ||
|
||
/** | ||
* A utility class for measuring and formatting time metrics. | ||
* | ||
* <p>This class allows you to create and track time metrics, measure the duration, | ||
* and format the duration in seconds using the default format. It also provides | ||
* methods for equality comparison based on the metric's name.</p> | ||
* | ||
* @author vico | ||
*/ | ||
public class TimeMetric { | ||
|
||
private final String name; | ||
private long start = Long.MIN_VALUE; | ||
private long stop = Long.MIN_VALUE; | ||
|
||
private TimeMetric(final String name) { | ||
final String metricId = UUID.randomUUID().toString(); | ||
this.name = (StringUtils.isNotBlank(name) ? name + "_" : "") + metricId; | ||
} | ||
|
||
public long getStart() { | ||
return start; | ||
} | ||
|
||
public long getStop() { | ||
return stop; | ||
} | ||
|
||
/** | ||
* Factory method to create a new TimeMetric instance with a given name and start timing. | ||
* | ||
* @param name The name of the time metric. | ||
* @return A new TimeMetric instance with the specified name and started timer. | ||
*/ | ||
public static TimeMetric mark(final String name) { | ||
return new TimeMetric(name).start(); | ||
} | ||
|
||
/** | ||
* Start the timer for the TimeMetric instance. | ||
* | ||
* @return The TimeMetric instance with the timer started. | ||
*/ | ||
public TimeMetric start() { | ||
start = System.currentTimeMillis(); | ||
reportStart(); | ||
return this; | ||
} | ||
|
||
/** | ||
* Stop the timer for the TimeMetric instance. | ||
* | ||
* @return The TimeMetric instance with the timer stopped. | ||
*/ | ||
public TimeMetric stop() { | ||
stop = System.currentTimeMillis(); | ||
reportStop(); | ||
return this; | ||
} | ||
|
||
/** | ||
* Get the name of the TimeMetric. | ||
* | ||
* @return The name of the TimeMetric. | ||
*/ | ||
public String getName() { | ||
return name; | ||
} | ||
|
||
/** | ||
* Get the duration of the TimeMetric in milliseconds. | ||
* | ||
* @return The duration of the TimeMetric in milliseconds. | ||
* @throws IllegalStateException if the TimeMetric is not started or stopped. | ||
*/ | ||
public long getDuration() { | ||
if (start == Long.MIN_VALUE || stop == Long.MIN_VALUE) { | ||
throw new IllegalStateException("TimeMetric not started or stopped"); | ||
} | ||
return stop - start; | ||
} | ||
|
||
/** | ||
* Report the start of a time metric. | ||
*/ | ||
private void reportStart() { | ||
Logger.debug(this, String.format(">>>>> START [%s] at [%d]", getName(), getStart())); | ||
} | ||
|
||
/** | ||
* Report the stop of a time metric. | ||
*/ | ||
private void reportStop() { | ||
Logger.debug( | ||
this, | ||
String.format( | ||
"<<<<< STOP [%s] at [%d] / duration [%s]", | ||
getName(), | ||
getStop(), | ||
TimeMetricHelper.get().formatDuration(this))); | ||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
dotCMS/src/main/java/com/dotcms/metrics/timing/TimeMetricHelper.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,47 @@ | ||
package com.dotcms.metrics.timing; | ||
|
||
import io.vavr.Lazy; | ||
|
||
|
||
/** | ||
* Utility class for formatting time metrics. | ||
* | ||
* @author vico | ||
*/ | ||
public class TimeMetricHelper { | ||
|
||
private static final Lazy<TimeMetricHelper> INSTANCE = Lazy.of(TimeMetricHelper::new); | ||
|
||
private TimeMetricHelper() {} | ||
|
||
/** | ||
* Get an instance of the {@link TimeMetricHelper} class. | ||
* | ||
* @return The singleton instance of {@link TimeMetricHelper}. | ||
*/ | ||
public static TimeMetricHelper get() { | ||
return INSTANCE.get(); | ||
} | ||
|
||
/** | ||
* Format a time metric in seconds using a custom mask. | ||
* | ||
* @param timeMetric The time metric to format. | ||
* @param mask The custom format mask (e.g., "%.2f"). | ||
* @return The formatted string representing the time metric. | ||
*/ | ||
public String formatDuration(final TimeMetric timeMetric, final String mask) { | ||
return String.format(mask, (float) timeMetric.getDuration() / 1000); | ||
} | ||
|
||
/** | ||
* Format a time metric in seconds using the default mask "%.4f". | ||
* | ||
* @param timeMetric The time metric to format. | ||
* @return The formatted string representing the time metric. | ||
*/ | ||
public String formatDuration(final TimeMetric timeMetric) { | ||
return formatDuration(timeMetric, "%.4f"); | ||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
dotCMS/src/test/java/com/dotcms/metrics/timing/TimeMetricHelperTest.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,47 @@ | ||
package com.dotcms.metrics.timing; | ||
|
||
import com.dotcms.UnitTestBase; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import static graphql.Assert.assertTrue; | ||
|
||
/** | ||
* Unit tests for the {@link TimeMetricHelper} class. | ||
* | ||
* @author vico | ||
*/ | ||
public class TimeMetricHelperTest extends UnitTestBase { | ||
|
||
private TimeMetricHelper timeMetricHelper; | ||
|
||
@Before | ||
public void setUp() { | ||
timeMetricHelper = TimeMetricHelper.get(); | ||
} | ||
|
||
/** | ||
* Test the {@link TimeMetricHelper#formatDuration(TimeMetric, String)} method with a custom mask. | ||
*/ | ||
@Test | ||
public void testFormatSecondsWithCustomMask() throws InterruptedException { | ||
final TimeMetric timeMetric = TimeMetric.mark("some-time-metric"); | ||
Thread.sleep(100); | ||
timeMetric.stop(); | ||
final String formatted = timeMetricHelper.formatDuration(timeMetric, "%.2f"); | ||
assertTrue(formatted.startsWith("0.1")); | ||
} | ||
|
||
/** | ||
* Test the {@link TimeMetricHelper#formatDuration(TimeMetric)} method with the default mask. | ||
*/ | ||
@Test | ||
public void testFormatSecondsWithDefaultMask() throws InterruptedException { | ||
final TimeMetric timeMetric = TimeMetric.mark("some-time-metric"); | ||
Thread.sleep(100); | ||
timeMetric.stop(); | ||
final String formatted = timeMetricHelper.formatDuration(timeMetric); | ||
assertTrue(formatted.startsWith("0.10")); | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
dotCMS/src/test/java/com/dotcms/metrics/timing/TimeMetricTest.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,36 @@ | ||
package com.dotcms.metrics.timing; | ||
|
||
import com.dotcms.UnitTestBase; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
/** | ||
* Unit tests for the {@link TimeMetric} class. | ||
*/ | ||
public class TimeMetricTest extends UnitTestBase { | ||
|
||
private TimeMetric timeMetric; | ||
|
||
/** | ||
* Set up the test environment by initializing the {@link TimeMetric} instance. | ||
*/ | ||
@Before | ||
public void setUp() { | ||
timeMetric = TimeMetric.mark("TestMetric"); | ||
} | ||
|
||
/** | ||
* Test the stop() method of the {@link TimeMetric} class. | ||
*/ | ||
@Test | ||
public void testStartAndStop() { | ||
assertNotNull("TimeMetric instance should not be null", timeMetric); | ||
timeMetric.start(); | ||
assertTrue("Timer should be stopped after calling stop()", timeMetric.stop().getDuration() >= 0); | ||
} | ||
|
||
} | ||
|