Skip to content

Commit

Permalink
feat: Follow-up CLI Improvements (#2184)
Browse files Browse the repository at this point in the history
1. Rename Workload1.java to W1R3 to prevent future confusion when it is used for other workloads
2. Take in temporary directory as a CLI argument
3. Utilize PrintWrite instead of System.out directly
  • Loading branch information
sydney-munro authored Aug 30, 2023
1 parent 5302201 commit d985976
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,12 @@
import com.google.api.core.ListenableFutureToApiFuture;
import com.google.api.gax.retrying.RetrySettings;
import com.google.api.gax.rpc.ApiExceptions;
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.DataGenerator;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import com.google.cloud.storage.TmpFile;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
Expand Down Expand Up @@ -81,6 +78,12 @@ public final class StorageSharedBenchmarkingCli implements Runnable {
required = true)
String testType;

@Option(
names = "-temp_dir_location",
defaultValue = "/tmp",
description = "Specify the path where the temporary directory should be located")
String tempDirLocation;

public static void main(String[] args) {
CommandLine cmd = new CommandLine(StorageSharedBenchmarkingCli.class);
System.exit(cmd.execute(args));
Expand All @@ -103,23 +106,18 @@ private void runWorkload1() {
StorageOptions retryStorageOptions =
StorageOptions.newBuilder().setProjectId(project).setRetrySettings(retrySettings).build();
Storage storageClient = retryStorageOptions.getService();
Path tempDir = Paths.get(System.getProperty("java.io.tmpdir"));
Path tempDir = Paths.get(tempDirLocation);
ListeningExecutorService executorService =
MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(workers));
List<ApiFuture<String>> workloadRuns = new ArrayList<>();
Range objectSizeRange = Range.of(objectSize);
for (int i = 0; i < samples; i++) {
try {
TmpFile file =
DataGenerator.base64Characters()
.tempFile(tempDir, getRandomInt(objectSizeRange.min, objectSizeRange.max));
BlobInfo blob = BlobInfo.newBuilder(bucket, file.toString()).build();
workloadRuns.add(
convert(
executorService.submit(new Workload1(file, blob, storageClient, workers, api))));
} catch (IOException e) {
throw new RuntimeException(e);
}
int objectSize = getRandomInt(objectSizeRange.min, objectSizeRange.max);
PrintWriter pw = new PrintWriter(System.out, true);
workloadRuns.add(
convert(
executorService.submit(
new W1R3(storageClient, workers, api, pw, objectSize, tempDir, bucket))));
}
ApiExceptions.callAndTranslateApiException(ApiFutures.allAsList(workloadRuns));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,54 +18,69 @@

import com.google.cloud.storage.Blob;
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.DataGenerator;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.TmpFile;
import java.io.PrintWriter;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.Callable;

final class Workload1 implements Callable<String> {
private final TmpFile file;
private final BlobInfo blob;
final class W1R3 implements Callable<String> {

private final Storage storage;
private final int workers;
private final String api;
private final PrintWriter printWriter;
private final int objectSize;
private final Path tempDirectory;
private final String bucketName;

Workload1(TmpFile file, BlobInfo blob, Storage storage, int workers, String api) {
this.file = file;
this.blob = blob;
W1R3(
Storage storage,
int workers,
String api,
PrintWriter printWriter,
int objectSize,
Path tempDirectory,
String bucketName) {
this.storage = storage;
this.workers = workers;
this.api = api;
this.printWriter = printWriter;
this.objectSize = objectSize;
this.tempDirectory = tempDirectory;
this.bucketName = bucketName;
}

@Override
public String call() throws Exception {
Clock clock = Clock.systemDefaultZone();
// Create the file to be uploaded and fill it with data
TmpFile file = DataGenerator.base64Characters().tempFile(tempDirectory, objectSize);
BlobInfo blob = BlobInfo.newBuilder(bucketName, file.toString()).build();

// Get the start time
Clock clock = Clock.systemDefaultZone();
Instant startTime = clock.instant();
Blob created = storage.createFrom(blob, file.getPath());
Instant endTime = clock.instant();
Duration elapsedTimeUpload = Duration.between(startTime, endTime);
System.out.println(
printWriter.println(
generateCloudMonitoringResult(
"WRITE",
StorageSharedBenchmarkingUtils.calculateThroughput(
created.getSize().longValue(), elapsedTimeUpload),
created)
.toString());
Path tempDir = Paths.get(System.getProperty("java.io.tmpdir"));
for (int i = 0; i <= StorageSharedBenchmarkingUtils.DEFAULT_NUMBER_OF_READS; i++) {
TmpFile dest = TmpFile.of(tempDir, "prefix", "bin");
TmpFile dest = TmpFile.of(tempDirectory, "prefix", "bin");
startTime = clock.instant();
storage.downloadTo(created.getBlobId(), dest.getPath());
endTime = clock.instant();
Duration elapsedTimeDownload = Duration.between(startTime, endTime);
System.out.println(
printWriter.println(
generateCloudMonitoringResult(
"READ[" + i + "]",
StorageSharedBenchmarkingUtils.calculateThroughput(
Expand Down

0 comments on commit d985976

Please sign in to comment.