-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from xdev-software/develop
Release
- Loading branch information
Showing
12 changed files
with
619 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# 1.0.0 | ||
<i>Initial release</i> |
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
70 changes: 70 additions & 0 deletions
70
hierarchical-stopwatch-demo/src/main/java/software/xdev/Application.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,70 @@ | ||
package software.xdev; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.stream.IntStream; | ||
|
||
import software.xdev.time.HierarchicalLoggingStopWatch; | ||
|
||
|
||
public final class Application | ||
{ | ||
public static void main(final String[] args) | ||
{ | ||
try(final var profiler = HierarchicalLoggingStopWatch.createStarted( | ||
"Run dummy", | ||
System.out::println, // Could also be LOGGER::debug | ||
true)) // Could also be LOGGER.isDebugEnabled() | ||
{ | ||
final List<CompletableFuture<Void>> completableFutures; | ||
try(final var ignored = profiler.nestedAC("Launch tasks")) | ||
{ | ||
completableFutures = IntStream.of(1, 2, 3) | ||
.mapToObj(i -> CompletableFuture.runAsync(() -> { | ||
try(final var process = profiler.nestedAC("Process " + i, true)) | ||
{ | ||
try(final var ignore = process.nestedAC("Fetch")) | ||
{ | ||
sleep(5); | ||
} | ||
|
||
try(final var ignore = process.nestedAC("Process")) | ||
{ | ||
sleep(i * 5); | ||
} | ||
|
||
if(i % 2 == 0) | ||
{ | ||
try(final var ignore = process.nestedAC("Finalize")) | ||
{ | ||
sleep(5); | ||
} | ||
} | ||
} | ||
})) | ||
.toList(); | ||
} | ||
|
||
try(final var ignored = profiler.nestedAC("Wait for tasks")) | ||
{ | ||
completableFutures.forEach(CompletableFuture::join); | ||
} | ||
} | ||
} | ||
|
||
private static void sleep(final int ms) | ||
{ | ||
try | ||
{ | ||
Thread.sleep(ms); | ||
} | ||
catch(final InterruptedException iex) | ||
{ | ||
Thread.currentThread().interrupt(); | ||
} | ||
} | ||
|
||
private Application() | ||
{ | ||
} | ||
} |
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
71 changes: 71 additions & 0 deletions
71
hierarchical-stopwatch/src/main/java/software/xdev/time/HierarchicalLoggingStopWatch.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,71 @@ | ||
/* | ||
* Copyright © 2024 XDEV Software (https://xdev.software) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package software.xdev.time; | ||
|
||
import java.util.function.Consumer; | ||
|
||
|
||
/** | ||
* Same as {@link HierarchicalStopWatchAutoClosable} but with a Consumer that handles | ||
* {@link HierarchicalStopWatch#getPrettyPrinted()} | ||
* | ||
* @see HierarchicalStopWatchAutoClosable | ||
*/ | ||
public class HierarchicalLoggingStopWatch extends HierarchicalStopWatchAutoClosable | ||
{ | ||
protected final Consumer<String> logConsumer; | ||
|
||
public HierarchicalLoggingStopWatch( | ||
final String taskName, | ||
final Consumer<String> logConsumer, | ||
final boolean async, | ||
final boolean enabled) | ||
{ | ||
super(taskName, async, enabled); | ||
this.logConsumer = logConsumer; | ||
} | ||
|
||
@Override | ||
public void close() | ||
{ | ||
super.close(); | ||
this.stopAll(); | ||
if(this.isEnabled()) | ||
{ | ||
this.logConsumer.accept(this.getPrettyPrinted()); | ||
} | ||
} | ||
|
||
public static HierarchicalLoggingStopWatch createStarted( | ||
final String taskName, | ||
final Consumer<String> logConsumer, | ||
final boolean async, | ||
final boolean enabled) | ||
{ | ||
final HierarchicalLoggingStopWatch sw = new HierarchicalLoggingStopWatch(taskName, logConsumer, async, | ||
enabled); | ||
sw.start(); | ||
return sw; | ||
} | ||
|
||
public static HierarchicalLoggingStopWatch createStarted( | ||
final String taskname, | ||
final Consumer<String> logConsumer, | ||
final boolean enabled) | ||
{ | ||
return createStarted(taskname, logConsumer, false, enabled); | ||
} | ||
} |
Oops, something went wrong.