ruperts | 8c9f87a | 2017-12-20 12:02:09 -0800 | [diff] [blame] | 1 | // 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 | |
| 15 | package com.google.devtools.build.lib.shell; |
| 16 | |
| 17 | import static com.google.common.truth.Truth.assertThat; |
| 18 | import static com.google.common.truth.Truth8.assertThat; |
| 19 | |
Philipp Wollermann | 23e1c5d | 2018-03-23 07:39:27 -0700 | [diff] [blame] | 20 | import com.google.devtools.build.lib.vfs.Path; |
ruperts | 8c9f87a | 2017-12-20 12:02:09 -0800 | [diff] [blame] | 21 | import java.io.IOException; |
| 22 | import java.time.Duration; |
| 23 | import java.util.List; |
| 24 | import 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 | */ |
| 30 | public 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 Wollermann | 23e1c5d | 2018-03-23 07:39:27 -0700 | [diff] [blame] | 44 | Path statisticsFilePath) |
ruperts | 8c9f87a | 2017-12-20 12:02:09 -0800 | [diff] [blame] | 45 | throws CommandException, IOException { |
| 46 | Duration userTimeLowerBound = userTimeToSpend; |
ruperts | 97293aa | 2018-06-20 15:58:39 -0700 | [diff] [blame] | 47 | Duration userTimeUpperBound = userTimeToSpend.plusSeconds(9); |
ruperts | 8c9f87a | 2017-12-20 12:02:09 -0800 | [diff] [blame] | 48 | Duration systemTimeLowerBound = systemTimeToSpend; |
ruperts | 02a6e0f | 2018-08-07 08:08:09 -0700 | [diff] [blame] | 49 | |
| 50 | // TODO(b/110456205) This check fails under very heavy load, investigate why and re-enable it |
| 51 | // Duration systemTimeUpperBound = systemTimeToSpend.plusSeconds(9); |
ruperts | 8c9f87a | 2017-12-20 12:02:09 -0800 | [diff] [blame] | 52 | |
| 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); |
ruperts | 02a6e0f | 2018-08-07 08:08:09 -0700 | [diff] [blame] | 67 | |
| 68 | // TODO(b/110456205) This check fails under very heavy load, investigate why and re-enable it |
| 69 | // assertThat(systemTime).isAtMost(systemTimeUpperBound); |
ruperts | 8c9f87a | 2017-12-20 12:02:09 -0800 | [diff] [blame] | 70 | } |
| 71 | } |