blob: 820261ab0f9df87bfff1025e37fc605ce94c1119 [file] [log] [blame]
ruperts89d13b82017-10-21 02:20:24 +02001// 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.
14package com.google.devtools.build.lib.actions;
15
ruperts52d77782017-11-09 16:12:45 +010016import static com.google.common.truth.Truth8.assertThat;
ruperts89d13b82017-10-21 02:20:24 +020017
ruperts7967f332017-11-21 16:37:13 -080018import com.google.common.collect.ImmutableList;
ruperts89d13b82017-10-21 02:20:24 +020019import java.time.Duration;
ruperts7967f332017-11-21 16:37:13 -080020import java.util.List;
ruperts89d13b82017-10-21 02:20:24 +020021import org.junit.Test;
22import org.junit.runner.RunWith;
23import org.junit.runners.JUnit4;
24
25/** Unit tests for {@link ActionResult}. */
26@RunWith(JUnit4.class)
27public final class ActionResultTest {
28
29 @Test
ruperts02fb4bb2017-11-04 06:40:14 +010030 public void testCumulativeCommandExecutionTime_NoSpawnResults() {
ruperts7967f332017-11-21 16:37:13 -080031 List<SpawnResult> spawnResults = ImmutableList.of();
ruperts89d13b82017-10-21 02:20:24 +020032 ActionResult actionResult = ActionResult.create(spawnResults);
ruperts52d77782017-11-09 16:12:45 +010033 assertThat(actionResult.cumulativeCommandExecutionWallTime()).isEmpty();
ruperts26bc2822017-12-01 15:55:38 -080034 assertThat(actionResult.cumulativeCommandExecutionCpuTime()).isEmpty();
ruperts52d77782017-11-09 16:12:45 +010035 assertThat(actionResult.cumulativeCommandExecutionUserTime()).isEmpty();
36 assertThat(actionResult.cumulativeCommandExecutionSystemTime()).isEmpty();
rupertsf35c8a02017-12-08 14:19:30 -080037 assertThat(actionResult.cumulativeCommandExecutionBlockOutputOperations()).isEmpty();
38 assertThat(actionResult.cumulativeCommandExecutionBlockInputOperations()).isEmpty();
39 assertThat(actionResult.cumulativeCommandExecutionInvoluntaryContextSwitches()).isEmpty();
ruperts89d13b82017-10-21 02:20:24 +020040 }
41
42 @Test
ruperts02fb4bb2017-11-04 06:40:14 +010043 public void testCumulativeCommandExecutionTime_OneSpawnResult() {
ruperts89d13b82017-10-21 02:20:24 +020044 SpawnResult spawnResult =
45 new SpawnResult.Builder()
ruperts02fb4bb2017-11-04 06:40:14 +010046 .setWallTime(Duration.ofMillis(1984))
47 .setUserTime(Duration.ofMillis(225))
48 .setSystemTime(Duration.ofMillis(42))
rupertsf35c8a02017-12-08 14:19:30 -080049 .setNumBlockOutputOperations(10)
50 .setNumBlockInputOperations(20)
51 .setNumInvoluntaryContextSwitches(30)
ruperts89d13b82017-10-21 02:20:24 +020052 .setStatus(SpawnResult.Status.SUCCESS)
Googler4dd6f002018-03-27 08:15:39 -070053 .setRunnerName("test")
ruperts89d13b82017-10-21 02:20:24 +020054 .build();
ruperts7967f332017-11-21 16:37:13 -080055 List<SpawnResult> spawnResults = ImmutableList.of(spawnResult);
ruperts89d13b82017-10-21 02:20:24 +020056 ActionResult actionResult = ActionResult.create(spawnResults);
ruperts52d77782017-11-09 16:12:45 +010057 assertThat(actionResult.cumulativeCommandExecutionWallTime()).hasValue(Duration.ofMillis(1984));
ruperts26bc2822017-12-01 15:55:38 -080058 assertThat(actionResult.cumulativeCommandExecutionCpuTime()).hasValue(Duration.ofMillis(267));
ruperts52d77782017-11-09 16:12:45 +010059 assertThat(actionResult.cumulativeCommandExecutionUserTime()).hasValue(Duration.ofMillis(225));
ruperts52d77782017-11-09 16:12:45 +010060 assertThat(actionResult.cumulativeCommandExecutionSystemTime()).hasValue(Duration.ofMillis(42));
rupertsf35c8a02017-12-08 14:19:30 -080061 assertThat(actionResult.cumulativeCommandExecutionBlockOutputOperations()).hasValue(10L);
62 assertThat(actionResult.cumulativeCommandExecutionBlockInputOperations()).hasValue(20L);
63 assertThat(actionResult.cumulativeCommandExecutionInvoluntaryContextSwitches()).hasValue(30L);
ruperts89d13b82017-10-21 02:20:24 +020064 }
65
66 @Test
ruperts02fb4bb2017-11-04 06:40:14 +010067 public void testCumulativeCommandExecutionTime_ManySpawnResults() {
ruperts89d13b82017-10-21 02:20:24 +020068 SpawnResult spawnResult1 =
69 new SpawnResult.Builder()
ruperts02fb4bb2017-11-04 06:40:14 +010070 .setWallTime(Duration.ofMillis(1979))
71 .setUserTime(Duration.ofMillis(1))
72 .setSystemTime(Duration.ofMillis(33))
rupertsf35c8a02017-12-08 14:19:30 -080073 .setNumBlockOutputOperations(10)
74 .setNumBlockInputOperations(20)
75 .setNumInvoluntaryContextSwitches(30)
ruperts89d13b82017-10-21 02:20:24 +020076 .setStatus(SpawnResult.Status.SUCCESS)
Googler4dd6f002018-03-27 08:15:39 -070077 .setRunnerName("test")
ruperts89d13b82017-10-21 02:20:24 +020078 .build();
79 SpawnResult spawnResult2 =
80 new SpawnResult.Builder()
ruperts02fb4bb2017-11-04 06:40:14 +010081 .setWallTime(Duration.ofMillis(4))
82 .setUserTime(Duration.ofMillis(1))
83 .setSystemTime(Duration.ofMillis(7))
rupertsf35c8a02017-12-08 14:19:30 -080084 .setNumBlockOutputOperations(100)
85 .setNumBlockInputOperations(200)
86 .setNumInvoluntaryContextSwitches(300)
ruperts89d13b82017-10-21 02:20:24 +020087 .setStatus(SpawnResult.Status.SUCCESS)
Googler4dd6f002018-03-27 08:15:39 -070088 .setRunnerName("test")
ruperts89d13b82017-10-21 02:20:24 +020089 .build();
90 SpawnResult spawnResult3 =
91 new SpawnResult.Builder()
ruperts02fb4bb2017-11-04 06:40:14 +010092 .setWallTime(Duration.ofMillis(1))
93 .setUserTime(Duration.ofMillis(2))
94 .setSystemTime(Duration.ofMillis(2))
rupertsf35c8a02017-12-08 14:19:30 -080095 .setNumBlockOutputOperations(1000)
96 .setNumBlockInputOperations(2000)
97 .setNumInvoluntaryContextSwitches(3000)
ruperts89d13b82017-10-21 02:20:24 +020098 .setStatus(SpawnResult.Status.SUCCESS)
Googler4dd6f002018-03-27 08:15:39 -070099 .setRunnerName("test")
ruperts89d13b82017-10-21 02:20:24 +0200100 .build();
ruperts7967f332017-11-21 16:37:13 -0800101 List<SpawnResult> spawnResults = ImmutableList.of(spawnResult1, spawnResult2, spawnResult3);
ruperts89d13b82017-10-21 02:20:24 +0200102 ActionResult actionResult = ActionResult.create(spawnResults);
ruperts52d77782017-11-09 16:12:45 +0100103 assertThat(actionResult.cumulativeCommandExecutionWallTime()).hasValue(Duration.ofMillis(1984));
ruperts26bc2822017-12-01 15:55:38 -0800104 assertThat(actionResult.cumulativeCommandExecutionCpuTime()).hasValue(Duration.ofMillis(46));
ruperts52d77782017-11-09 16:12:45 +0100105 assertThat(actionResult.cumulativeCommandExecutionUserTime()).hasValue(Duration.ofMillis(4));
ruperts52d77782017-11-09 16:12:45 +0100106 assertThat(actionResult.cumulativeCommandExecutionSystemTime()).hasValue(Duration.ofMillis(42));
rupertsf35c8a02017-12-08 14:19:30 -0800107 assertThat(actionResult.cumulativeCommandExecutionBlockOutputOperations()).hasValue(1110L);
108 assertThat(actionResult.cumulativeCommandExecutionBlockInputOperations()).hasValue(2220L);
109 assertThat(actionResult.cumulativeCommandExecutionInvoluntaryContextSwitches()).hasValue(3330L);
ruperts02fb4bb2017-11-04 06:40:14 +0100110 }
111
112 @Test
113 public void testCumulativeCommandExecutionTime_ManyEmptySpawnResults() {
114 SpawnResult spawnResult1 =
Googler4dd6f002018-03-27 08:15:39 -0700115 new SpawnResult.Builder()
116 .setStatus(SpawnResult.Status.SUCCESS)
117 .setRunnerName("test")
118 .build();
ruperts02fb4bb2017-11-04 06:40:14 +0100119 SpawnResult spawnResult2 =
Googler4dd6f002018-03-27 08:15:39 -0700120 new SpawnResult.Builder()
121 .setStatus(SpawnResult.Status.SUCCESS)
122 .setRunnerName("test")
123 .build();
ruperts02fb4bb2017-11-04 06:40:14 +0100124 SpawnResult spawnResult3 =
Googler4dd6f002018-03-27 08:15:39 -0700125 new SpawnResult.Builder()
126 .setStatus(SpawnResult.Status.SUCCESS)
127 .setRunnerName("test")
128 .build();
ruperts7967f332017-11-21 16:37:13 -0800129 List<SpawnResult> spawnResults = ImmutableList.of(spawnResult1, spawnResult2, spawnResult3);
ruperts02fb4bb2017-11-04 06:40:14 +0100130 ActionResult actionResult = ActionResult.create(spawnResults);
ruperts52d77782017-11-09 16:12:45 +0100131 assertThat(actionResult.cumulativeCommandExecutionWallTime()).isEmpty();
ruperts26bc2822017-12-01 15:55:38 -0800132 assertThat(actionResult.cumulativeCommandExecutionCpuTime()).isEmpty();
ruperts52d77782017-11-09 16:12:45 +0100133 assertThat(actionResult.cumulativeCommandExecutionUserTime()).isEmpty();
134 assertThat(actionResult.cumulativeCommandExecutionSystemTime()).isEmpty();
rupertsf35c8a02017-12-08 14:19:30 -0800135 assertThat(actionResult.cumulativeCommandExecutionBlockOutputOperations()).isEmpty();
136 assertThat(actionResult.cumulativeCommandExecutionBlockInputOperations()).isEmpty();
137 assertThat(actionResult.cumulativeCommandExecutionInvoluntaryContextSwitches()).isEmpty();
ruperts89d13b82017-10-21 02:20:24 +0200138 }
ruperts26bc2822017-12-01 15:55:38 -0800139
140 @Test
141 public void testCumulativeCommandExecutionTime_ManySpawnResults_ButOnlyUserTime() {
142 SpawnResult spawnResult1 =
143 new SpawnResult.Builder()
144 .setUserTime(Duration.ofMillis(2))
145 .setStatus(SpawnResult.Status.SUCCESS)
Googler4dd6f002018-03-27 08:15:39 -0700146 .setRunnerName("test")
ruperts26bc2822017-12-01 15:55:38 -0800147 .build();
148 SpawnResult spawnResult2 =
149 new SpawnResult.Builder()
150 .setUserTime(Duration.ofMillis(3))
151 .setStatus(SpawnResult.Status.SUCCESS)
Googler4dd6f002018-03-27 08:15:39 -0700152 .setRunnerName("test")
ruperts26bc2822017-12-01 15:55:38 -0800153 .build();
154 SpawnResult spawnResult3 =
155 new SpawnResult.Builder()
156 .setUserTime(Duration.ofMillis(4))
157 .setStatus(SpawnResult.Status.SUCCESS)
Googler4dd6f002018-03-27 08:15:39 -0700158 .setRunnerName("test")
ruperts26bc2822017-12-01 15:55:38 -0800159 .build();
160 List<SpawnResult> spawnResults = ImmutableList.of(spawnResult1, spawnResult2, spawnResult3);
161 ActionResult actionResult = ActionResult.create(spawnResults);
162 assertThat(actionResult.cumulativeCommandExecutionCpuTime()).hasValue(Duration.ofMillis(9));
163 assertThat(actionResult.cumulativeCommandExecutionUserTime()).hasValue(Duration.ofMillis(9));
164 }
165
166 @Test
167 public void testCumulativeCommandExecutionTime_ManySpawnResults_ButOnlySystemTime() {
168 SpawnResult spawnResult1 =
169 new SpawnResult.Builder()
170 .setSystemTime(Duration.ofMillis(33))
171 .setStatus(SpawnResult.Status.SUCCESS)
Googler4dd6f002018-03-27 08:15:39 -0700172 .setRunnerName("test")
ruperts26bc2822017-12-01 15:55:38 -0800173 .build();
174 SpawnResult spawnResult2 =
175 new SpawnResult.Builder()
176 .setSystemTime(Duration.ofMillis(7))
177 .setStatus(SpawnResult.Status.SUCCESS)
Googler4dd6f002018-03-27 08:15:39 -0700178 .setRunnerName("test")
ruperts26bc2822017-12-01 15:55:38 -0800179 .build();
180 SpawnResult spawnResult3 =
181 new SpawnResult.Builder()
182 .setSystemTime(Duration.ofMillis(2))
183 .setStatus(SpawnResult.Status.SUCCESS)
Googler4dd6f002018-03-27 08:15:39 -0700184 .setRunnerName("test")
ruperts26bc2822017-12-01 15:55:38 -0800185 .build();
186 List<SpawnResult> spawnResults = ImmutableList.of(spawnResult1, spawnResult2, spawnResult3);
187 ActionResult actionResult = ActionResult.create(spawnResults);
188 assertThat(actionResult.cumulativeCommandExecutionCpuTime()).hasValue(Duration.ofMillis(42));
189 assertThat(actionResult.cumulativeCommandExecutionSystemTime()).hasValue(Duration.ofMillis(42));
190 }
ruperts89d13b82017-10-21 02:20:24 +0200191}