Skip to content
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

[Internship] Binary Input Example #363

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions java/com/engflow/binaryinput/BUILD
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)],
)
21 changes: 21 additions & 0 deletions java/com/engflow/binaryinput/Main.java
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);
}
}
}
49 changes: 49 additions & 0 deletions java/com/engflow/binaryinput/README.md
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.
NicolasHuertas marked this conversation as resolved.
Show resolved Hide resolved
- 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.
Loading