diff options
author | Yue Gan <yueg@google.com> | 2017-02-03 11:27:12 +0000 |
---|---|---|
committer | Yun Peng <pcloudy@google.com> | 2017-02-03 11:32:42 +0000 |
commit | 67a53d22ea83936cd048ceb454b0d81273ee2a79 (patch) | |
tree | 185a0c9edabae1eddd3c71ba33b47705d1730d8d /src/tools/benchmark/javatests | |
parent | e152f72df9c5a34c0f5041925131487280646477 (diff) |
Benchmark part 2 (preparation for benchmark)
Builders: tools to benchmark the performance.
BuildCases: provide all targets for Builders to build.
Fixes BazelBuilderTest
--
PiperOrigin-RevId: 146459295
MOS_MIGRATED_REVID=146459295
Diffstat (limited to 'src/tools/benchmark/javatests')
3 files changed, 229 insertions, 0 deletions
diff --git a/src/tools/benchmark/javatests/com/google/devtools/build/benchmark/BUILD b/src/tools/benchmark/javatests/com/google/devtools/build/benchmark/BUILD new file mode 100644 index 0000000000..f2bc4e85b4 --- /dev/null +++ b/src/tools/benchmark/javatests/com/google/devtools/build/benchmark/BUILD @@ -0,0 +1,31 @@ +package(default_visibility = ["//src/tools/benchmark:__subpackages__"]) + +java_test( + name = "BazelBuildCaseTest", + srcs = ["BazelBuildCaseTest.java"], + deps = [ + "//src/tools/benchmark/java/com/google/devtools/build/benchmark:benchmark_lib", + "//src/tools/benchmark/java/com/google/devtools/build/benchmark:build_data_proto", + "//third_party:guava", + "//third_party:junit4", + "//third_party:truth", + ], +) + +java_test( + name = "BazelBuilderTest", + srcs = ["BazelBuilderTest.java"], + deps = [ + "//src/main/java/com/google/devtools/build/lib:shell", + "//src/tools/benchmark/java/com/google/devtools/build/benchmark:benchmark_lib", + "//src/tools/benchmark/java/com/google/devtools/build/benchmark:build_data_proto", + "//third_party:guava", + "//third_party:junit4", + "//third_party:truth", + ], +) + +filegroup( + name = "srcs", + srcs = glob(["**"]), +) diff --git a/src/tools/benchmark/javatests/com/google/devtools/build/benchmark/BazelBuildCaseTest.java b/src/tools/benchmark/javatests/com/google/devtools/build/benchmark/BazelBuildCaseTest.java new file mode 100644 index 0000000000..44fbc41da0 --- /dev/null +++ b/src/tools/benchmark/javatests/com/google/devtools/build/benchmark/BazelBuildCaseTest.java @@ -0,0 +1,86 @@ +// Copyright 2017 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.benchmark; + +import static com.google.common.truth.Truth.assertThat; +import static java.nio.charset.StandardCharsets.UTF_8; + +import com.google.common.collect.ImmutableSet; +import java.io.File; +import java.io.IOException; +import java.io.PrintWriter; +import java.nio.file.Path; +import java.util.Scanner; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class BazelBuildCaseTest { + + @Rule public TemporaryFolder folder = new TemporaryFolder(); + + @Test + public void testPrepareGeneratedCode_Copy() throws IOException { + Path root = folder.newFolder("PrepareGeneratedCodeCopy").toPath(); + // Prepare source + Path source = root.resolve("source"); + source.toFile().mkdir(); + try (PrintWriter writer = new PrintWriter(source.resolve("file").toFile(), UTF_8.name())) { + writer.println("content"); + } + // Prepare destination + Path destination = root.resolve("destination"); + destination.toFile().mkdir(); + + new BazelBuildCase().prepareGeneratedCode(source, destination); + + ImmutableSet<String> filenames = fileArrayToImmutableSet(destination.toFile().listFiles()); + assertThat(filenames).containsExactly("WORKSPACE", "file"); + assertThat( + new Scanner(destination.resolve("file")).useDelimiter("\\Z").next()).isEqualTo("content"); + } + + @Test + public void testPrepareGeneratedCode_Generate() throws IOException { + Path root = folder.newFolder("PrepareGeneratedCodeGenerate").toPath(); + // Prepare source, don't mkdir + Path source = root.resolve("source"); + // Prepare destination + Path destination = root.resolve("destination"); + destination.toFile().mkdir(); + + new BazelBuildCase().prepareGeneratedCode(source, destination); + + // Check both source and destination directory include generated code + ImmutableSet<String> sourceList = fileArrayToImmutableSet(source.toFile().listFiles()); + ImmutableSet<String> destinationList = + fileArrayToImmutableSet(destination.toFile().listFiles()); + assertThat(sourceList) + .containsExactly("AFewFiles", "LongChainedDeps", "ManyFiles", "ParallelDeps"); + assertThat(destinationList) + .containsExactly("AFewFiles", "LongChainedDeps", "ManyFiles", "ParallelDeps", "WORKSPACE"); + } + + private static ImmutableSet<String> fileArrayToImmutableSet(File[] files) { + ImmutableSet.Builder<String> fileNames = ImmutableSet.builder(); + for (File file : files) { + fileNames.add(file.getName()); + } + return fileNames.build(); + } +} diff --git a/src/tools/benchmark/javatests/com/google/devtools/build/benchmark/BazelBuilderTest.java b/src/tools/benchmark/javatests/com/google/devtools/build/benchmark/BazelBuilderTest.java new file mode 100644 index 0000000000..33c53bef42 --- /dev/null +++ b/src/tools/benchmark/javatests/com/google/devtools/build/benchmark/BazelBuilderTest.java @@ -0,0 +1,112 @@ +// Copyright 2017 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.benchmark; + +import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.fail; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; +import com.google.devtools.build.lib.shell.Command; +import com.google.devtools.build.lib.shell.CommandException; +import java.io.File; +import java.io.IOException; +import java.io.PrintWriter; +import java.nio.file.Files; +import java.nio.file.Path; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class BazelBuilderTest { + + private static final double EPSILON = 1e-4; + + @Rule public TemporaryFolder folder = new TemporaryFolder(); + + @Test + public void testGetCommandFromConfig() { + BuildTargetConfig targetConfig = BuildTargetConfig.newBuilder().setBuildTarget("foo").build(); + BuildEnvConfig envConfig = + BuildEnvConfig.newBuilder().addBuildArgs("apple").addBuildArgs("mango").build(); + + ImmutableList<String> command = + new BazelBuilder(null, null).getCommandFromConfig(targetConfig, envConfig); + + assertThat(command).containsExactly("build", "foo", "apple", "mango"); + } + + @Test + public void testBuildAndGetElapsedTime() throws IOException, CommandException { + Path root = folder.newFolder("BuildAndGetElapsedTime").toPath(); + Path generatedCode = root.resolve("GeneratedCode"); + Files.createDirectories(generatedCode); + // Prepare binary + Path buildBinary = root.resolve("binary.sh"); + Files.createFile(buildBinary); + if (!buildBinary.toFile().setExecutable(true)) { + fail("Failed to set executable"); + } + double expectedValue = 10.42; + try (PrintWriter writer = new PrintWriter(buildBinary.toFile())) { + writer.format( + "#!/bin/bash\n>&2 echo 'blah blah Elapsed time: %.2f blah blah'", expectedValue); + } + + double result = + new BazelBuilder(generatedCode, null) + .buildAndGetElapsedTime(buildBinary, ImmutableList.<String>of()); + + assertThat(result).isWithin(EPSILON).of(expectedValue); + } + + @Test + public void testPrepareFromGitRepo() throws IOException, CommandException { + Path root = folder.newFolder("Prepare").toPath(); + // Create a git repo for clone + Path repo = root.resolve("SimpleRepo"); + Files.createDirectories(repo); + Files.createFile(repo.resolve("BUILD")); + Files.createFile(repo.resolve("WORKSPACE")); + + ImmutableList<String[]> gitCommands = ImmutableList.of( + new String[]{"git", "init"}, + new String[]{"git", "add", "."}, + new String[]{"git", "config", "user.email", "you@example.com"}, + new String[]{"git", "config", "user.name", "Your Name"}, + new String[]{"git", "commit", "-m", "empty"}); + for (String[] gitCommand : gitCommands) { + (new Command(gitCommand, null, repo.toFile())).execute(); + } + + BazelBuilder builder = new BazelBuilder(root.resolve("GeneratedCode"), root.resolve("Builder")); + builder.prepareFromGitRepo(repo.toString()); + + ImmutableSet<String> fileList = + fileArrayToImmutableSet(root.resolve("Builder").toFile().listFiles()); + assertThat(fileList).containsExactly(".git", "BUILD", "WORKSPACE"); + } + + private static ImmutableSet<String> fileArrayToImmutableSet(File[] files) { + ImmutableSet.Builder<String> fileNames = ImmutableSet.builder(); + for (File file : files) { + fileNames.add(file.getName()); + } + return fileNames.build(); + } +} |