Skip to content

Commit 8cd1877

Browse files
Added varying input example
1 parent 4fcc28b commit 8cd1877

File tree

10 files changed

+348
-0
lines changed

10 files changed

+348
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Varying Inputs 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 for different input sizes. The project involves creating a specified number of txt files, each containing 100 random characters, using the `Writer` class, and then reading and printing out their contents with the `Reader` class.
6+
7+
## Project Structure
8+
9+
- `java/com/engflow/internship/varyinginputs/input`: Directory where the generated `.txt` files are saved.
10+
- `java/com/engflow/internship/varyinginputs/main`: Contains the `Main` class which orchestrates the file creation and reading process.
11+
- `java/com/engflow/internship/varyinginputs/reader`: Contains the `Reader` class responsible for reading the files.
12+
- `java/com/engflow/internship/varyinginputs/writer`: Contains the `Writer` class responsible for writing the files.
13+
14+
## Usage
15+
16+
To run the project and specify the number of files to be created, use the following command:
17+
18+
```sh
19+
bazel run //java/com/engflow/internship/varyinginputs/main -- <num_files>
20+
```
21+
22+
Replace `<num_files>` with the desired number of files to be created.
23+
24+
## Example
25+
26+
To create and read 10 files, you would run:
27+
28+
```sh
29+
bazel run //java/com/engflow/internship/varyinginputs/main -- 10
30+
```
31+
32+
This command will:
33+
1. Use the `Writer` class to create 10 files, each containing 100 random characters, in the `input` directory.
34+
2. Use the `Reader` class to read and print the contents of these files.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
filegroup(
2+
name = "input",
3+
srcs = glob(["*.txt"]),
4+
visibility = ["//visibility:public"],
5+
)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
load("@rules_java//java:defs.bzl", "java_binary")
2+
3+
java_binary(
4+
name = "main",
5+
srcs = ["Main.java"],
6+
deps = [
7+
"//java/com/engflow/internship/varyinginputs/reader",
8+
"//java/com/engflow/internship/varyinginputs/writer",
9+
],
10+
data = [
11+
"//java/com/engflow/internship/varyinginputs/input",
12+
],
13+
main_class = "com.engflow.internship.varyinginputs.main.Main",
14+
)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.engflow.internship.varyinginputs.main;
2+
3+
import com.engflow.internship.varyinginputs.reader.Reader;
4+
import com.engflow.internship.varyinginputs.writer.Writer;
5+
6+
public class Main {
7+
8+
public static void main(String[] args) {
9+
if (args.length < 1) {
10+
System.out.println("Please provide the number of files as an argument.");
11+
return;
12+
}
13+
14+
int numberOfFiles;
15+
try {
16+
numberOfFiles = Integer.parseInt(args[0]);
17+
} catch (NumberFormatException e) {
18+
System.out.println("Invalid number format: " + args[0]);
19+
return;
20+
}
21+
22+
Writer writer = new Writer();
23+
Reader reader = new Reader();
24+
25+
String filePath = "java/com/engflow/internship/varyinginputs/input";
26+
27+
writer.writeFiles(numberOfFiles, filePath);
28+
reader.readFiles(filePath);
29+
}
30+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
java_library(
2+
name = "reader",
3+
srcs = ["Reader.java"],
4+
visibility = ["//visibility:public"],
5+
)
6+
7+
java_test(
8+
name = "reader_test",
9+
srcs = ["ReaderTest.java"],
10+
test_class = "com.engflow.internship.varyinginputs.reader.ReaderTest",
11+
deps = [
12+
":reader",
13+
],
14+
)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.engflow.internship.varyinginputs.reader;
2+
3+
import java.io.File;
4+
import java.io.FileReader;
5+
import java.io.IOException;
6+
import java.io.BufferedReader;
7+
8+
public class Reader {
9+
10+
/**
11+
* Reads the content of all files in the specified directory and prints it to the console.
12+
* @param inputPath
13+
*/
14+
public void readFiles(String inputPath) {
15+
File directory = new File(inputPath);
16+
17+
// Check if the directory exists and is a directory
18+
if (!directory.exists() || !directory.isDirectory()) {
19+
System.out.println("Invalid directory path: " + inputPath);
20+
return;
21+
}
22+
23+
// List all files in the directory and check if there are any
24+
File[] files = directory.listFiles();
25+
if (files == null || files.length == 0) {
26+
System.out.println("No files found in the directory: " + inputPath);
27+
return;
28+
}
29+
30+
// Read the content of each file and print it to the console
31+
for (File file : files) {
32+
if (file.isFile()) {
33+
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
34+
String line;
35+
System.out.println("Reading file: " + file.getName());
36+
while ((line = reader.readLine()) != null) {
37+
System.out.println(line);
38+
}
39+
} catch (IOException e) {
40+
e.printStackTrace();
41+
}
42+
}
43+
}
44+
}
45+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.engflow.internship.varyinginputs.reader;
2+
3+
import org.junit.Test;
4+
import java.io.File;
5+
import java.io.FileWriter;
6+
import java.io.IOException;
7+
import static org.junit.Assert.assertTrue;
8+
9+
public class ReaderTest {
10+
11+
@Test
12+
public void testReadFilesPrintsFileContents() throws IOException {
13+
Reader reader = new Reader();
14+
String inputPath = "test_input";
15+
String fileName = "testFile.txt";
16+
String fileContent = "Hello, World!";
17+
18+
// Set up test files
19+
File directory = new File(inputPath);
20+
directory.mkdirs();
21+
File testFile = new File(inputPath + "/" + fileName);
22+
try (FileWriter writer = new FileWriter(testFile)) {
23+
writer.write(fileContent);
24+
}
25+
26+
// Capture the output
27+
java.io.ByteArrayOutputStream outContent = new java.io.ByteArrayOutputStream();
28+
System.setOut(new java.io.PrintStream(outContent));
29+
30+
// Run the method
31+
reader.readFiles(inputPath);
32+
33+
// Verify the output
34+
String expectedOutput = "Reading file: " + fileName + "\n" + fileContent + "\n";
35+
assertTrue(outContent.toString().contains(expectedOutput));
36+
37+
// Clean up after test
38+
testFile.delete();
39+
directory.delete();
40+
}
41+
42+
@Test
43+
public void testReadFilesHandlesEmptyDirectory() {
44+
Reader reader = new Reader();
45+
String inputPath = "empty_test_input";
46+
47+
// Set up empty directory
48+
File directory = new File(inputPath);
49+
directory.mkdirs();
50+
51+
// Capture the output
52+
java.io.ByteArrayOutputStream outContent = new java.io.ByteArrayOutputStream();
53+
System.setOut(new java.io.PrintStream(outContent));
54+
55+
// Run the method
56+
reader.readFiles(inputPath);
57+
58+
// Verify the output
59+
String expectedOutput = "No files found in the directory: " + inputPath + "\n";
60+
assertTrue(outContent.toString().contains(expectedOutput));
61+
62+
// Clean up after test
63+
directory.delete();
64+
}
65+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
java_library(
2+
name = "writer",
3+
srcs = ["Writer.java"],
4+
visibility = ["//visibility:public"],
5+
)
6+
7+
java_test(
8+
name = "writer_test",
9+
srcs = ["WriterTest.java"],
10+
test_class = "com.engflow.internship.varyinginputs.writer.WriterTest",
11+
deps = [
12+
":writer",
13+
],
14+
)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.engflow.internship.varyinginputs.writer;
2+
3+
import java.io.File;
4+
import java.io.FileWriter;
5+
import java.io.IOException;
6+
import java.util.Random;
7+
import java.util.UUID;
8+
9+
public class Writer {
10+
11+
/**
12+
* Write the specified number of files to the output path.
13+
* @param numberOfFiles
14+
* @param outputPath
15+
*/
16+
public void writeFiles(int numberOfFiles, String outputPath) {
17+
File directory = new File(outputPath);
18+
19+
if (!directory.exists()) {
20+
directory.mkdirs();
21+
}
22+
23+
// list all files in the directory and count them
24+
File[] existingFiles = directory.listFiles();
25+
int existingFilesCount = existingFiles != null ? existingFiles.length : 0;
26+
27+
Random random = new Random();
28+
29+
// create new files if the number of existing files is less than the required number
30+
if (existingFilesCount < numberOfFiles) {
31+
for (int i = existingFilesCount; i < numberOfFiles; i++) {
32+
// create a new file with a unique name to avoid conflicts
33+
String fileName = outputPath + "/file" + UUID.randomUUID() + ".txt";
34+
File file = new File(fileName);
35+
36+
try {
37+
// write 100 random characters to the file
38+
file.createNewFile();
39+
FileWriter writer = new FileWriter(file);
40+
for (int j = 0; j < 100; j++) {
41+
char randomChar = (char) (random.nextInt(26) + 'a');
42+
writer.write(randomChar);
43+
}
44+
writer.close();
45+
} catch (IOException e) {
46+
e.printStackTrace();
47+
}
48+
}
49+
}
50+
// delete files if the number of existing files is greater than the required number
51+
else if (existingFilesCount > numberOfFiles) {
52+
while (existingFilesCount > numberOfFiles) {
53+
int fileIndex = random.nextInt(existingFilesCount);
54+
File fileToDelete = existingFiles[fileIndex];
55+
if (fileToDelete.delete()) {
56+
existingFilesCount--;
57+
}
58+
}
59+
}
60+
}
61+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.engflow.internship.varyinginputs.writer;
2+
3+
import org.junit.Test;
4+
import java.io.File;
5+
import java.io.IOException;
6+
import static org.junit.Assert.assertEquals;
7+
8+
public class WriterTest {
9+
10+
@Test
11+
public void testWriteFilesCreatesCorrectNumberOfFiles() throws IOException {
12+
Writer writer = new Writer();
13+
String outputPath = "test_output";
14+
int numberOfFiles = 5;
15+
16+
// Clean up before test
17+
File directory = new File(outputPath);
18+
if (directory.exists()) {
19+
for (File file : directory.listFiles()) {
20+
file.delete();
21+
}
22+
directory.delete();
23+
}
24+
25+
// Run the method
26+
writer.writeFiles(numberOfFiles, outputPath);
27+
28+
// Verify the number of files created
29+
File[] files = new File(outputPath).listFiles();
30+
assertEquals(numberOfFiles, files.length);
31+
32+
// Clean up after test
33+
for (File file : files) {
34+
file.delete();
35+
}
36+
new File(outputPath).delete();
37+
}
38+
39+
@Test
40+
public void testWriteFilesDeletesExcessFiles() throws IOException {
41+
Writer writer = new Writer();
42+
String outputPath = "test_output";
43+
int initialNumberOfFiles = 10;
44+
int finalNumberOfFiles = 5;
45+
46+
// Create initial files
47+
File directory = new File(outputPath);
48+
directory.mkdirs();
49+
for (int i = 0; i < initialNumberOfFiles; i++) {
50+
new File(outputPath + "/file" + i + ".txt").createNewFile();
51+
}
52+
53+
// Run the method
54+
writer.writeFiles(finalNumberOfFiles, outputPath);
55+
56+
// Verify the number of files after deletion
57+
File[] files = new File(outputPath).listFiles();
58+
assertEquals(finalNumberOfFiles, files.length);
59+
60+
// Clean up after test
61+
for (File file : files) {
62+
file.delete();
63+
}
64+
new File(outputPath).delete();
65+
}
66+
}

0 commit comments

Comments
 (0)