diff --git a/java/com/engflow/binaryinput/BUILD b/java/com/engflow/binaryinput/BUILD new file mode 100644 index 00000000..8252be0e --- /dev/null +++ b/java/com/engflow/binaryinput/BUILD @@ -0,0 +1,32 @@ +load("@rules_java//java:defs.bzl", "java_binary", "java_library") + +NUM_FILES = 100 + +# Generates a number of java files based on the value of NUM_FILES +# Each file is named HelloX.java where X is the number of the file +# Each file contains a class with a greetNum method that prints "Hello" + the number of the file +[genrule( + name = "Hello" + str(x), + outs = ["Hello" + str(x) + ".java"], + cmd_bash = "echo 'package com.engflow.binaryinput;" + "\n" + + "public class Hello" + str(x) + + " { public static void greetNum() { System.out.println(\"Hello " + str(x) + "\"); } }' > $@", +) for x in range(1,NUM_FILES+1)] + +# Generates a java library that contains all the generated java files +[java_library( + name = "genbinary" + str(x), + srcs = [":Hello" + str(x) + ".java" for x in range(1,NUM_FILES+1)], + visibility = ["//visibility:public"], +) for x in range(1,NUM_FILES+1)] + +# Main class +java_binary( + name = "main", + srcs = ["Main.java"], + main_class = "com.engflow.binaryinput.Main", + deps = [ + ":genbinary" + str(x) for x in range(1,NUM_FILES+1) + ], + args = [str(NUM_FILES)], +) \ No newline at end of file diff --git a/java/com/engflow/binaryinput/Main.java b/java/com/engflow/binaryinput/Main.java new file mode 100644 index 00000000..d430ce2f --- /dev/null +++ b/java/com/engflow/binaryinput/Main.java @@ -0,0 +1,21 @@ +package com.engflow.binaryinput; + +import java.lang.reflect.InvocationTargetException; + +public class Main { + public static void main(String[] args) { + try { + // args[0] is the number of files to read + int numFiles = Integer.parseInt(args[0]); + + // Load and run the greetNum method from each class + for(int i = 1; i <= numFiles; i++){ + Class clazz = Class.forName("com.engflow.binaryinput.Hello" + i); + clazz.getMethod("greetNum").invoke(null); + } + + } catch (ClassNotFoundException | InvocationTargetException | IllegalAccessException | NoSuchMethodException e) { + throw new RuntimeException(e); + } + } +} diff --git a/java/com/engflow/binaryinput/README.md b/java/com/engflow/binaryinput/README.md new file mode 100644 index 00000000..e5e93a7e --- /dev/null +++ b/java/com/engflow/binaryinput/README.md @@ -0,0 +1,49 @@ +# Multiple Binary Input Example + +## Usage +Set the `NUM_FILES` variable in the BUILD file to the desired input size. + +To generate the test files, build the `genbinary` library using the `genrule`: +```sh +bazel build //java/com/engflow/binaryinput:genbinary{1..} +``` + +Then, the program can be built with the following command: +```sh +bazel build //java/com/engflow/binaryinput:main +``` + +## How It Works + +1. **Generation of Java Binaries:** + - The `genrule` in the `BUILD` file generates a specified number of Java classes (`Hello1.java`, `Hello2.java`, ..., `HelloN.java`). + - Each generated class contains a `greetNum` method that prints a unique message. + - A java library is created for each file (`Hello1.jar`, `Hello2.jar`, ..., `HelloN.jar`). + +2. **Building the main target:** + - The previously created libraries are added to the main class as dependencies through a for loop. + - The consistent naming scheme of the libraries simplifies their inclusion in the build process. + +3. **Main Class Execution:** + - The `Main.java` file in `binaryinput` dynamically loads each generated class using reflection. + - It then invokes the `greetNum` method of each class, printing the corresponding message. + +## Configuration + +The number of generated files is controlled by the `NUM_FILES` variable in the `BUILD` file of the `binaryinput` package. Modify this variable to change the number of generated classes and observe the performance impact on Engflow's remote execution and caching service. + +## Example + +To generate and run the program with 10 input binary files: + +1. Set `NUM_FILES` to 10 in `java/com/engflow/binaryinput/BUILD`. +2. Build the `genbinary` library: + ```sh + bazel build //java/com/engflow/binaryinput:genbinary{1..10} + ``` +3. Build the `main` binary: + ```sh + bazel build //java/com/engflow/binaryinput:main + ``` + +This will generate 10 Java classes, build the `genbinary` library, and build the `main` binary. Using `bazel run` will also print messages from each generated class.