Skip to content

Commit

Permalink
Avoid unnecessary worker metrics work if possible
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 479154020
Change-Id: Id34252a066d86354c6122d4914040f890e3d0690
  • Loading branch information
anakanemison authored and aiuto committed Oct 12, 2022
1 parent 991f065 commit 2e38b6c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void setClock(Clock clock) {
*/
MemoryCollectionResult collectMemoryUsageByPid(OS os, ImmutableSet<Long> processIds) {
// TODO(b/181317827): Support Windows.
if (os != OS.LINUX && os != OS.DARWIN) {
if (processIds.isEmpty() || (os != OS.LINUX && os != OS.DARWIN)) {
return new MemoryCollectionResult(
ImmutableMap.of(), Instant.ofEpochMilli(clock.currentTimeMillis()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import static com.google.common.truth.Truth.assertThat;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
Expand All @@ -30,22 +32,15 @@
import java.io.InputStream;
import java.time.Instant;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;

/** Unit tests for the WorkerSpawnRunner. */
@RunWith(JUnit4.class)
public class WorkerMetricsCollectorTest {

@Rule public final MockitoRule mockito = MockitoJUnit.rule();
private final WorkerMetricsCollector spyCollector = spy(WorkerMetricsCollector.instance());
@Captor ArgumentCaptor<ImmutableSet<Long>> pidsCaptor;
ManualClock clock = new ManualClock();

@Before
Expand Down Expand Up @@ -212,8 +207,10 @@ public void testcollectMetrics() throws Exception {
ImmutableList<WorkerMetric> expectedMetrics =
ImmutableList.of(workerMetric1, workerMetric2, workerMetric3);

when(spyCollector.collectMemoryUsageByPid(any(), pidsCaptor.capture()))
.thenReturn(memoryCollectionResult);
doReturn(memoryCollectionResult)
.when(spyCollector)
.collectMemoryUsageByPid(any(), eq(expectedPids));

clock.setTime(registrationTime.toEpochMilli());

spyCollector.registerWorker(props1);
Expand All @@ -222,7 +219,6 @@ public void testcollectMetrics() throws Exception {

ImmutableList<WorkerMetric> metrics = spyCollector.collectMetrics();

assertThat(pidsCaptor.getValue()).containsExactlyElementsIn(expectedPids);
assertThat(metrics).containsExactlyElementsIn(expectedMetrics);
assertThat(spyCollector.getWorkerIdToWorkerProperties()).isEqualTo(propsMap);
}
Expand Down

0 comments on commit 2e38b6c

Please sign in to comment.