Search in sources :

Example 1 with SimpleEvaluationObject

use of com.twosigma.beakerx.jvm.object.SimpleEvaluationObject 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.");
    }
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) MagicCommandOutput(com.twosigma.beakerx.kernel.magic.command.outcome.MagicCommandOutput) TryResult(com.twosigma.beakerx.TryResult) PlainCode.createSimpleEvaluationObject(com.twosigma.beakerx.kernel.PlainCode.createSimpleEvaluationObject) SimpleEvaluationObject(com.twosigma.beakerx.jvm.object.SimpleEvaluationObject) CompletableFuture(java.util.concurrent.CompletableFuture) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with SimpleEvaluationObject

use of com.twosigma.beakerx.jvm.object.SimpleEvaluationObject 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");
}
Also used : IntStream(java.util.stream.IntStream) StrTokenizer(org.apache.commons.text.StrTokenizer) CommandLineParser(org.apache.commons.cli.CommandLineParser) Options(org.apache.commons.cli.Options) PlainCode.createSimpleEvaluationObject(com.twosigma.beakerx.kernel.PlainCode.createSimpleEvaluationObject) CompletableFuture(java.util.concurrent.CompletableFuture) ThreadMXBean(java.lang.management.ThreadMXBean) TryResult(com.twosigma.beakerx.TryResult) Message(com.twosigma.beakerx.message.Message) ArrayList(java.util.ArrayList) MagicCommandFunctionality(com.twosigma.beakerx.kernel.magic.command.MagicCommandFunctionality) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) ParseException(org.apache.commons.cli.ParseException) KernelFunctionality(com.twosigma.beakerx.kernel.KernelFunctionality) CommandLine(org.apache.commons.cli.CommandLine) MagicCommandOutput(com.twosigma.beakerx.kernel.magic.command.outcome.MagicCommandOutput) ManagementFactory(java.lang.management.ManagementFactory) SimpleEvaluationObject(com.twosigma.beakerx.jvm.object.SimpleEvaluationObject) PosixParser(org.apache.commons.cli.PosixParser) Code(com.twosigma.beakerx.kernel.Code) MagicCommandOutput(com.twosigma.beakerx.kernel.magic.command.outcome.MagicCommandOutput) TryResult(com.twosigma.beakerx.TryResult) PlainCode.createSimpleEvaluationObject(com.twosigma.beakerx.kernel.PlainCode.createSimpleEvaluationObject) SimpleEvaluationObject(com.twosigma.beakerx.jvm.object.SimpleEvaluationObject) ArrayList(java.util.ArrayList) CompletableFuture(java.util.concurrent.CompletableFuture) ExecutionException(java.util.concurrent.ExecutionException)

Example 3 with SimpleEvaluationObject

use of com.twosigma.beakerx.jvm.object.SimpleEvaluationObject in project beakerx by twosigma.

the class TimeMagicCommand method getBestNumber.

private int getBestNumber(String codeToExecute, boolean showResult) {
    for (int value = 0; value < 10; ) {
        Double numberOfExecution = Math.pow(10, value);
        CompletableFuture<Boolean> keepLooking = new CompletableFuture<>();
        Long startTime = System.nanoTime();
        IntStream.range(0, numberOfExecution.intValue()).forEach(indexOfExecution -> {
            SimpleEvaluationObject simpleEvaluationObject = createSimpleEvaluationObject(codeToExecute, kernel, new Message(), 0);
            if (!showResult) {
                simpleEvaluationObject.noResult();
            }
            kernel.executeCode(codeToExecute, simpleEvaluationObject);
            if (numberOfExecution.intValue() - 1 == indexOfExecution) {
                if (TimeUnit.NANOSECONDS.toSeconds(System.nanoTime() - startTime) > 0.2) {
                    keepLooking.complete(false);
                } else {
                    keepLooking.complete(true);
                }
            }
        });
        try {
            if (keepLooking.get()) {
                value++;
            } else {
                return numberOfExecution.intValue();
            }
        } catch (ExecutionException | InterruptedException e) {
            throw new IllegalStateException("Cannot create best number of execution.");
        }
    }
    throw new IllegalStateException("Cannot create best number of execution.");
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Message(com.twosigma.beakerx.message.Message) PlainCode.createSimpleEvaluationObject(com.twosigma.beakerx.kernel.PlainCode.createSimpleEvaluationObject) SimpleEvaluationObject(com.twosigma.beakerx.jvm.object.SimpleEvaluationObject) ExecutionException(java.util.concurrent.ExecutionException)

Example 4 with SimpleEvaluationObject

use of com.twosigma.beakerx.jvm.object.SimpleEvaluationObject 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);
}
Also used : TryResult(com.twosigma.beakerx.TryResult) SimpleEvaluationObject(com.twosigma.beakerx.jvm.object.SimpleEvaluationObject)

Example 5 with SimpleEvaluationObject

use of com.twosigma.beakerx.jvm.object.SimpleEvaluationObject in project beakerx by twosigma.

the class PlainCode method createSimpleEvaluationObject.

public static SimpleEvaluationObject createSimpleEvaluationObject(String code, KernelFunctionality kernel, Message message, int executionCount) {
    SimpleEvaluationObject seo = new SimpleEvaluationObject(code);
    seo.setJupyterMessage(message);
    seo.setExecutionCount(executionCount);
    seo.addObserver(kernel.getExecutionResultSender());
    return seo;
}
Also used : SimpleEvaluationObject(com.twosigma.beakerx.jvm.object.SimpleEvaluationObject)

Aggregations

SimpleEvaluationObject (com.twosigma.beakerx.jvm.object.SimpleEvaluationObject)57 TryResult (com.twosigma.beakerx.TryResult)37 Test (org.junit.Test)33 Message (com.twosigma.beakerx.message.Message)9 KernelTest (com.twosigma.beakerx.KernelTest)7 PlainCode.createSimpleEvaluationObject (com.twosigma.beakerx.kernel.PlainCode.createSimpleEvaluationObject)5 HashMap (java.util.HashMap)4 EvaluatorParameters (com.twosigma.beakerx.kernel.EvaluatorParameters)3 MagicCommandOutput (com.twosigma.beakerx.kernel.magic.command.outcome.MagicCommandOutput)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 ExecutionException (java.util.concurrent.ExecutionException)3 Plot (com.twosigma.beakerx.chart.xychart.Plot)2 MagicCommandOutcomeItem (com.twosigma.beakerx.kernel.magic.command.outcome.MagicCommandOutcomeItem)2 MessageHolder (com.twosigma.beakerx.kernel.msg.MessageHolder)2 ThreadMXBean (java.lang.management.ThreadMXBean)2 Before (org.junit.Before)2 AutocompleteResult (com.twosigma.beakerx.autocomplete.AutocompleteResult)1 EvaluatorTest (com.twosigma.beakerx.evaluator.EvaluatorTest)1 Code (com.twosigma.beakerx.kernel.Code)1 KernelFunctionality (com.twosigma.beakerx.kernel.KernelFunctionality)1