aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test
diff options
context:
space:
mode:
authorGravatar Googler <noreply@google.com>2018-04-03 08:08:18 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-04-03 08:10:04 -0700
commita8023b796db7d05e329d0eb9a51cb4ce8222e4a0 (patch)
tree4c11435914e52a5f8b36c9796d169c41482b340c /src/test
parent81dbe79d09b4fa98d7803018b5d6d026e2e52a2e (diff)
Add stats about cache hits and execution strategies to Bazel's UI.
Fixes: 2846 RELNOTES: Bazel now displays information about remote cache hits and execution strategies used in its UI after every build and test, and adds a corresponding line "process stats" to BuildToolLogs in BEP. PiperOrigin-RevId: 191441770
Diffstat (limited to 'src/test')
-rw-r--r--src/test/java/com/google/devtools/build/lib/runtime/SpawnStatsTest.java160
-rw-r--r--src/test/shell/bazel/BUILD6
-rwxr-xr-xsrc/test/shell/bazel/bazel_spawnstats_test.sh85
3 files changed, 251 insertions, 0 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/runtime/SpawnStatsTest.java b/src/test/java/com/google/devtools/build/lib/runtime/SpawnStatsTest.java
new file mode 100644
index 0000000000..968108db64
--- /dev/null
+++ b/src/test/java/com/google/devtools/build/lib/runtime/SpawnStatsTest.java
@@ -0,0 +1,160 @@
+// 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.runtime;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import com.google.devtools.build.lib.actions.ActionResult;
+import com.google.devtools.build.lib.actions.SpawnResult;
+import java.util.ArrayList;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Testing SpawnStats */
+@RunWith(JUnit4.class)
+public final class SpawnStatsTest {
+
+ SpawnStats stats;
+
+ @Before
+ public void setUp() {
+ stats = new SpawnStats();
+ }
+
+ @Test
+ public void emptySet() {
+ assertThat(stats.getSummary()).isEqualTo("0 processes.");
+ }
+
+ @Test
+ public void one() {
+ stats.countRunnerName("foo");
+ assertThat(stats.getSummary()).isEqualTo("1 process, foo.");
+ }
+
+ @Test
+ public void oneRemote() {
+ stats.countRunnerName("remote cache hit");
+ assertThat(stats.getSummary()).isEqualTo("1 process, remote cache hit.");
+ }
+
+ @Test
+ public void two() {
+ stats.countRunnerName("foo");
+ stats.countRunnerName("foo");
+ assertThat(stats.getSummary()).isEqualTo("2 processes, foo.");
+ }
+
+ @Test
+ public void order() {
+ stats.countRunnerName("a");
+ stats.countRunnerName("b");
+ stats.countRunnerName("b");
+ stats.countRunnerName("c");
+ stats.countRunnerName("c");
+ stats.countRunnerName("c");
+ assertThat(stats.getSummary()).isEqualTo("6 processes: 1 a, 2 b, 3 c.");
+ }
+
+ @Test
+ public void reverseOrder() {
+ stats.countRunnerName("a");
+ stats.countRunnerName("a");
+ stats.countRunnerName("a");
+ stats.countRunnerName("b");
+ stats.countRunnerName("b");
+ stats.countRunnerName("c");
+ assertThat(stats.getSummary()).isEqualTo("6 processes: 3 a, 2 b, 1 c.");
+ }
+
+ @Test
+ public void cacheFirst() {
+ stats.countRunnerName("a");
+ stats.countRunnerName("a");
+ stats.countRunnerName("a");
+ stats.countRunnerName("b");
+ stats.countRunnerName("remote cache hit");
+ stats.countRunnerName("b");
+ stats.countRunnerName("c");
+ assertThat(stats.getSummary()).isEqualTo("7 processes: 1 remote cache hit, 3 a, 2 b, 1 c.");
+ }
+
+ private final SpawnResult rA =
+ new SpawnResult.Builder().setStatus(SpawnResult.Status.SUCCESS).setRunnerName("abc").build();
+ private final SpawnResult rB =
+ new SpawnResult.Builder().setStatus(SpawnResult.Status.SUCCESS).setRunnerName("cde").build();
+
+ @Test
+ public void actionOneSpawn() {
+
+ ArrayList<SpawnResult> spawns = new ArrayList<>();
+ spawns.add(rA);
+
+ stats.countActionResult(ActionResult.create(spawns));
+ assertThat(stats.getSummary()).isEqualTo("1 process, abc.");
+ }
+
+ @Test
+ public void actionManySpawn() {
+ // Different spawns with the same runner count as one action
+
+ ArrayList<SpawnResult> spawns = new ArrayList<>();
+ spawns.add(rA);
+ spawns.add(rA);
+ spawns.add(rA);
+
+ stats.countActionResult(ActionResult.create(spawns));
+ assertThat(stats.getSummary()).isEqualTo("3 processes, abc.");
+ }
+
+ @Test
+ public void actionManySpawnMixed() {
+ // Different spawns mixed runners
+
+ ArrayList<SpawnResult> spawns = new ArrayList<>();
+ spawns.add(rA);
+ spawns.add(rA);
+ spawns.add(rB);
+
+ stats.countActionResult(ActionResult.create(spawns));
+ assertThat(stats.getSummary()).isEqualTo("3 processes: 2 abc, 1 cde.");
+ }
+
+ @Test
+ public void actionManyActionsMixed() {
+ // Five actions:
+ // abc
+ // abc, abc
+ // abc, abc, cde
+ // abc, abc, cde
+ // abc, abc, cde
+
+ ArrayList<SpawnResult> spawns = new ArrayList<>();
+ spawns.add(rA);
+ stats.countActionResult(ActionResult.create(spawns));
+
+ spawns.add(rA);
+ stats.countActionResult(ActionResult.create(spawns));
+
+ spawns.add(rB);
+ stats.countActionResult(ActionResult.create(spawns));
+ stats.countActionResult(ActionResult.create(spawns));
+ stats.countActionResult(ActionResult.create(spawns));
+
+ assertThat(stats.getSummary()).isEqualTo("12 processes: 9 abc, 3 cde.");
+ }
+}
diff --git a/src/test/shell/bazel/BUILD b/src/test/shell/bazel/BUILD
index d2c79390ad..9aab738386 100644
--- a/src/test/shell/bazel/BUILD
+++ b/src/test/shell/bazel/BUILD
@@ -154,6 +154,12 @@ sh_test(
)
sh_test(
+ name = "bazel_spawnstats_test",
+ srcs = ["bazel_spawnstats_test.sh"],
+ data = [":test-deps"],
+)
+
+sh_test(
name = "bazel_coverage_test",
srcs = ["bazel_coverage_test.sh"],
data = [":test-deps"],
diff --git a/src/test/shell/bazel/bazel_spawnstats_test.sh b/src/test/shell/bazel/bazel_spawnstats_test.sh
new file mode 100755
index 0000000000..958296f47f
--- /dev/null
+++ b/src/test/shell/bazel/bazel_spawnstats_test.sh
@@ -0,0 +1,85 @@
+#!/bin/bash
+#
+# 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.
+#
+# Test lightweight spawn stats generation in Bazel
+#
+
+set -eu
+
+# Load the test setup defined in the parent directory
+CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
+source "${CURRENT_DIR}/../integration_test_setup.sh" \
+ || { echo "integration_test_setup.sh not found!" >&2; exit 1; }
+
+function set_up() {
+ cat > BUILD <<EOF
+genrule(
+ name = "foo",
+ cmd = "echo hello > \$@",
+ outs = ["foo.txt"],
+)
+EOF
+}
+
+function test_order() {
+ # Ensure the new stats are printed before Build completed
+ bazel build :foo 2>&1 | tee ${TEST_log} | sed -n '/process/,$p' | grep "Build complete" || fail "Expected \"process\" to be followed by \"Build completed\""
+}
+
+# Single execution of Bazel
+function statistics_single() {
+ flags=$1 # flags to pass to Bazel
+ expect=$2 # string to expect
+
+ echo "Starting single run for $flags $expect" &> $TEST_log
+ output=`bazel build :foo $flags 2>&1 | tee ${TEST_log} | grep " process" | tr -d '\r'`
+
+ if ! [[ $output =~ ${expect} ]]; then
+ fail "bazel ${flags}: Want |${expect}|, got |${output}| "
+ fi
+
+ echo "Done $flags $expect" &> $TEST_log
+}
+
+function test_local() {
+ statistics_single "--spawn_strategy=local" ", local"
+}
+
+function test_local_sandbox() {
+ if [[ "$PLATFORM" == "linux" ]]; then
+ statistics_single "--spawn_strategy=linux-sandbox" ", linux-sandbox"
+ fi
+}
+
+# We are correctly resetting the counts
+function test_repeat() {
+ flags="--spawn_strategy=local"
+ statistics_single $flags ", local"
+ bazel clean $flags
+ statistics_single $flags ", local"
+}
+
+# Locally cached results are not yet displayed
+function test_localcache() {
+ flags="--spawn_strategy=local"
+ # We are correctly resetting the counts
+ statistics_single $flags ", local"
+ statistics_single $flags "0 processes."
+}
+
+run_suite "bazel statistics tests"
+
+