use of com.twosigma.beakerx.TryResult in project beakerx by twosigma.
the class TimeMagicCommand method time.
public MagicCommandOutput time(String codeToExecute, Message message, int executionCount, boolean showResult) {
CompletableFuture<TimeMeasureData> compileTime = new CompletableFuture<>();
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
long currentThreadId = Thread.currentThread().getId();
Long startWallTime = System.nanoTime();
Long startCpuTotalTime = threadMXBean.getCurrentThreadCpuTime();
Long startUserTime = threadMXBean.getCurrentThreadUserTime();
SimpleEvaluationObject simpleEvaluationObject = createSimpleEvaluationObject(codeToExecute, kernel, message, executionCount);
if (!showResult) {
simpleEvaluationObject.noResult();
}
TryResult either = kernel.executeCode(codeToExecute, simpleEvaluationObject);
Long endWallTime = System.nanoTime();
Long endCpuTotalTime = threadMXBean.getThreadCpuTime(currentThreadId);
Long endUserTime = threadMXBean.getThreadUserTime(currentThreadId);
compileTime.complete(new TimeMeasureData(endCpuTotalTime - startCpuTotalTime, endUserTime - startUserTime, endWallTime - startWallTime));
String messageInfo = "CPU times: user %s, sys: %s, total: %s \nWall Time: %s\n";
try {
TimeMeasureData timeMeasuredData = compileTime.get();
return new MagicCommandOutput(MagicCommandOutput.Status.OK, String.format(messageInfo, format(timeMeasuredData.getCpuUserTime()), format(timeMeasuredData.getCpuTotalTime() - timeMeasuredData.getCpuUserTime()), format(timeMeasuredData.getCpuTotalTime()), format(timeMeasuredData.getWallTime())), either, simpleEvaluationObject);
} catch (InterruptedException | ExecutionException e) {
return new MagicCommandOutput(MagicCommandOutput.Status.ERROR, "There occurs problem during measuring time for your statement.");
}
}
use of com.twosigma.beakerx.TryResult in project beakerx by twosigma.
the class TimeMagicCommand method timeIt.
protected MagicCommandOutput timeIt(TimeItOption timeItOption, String codeToExecute, Message message, int executionCount, boolean showResult) {
String output = "%s ± %s per loop (mean ± std. dev. of %d run, %d loop each)";
if (timeItOption.getNumber() < 0) {
return new MagicCommandOutput(MagicCommandOutput.Status.ERROR, "Number of execution must be bigger then 0");
}
int number = timeItOption.getNumber() == 0 ? getBestNumber(codeToExecute, showResult) : timeItOption.getNumber();
if (timeItOption.getRepeat() == 0) {
return new MagicCommandOutput(MagicCommandOutput.Status.ERROR, "Repeat value must be bigger then 0");
}
SimpleEvaluationObject seo = createSimpleEvaluationObject(codeToExecute, kernel, message, executionCount);
seo.noResult();
TryResult either = kernel.executeCode(codeToExecute, seo);
try {
if (either.isError()) {
return new MagicCommandOutput(MagicCommandOutput.Status.ERROR, "Please correct your statement");
}
List<Long> allRuns = new ArrayList<>();
List<Long> timings = new ArrayList<>();
CompletableFuture<Boolean> isReady = new CompletableFuture<>();
IntStream.range(0, timeItOption.getRepeat()).forEach(repeatIter -> {
IntStream.range(0, number).forEach(numberIter -> {
SimpleEvaluationObject seo2 = createSimpleEvaluationObject(codeToExecute, kernel, message, executionCount);
seo2.noResult();
Long startOfEvaluationInNanoseconds = System.nanoTime();
TryResult result = kernel.executeCode(codeToExecute, seo2);
Long endOfEvaluationInNanoseconds = System.nanoTime();
allRuns.add(endOfEvaluationInNanoseconds - startOfEvaluationInNanoseconds);
if (repeatIter == timeItOption.getRepeat() - 1 && numberIter == number - 1) {
isReady.complete(true);
}
});
});
if (isReady.get()) {
allRuns.forEach(run -> timings.add(run / number));
// calculating average
long average = timings.stream().reduce((aLong, aLong2) -> aLong + aLong2).orElse(0L) / timings.size();
double stdev = Math.pow(timings.stream().map(currentValue -> Math.pow(currentValue - average, 2)).reduce((aDouble, aDouble2) -> aDouble + aDouble2).orElse(0.0) / timings.size(), 0.5);
if (timeItOption.getQuietMode()) {
output = "";
} else {
output = String.format(output, format(average), format((long) stdev), timeItOption.getRepeat(), number);
}
return new MagicCommandOutput(MagicCommandOutput.Status.OK, output);
}
} catch (InterruptedException | ExecutionException e) {
return new MagicCommandOutput(MagicCommandOutput.Status.ERROR, "There occurs problem with " + e.getMessage());
}
return new MagicCommandOutput(MagicCommandOutput.Status.ERROR, "There occurs problem with timeIt operations");
}
use of com.twosigma.beakerx.TryResult in project beakerx by twosigma.
the class PlainCode method executeLastFrame.
@Override
public void executeLastFrame(Code code, KernelFunctionality kernel, Message message, int executionCount) {
SimpleEvaluationObject seo = createSimpleEvaluationObject(this.plainCode, kernel, message, executionCount);
TryResult either = kernel.executeCode(this.plainCode, seo);
handleResult(seo, either);
}
use of com.twosigma.beakerx.TryResult in project beakerx by twosigma.
the class PlainCode method executeFrame.
@Override
public void executeFrame(Code code, KernelFunctionality kernel, Message message, int executionCount) {
SimpleEvaluationObject seo = createSimpleEvaluationObject(this.plainCode, kernel, message, executionCount);
seo.noResult();
TryResult either = kernel.executeCode(this.plainCode, seo);
handleResult(seo, either);
}
use of com.twosigma.beakerx.TryResult in project beakerx by twosigma.
the class ClojureCodeRunner method call.
@Override
public TryResult call() throws Exception {
ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(clojureEvaluator.getClassLoader());
TryResult either;
try {
theOutput.setOutputHandler();
InternalVariable.setValue(theOutput);
Object o = clojureEvaluator.runCode(theCode);
try {
checkingOfCorruptedClojureObjects(o);
either = TryResult.createResult(o);
} catch (Exception e) {
either = TryResult.createError("Object: " + o.getClass() + ", value cannot be displayed due to following error: " + e.getMessage());
}
} catch (Throwable e) {
if (e instanceof InterruptedException || e instanceof InvocationTargetException || e instanceof ThreadDeath) {
either = TryResult.createError(INTERUPTED_MSG);
} else {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
if (null != e.getCause()) {
e.getCause().printStackTrace(pw);
} else {
e.printStackTrace(pw);
}
either = TryResult.createError(sw.toString());
}
} finally {
theOutput.setOutputHandler();
Thread.currentThread().setContextClassLoader(oldLoader);
}
return either;
}
Aggregations