Search in sources :

Example 1 with Scenario

use of io.nosqlbench.engine.core.script.Scenario in project nosqlbench by nosqlbench.

the class ScenariosResults method reportToLog.

public void reportToLog() {
    for (Map.Entry<Scenario, ScenarioResult> entry : this.scenarioResultMap.entrySet()) {
        Scenario scenario = entry.getKey();
        ScenarioResult oresult = entry.getValue();
        logger.info("results for scenario: " + scenario);
        if (oresult != null) {
            oresult.reportElapsedMillis();
        } else {
            logger.error(scenario.getScenarioName() + ": incomplete (missing result)");
        }
    }
}
Also used : LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Scenario(io.nosqlbench.engine.core.script.Scenario)

Example 2 with Scenario

use of io.nosqlbench.engine.core.script.Scenario in project nosqlbench by nosqlbench.

the class ScenarioTest method shouldLoadScriptText.

@Test
public void shouldLoadScriptText() {
    ScriptEnvBuffer buffer = new ScriptEnvBuffer();
    Scenario env = new Scenario("testing", Scenario.Engine.Graalvm, "stdout:300", Maturity.Any);
    env.addScriptText("print('loaded script environment...');\n");
    env.runScenario();
    assertThat(env.getIOLog().get().get(0)).contains("loaded script environment...");
}
Also used : ScriptEnvBuffer(io.nosqlbench.engine.api.scripting.ScriptEnvBuffer) Scenario(io.nosqlbench.engine.core.script.Scenario) Test(org.junit.jupiter.api.Test)

Example 3 with Scenario

use of io.nosqlbench.engine.core.script.Scenario in project nosqlbench by nosqlbench.

the class NBCLI method run.

public void run(String[] args) {
    // Initial logging config covers only command line parsing
    // We don't want anything to go to console here unless it is a real problem
    // as some integrations will depend on a stable and parsable program output
    // new LoggerConfig()
    // .setConsoleLevel(NBLogLevel.INFO.ERROR)
    // .setLogfileLevel(NBLogLevel.ERROR)
    // .activate();
    // logger = LogManager.getLogger("NBCLI");
    loggerConfig.setConsoleLevel(NBLogLevel.ERROR);
    NBCLIOptions globalOptions = new NBCLIOptions(args, NBCLIOptions.Mode.ParseGlobalsOnly);
    String sessionName = SessionNamer.format(globalOptions.getSessionName());
    loggerConfig.setSessionName(sessionName).setConsoleLevel(globalOptions.getConsoleLogLevel()).setConsolePattern(globalOptions.getConsoleLoggingPattern()).setLogfileLevel(globalOptions.getScenarioLogLevel()).setLogfilePattern(globalOptions.getLogfileLoggingPattern()).getLoggerLevelOverrides(globalOptions.getLogLevelOverrides()).setMaxLogs(globalOptions.getLogsMax()).setLogsDirectory(globalOptions.getLogsDirectory()).setAnsiEnabled(globalOptions.isEnableAnsi()).activate();
    ConfigurationFactory.setConfigurationFactory(loggerConfig);
    logger = LogManager.getLogger("NBCLI");
    loggerConfig.purgeOldFiles(LogManager.getLogger("SCENARIO"));
    logger.info("Configured scenario log at " + loggerConfig.getLogfileLocation());
    logger.debug("Scenario log started");
    // Global only processing
    if (args.length == 0) {
        System.out.println(loadHelpFile("commandline.md"));
        System.exit(0);
    }
    logger.info("Running NoSQLBench Version " + new VersionInfo().getVersion());
    logger.info("command-line: " + Arrays.stream(args).collect(Collectors.joining(" ")));
    logger.info("client-hardware: " + SystemId.getHostSummary());
    boolean dockerMetrics = globalOptions.wantsDockerMetrics();
    String dockerMetricsAt = globalOptions.wantsDockerMetricsAt();
    String reportGraphiteTo = globalOptions.wantsReportGraphiteTo();
    String annotatorsConfig = globalOptions.getAnnotatorsConfig();
    int mOpts = (dockerMetrics ? 1 : 0) + (dockerMetricsAt != null ? 1 : 0) + (reportGraphiteTo != null ? 1 : 0);
    if (mOpts > 1 && (reportGraphiteTo == null || annotatorsConfig == null)) {
        throw new BasicError("You have multiple conflicting options which attempt to set\n" + " the destination for metrics and annotations. Please select only one of\n" + " --docker-metrics, --docker-metrics-at <addr>, or other options like \n" + " --report-graphite-to <addr> and --annotators <config>\n" + " For more details, see run 'nb help docker-metrics'");
    }
    String metricsAddr = null;
    if (dockerMetrics) {
        // Setup docker stack for local docker metrics
        logger.info("Docker metrics is enabled. Docker must be installed for this to work");
        DockerMetricsManager dmh = new DockerMetricsManager();
        Map<String, String> dashboardOptions = Map.of(DockerMetricsManager.GRAFANA_TAG, globalOptions.getDockerGrafanaTag(), DockerMetricsManager.PROM_TAG, globalOptions.getDockerPromTag(), DockerMetricsManager.TSDB_RETENTION, String.valueOf(globalOptions.getDockerPromRetentionDays()));
        dmh.startMetrics(dashboardOptions);
        String warn = "Docker Containers are started, for grafana and prometheus, hit" + " these urls in your browser: http://<host>:3000 and http://<host>:9090";
        logger.warn(warn);
        metricsAddr = "localhost";
    } else if (dockerMetricsAt != null) {
        metricsAddr = dockerMetricsAt;
    }
    if (metricsAddr != null) {
        reportGraphiteTo = metricsAddr + ":9109";
        annotatorsConfig = "[{type:'log',level:'info'},{type:'grafana',baseurl:'http://" + metricsAddr + ":3000" + "/'," + "tags:'appname:nosqlbench',timeoutms:5000,onerror:'warn'}]";
    } else {
        annotatorsConfig = "[{type:'log',level:'info'}]";
    }
    if (args.length > 0 && args[0].toLowerCase().equals("export-docs")) {
        BundledMarkdownExporter.main(Arrays.copyOfRange(args, 1, args.length));
        System.exit(0);
    }
    if (args.length > 0 && args[0].toLowerCase().equals("virtdata")) {
        VirtDataMainApp.main(Arrays.copyOfRange(args, 1, args.length));
        System.exit(0);
    }
    if (args.length > 0 && args[0].toLowerCase().matches("docserver|appserver")) {
        NBWebServerApp.main(Arrays.copyOfRange(args, 1, args.length));
        System.exit(0);
    }
    if (args.length > 0 && args[0].toLowerCase().equals(MarkdownExporter.APP_NAME)) {
        MarkdownExporter.main(Arrays.copyOfRange(args, 1, args.length));
        System.exit(0);
    }
    NBCLIOptions options = new NBCLIOptions(args);
    logger = LogManager.getLogger("NBCLI");
    NBIO.addGlobalIncludes(options.wantsIncludes());
    ActivityMetrics.setHdrDigits(options.getHdrDigits());
    if (options.wantsBasicHelp()) {
        System.out.println(loadHelpFile("basic.md"));
        System.exit(0);
    }
    if (options.isWantsVersionShort()) {
        System.out.println(new VersionInfo().getVersion());
        System.exit(0);
    }
    if (options.wantsVersionCoords()) {
        System.out.println(new VersionInfo().getArtifactCoordinates());
        System.exit(0);
    }
    if (options.wantsActivityTypes()) {
        new ActivityTypeLoader().getAllSelectors().forEach(System.out::println);
        System.exit(0);
    }
    if (options.wantsWorkloadsList()) {
        NBCLIScenarios.printWorkloads(false, options.wantsIncludes());
        System.exit(0);
    }
    if (options.wantsScenariosList()) {
        NBCLIScenarios.printWorkloads(true, options.wantsIncludes());
        System.exit(0);
    }
    if (options.wantsScriptList()) {
        NBCLIScripts.printScripts(true, options.wantsIncludes());
        System.exit(0);
    }
    if (options.wantsToCopyResource()) {
        String resourceToCopy = options.wantsToCopyResourceNamed();
        logger.debug("user requests to copy out " + resourceToCopy);
        Optional<Content<?>> tocopy = NBIO.classpath().prefix("activities").prefix(options.wantsIncludes()).name(resourceToCopy).extension(RawStmtsLoader.YAML_EXTENSIONS).first();
        if (tocopy.isEmpty()) {
            tocopy = NBIO.classpath().prefix().prefix(options.wantsIncludes()).prefix(options.wantsIncludes()).name(resourceToCopy).first();
        }
        Content<?> data = tocopy.orElseThrow(() -> new BasicError("Unable to find " + resourceToCopy + " in classpath to copy out"));
        Path writeTo = Path.of(data.asPath().getFileName().toString());
        if (Files.exists(writeTo)) {
            throw new BasicError("A file named " + writeTo.toString() + " exists. Remove it first.");
        }
        try {
            Files.writeString(writeTo, data.getCharBuffer(), StandardCharsets.UTF_8);
        } catch (IOException e) {
            throw new BasicError("Unable to write to " + writeTo.toString() + ": " + e.getMessage());
        }
        logger.info("Copied internal resource '" + data.asPath() + "' to '" + writeTo.toString() + "'");
        System.exit(0);
    }
    if (options.wantsInputTypes()) {
        InputType.FINDER.getAllSelectors().forEach((k, v) -> System.out.println(k + " (" + v.name() + ")"));
        System.exit(0);
    }
    if (options.wantsMarkerTypes()) {
        OutputType.FINDER.getAllSelectors().forEach((k, v) -> System.out.println(k + " (" + v.name() + ")"));
        System.exit(0);
    }
    if (options.wantsToDumpCyclelog()) {
        CycleLogDumperUtility.main(options.getCycleLogExporterOptions());
        System.exit(0);
    }
    if (options.wantsToImportCycleLog()) {
        CycleLogImporterUtility.main(options.getCyclelogImportOptions());
        System.exit(0);
    }
    if (options.wantsTopicalHelp()) {
        Optional<String> helpDoc = MarkdownDocInfo.forHelpTopic(options.wantsTopicalHelpFor());
        System.out.println(helpDoc.orElseThrow(() -> new RuntimeException("No help could be found for " + options.wantsTopicalHelpFor())));
        System.exit(0);
    }
    if (options.wantsMetricsForActivity() != null) {
        String metricsHelp = getMetricsHelpFor(options.wantsMetricsForActivity());
        System.out.println("Available metric names for activity:" + options.wantsMetricsForActivity() + ":");
        System.out.println(metricsHelp);
        System.exit(0);
    }
    logger.debug("initializing annotators with config:'" + annotatorsConfig + "'");
    Annotators.init(annotatorsConfig);
    Annotators.recordAnnotation(Annotation.newBuilder().session(sessionName).now().layer(Layer.CLI).detail("cli", String.join("\n", args)).build());
    if (reportGraphiteTo != null || options.wantsReportCsvTo() != null) {
        MetricReporters reporters = MetricReporters.getInstance();
        reporters.addRegistry("workloads", ActivityMetrics.getMetricRegistry());
        if (reportGraphiteTo != null) {
            reporters.addGraphite(reportGraphiteTo, options.wantsMetricsPrefix());
        }
        if (options.wantsReportCsvTo() != null) {
            reporters.addCSVReporter(options.wantsReportCsvTo(), options.wantsMetricsPrefix());
        }
        reporters.start(10, options.getReportInterval());
    }
    if (options.wantsEnableChart()) {
        logger.info("Charting enabled");
        if (options.getHistoLoggerConfigs().size() == 0) {
            logger.info("Adding default histologger configs");
            String pattern = ".*";
            String file = options.getChartHdrFileName();
            String interval = "1s";
            options.setHistoLoggerConfigs(pattern, file, interval);
        }
    }
    for (NBCLIOptions.LoggerConfigData histoLogger : options.getHistoLoggerConfigs()) {
        ActivityMetrics.addHistoLogger(sessionName, histoLogger.pattern, histoLogger.file, histoLogger.interval);
    }
    for (NBCLIOptions.LoggerConfigData statsLogger : options.getStatsLoggerConfigs()) {
        ActivityMetrics.addStatsLogger(sessionName, statsLogger.pattern, statsLogger.file, statsLogger.interval);
    }
    for (NBCLIOptions.LoggerConfigData classicConfigs : options.getClassicHistoConfigs()) {
        ActivityMetrics.addClassicHistos(sessionName, classicConfigs.pattern, classicConfigs.file, classicConfigs.interval);
    }
    // intentionally not shown for warn-only
    logger.info("console logging level is " + options.getConsoleLogLevel());
    ScenariosExecutor executor = new ScenariosExecutor("executor-" + sessionName, 1);
    if (options.getConsoleLogLevel().isGreaterOrEqualTo(NBLogLevel.WARN)) {
        options.setWantsStackTraces(true);
        logger.debug("enabling stack traces since log level is " + options.getConsoleLogLevel());
    }
    Scenario scenario = new Scenario(sessionName, options.getScriptFile(), options.getScriptingEngine(), options.getProgressSpec(), options.wantsGraaljsCompatMode(), options.wantsStackTraces(), options.wantsCompileScript(), options.getReportSummaryTo(), String.join("\n", args), options.getLogsDirectory(), Maturity.Unspecified);
    ScriptBuffer buffer = new BasicScriptBuffer().add(options.getCommands().toArray(new Cmd[0]));
    String scriptData = buffer.getParsedScript();
    if (options.wantsShowScript()) {
        System.out.println("// Rendered Script");
        System.out.println(scriptData);
        System.exit(0);
    }
    if (options.wantsEnableChart()) {
        logger.info("Charting enabled");
        scenario.enableCharting();
    } else {
        logger.info("Charting disabled");
    }
    // Execute Scenario!
    if (options.getCommands().size() == 0) {
        logger.info("No commands provided. Exiting before scenario.");
        System.exit(0);
    }
    scenario.addScriptText(scriptData);
    ScriptParams scriptParams = new ScriptParams();
    scriptParams.putAll(buffer.getCombinedParams());
    scenario.addScenarioScriptParams(scriptParams);
    executor.execute(scenario);
    while (true) {
        Optional<ScenarioResult> pendingResult = executor.getPendingResult(scenario.getScenarioName());
        if (pendingResult.isEmpty()) {
            LockSupport.parkNanos(100000000L);
        } else {
            break;
        }
    }
    ScenariosResults scenariosResults = executor.awaitAllResults();
    ActivityMetrics.closeMetrics(options.wantsEnableChart());
    // scenariosResults.reportToLog();
    ShutdownManager.shutdown();
    if (scenariosResults.hasError()) {
        Exception exception = scenariosResults.getOne().getException().get();
        // logger.warn(scenariosResults.getExecutionSummary());
        ScenarioErrorHandler.handle(exception, options.wantsStackTraces());
        // TODO: make this consistent with ConsoleLogging sequencing
        System.err.println(exception.getMessage());
        System.exit(2);
    } else {
        logger.info(scenariosResults.getExecutionSummary());
        System.exit(0);
    }
}
Also used : DockerMetricsManager(io.nosqlbench.engine.docker.DockerMetricsManager) MetricReporters(io.nosqlbench.engine.core.metrics.MetricReporters) Path(java.nio.file.Path) ScriptParams(io.nosqlbench.engine.core.script.ScriptParams) ScenariosExecutor(io.nosqlbench.engine.core.script.ScenariosExecutor) IOException(java.io.IOException) IOException(java.io.IOException) Scenario(io.nosqlbench.engine.core.script.Scenario) Content(io.nosqlbench.nb.api.content.Content) BasicError(io.nosqlbench.nb.api.errors.BasicError)

Example 4 with Scenario

use of io.nosqlbench.engine.core.script.Scenario in project nosqlbench by nosqlbench.

the class ScenarioExecutorEndpoint method invokeCommand.

/**
 * Run a NoSQLBench command just as you would on the command line. Certain parameters are translated
 * (virtualized) into the workspace view for you automatically. That is, any path which would otherwise
 * be resolved on the local file system will now be resolved in that same way but with the designated workspace
 * as the base directory. All filesystem interaction which would otherwise happen in the current working
 * directory should also be done relative to the designated workspace.
 * @param rq
 * @return
 */
@POST
@Path("cli")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public synchronized Response invokeCommand(RunScenarioRequest rq) {
    String name = rq.getScenarioName();
    if (name.equals("auto")) {
        rq.setScenarioName("scenario" + System.currentTimeMillis());
    }
    org.joda.time.format.DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyyMMddHHmmssSSS");
    name = name.replaceAll("EPOCHMS", String.valueOf(System.currentTimeMillis()));
    name = name.replaceAll("DATESTAMP", dtf.print(new DateTime()));
    name = name.replaceAll("[:/ ]", "");
    rq.setScenarioName(name);
    WorkSpace workspace = new WorkspaceFinder(config).getWorkspace(rq.getWorkspace());
    // First, virtualize files provided
    storeFiles(rq);
    LinkedList<Cmd> cmdList = new LinkedList<>();
    LinkedList<String> args = new LinkedList<>(rq.getCommands());
    for (String arg : args) {
        if (arg.startsWith("-")) {
            throw new RuntimeException("Only commands (verbs and params) can be used here");
        }
    }
    args = substituteFilenames(rq, args);
    NBCLICommandParser.parse(args, cmdList, workspace.asIncludes());
    ScriptBuffer buffer = new BasicScriptBuffer();
    buffer.add(cmdList.toArray(new Cmd[0]));
    Scenario scenario = new Scenario(rq.getScenarioName(), "", Scenario.Engine.Graalvm, "disabled", false, true, false, "", cmdList.toString(), (java.nio.file.Path) config.getProperties().get("logpath"), Maturity.Unspecified);
    scenario.addScriptText(buffer.getParsedScript());
    executor.execute(scenario);
    return Response.created(UriBuilder.fromResource(ScenarioExecutorEndpoint.class).path("scenario/" + rq.getScenarioName()).build()).entity("started").build();
}
Also used : WorkspaceFinder(io.nosqlbench.engine.rest.services.WorkspaceFinder) BasicScriptBuffer(io.nosqlbench.engine.cli.BasicScriptBuffer) ScriptBuffer(io.nosqlbench.engine.cli.ScriptBuffer) DateTime(org.joda.time.DateTime) Scenario(io.nosqlbench.engine.core.script.Scenario) WorkSpace(io.nosqlbench.engine.rest.services.WorkSpace) BasicScriptBuffer(io.nosqlbench.engine.cli.BasicScriptBuffer) Cmd(io.nosqlbench.engine.cli.Cmd)

Aggregations

Scenario (io.nosqlbench.engine.core.script.Scenario)4 ScriptEnvBuffer (io.nosqlbench.engine.api.scripting.ScriptEnvBuffer)1 BasicScriptBuffer (io.nosqlbench.engine.cli.BasicScriptBuffer)1 Cmd (io.nosqlbench.engine.cli.Cmd)1 ScriptBuffer (io.nosqlbench.engine.cli.ScriptBuffer)1 MetricReporters (io.nosqlbench.engine.core.metrics.MetricReporters)1 ScenariosExecutor (io.nosqlbench.engine.core.script.ScenariosExecutor)1 ScriptParams (io.nosqlbench.engine.core.script.ScriptParams)1 DockerMetricsManager (io.nosqlbench.engine.docker.DockerMetricsManager)1 WorkSpace (io.nosqlbench.engine.rest.services.WorkSpace)1 WorkspaceFinder (io.nosqlbench.engine.rest.services.WorkspaceFinder)1 Content (io.nosqlbench.nb.api.content.Content)1 BasicError (io.nosqlbench.nb.api.errors.BasicError)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 DateTime (org.joda.time.DateTime)1 Test (org.junit.jupiter.api.Test)1