[Java] Avoid unnecessary memory copy and addd a benchmark (#4611)

This commit is contained in:
Hao Chen
2019-04-14 00:17:04 +08:00
committed by GitHub
parent 0bfb0d2c29
commit d52b080081
6 changed files with 68 additions and 16 deletions
@@ -0,0 +1,48 @@
package org.ray.api.benchmark;
import org.ray.api.Ray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MicroBenchmarks {
private static final Logger LOGGER = LoggerFactory.getLogger(MicroBenchmarks.class);
public static Object simpleFunction() {
return null;
}
private static void time(Runnable runnable, int numRepeats, String name) {
LOGGER.info("Benchmark \"{}\" started.", name);
final long start = System.nanoTime();
for (int i = 0; i < numRepeats; i++) {
runnable.run();
}
final long duration = System.nanoTime() - start;
LOGGER.info(
"Benchmark \"{}\" finished, repeated {} times, total duration {} ms, average duration {} ns.",
name, numRepeats, duration / 1_000_000, duration / numRepeats);
}
/**
* Benchmark task submission.
*
* Note, this benchmark is supposed to measure the elapased time in Java worker, we should disable
* submitting tasks to raylet in `raylet_client.cc` before running this benchmark.
*/
public static void benchmarkTaskSubmission() {
final int numRepeats = 1_000_000;
Ray.init();
try {
time(() -> {
Ray.call(MicroBenchmarks::simpleFunction);
}, numRepeats, "task submission");
} finally {
Ray.shutdown();
}
}
public static void main(String[] args) {
benchmarkTaskSubmission();
}
}