-
Notifications
You must be signed in to change notification settings - Fork 13
Binary input java example #363
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
779f95f
added binary input example
NicolasHuertas f5d30ad
Suggested change to README
NicolasHuertas 41e8078
Added README recommendations and usage for BenchmarkScript.py
NicolasHuertas 57ac595
Deleted BenchmarkScript.py
NicolasHuertas 7e0f693
Removed mentions of BenchmarkScript from README
NicolasHuertas cb72a95
Merge remote-tracking branch 'origin/main' into internship_binrayinput
anfelbar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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)], | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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..<NUM_FILES>} | ||
| ``` | ||
|
|
||
| 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. | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.