Skip to content

Commit 2905e21

Browse files
Added biinary input example
1 parent b1c8d09 commit 2905e21

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
load("@rules_java//java:defs.bzl", "java_binary", "java_library")
2+
3+
NUM_FILES = 10
4+
5+
#Main class
6+
java_binary(
7+
name = "main",
8+
srcs = ["Main.java"],
9+
main_class = "com.engflow.internship.binaryinput.Main",
10+
deps = [
11+
":genbinary"
12+
],
13+
args = [str(NUM_FILES)],
14+
)
15+
16+
#Generates a number of java files based on the value of NUM_FILES
17+
#Each file is named HelloX.java where X is the number of the file
18+
#Each file contains a class with a greetNum method that prints "Hello" + the number of the file
19+
[genrule(
20+
name = "Hello" + str(x),
21+
outs = ["Hello" + str(x) + ".java"],
22+
cmd_bash = "echo 'package com.engflow.internship.binaryinput;" + "\n" +
23+
"public class Hello" + str(x) +
24+
" { public static void greetNum() { System.out.println(\"Hello " + str(x) + "\"); } }' > $@",
25+
) for x in range(1,NUM_FILES+1)]
26+
27+
#Generates a java library that contains all the generated java files
28+
java_library(
29+
name = "genbinary",
30+
srcs = [":Hello" + str(x) + ".java" for x in range(1,NUM_FILES+1)],
31+
visibility = ["//visibility:public"],
32+
)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.engflow.internship.binaryinput;
2+
3+
import java.lang.reflect.InvocationTargetException;
4+
5+
public class Main {
6+
public static void main(String[] args) {
7+
try {
8+
// args[0] is the number of files to read
9+
int numFiles = Integer.parseInt(args[0]);
10+
11+
// Load and run the greetNum method from each class
12+
for(int i = 1; i <= numFiles; i++){
13+
Class<?> clazz = Class.forName("com.engflow.internship.binaryinput.Hello" + i);
14+
clazz.getMethod("greetNum").invoke(null);
15+
}
16+
17+
} catch (ClassNotFoundException | InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
18+
throw new RuntimeException(e);
19+
}
20+
}
21+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Multiple Binary Input Example
2+
3+
## Overview
4+
5+
The goal of this example project is to test the performance of Engflow's remote execution and caching service based on the number of input binary files in the dependency graph. The project contains a `genrule` that generates a specified number of Java binaries for the `genbinary` Java library, which are then listed as dependencies in the main binary. The `Main.java` file loops through each generated class and calls its `greetNum` method.
6+
7+
## Project Structure
8+
9+
- `java/com/engflow/internship/binaryinput/Main.java`: Main class that dynamically loads and invokes methods from generated classes.
10+
- `java/com/engflow/internship/binaryinput/BUILD`: Bazel build file for the `main` java binary and the `genbinary` library.
11+
12+
## Usage
13+
14+
To generate the test files, build the `genbinary` library using the `genrule`:
15+
```sh
16+
bazel build //java/com/engflow/internship/binaryinput:genbinary
17+
```
18+
19+
Then, the program can be run with the following command:
20+
```sh
21+
bazel run //java/com/engflow/internship/binaryinput:main
22+
```
23+
24+
## How It Works
25+
26+
1. **Generation of Java Binaries:**
27+
- The `genrule` in the `BUILD` file generates a specified number of Java classes (`Hello1.java`, `Hello2.java`, ..., `HelloN.java`).
28+
- Each generated class contains a `greetNum` method that prints a unique message.
29+
30+
2. **Main Class Execution:**
31+
- The `Main.java` file in `binaryinput` dynamically loads each generated class using reflection.
32+
- It then invokes the `greetNum` method of each class, printing the corresponding message.
33+
34+
## Configuration
35+
36+
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.
37+
38+
## Example
39+
40+
To generate and run the program with 10 input binary files:
41+
42+
1. Set `NUM_FILES` to 10 in `java/com/engflow/internship/binaryinput/BUILD`.
43+
2. Build the `genbinary` library:
44+
```sh
45+
bazel build //java/com/engflow/internship/binaryinput:genbinary
46+
```
47+
3. Run the `main` binary:
48+
```sh
49+
bazel run //java/com/engflow/internship/binaryinput:main
50+
```
51+
52+
This will generate 10 Java classes, build the `genbinary` library, and run the `main` binary, which will print messages from each generated class.

0 commit comments

Comments
 (0)