Skip to content

Commit d985976

Browse files
authored
feat: Follow-up CLI Improvements (#2184)
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
1 parent 5302201 commit d985976

2 files changed

Lines changed: 41 additions & 28 deletions

File tree

storage-shared-benchmarking/src/main/java/com/google/cloud/storage/benchmarking/StorageSharedBenchmarkingCli.java

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,12 @@
2121
import com.google.api.core.ListenableFutureToApiFuture;
2222
import com.google.api.gax.retrying.RetrySettings;
2323
import com.google.api.gax.rpc.ApiExceptions;
24-
import com.google.cloud.storage.BlobInfo;
25-
import com.google.cloud.storage.DataGenerator;
2624
import com.google.cloud.storage.Storage;
2725
import com.google.cloud.storage.StorageOptions;
28-
import com.google.cloud.storage.TmpFile;
2926
import com.google.common.util.concurrent.ListenableFuture;
3027
import com.google.common.util.concurrent.ListeningExecutorService;
3128
import com.google.common.util.concurrent.MoreExecutors;
32-
import java.io.IOException;
29+
import java.io.PrintWriter;
3330
import java.nio.file.Path;
3431
import java.nio.file.Paths;
3532
import java.util.ArrayList;
@@ -81,6 +78,12 @@ public final class StorageSharedBenchmarkingCli implements Runnable {
8178
required = true)
8279
String testType;
8380

81+
@Option(
82+
names = "-temp_dir_location",
83+
defaultValue = "/tmp",
84+
description = "Specify the path where the temporary directory should be located")
85+
String tempDirLocation;
86+
8487
public static void main(String[] args) {
8588
CommandLine cmd = new CommandLine(StorageSharedBenchmarkingCli.class);
8689
System.exit(cmd.execute(args));
@@ -103,23 +106,18 @@ private void runWorkload1() {
103106
StorageOptions retryStorageOptions =
104107
StorageOptions.newBuilder().setProjectId(project).setRetrySettings(retrySettings).build();
105108
Storage storageClient = retryStorageOptions.getService();
106-
Path tempDir = Paths.get(System.getProperty("java.io.tmpdir"));
109+
Path tempDir = Paths.get(tempDirLocation);
107110
ListeningExecutorService executorService =
108111
MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(workers));
109112
List<ApiFuture<String>> workloadRuns = new ArrayList<>();
110113
Range objectSizeRange = Range.of(objectSize);
111114
for (int i = 0; i < samples; i++) {
112-
try {
113-
TmpFile file =
114-
DataGenerator.base64Characters()
115-
.tempFile(tempDir, getRandomInt(objectSizeRange.min, objectSizeRange.max));
116-
BlobInfo blob = BlobInfo.newBuilder(bucket, file.toString()).build();
117-
workloadRuns.add(
118-
convert(
119-
executorService.submit(new Workload1(file, blob, storageClient, workers, api))));
120-
} catch (IOException e) {
121-
throw new RuntimeException(e);
122-
}
115+
int objectSize = getRandomInt(objectSizeRange.min, objectSizeRange.max);
116+
PrintWriter pw = new PrintWriter(System.out, true);
117+
workloadRuns.add(
118+
convert(
119+
executorService.submit(
120+
new W1R3(storageClient, workers, api, pw, objectSize, tempDir, bucket))));
123121
}
124122
ApiExceptions.callAndTranslateApiException(ApiFutures.allAsList(workloadRuns));
125123
}

storage-shared-benchmarking/src/main/java/com/google/cloud/storage/benchmarking/Workload1.java renamed to storage-shared-benchmarking/src/main/java/com/google/cloud/storage/benchmarking/W1R3.java

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,54 +18,69 @@
1818

1919
import com.google.cloud.storage.Blob;
2020
import com.google.cloud.storage.BlobInfo;
21+
import com.google.cloud.storage.DataGenerator;
2122
import com.google.cloud.storage.Storage;
2223
import com.google.cloud.storage.TmpFile;
24+
import java.io.PrintWriter;
2325
import java.nio.file.Path;
24-
import java.nio.file.Paths;
2526
import java.time.Clock;
2627
import java.time.Duration;
2728
import java.time.Instant;
2829
import java.util.concurrent.Callable;
2930

30-
final class Workload1 implements Callable<String> {
31-
private final TmpFile file;
32-
private final BlobInfo blob;
31+
final class W1R3 implements Callable<String> {
32+
3333
private final Storage storage;
3434
private final int workers;
3535
private final String api;
36+
private final PrintWriter printWriter;
37+
private final int objectSize;
38+
private final Path tempDirectory;
39+
private final String bucketName;
3640

37-
Workload1(TmpFile file, BlobInfo blob, Storage storage, int workers, String api) {
38-
this.file = file;
39-
this.blob = blob;
41+
W1R3(
42+
Storage storage,
43+
int workers,
44+
String api,
45+
PrintWriter printWriter,
46+
int objectSize,
47+
Path tempDirectory,
48+
String bucketName) {
4049
this.storage = storage;
4150
this.workers = workers;
4251
this.api = api;
52+
this.printWriter = printWriter;
53+
this.objectSize = objectSize;
54+
this.tempDirectory = tempDirectory;
55+
this.bucketName = bucketName;
4356
}
4457

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

4964
// Get the start time
65+
Clock clock = Clock.systemDefaultZone();
5066
Instant startTime = clock.instant();
5167
Blob created = storage.createFrom(blob, file.getPath());
5268
Instant endTime = clock.instant();
5369
Duration elapsedTimeUpload = Duration.between(startTime, endTime);
54-
System.out.println(
70+
printWriter.println(
5571
generateCloudMonitoringResult(
5672
"WRITE",
5773
StorageSharedBenchmarkingUtils.calculateThroughput(
5874
created.getSize().longValue(), elapsedTimeUpload),
5975
created)
6076
.toString());
61-
Path tempDir = Paths.get(System.getProperty("java.io.tmpdir"));
6277
for (int i = 0; i <= StorageSharedBenchmarkingUtils.DEFAULT_NUMBER_OF_READS; i++) {
63-
TmpFile dest = TmpFile.of(tempDir, "prefix", "bin");
78+
TmpFile dest = TmpFile.of(tempDirectory, "prefix", "bin");
6479
startTime = clock.instant();
6580
storage.downloadTo(created.getBlobId(), dest.getPath());
6681
endTime = clock.instant();
6782
Duration elapsedTimeDownload = Duration.between(startTime, endTime);
68-
System.out.println(
83+
printWriter.println(
6984
generateCloudMonitoringResult(
7085
"READ[" + i + "]",
7186
StorageSharedBenchmarkingUtils.calculateThroughput(

0 commit comments

Comments
 (0)