diff options
author | janakr <janakr@google.com> | 2018-02-28 09:46:06 -0800 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-02-28 09:48:17 -0800 |
commit | dfa0b12a44c6cd434de612db2c6b5573ef4e64bb (patch) | |
tree | f601c579874ebcbeee6881c1e274b194f6ac9550 /src/test/java/com/google | |
parent | 1fe23126d4a30d49b7668b235ea1bfb2e2c8a39e (diff) |
Add functionality to MemoryProfiler to do multiple garbage collections at the end of the build in an effort to get an accurate measurement of used memory.
PiperOrigin-RevId: 187337487
Diffstat (limited to 'src/test/java/com/google')
3 files changed, 99 insertions, 13 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/BUILD b/src/test/java/com/google/devtools/build/lib/BUILD index 9f741f6503..72f1f9fd6c 100644 --- a/src/test/java/com/google/devtools/build/lib/BUILD +++ b/src/test/java/com/google/devtools/build/lib/BUILD @@ -977,7 +977,9 @@ java_test( "//src/main/java/com/google/devtools/build/lib/profiler", "//src/main/java/com/google/devtools/build/lib/profiler:profiler-output", "//src/main/java/com/google/devtools/build/lib/vfs", + "//third_party:guava", "//third_party:jsr305", + "//third_party:mockito", ], ) diff --git a/src/test/java/com/google/devtools/build/lib/profiler/MemoryProfilerTest.java b/src/test/java/com/google/devtools/build/lib/profiler/MemoryProfilerTest.java new file mode 100644 index 0000000000..c00a1336ca --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/profiler/MemoryProfilerTest.java @@ -0,0 +1,78 @@ +// Copyright 2018 The Bazel Authors. All rights reserved. +// +// 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 com.google.devtools.build.lib.profiler; + +import static com.google.common.truth.Truth.assertThat; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import com.google.common.io.ByteStreams; +import com.google.devtools.build.lib.profiler.MemoryProfiler.MemoryProfileStableHeapParameters; +import com.google.devtools.build.lib.profiler.MemoryProfiler.Sleeper; +import java.lang.management.MemoryMXBean; +import java.time.Duration; +import java.util.ArrayList; +import java.util.List; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; +import org.mockito.Mockito; + +/** Tests for {@link MemoryProfiler}. */ +@RunWith(JUnit4.class) +public class MemoryProfilerTest { + @Test + public void profilerDoesOneGcAndNoSleepNormally() throws Exception { + MemoryProfiler profiler = MemoryProfiler.instance(); + profiler.setStableMemoryParameters( + new MemoryProfileStableHeapParameters.Converter().convert("1,10")); + profiler.start(ByteStreams.nullOutputStream()); + MemoryMXBean bean = Mockito.mock(MemoryMXBean.class); + RecordingSleeper sleeper = new RecordingSleeper(); + profiler.prepareBean(ProfilePhase.ANALYZE, bean, sleeper); + assertThat(sleeper.sleeps).isEmpty(); + verify(bean, times(1)).gc(); + profiler.prepareBean(ProfilePhase.FINISH, bean, sleeper); + verify(bean, times(2)).gc(); + assertThat(sleeper.sleeps).isEmpty(); + } + + @Test + public void profilerDoesOneGcAndNoSleepExceptInFinish() throws Exception { + MemoryProfiler profiler = MemoryProfiler.instance(); + profiler.setStableMemoryParameters( + new MemoryProfileStableHeapParameters.Converter().convert("3,10")); + profiler.start(ByteStreams.nullOutputStream()); + MemoryMXBean bean = Mockito.mock(MemoryMXBean.class); + RecordingSleeper sleeper = new RecordingSleeper(); + profiler.prepareBean(ProfilePhase.ANALYZE, bean, sleeper); + assertThat(sleeper.sleeps).isEmpty(); + verify(bean, times(1)).gc(); + profiler.prepareBean(ProfilePhase.FINISH, bean, sleeper); + assertThat(sleeper.sleeps) + .containsExactly(Duration.ofSeconds(10), Duration.ofSeconds(10)) + .inOrder(); + verify(bean, times(4)).gc(); + } + + private static class RecordingSleeper implements Sleeper { + private final List<Duration> sleeps = new ArrayList<>(); + + @Override + public void sleep(Duration duration) { + sleeps.add(duration); + } + } +} diff --git a/src/test/java/com/google/devtools/build/lib/profiler/ProfilerTest.java b/src/test/java/com/google/devtools/build/lib/profiler/ProfilerTest.java index 2f988dd96a..ffc7e7f31a 100644 --- a/src/test/java/com/google/devtools/build/lib/profiler/ProfilerTest.java +++ b/src/test/java/com/google/devtools/build/lib/profiler/ProfilerTest.java @@ -308,19 +308,25 @@ public class ProfilerTest extends FoundationTestCase { thread1.join(); clock.advanceMillis(1); profiler.markPhase(ProfilePhase.ANALYZE); - Thread thread2 = new Thread() { - @Override public void run() { - profiler.startTask(ProfilerTask.TEST, "complex task"); - for (int i = 0; i < 100; i++) { - Profiler.instance().logEvent(ProfilerTask.TEST, "thread2a"); - } - profiler.completeTask(ProfilerTask.TEST); - profiler.markPhase(ProfilePhase.EXECUTE); - for (int i = 0; i < 100; i++) { - Profiler.instance().logEvent(ProfilerTask.TEST, "thread2b"); - } - } - }; + Thread thread2 = + new Thread() { + @Override + public void run() { + profiler.startTask(ProfilerTask.TEST, "complex task"); + for (int i = 0; i < 100; i++) { + Profiler.instance().logEvent(ProfilerTask.TEST, "thread2a"); + } + profiler.completeTask(ProfilerTask.TEST); + try { + profiler.markPhase(ProfilePhase.EXECUTE); + } catch (InterruptedException e) { + throw new IllegalStateException(e); + } + for (int i = 0; i < 100; i++) { + Profiler.instance().logEvent(ProfilerTask.TEST, "thread2b"); + } + } + }; thread2.start(); thread2.join(); profiler.logEvent(ProfilerTask.TEST, "last task"); |