Skip to content

Commit 4fcc28b

Browse files
Program now reads through an input file
1 parent 13f6962 commit 4fcc28b

File tree

16 files changed

+139
-42
lines changed

16 files changed

+139
-42
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Cyclical Dependency Example
2+
3+
## Overview
4+
5+
This project is designed to evaluate and benchmark the performance of EngFlow's remote execution and caching services. It specifically focuses on testing scenarios involving cyclical-like structures in the dependency graph, which are handled through interfaces and constructor injection.
6+
7+
## Purpose
8+
9+
The primary objectives of this project are to:
10+
11+
1. **Generate Performance Data**: Create examples with cyclical-like dependencies to test and gather performance data for EngFlow’s remote caching and execution services.
12+
2. **Benchmark Analysis**: Compare the performance of local versus remote execution and caching to evaluate the efficiency and effectiveness of the service.
13+
3. **Support Automation Development**: Contribute to the development of automation algorithms for resource assignment by providing valuable data on how cyclical dependencies impact performance.
14+
15+
## Project Structure
16+
17+
The project is organized into several packages, each representing different components of the cyclical dependency example:
18+
19+
- `class_a`: Contains `ClassA` which depends on `ClassB` through an interface.
20+
- `class_b`: Contains `ClassB` which implements `InterfaceB` and depends on `ClassC`.
21+
- `class_c`: Contains `ClassC` which implements `InterfaceA` and can be initialized with a reference to `ClassA`.
22+
- `interface_a`: Defines the interface `InterfaceA` implemented by `ClassA` and `ClassC`.
23+
- `interface_b`: Defines the interface `InterfaceB` implemented by `ClassB`.
24+
- `main`: Contains the `Main` class which processes the input file.
25+
- `input`: Contains the input text file used by the `Main` class.
26+
27+
## How the Program Works
28+
29+
The program takes a text input file and recursively prints each word with each class (`ClassA` prints a word, then `ClassB`, and so on) until the string is empty. The input file should be specified in the `data` attribute of the `java_binary` rule in the `BUILD` file.
30+
31+
Here is an example of how the `Main` class processes the input file:
32+
33+
34+
## How to Run Tests
35+
36+
To run the tests and gather performance data, use the following Bazel command:
37+
38+
```sh
39+
bazel test //java/com/engflow/internship/cycleexample/class_a:class_a_test

java/com/engflow/internship/cycleexample/class_a/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ java_library(
1212
java_test(
1313
name = "class_a_test",
1414
srcs = ["ClassATest.java"],
15-
test_class = "cycleexample",
15+
test_class = "com.engflow.internship.cycleexample.class_a.ClassATest",
1616
deps = [
1717
":class_a",
1818
"//java/com/engflow/internship/cycleexample/class_b:class_b",

java/com/engflow/internship/cycleexample/class_a/ClassA.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,28 @@
44
import com.engflow.internship.cycleexample.interface_a.InterfaceA;
55

66
public class ClassA implements InterfaceA {
7-
private static final int MAX_CALLS = 10;
8-
private int callCount = 0;
97
private ClassB classB;
108

119
public ClassA(ClassB classB) {
1210
this.classB = classB;
1311
}
1412

1513
@Override
16-
public void methodA() {
17-
if (callCount >= MAX_CALLS) {
18-
System.out.println("ClassA.methodA() reached max calls");
14+
public void methodA(String input) {
15+
// If the input is null or empty, return immediately
16+
if (input == null || input.isEmpty()) {
1917
return;
2018
}
21-
System.out.println("ClassA.methodA()");
22-
callCount++;
23-
classB.methodB();
19+
20+
//Find the index of the first space character in the input string.
21+
int spaceIndex = input.indexOf(' ');
22+
//Extract the word from the beginning of the input string up to the space character.
23+
String word = (spaceIndex == -1) ? input : input.substring(0, spaceIndex);
24+
//Extract the remaining part of the input string after the space character.
25+
String remaining = (spaceIndex == -1) ? "" : input.substring(spaceIndex + 1);
26+
27+
//Print the word extracted from the input string.
28+
System.out.println("ClassA: " + word);
29+
classB.methodB(remaining);
2430
}
25-
}
31+
}

java/com/engflow/internship/cycleexample/class_a/ClassATest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,27 @@ public TestClassB(ClassC classC) {
1414
}
1515

1616
@Override
17-
public void methodB() {
17+
public void methodB(String input) {
1818
methodBCalled = true;
1919
}
2020
}
2121

2222
@Test
2323
public void testMethodA() {
24-
// Create an instance of ClassA
25-
ClassA classA = new ClassA(null);
26-
27-
// Create a ClassC instance with the ClassA object
28-
ClassC classC = new ClassC(classA);
24+
// Create a ClassC instance with a null ClassA object
25+
ClassC classC = new ClassC(null);
2926

3027
// Create a TestClassB instance with the ClassC object
3128
TestClassB testClassB = new TestClassB(classC);
3229

3330
// Create a new ClassA instance with the TestClassB object
34-
classA = new ClassA(testClassB);
31+
ClassA classA = new ClassA(testClassB);
32+
33+
// Properly initialize classC with classA
34+
classC.setClassA(classA);
3535

36-
// Call methodA on classA
37-
classA.methodA();
36+
// Call methodA on classA with a sample input
37+
classA.methodA("sample input");
3838

3939
// Verify that methodB on the TestClassB was called
4040
assertTrue(testClassB.methodBCalled);

java/com/engflow/internship/cycleexample/class_b/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ java_library(
1212
java_test(
1313
name = "class_b_test",
1414
srcs = ["ClassBTest.java"],
15-
test_class = "cycleexample",
15+
test_class = "com.engflow.internship.cycleexample.class_b.ClassBTest",
1616
deps = [
1717
":class_b",
1818
"//java/com/engflow/internship/cycleexample/class_c:class_c",

java/com/engflow/internship/cycleexample/class_b/ClassB.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,21 @@ public ClassB(ClassC classC) {
1111
}
1212

1313
@Override
14-
public void methodB() {
15-
System.out.println("ClassB.methodB()");
16-
classC.methodA();
14+
public void methodB(String input) {
15+
// If the input is null or empty, return immediately
16+
if (input == null || input.isEmpty()) {
17+
return;
18+
}
19+
20+
//Find the index of the first space character in the input string.
21+
int spaceIndex = input.indexOf(' ');
22+
//Extract the word from the beginning of the input string up to the space character.
23+
String word = (spaceIndex == -1) ? input : input.substring(0, spaceIndex);
24+
//Extract the remaining part of the input string after the space character.
25+
String remaining = (spaceIndex == -1) ? "" : input.substring(spaceIndex + 1);
26+
27+
//Print the word extracted from the input string.
28+
System.out.println("ClassB: " + word);
29+
classC.methodA(remaining);
1730
}
18-
}
31+
}

java/com/engflow/internship/cycleexample/class_b/ClassBTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public TestClassC() {
1313
}
1414

1515
@Override
16-
public void methodA() {
16+
public void methodA(String input) {
1717
methodACalled = true;
1818
}
1919
}
@@ -27,7 +27,7 @@ public void testMethodB() {
2727
ClassB classB = new ClassB(testClassC);
2828

2929
// Call methodB on classB
30-
classB.methodB();
30+
classB.methodB("Sample input");
3131

3232
// Verify that methodA on the TestClassC was called
3333
assertTrue(testClassC.methodACalled);

java/com/engflow/internship/cycleexample/class_c/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ java_library(
1212
java_test(
1313
name = "class_c_test",
1414
srcs = ["ClassCTest.java"],
15-
test_class = "cycleexample",
15+
test_class = "com.engflow.internship.cycleexample.class_c.ClassCTest",
1616
deps = [
1717
":class_c",
1818
"//java/com/engflow/internship/cycleexample/class_a:class_a",

java/com/engflow/internship/cycleexample/class_c/ClassC.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,29 @@ public ClassC(InterfaceA classA) {
1010
}
1111

1212
@Override
13-
public void methodA() {
14-
System.out.println("ClassC.methodA()");
15-
if (classA != null) {
16-
classA.methodA();
17-
} else {
18-
System.out.println("classA is null");
13+
public void methodA(String input) {
14+
// If the input is null or empty, return immediately
15+
if (input == null || input.isEmpty()) {
16+
return;
1917
}
18+
19+
// Find the index of the first space character in the input string.
20+
int spaceIndex = input.indexOf(' ');
21+
// Extract the word from the beginning of the input string up to the space character.
22+
String word = (spaceIndex == -1) ? input : input.substring(0, spaceIndex);
23+
// Extract the remaining part of the input string after the space character.
24+
String remaining = (spaceIndex == -1) ? "" : input.substring(spaceIndex + 1);
25+
26+
// Print the word extracted from the input string.
27+
System.out.println("ClassC: " + word);
28+
classA.methodA(remaining);
2029
}
2130

31+
/**
32+
* Set the classA field of this class.
33+
* @param classA
34+
*/
2235
public void setClassA(InterfaceA classA) {
2336
this.classA = classA;
2437
}
25-
26-
public void methodC() {
27-
System.out.println("ClassC.methodC()");
28-
}
2938
}

java/com/engflow/internship/cycleexample/class_c/ClassCTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public TestClassA() {
1313
}
1414

1515
@Override
16-
public void methodA() {
16+
public void methodA(String input) {
1717
methodACalled = true;
1818
}
1919
}
@@ -27,7 +27,7 @@ public void testMethodA() {
2727
ClassC classC = new ClassC(testClassA);
2828

2929
// Call methodA on classC
30-
classC.methodA();
30+
classC.methodA("sample input");
3131

3232
// Verify that methodA on the TestClassA was called
3333
assertTrue(testClassA.methodACalled);

0 commit comments

Comments
 (0)