Skip to content

Commit c95ba5a

Browse files
klueverGoogle Java Core Libraries
authored andcommitted
Add ServiceManager.startupDurations().
RELNOTES=Add `ServiceManager.startupDurations()`. PiperOrigin-RevId: 368421439
1 parent e61cf2e commit c95ba5a

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

android/guava/src/com/google/common/util/concurrent/ServiceManager.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -595,9 +595,9 @@ ImmutableMap<Service, Long> startupTimes() {
595595
// N.B. There will only be an entry in the map if the service has started
596596
for (Entry<Service, Stopwatch> entry : startupTimers.entrySet()) {
597597
Service service = entry.getKey();
598-
Stopwatch stopWatch = entry.getValue();
599-
if (!stopWatch.isRunning() && !(service instanceof NoOpService)) {
600-
loadTimes.add(Maps.immutableEntry(service, stopWatch.elapsed(MILLISECONDS)));
598+
Stopwatch stopwatch = entry.getValue();
599+
if (!stopwatch.isRunning() && !(service instanceof NoOpService)) {
600+
loadTimes.add(Maps.immutableEntry(service, stopwatch.elapsed(MILLISECONDS)));
601601
}
602602
}
603603
} finally {

guava-tests/test/com/google/common/util/concurrent/ServiceManagerTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.google.common.testing.TestLogHandler;
3030
import com.google.common.util.concurrent.Service.State;
3131
import com.google.common.util.concurrent.ServiceManager.Listener;
32+
import java.time.Duration;
3233
import java.util.Arrays;
3334
import java.util.Collection;
3435
import java.util.List;
@@ -132,6 +133,18 @@ public void testServiceStartupTimes() {
132133
}
133134

134135

136+
public void testServiceStartupDurations() {
137+
Service a = new NoOpDelayedService(150);
138+
Service b = new NoOpDelayedService(353);
139+
ServiceManager serviceManager = new ServiceManager(asList(a, b));
140+
serviceManager.startAsync().awaitHealthy();
141+
ImmutableMap<Service, Duration> startupTimes = serviceManager.startupDurations();
142+
assertThat(startupTimes).hasSize(2);
143+
assertThat(startupTimes.get(a)).isAtLeast(Duration.ofMillis(150));
144+
assertThat(startupTimes.get(b)).isAtLeast(Duration.ofMillis(353));
145+
}
146+
147+
135148
public void testServiceStartupTimes_selfStartingServices() {
136149
// This tests to ensure that:
137150
// 1. service times are accurate when the service is started by the manager

guava/src/com/google/common/util/concurrent/ServiceManager.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,18 @@ public ImmutableMap<Service, Long> startupTimes() {
413413
return state.startupTimes();
414414
}
415415

416+
/**
417+
* Returns the service load times. This value will only return startup times for services that
418+
* have finished starting.
419+
*
420+
* @return Map of services and their corresponding startup time, the map entries will be ordered
421+
* by startup time.
422+
* @since NEXT
423+
*/
424+
public ImmutableMap<Service, Duration> startupDurations() {
425+
return ImmutableMap.copyOf(Maps.transformValues(startupTimes(), Duration::ofMillis));
426+
}
427+
416428
@Override
417429
public String toString() {
418430
return MoreObjects.toStringHelper(ServiceManager.class)
@@ -625,9 +637,9 @@ ImmutableMap<Service, Long> startupTimes() {
625637
// N.B. There will only be an entry in the map if the service has started
626638
for (Entry<Service, Stopwatch> entry : startupTimers.entrySet()) {
627639
Service service = entry.getKey();
628-
Stopwatch stopWatch = entry.getValue();
629-
if (!stopWatch.isRunning() && !(service instanceof NoOpService)) {
630-
loadTimes.add(Maps.immutableEntry(service, stopWatch.elapsed(MILLISECONDS)));
640+
Stopwatch stopwatch = entry.getValue();
641+
if (!stopwatch.isRunning() && !(service instanceof NoOpService)) {
642+
loadTimes.add(Maps.immutableEntry(service, stopwatch.elapsed(MILLISECONDS)));
631643
}
632644
}
633645
} finally {

0 commit comments

Comments
 (0)