-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWriteFileThroughputBenchmark.java
More file actions
182 lines (150 loc) · 6.34 KB
/
WriteFileThroughputBenchmark.java
File metadata and controls
182 lines (150 loc) · 6.34 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package org.apache.hadoop.ozone.freon;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hdds.cli.HddsVersionProvider;
import org.apache.hadoop.hdds.conf.OzoneConfiguration;
import org.slf4j.LoggerFactory;
import picocli.CommandLine;
import picocli.CommandLine.Option;
import org.slf4j.Logger;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URI;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@CommandLine.Command(name = "write-throughput-benchmark",
aliases = "wtb",
description = "Benchmark for creating a file",
versionProvider = HddsVersionProvider.class,
mixinStandardHelpOptions = true,
showDefaultValues = true)
public class WriteFileThroughputBenchmark extends BaseFreonGenerator implements Callable<Void>{
@Option(names = {"-o"},
description = "Ozone filesystem path",
defaultValue = "o3fs://bucket1.vol1")
private String rootPath;
@Option(names = {"-s", "--size"},
description = "Size of each generated files (in GB)",
defaultValue = "1")
private long fileSize;
@Option(names = {"-b", "--block"},
description = "Specify the Block Size in MB",
defaultValue = "128")
private long blockSize;
@Option(names = {"-i", "--buffer"},
description = "Size of buffer used store the generated key content",
defaultValue = "10240")
private int bufferSize;
@Option(names = {"-th", "--throttle"},
description = "Specify the Delay in Input/Output",
defaultValue = "0")
private int throttle;
@Option(names = {"-re", "--replication"},
description = "Specify the Replication factor",
defaultValue = "3")
private short replication;
@Option(names= {"--sync"},
description = "Optionally Issue hsync after every write Cannot be used with hflush",
defaultValue = "false"
)
private boolean hSync;
@Option(names= {"--flush"},
description = "Optionally Issue hsync after every write Cannot be used with hflush",
defaultValue = "false"
)
private boolean hFlush;
// For Generating the content of the files
private ContentGenerator contentGenerator;
// For Creating the required configurations for the file system
private OzoneConfiguration configuration;
private URI uri;
private boolean isThrottled;
public static final Logger LOG =
LoggerFactory.getLogger(WriteFileThroughputBenchmark.class);
// Checking whether an output directory is created inside the bucket
private static void ensureOutputDirExists(FileSystem fs, Path outputDir)
throws IOException {
if (fs.exists(outputDir)) {
LOG.error("No Such Output Directory exists : {}", outputDir);
System.exit(1);
}
}
public Void call() throws Exception{
// Initialize the configuration variable
configuration = createOzoneConfiguration();
//Constructs a URI by parsing the given string rootPath
// We Initialize the uri variable with the path
uri = URI.create(rootPath);
LOG.info("NumFiles=" + getTestNo());
LOG.info("Total FileSize=" + fileSize);
LOG.info("BlockSize=" + blockSize);
LOG.info("BufferSize=" + bufferSize);
LOG.info("Replication=" + replication);
LOG.info("Threads=" + getThreadNo());
LOG.info("URI Scheme Used=" + uri.getScheme());
if(hSync){
LOG.info("Hsync after every write= True");
}
else if(hFlush){
LOG.info("Hflush after every write= True");
}
// Create an object which accesses the filesystem API
FileSystem fileSystem = createFS();
// Creating a thread pool of 10 threads
ExecutorService service = Executors.newFixedThreadPool(10);
// Code for handling the throttle delay
if(throttle > 0) isThrottled = true;
final byte[] data = new byte[bufferSize];
final long expectedIoTimeNs =
(isThrottled ? (((long) data.length * 1_000_000_000) / throttle)
: 0);
for (int i=0;i<10;i++){
service.execute(new Runnable() {
// Override the run method
public void run()
{
try {
createFile(fileSystem,data,expectedIoTimeNs);
} catch (Exception e) {
LOG.info("Stack Trace: {0}", e);
}
}
});
}
service.shutdown();
fileSystem.close();
return null;
}
private void createFile(FileSystem fileSystem, byte[] data, long expectedIoTimeNs) throws Exception {
// generateObjectName is taken from the BaseFreonGenerator class
// file parameter will have the path as well as the name of the file to be written
Path file = new Path(rootPath + "/" + generateObjectName(0));
// Checks if output directory is available
ensureOutputDirExists(fileSystem,file);
// Initialize the size of the file to be written
long filesizeinBytes = fileSize*1_000_000_000;
contentGenerator = new ContentGenerator(filesizeinBytes, bufferSize, hSync, hFlush);
final long ioStartTimeNs = (isThrottled ? System.nanoTime() : 0);
FSDataOutputStream outputStream = fileSystem.create(file,false,bufferSize,replication,blockSize);
contentGenerator.write(outputStream);
final long ioEndTimeNs = (isThrottled ? System.nanoTime() : 0);
enforceThrottle(ioEndTimeNs - ioStartTimeNs, expectedIoTimeNs);
}
private FileSystem createFS() {
try {
return FileSystem.get(uri, configuration);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
static void enforceThrottle(long ioTimeNs, long expectedIoTimeNs)
throws InterruptedException {
if (ioTimeNs < expectedIoTimeNs) {
// The IO completed too fast, so sleep for some time.
long sleepTimeNs = expectedIoTimeNs - ioTimeNs;
Thread.sleep(sleepTimeNs / 1_000_000, (int) (sleepTimeNs % 1_000_000));
}
}
}