Skip to content

Commit

Permalink
Add CppCodeGenerator for bencharmk without enabling it. It will be en…
Browse files Browse the repository at this point in the history
…abled in a following change.

--
PiperOrigin-RevId: 150434910
MOS_MIGRATED_REVID=150434910
  • Loading branch information
hermione521 committed Mar 20, 2017
1 parent dd142c9 commit 45c7310
Show file tree
Hide file tree
Showing 11 changed files with 925 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ public ImmutableList<BuildEnvConfig> getBuildEnvConfigs() {
public void prepareGeneratedCode(Path copyDir, Path generatedCodePath) throws IOException {
// Prepare generated code for copy
if (!copyDir.toFile().exists()) {
JavaCodeGenerator.generateNewProject(
JavaCodeGenerator javaCodeGenerator = new JavaCodeGenerator();
javaCodeGenerator.generateNewProject(
copyDir.toString(),
INCLUDE_TARGET_A_FEW_FILES,
INCLUDE_TARGET_MANY_FILES,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ private boolean runForConfigAndReturnLastIsIncremental(
workspace.resolve(GENERATED_CODE_DIR));
}
if (!lastIsIncremental && envConfig.getIncremental()) {
JavaCodeGenerator.modifyExistingProject(
JavaCodeGenerator javaCodeGenerator = new JavaCodeGenerator();
javaCodeGenerator.modifyExistingProject(
workspace.resolve(GENERATED_CODE_DIR).toString(), true, true, true, true);
}
lastIsIncremental = envConfig.getIncremental();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// 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.codegenerator;

import com.google.common.annotations.VisibleForTesting;

import java.nio.file.Path;
import java.nio.file.Paths;

/**
* Abstract base class for creating 4 types of project, or modify existing ones.
* Subclasses are for different languages.
*/
public abstract class CodeGenerator {

@VisibleForTesting static final String TARGET_A_FEW_FILES = "AFewFiles";
@VisibleForTesting static final int SIZE_A_FEW_FILES = 10;

@VisibleForTesting static final String TARGET_MANY_FILES = "ManyFiles";
@VisibleForTesting static final int SIZE_MANY_FILES = 1000;

@VisibleForTesting static final String TARGET_LONG_CHAINED_DEPS = "LongChainedDeps";
@VisibleForTesting static final int SIZE_LONG_CHAINED_DEPS = 20;

@VisibleForTesting static final String TARGET_PARALLEL_DEPS = "ParallelDeps";
@VisibleForTesting static final int SIZE_PARALLEL_DEPS = 20;

public void generateNewProject(
String outputDir,
boolean aFewFiles,
boolean manyFiles,
boolean longChainedDeps,
boolean parallelDeps) {
Path dir = Paths.get(outputDir);
if (aFewFiles) {
createTargetWithSomeFiles(dir.resolve(TARGET_A_FEW_FILES), SIZE_A_FEW_FILES);
}
if (manyFiles) {
createTargetWithSomeFiles(dir.resolve(TARGET_MANY_FILES), SIZE_MANY_FILES);
}
if (longChainedDeps) {
createTargetWithLongChainedDeps(dir.resolve(TARGET_LONG_CHAINED_DEPS));
}
if (parallelDeps) {
createTargetWithParallelDeps(dir.resolve(TARGET_PARALLEL_DEPS));
}
}

public void modifyExistingProject(
String outputDir,
boolean aFewFiles,
boolean manyFiles,
boolean longChainedDeps,
boolean parallelDeps) {
Path dir = Paths.get(outputDir);
if (aFewFiles) {
modifyTargetWithSomeFiles(dir.resolve(TARGET_A_FEW_FILES));
}
if (manyFiles) {
modifyTargetWithSomeFiles(dir.resolve(TARGET_MANY_FILES));
}
if (longChainedDeps) {
modifyTargetWithLongChainedDeps(dir.resolve(TARGET_LONG_CHAINED_DEPS));
}
if (parallelDeps) {
modifyTargetWithParallelDeps(dir.resolve(TARGET_PARALLEL_DEPS));
}
}

abstract void createTargetWithSomeFiles(Path projectPath, int numberOfFiles);
abstract void modifyTargetWithSomeFiles(Path projectPath);

abstract void createTargetWithLongChainedDeps(Path projectPath);
abstract void modifyTargetWithLongChainedDeps(Path projectPath);

abstract void createTargetWithParallelDeps(Path projectPath);
abstract void modifyTargetWithParallelDeps(Path projectPath);

public abstract String getDirSuffix();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
// 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.codegenerator;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

/** Create 4 types of Cpp project, or modify existing ones. */
public class CppCodeGenerator extends CodeGenerator {

private static final String DIR_SUFFIX = "/cpp";

@Override
public String getDirSuffix() {
return DIR_SUFFIX;
}

/** Target type 1/2: Create targets with some files */
@Override
void createTargetWithSomeFiles(Path projectPath, int numberOfFiles) {
if (pathExists(projectPath)) {
return;
}

try {
Files.createDirectories(projectPath);
for (int i = 0; i < numberOfFiles; ++i) {
CppCodeGeneratorHelper.createRandomClass("RandomClass" + i, projectPath);
}
CppCodeGeneratorHelper.writeBuildFileWithAllFilesToDir(
projectPath.getFileName().toString(), projectPath);
} catch (IOException e) {
System.err.println("Error creating target with some files: " + e.getMessage());
}
}

/** Target type 1/2: Modify targets with some files */
@Override
void modifyTargetWithSomeFiles(Path projectPath) {
File dir = projectPath.toFile();
if (directoryNotExists(dir)) {
System.err.format(
"Project dir (%s) does not contain code for modification.\n", projectPath.toString());
return;
}

try {
CppCodeGeneratorHelper.createRandomClassExtra("RandomClass0", projectPath);
} catch (IOException e) {
System.err.println("Error modifying targets some files: " + e.getMessage());
}
}

/** Target type 3: Create targets with a few long chained dependencies (A -> B -> C -> … -> Z) */
@Override
void createTargetWithLongChainedDeps(Path projectPath) {
if (pathExists(projectPath)) {
return;
}

try {
Files.createDirectories(projectPath);

int count = SIZE_LONG_CHAINED_DEPS;

// Call next one for 1..(count-2)
for (int i = 1; i < count - 1; ++i) {
CppCodeGeneratorHelper.createClassAndBuildFileWithDepsNext(i, projectPath);
}
// Don't call next one for (count-1)
CppCodeGeneratorHelper.createRandomClass("Deps" + (count - 1), projectPath);
CppCodeGeneratorHelper.appendTargetToBuildFile("Deps" + (count - 1), projectPath);

// Main
String deps = " deps=[ ':Deps1' ],";
CppCodeGeneratorHelper.createMainClassAndBuildFileWithDeps(
TARGET_LONG_CHAINED_DEPS, deps, projectPath);
} catch (IOException e) {
System.err.println(
"Error creating targets with a few long chained dependencies: " + e.getMessage());
}
}

/** Target type 3: Modify targets with a few long chained dependencies (A -> B -> C -> … -> Z) */
@Override
void modifyTargetWithLongChainedDeps(Path projectPath) {
File dir = projectPath.toFile();
if (directoryNotExists(dir)) {
System.err.format(
"Project dir (%s) does not contain code for modification.\n", projectPath.toString());
return;
}

try {
CppCodeGeneratorHelper.createClassWithDepsNextExtra(
(SIZE_LONG_CHAINED_DEPS + 1) >> 1, projectPath);
} catch (IOException e) {
System.err.println(
"Error modifying targets with a few long chained dependencies: " + e.getMessage());
}
}

/** Target type 4: Create targets with lots of parallel dependencies (A -> B, C, D, E, F, G, H) */
@Override
void createTargetWithParallelDeps(Path projectPath) {
if (pathExists(projectPath)) {
return;
}

try {
Files.createDirectories(projectPath);

int count = SIZE_PARALLEL_DEPS;

// parallel dependencies B~Z
for (int i = 1; i < count; ++i) {
CppCodeGeneratorHelper.createRandomClass("Deps" + i, projectPath);
CppCodeGeneratorHelper.appendTargetToBuildFile("Deps" + i, projectPath);
}

// A(Main)
String deps = " deps=[ ";
for (int i = 1; i < count; ++i) {
deps += "\":Deps" + i + "\", ";
}
deps += "],";
CppCodeGeneratorHelper.createMainClassAndBuildFileWithDeps(
TARGET_PARALLEL_DEPS, deps, projectPath);
} catch (IOException e) {
System.err.println(
"Error creating targets with lots of parallel dependencies: " + e.getMessage());
}
}

/** Target type 4: Modify targets with lots of parallel dependencies (A -> B, C, D, E, F, G, H) */
@Override
void modifyTargetWithParallelDeps(Path projectPath) {
File dir = projectPath.toFile();
if (directoryNotExists(dir)) {
System.err.format(
"Project dir (%s) does not contain code for modification.\n", projectPath.toString());
return;
}
try {
CppCodeGeneratorHelper.createRandomClassExtra("Deps1", projectPath);
} catch (IOException e) {
System.err.println(
"Error creating targets with lots of parallel dependencies: " + e.getMessage());
}
}

private static boolean pathExists(Path path) {
File dir = path.toFile();
if (dir.exists()) {
System.err.println("File or directory exists, not rewriting it: " + path);
return true;
}

return false;
}

private static boolean directoryNotExists(File file) {
return !(file.exists() && file.isDirectory());
}
}
Loading

0 comments on commit 45c7310

Please sign in to comment.