Search in sources :

Example 6 with Message

use of com.twosigma.beakerx.message.Message in project beakerx by twosigma.

the class MessageCreator method buildReply.

private static Message buildReply(Message message, SimpleEvaluationObject seo) {
    // Send the REPLY to the original message. This is NOT the result of
    // executing the cell. This is the equivalent of 'exit 0' or 'exit 1'
    // at the end of a shell script.
    Message reply = buildReplyWithoutStatus(message, seo.getExecutionCount());
    if (EvaluationStatus.FINISHED == seo.getStatus()) {
        reply.getMetadata().put("status", "ok");
        reply.getContent().put("status", "ok");
        reply.getContent().put("user_expressions", new HashMap<>());
    } else if (EvaluationStatus.ERROR == seo.getStatus()) {
        reply.getMetadata().put("status", "error");
        reply.getContent().put("status", "error");
    }
    return reply;
}
Also used : Message(com.twosigma.beakerx.message.Message)

Example 7 with Message

use of com.twosigma.beakerx.message.Message 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 8 with Message

use of com.twosigma.beakerx.message.Message 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 9 with Message

use of com.twosigma.beakerx.message.Message in project beakerx by twosigma.

the class CommOpenHandler method handleMsg.

private void handleMsg(Message message) {
    logger.debug("Processing CommOpenHandler");
    Message reply = new Message();
    HashMap<String, Serializable> map = new HashMap<>(6);
    Map<String, Serializable> commMap = message.getContent();
    Comm newComm = null;
    if (isValidMessage(commMap)) {
        newComm = readComm(commMap);
        reply.setHeader(new Header(COMM_OPEN, message.getHeader().getSession()));
        map.put(COMM_ID, newComm.getCommId());
        map.put(TARGET_NAME, newComm.getTargetName());
        map.put(DATA, new HashMap<>());
        map.put(TARGET_MODULE, newComm.getTargetModule());
    } else {
        reply.setHeader(new Header(COMM_CLOSE, message.getHeader().getSession()));
        map.put(DATA, new HashMap<>());
    }
    if (newComm != null) {
        logger.debug("Comm opened, target name = " + newComm.getTargetName());
        for (Handler<Message> handler : getKernelControlChanelHandlers(newComm.getTargetName())) {
            newComm.addMsgCallbackList(handler);
        }
        kernel.addComm(newComm.getCommId(), newComm);
    }
    reply.setContent(map);
    reply.setParentHeader(message.getHeader());
    reply.setIdentities(message.getIdentities());
    send(reply);
}
Also used : Serializable(java.io.Serializable) Message(com.twosigma.beakerx.message.Message) Header(com.twosigma.beakerx.message.Header) HashMap(java.util.HashMap) Comm(com.twosigma.beakerx.kernel.comm.Comm)

Example 10 with Message

use of com.twosigma.beakerx.message.Message in project beakerx by twosigma.

the class TimeItCellModeMagicCommand method execute.

@Override
public MagicCommandOutcomeItem execute(MagicCommandExecutionParam param) {
    Code code = param.getCode();
    Message message = param.getCode().getMessage();
    int executionCount = param.getExecutionCount();
    try {
        return timeIt(buildTimeItOption(code), param.getCommandCodeBlock(), message, executionCount, param.isShowResult());
    } catch (IllegalArgumentException e) {
        return new MagicCommandOutput(MagicCommandOutput.Status.ERROR, e.getMessage());
    }
}
Also used : MagicCommandOutput(com.twosigma.beakerx.kernel.magic.command.outcome.MagicCommandOutput) Message(com.twosigma.beakerx.message.Message) Code(com.twosigma.beakerx.kernel.Code)

Aggregations

Message (com.twosigma.beakerx.message.Message)270 Test (org.junit.Test)170 KernelTest (com.twosigma.beakerx.KernelTest)121 EvaluatorResultTestWatcher.waitForIdleMessage (com.twosigma.beakerx.evaluator.EvaluatorResultTestWatcher.waitForIdleMessage)64 EvaluatorTest (com.twosigma.beakerx.evaluator.EvaluatorTest)57 Code (com.twosigma.beakerx.kernel.Code)55 Serializable (java.io.Serializable)36 MessageFactoryTest.getExecuteRequestMessage (com.twosigma.beakerx.MessageFactoryTest.getExecuteRequestMessage)35 EvaluatorResultTestWatcher.waitForErrorMessage (com.twosigma.beakerx.evaluator.EvaluatorResultTestWatcher.waitForErrorMessage)28 MessageTest (com.twosigma.beakerx.message.MessageTest)27 Map (java.util.Map)26 HashMap (java.util.HashMap)24 Header (com.twosigma.beakerx.message.Header)23 MessageAssertions.verifyExecuteReplyMessage (com.twosigma.MessageAssertions.verifyExecuteReplyMessage)19 EvaluatorResultTestWatcher.waitForSentMessage (com.twosigma.beakerx.evaluator.EvaluatorResultTestWatcher.waitForSentMessage)19 PlainCode (com.twosigma.beakerx.kernel.PlainCode)14 KernelExecutionTest (com.twosigma.beakerx.KernelExecutionTest)13 EvaluatorResultTestWatcher.waitForUpdateMessage (com.twosigma.beakerx.evaluator.EvaluatorResultTestWatcher.waitForUpdateMessage)13 LinkedHashMap (java.util.LinkedHashMap)12 SimpleEvaluationObject (com.twosigma.beakerx.jvm.object.SimpleEvaluationObject)11