Skip to content

Commit

Permalink
Some improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
peisun1115 committed Apr 28, 2017
1 parent 0743fa1 commit a2827eb
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions examples/src/main/java/alluxio/cli/MiniBenchmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

import javax.annotation.concurrent.ThreadSafe;

Expand Down Expand Up @@ -127,19 +129,19 @@ public static void main(String[] args) throws Exception {

CommonUtils.warmUpLoop();

long start = System.nanoTime();

for (int i = 0; i < sIterations; ++i) {
final AtomicInteger count = new AtomicInteger(0);
final CyclicBarrier barrier = new CyclicBarrier(sConcurrency);
ExecutorService executorService = Executors.newFixedThreadPool(sConcurrency);
final AtomicLong runtime = new AtomicLong(0);
for (int j = 0; j < sConcurrency; ++j) {
switch (sType) {
case READ:
executorService.submit(new Runnable() {
@Override
public void run() {
try {
readFile(count.addAndGet(1));
readFile(barrier, runtime, count.addAndGet(1));
} catch (Exception e) {
LOG.error("Failed to read file.", e);
System.exit(-1);
Expand All @@ -152,7 +154,7 @@ public void run() {
@Override
public void run() {
try {
writeFile(count.addAndGet(1));
writeFile(barrier, runtime, count.addAndGet(1));
} catch (Exception e) {
LOG.error("Failed to write file.", e);
System.exit(-1);
Expand All @@ -166,9 +168,10 @@ public void run() {
}
executorService.shutdown();
Preconditions.checkState(executorService.awaitTermination(1, TimeUnit.HOURS));
double time = runtime.get() * 1.0 / sConcurrency / Constants.SECOND_NANO;
System.out.printf("Iteration: %d; Duration: %f seconds; Aggregated throughput: %f GB/second.%n",
i, time, sConcurrency * 1.0 * sFileSize / time / Constants.GB);
}
System.out.printf("Runtime: %f seconds.%n",
(System.nanoTime() - start) * 1.0 / Constants.SECOND_NANO);
}

/**
Expand All @@ -177,14 +180,18 @@ public void run() {
* @param count the count to determine the filename
* @throws Exception if it fails to read
*/
private static void readFile(int count) throws Exception {
private static void readFile(CyclicBarrier barrier, AtomicLong runTime, int count)
throws Exception {
FileSystem fileSystem = FileSystem.Factory.get();
byte[] buffer = new byte[(int) Math.min(sFileSize, 4 * Constants.MB)];

barrier.await();
long startTime = System.nanoTime();
try (FileInStream inStream = fileSystem.openFile(filename(count))) {
while (inStream.read(buffer) != -1) {
}
}
runTime.addAndGet(System.nanoTime() - startTime);
}

/**
Expand All @@ -193,7 +200,8 @@ private static void readFile(int count) throws Exception {
* @param count the count to determine the filename
* @throws Exception if it fails to write
*/
private static void writeFile(int count) throws Exception {
private static void writeFile(CyclicBarrier barrier, AtomicLong runtime, int count)
throws Exception {
FileSystem fileSystem = FileSystem.Factory.get();
byte[] buffer = new byte[(int) Math.min(sFileSize, 4 * Constants.MB)];
Arrays.fill(buffer, (byte) 'a');
Expand All @@ -203,13 +211,16 @@ private static void writeFile(int count) throws Exception {
fileSystem.delete(path);
}

barrier.await();
long startTime = System.nanoTime();
long bytesWritten = 0;
try (FileOutStream outStream = fileSystem.createFile(path)) {
while (bytesWritten < sFileSize) {
outStream.write(buffer, 0, (int) Math.min(buffer.length, sFileSize - bytesWritten));
bytesWritten += buffer.length;
}
}
runtime.addAndGet(System.nanoTime() - startTime);
}

private static AlluxioURI filename(int count) {
Expand Down

0 comments on commit a2827eb

Please sign in to comment.