blob: 7030e191e396389c2209fdc93b8d07c6ce526e6d [file] [log] [blame]
ruperts8c9f87a2017-12-20 12:02:09 -08001// Copyright 2017 The Bazel Authors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package com.google.devtools.build.lib.shell;
16
17import static com.google.common.truth.Truth.assertThat;
18import static com.google.common.truth.Truth8.assertThat;
19
Philipp Wollermann23e1c5d2018-03-23 07:39:27 -070020import com.google.devtools.build.lib.vfs.Path;
ruperts8c9f87a2017-12-20 12:02:09 -080021import java.io.IOException;
22import java.time.Duration;
23import java.util.List;
24import java.util.Optional;
25
26/**
27 * Utilities to assist with testing execution statistics generated via the {@code process-wrapper}
28 * and {@code linux-sandbox} tools.
29 */
30public class ExecutionStatisticsTestUtil {
31 /**
32 * Executes a command and checks that the execution statistics timing info for that command
33 * satisfy certain constraints.
34 *
35 * @param userTimeToSpend a lower bound for how much CPU user execution time was expected
36 * @param systemTimeToSpend a lower bound for how much CPU system execution time was expected
37 * @param fullCommandLine the command to execute, including any wrappers used (like linux-sandbox)
38 * @param statisticsFilePath where the execution statistics file will be generated (to be read)
39 */
40 public static void executeCommandAndCheckStatisticsAboutCpuTimeSpent(
41 Duration userTimeToSpend,
42 Duration systemTimeToSpend,
43 List<String> fullCommandLine,
Philipp Wollermann23e1c5d2018-03-23 07:39:27 -070044 Path statisticsFilePath)
ruperts8c9f87a2017-12-20 12:02:09 -080045 throws CommandException, IOException {
46 Duration userTimeLowerBound = userTimeToSpend;
ruperts97293aa2018-06-20 15:58:39 -070047 Duration userTimeUpperBound = userTimeToSpend.plusSeconds(9);
ruperts8c9f87a2017-12-20 12:02:09 -080048 Duration systemTimeLowerBound = systemTimeToSpend;
ruperts02a6e0f2018-08-07 08:08:09 -070049
50 // TODO(b/110456205) This check fails under very heavy load, investigate why and re-enable it
51 // Duration systemTimeUpperBound = systemTimeToSpend.plusSeconds(9);
ruperts8c9f87a2017-12-20 12:02:09 -080052
53 Command command = new Command(fullCommandLine.toArray(new String[0]));
54 CommandResult commandResult = command.execute();
55 assertThat(commandResult.getTerminationStatus().success()).isTrue();
56
57 Optional<ExecutionStatistics.ResourceUsage> resourceUsage =
58 ExecutionStatistics.getResourceUsage(statisticsFilePath);
59 assertThat(resourceUsage).isPresent();
60
61 Duration userTime = resourceUsage.get().getUserExecutionTime();
62 assertThat(userTime).isAtLeast(userTimeLowerBound);
63 assertThat(userTime).isAtMost(userTimeUpperBound);
64
65 Duration systemTime = resourceUsage.get().getSystemExecutionTime();
66 assertThat(systemTime).isAtLeast(systemTimeLowerBound);
ruperts02a6e0f2018-08-07 08:08:09 -070067
68 // TODO(b/110456205) This check fails under very heavy load, investigate why and re-enable it
69 // assertThat(systemTime).isAtMost(systemTimeUpperBound);
ruperts8c9f87a2017-12-20 12:02:09 -080070 }
71}