-
Notifications
You must be signed in to change notification settings - Fork 70
/
StorageSharedBenchmarkingCli.java
159 lines (139 loc) · 5.33 KB
/
StorageSharedBenchmarkingCli.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.storage.benchmarking;
import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutures;
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.Storage;
import com.google.cloud.storage.StorageOptions;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import java.io.PrintWriter;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Executors;
import java.util.regex.Pattern;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
@Command(name = "ssb")
public final class StorageSharedBenchmarkingCli implements Runnable {
// TODO: check what input validation is needed for option values.
@Option(names = "-project", description = "GCP Project Identifier", required = true)
String project;
@Option(names = "-bucket", description = "Name of the bucket to use", required = true)
String bucket;
@Option(names = "-samples", defaultValue = "8000", description = "Number of samples to report")
int samples;
@Option(
names = "-workers",
defaultValue = "16",
description = "Number of workers to run in parallel for the workload")
int workers;
@Option(names = "-api", defaultValue = "JSON", description = "API to use")
String api;
@Option(
names = "-object_size",
defaultValue = "1048576..1048576",
description =
"any positive integer, or an inclusive range such as min..max where min and max are positive integers")
String objectSize;
@Option(
names = "-output_type",
defaultValue = "cloud-monitoring",
description = "Output results format")
String outputType;
@Option(
names = "-test_type",
description = "Specify which workload the cli should run",
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));
}
@Override
public void run() {
switch (testType) {
case "w1r3":
runWorkload1();
break;
default:
throw new IllegalStateException("Specify a workload to run");
}
}
private void runWorkload1() {
RetrySettings retrySettings = StorageOptions.getDefaultRetrySettings().toBuilder().build();
StorageOptions retryStorageOptions =
StorageOptions.newBuilder().setProjectId(project).setRetrySettings(retrySettings).build();
Storage storageClient = retryStorageOptions.getService();
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++) {
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));
}
public static int getRandomInt(int min, int max) {
if (min == max) return min;
Random random = new Random();
return random.nextInt((max - min) + 1) + min;
}
private static <T> ApiFuture<T> convert(ListenableFuture<T> lf) {
return new ListenableFutureToApiFuture<>(lf);
}
private static final class Range {
private final int min;
private final int max;
private Range(int min, int max) {
this.min = min;
this.max = max;
}
public static Range of(int min, int max) {
return new Range(min, max);
}
// Takes an object size range of format min..max and creates a range object
public static Range of(String range) {
Pattern p = Pattern.compile("\\.\\.");
String[] splitRangeVals = p.split(range);
if (splitRangeVals.length == 2) {
String min = splitRangeVals[0];
String max = splitRangeVals[1];
return of(Integer.parseInt(min), Integer.parseInt(max));
}
throw new IllegalStateException("Expected a size range of format min..max, but got " + range);
}
}
}