-
Notifications
You must be signed in to change notification settings - Fork 13
Varying inputs example #360
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
Open
NicolasHuertas
wants to merge
7
commits into
main
Choose a base branch
from
varying-inputs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d3771dd
Varying inputs example
NicolasHuertas ecaaf31
Update README.md
NicolasHuertas 3361c86
Update BUILD
NicolasHuertas b644a4b
Update BUILD
NicolasHuertas 5ee320d
added line at the end of every file in varyinginputs
NicolasHuertas a193ac1
Merge branch 'varying-inputs' of https://github.com/EngFlow/example i…
NicolasHuertas fb3eb28
added line at the end of files in varyinginputs
NicolasHuertas 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,34 @@ | ||
| # Varying Inputs Example | ||
|
|
||
| ## Overview | ||
|
|
||
| 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. | ||
|
|
||
| ## Project Structure | ||
|
|
||
| - `java/com/engflow/varyinginputs/input`: Directory where the generated `.txt` files are saved. | ||
| - `java/com/engflow/varyinginputs/main`: Contains the `Main` class which orchestrates the file creation and reading process. | ||
| - `java/com/engflow/varyinginputs/reader`: Contains the `Reader` class responsible for reading the files. | ||
| - `java/com/engflow/varyinginputs/writer`: Contains the `Writer` class responsible for writing the files. | ||
|
|
||
| ## Usage | ||
|
|
||
| To run the project and specify the number of files to be created, use the following command: | ||
|
|
||
| ```sh | ||
| bazel run //java/com/engflow/varyinginputs/main -- <num_files> | ||
| ``` | ||
|
|
||
| Replace `<num_files>` with the desired number of files to be created. | ||
|
|
||
| ## Example | ||
|
|
||
| To create and read 10 files, you would run: | ||
|
|
||
| ```sh | ||
| bazel run //java/com/engflow/varyinginputs/main -- 10 | ||
| ``` | ||
|
|
||
| This command will: | ||
| 1. Use the `Writer` class to create 10 files, each containing 100 random characters, in the `input` directory. | ||
| 2. Use the `Reader` class to read and print the contents of these 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,5 @@ | ||
| filegroup( | ||
| name = "input", | ||
| srcs = glob(["*.txt"]), | ||
| visibility = ["//visibility:public"], | ||
| ) | ||
NicolasHuertas marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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,14 @@ | ||
| load("@rules_java//java:defs.bzl", "java_binary") | ||
|
|
||
| java_binary( | ||
| name = "main", | ||
| srcs = ["Main.java"], | ||
| deps = [ | ||
| "//java/com/engflow/varyinginputs/reader:reader", | ||
| "//java/com/engflow/varyinginputs/writer:writer", | ||
| ], | ||
| data = [ | ||
| "//java/com/engflow/varyinginputs/input:input", | ||
| ], | ||
| main_class = "com.engflow.varyinginputs.main.Main", | ||
| ) |
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,30 @@ | ||
| package com.engflow.varyinginputs.main; | ||
|
|
||
| import com.engflow.varyinginputs.reader.Reader; | ||
| import com.engflow.varyinginputs.writer.Writer; | ||
|
|
||
| public class Main { | ||
|
|
||
| public static void main(String[] args) { | ||
| if (args.length < 1) { | ||
| System.out.println("Please provide the number of files as an argument."); | ||
| return; | ||
| } | ||
|
|
||
| int numberOfFiles; | ||
| try { | ||
| numberOfFiles = Integer.parseInt(args[0]); | ||
| } catch (NumberFormatException e) { | ||
| System.out.println("Invalid number format: " + args[0]); | ||
| return; | ||
| } | ||
|
|
||
| Writer writer = new Writer(); | ||
| Reader reader = new Reader(); | ||
|
|
||
| String filePath = "com/engflow/varyinginputs/input"; | ||
|
|
||
| writer.writeFiles(numberOfFiles, filePath); | ||
| reader.readFiles(filePath); | ||
| } | ||
| } |
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,14 @@ | ||
| java_library( | ||
| name = "reader", | ||
| srcs = ["Reader.java"], | ||
| visibility = ["//visibility:public"], | ||
| ) | ||
|
|
||
| java_test( | ||
| name = "reader_test", | ||
| srcs = ["ReaderTest.java"], | ||
| test_class = "com.engflow.varyinginputs.reader.ReaderTest", | ||
| deps = [ | ||
| ":reader", | ||
| ], | ||
| ) |
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,45 @@ | ||
| package com.engflow.varyinginputs.reader; | ||
|
|
||
| import java.io.File; | ||
| import java.io.FileReader; | ||
| import java.io.IOException; | ||
| import java.io.BufferedReader; | ||
|
|
||
| public class Reader { | ||
|
|
||
| /** | ||
| * Reads the content of all files in the specified directory and prints it to the console. | ||
| * @param inputPath | ||
| */ | ||
| public void readFiles(String inputPath) { | ||
| File directory = new File(inputPath); | ||
|
|
||
| // Check if the directory exists and is a directory | ||
| if (!directory.exists() || !directory.isDirectory()) { | ||
| System.out.println("Invalid directory path: " + inputPath); | ||
| return; | ||
| } | ||
|
|
||
| // List all files in the directory and check if there are any | ||
| File[] files = directory.listFiles(); | ||
| if (files == null || files.length == 0) { | ||
| System.out.println("No files found in the directory: " + inputPath); | ||
| return; | ||
| } | ||
|
|
||
| // Read the content of each file and print it to the console | ||
| for (File file : files) { | ||
| if (file.isFile()) { | ||
| try (BufferedReader reader = new BufferedReader(new FileReader(file))) { | ||
| String line; | ||
| System.out.println("Reading file: " + file.getName()); | ||
| while ((line = reader.readLine()) != null) { | ||
| System.out.println(line); | ||
| } | ||
| } catch (IOException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
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,65 @@ | ||
| package com.engflow.varyinginputs.reader; | ||
|
|
||
| import org.junit.Test; | ||
| import java.io.File; | ||
| import java.io.FileWriter; | ||
| import java.io.IOException; | ||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| public class ReaderTest { | ||
|
|
||
| @Test | ||
| public void testReadFilesPrintsFileContents() throws IOException { | ||
| Reader reader = new Reader(); | ||
| String inputPath = "test_input"; | ||
| String fileName = "testFile.txt"; | ||
| String fileContent = "Hello, World!"; | ||
|
|
||
| // Set up test files | ||
| File directory = new File(inputPath); | ||
| directory.mkdirs(); | ||
| File testFile = new File(inputPath + "/" + fileName); | ||
| try (FileWriter writer = new FileWriter(testFile)) { | ||
| writer.write(fileContent); | ||
| } | ||
|
|
||
| // Capture the output | ||
| java.io.ByteArrayOutputStream outContent = new java.io.ByteArrayOutputStream(); | ||
| System.setOut(new java.io.PrintStream(outContent)); | ||
|
|
||
| // Run the method | ||
| reader.readFiles(inputPath); | ||
|
|
||
| // Verify the output | ||
| String expectedOutput = "Reading file: " + fileName + "\n" + fileContent + "\n"; | ||
| assertTrue(outContent.toString().contains(expectedOutput)); | ||
|
|
||
| // Clean up after test | ||
| testFile.delete(); | ||
| directory.delete(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testReadFilesHandlesEmptyDirectory() { | ||
| Reader reader = new Reader(); | ||
| String inputPath = "empty_test_input"; | ||
|
|
||
| // Set up empty directory | ||
| File directory = new File(inputPath); | ||
| directory.mkdirs(); | ||
|
|
||
| // Capture the output | ||
| java.io.ByteArrayOutputStream outContent = new java.io.ByteArrayOutputStream(); | ||
| System.setOut(new java.io.PrintStream(outContent)); | ||
|
|
||
| // Run the method | ||
| reader.readFiles(inputPath); | ||
|
|
||
| // Verify the output | ||
| String expectedOutput = "No files found in the directory: " + inputPath + "\n"; | ||
| assertTrue(outContent.toString().contains(expectedOutput)); | ||
|
|
||
| // Clean up after test | ||
| directory.delete(); | ||
| } | ||
| } |
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,14 @@ | ||
| java_library( | ||
| name = "writer", | ||
| srcs = ["Writer.java"], | ||
| visibility = ["//visibility:public"], | ||
| ) | ||
|
|
||
| java_test( | ||
| name = "writer_test", | ||
| srcs = ["WriterTest.java"], | ||
| test_class = "com.engflow.varyinginputs.writer.WriterTest", | ||
| deps = [ | ||
| ":writer", | ||
| ], | ||
| ) |
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,61 @@ | ||
| package com.engflow.varyinginputs.writer; | ||
|
|
||
| import java.io.File; | ||
| import java.io.FileWriter; | ||
| import java.io.IOException; | ||
| import java.util.Random; | ||
| import java.util.UUID; | ||
|
|
||
| public class Writer { | ||
|
|
||
| /** | ||
| * Write the specified number of files to the output path. | ||
| * @param numberOfFiles | ||
| * @param outputPath | ||
| */ | ||
| public void writeFiles(int numberOfFiles, String outputPath) { | ||
| File directory = new File(outputPath); | ||
|
|
||
| if (!directory.exists()) { | ||
| directory.mkdirs(); | ||
| } | ||
|
|
||
| // list all files in the directory and count them | ||
| File[] existingFiles = directory.listFiles(); | ||
| int existingFilesCount = existingFiles != null ? existingFiles.length : 0; | ||
|
|
||
| Random random = new Random(); | ||
|
|
||
| // create new files if the number of existing files is less than the required number | ||
| if (existingFilesCount < numberOfFiles) { | ||
| for (int i = existingFilesCount; i < numberOfFiles; i++) { | ||
| // create a new file with a unique name to avoid conflicts | ||
| String fileName = outputPath + "/file" + UUID.randomUUID() + ".txt"; | ||
| File file = new File(fileName); | ||
|
|
||
| try { | ||
| // write 100 random characters to the file | ||
| file.createNewFile(); | ||
| FileWriter writer = new FileWriter(file); | ||
| for (int j = 0; j < 100; j++) { | ||
| char randomChar = (char) (random.nextInt(26) + 'a'); | ||
| writer.write(randomChar); | ||
| } | ||
| writer.close(); | ||
| } catch (IOException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
| } | ||
| // delete files if the number of existing files is greater than the required number | ||
| else if (existingFilesCount > numberOfFiles) { | ||
| while (existingFilesCount > numberOfFiles) { | ||
| int fileIndex = random.nextInt(existingFilesCount); | ||
| File fileToDelete = existingFiles[fileIndex]; | ||
| if (fileToDelete.delete()) { | ||
| existingFilesCount--; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
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,66 @@ | ||
| package com.engflow.varyinginputs.writer; | ||
|
|
||
| import org.junit.Test; | ||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import static org.junit.Assert.assertEquals; | ||
|
|
||
| public class WriterTest { | ||
|
|
||
| @Test | ||
| public void testWriteFilesCreatesCorrectNumberOfFiles() throws IOException { | ||
| Writer writer = new Writer(); | ||
| String outputPath = "test_output"; | ||
| int numberOfFiles = 5; | ||
|
|
||
| // Clean up before test | ||
| File directory = new File(outputPath); | ||
| if (directory.exists()) { | ||
| for (File file : directory.listFiles()) { | ||
| file.delete(); | ||
| } | ||
| directory.delete(); | ||
| } | ||
|
|
||
| // Run the method | ||
| writer.writeFiles(numberOfFiles, outputPath); | ||
|
|
||
| // Verify the number of files created | ||
| File[] files = new File(outputPath).listFiles(); | ||
| assertEquals(numberOfFiles, files.length); | ||
|
|
||
| // Clean up after test | ||
| for (File file : files) { | ||
| file.delete(); | ||
| } | ||
| new File(outputPath).delete(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testWriteFilesDeletesExcessFiles() throws IOException { | ||
| Writer writer = new Writer(); | ||
| String outputPath = "test_output"; | ||
| int initialNumberOfFiles = 10; | ||
| int finalNumberOfFiles = 5; | ||
|
|
||
| // Create initial files | ||
| File directory = new File(outputPath); | ||
| directory.mkdirs(); | ||
| for (int i = 0; i < initialNumberOfFiles; i++) { | ||
| new File(outputPath + "/file" + i + ".txt").createNewFile(); | ||
| } | ||
|
|
||
| // Run the method | ||
| writer.writeFiles(finalNumberOfFiles, outputPath); | ||
|
|
||
| // Verify the number of files after deletion | ||
| File[] files = new File(outputPath).listFiles(); | ||
| assertEquals(finalNumberOfFiles, files.length); | ||
|
|
||
| // Clean up after test | ||
| for (File file : files) { | ||
| file.delete(); | ||
| } | ||
| new File(outputPath).delete(); | ||
| } | ||
| } |
Oops, something went wrong.
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.