Search in sources :

Example 1 with CLArgsParser

use of org.apache.commons.cli.avalon.CLArgsParser in project jmeter by apache.

the class JMeter method start.

/**
     * Takes the command line arguments and uses them to determine how to
     * startup JMeter.
     * 
     * Called reflectively by {@link NewDriver#main(String[])}
     * @param args The arguments for JMeter
     */
public void start(String[] args) {
    CLArgsParser parser = new CLArgsParser(args, options);
    String error = parser.getErrorString();
    if (error == null) {
        // Check option combinations
        boolean gui = parser.getArgumentById(NONGUI_OPT) == null;
        boolean nonGuiOnly = parser.getArgumentById(REMOTE_OPT) != null || parser.getArgumentById(REMOTE_OPT_PARAM) != null || parser.getArgumentById(REMOTE_STOP) != null;
        if (gui && nonGuiOnly) {
            error = "-r and -R and -X are only valid in non-GUI mode";
        }
    }
    if (null != error) {
        //NOSONAR
        System.err.println("Error: " + error);
        //NOSONAR
        System.out.println("Usage");
        //NOSONAR
        System.out.println(CLUtil.describeOptions(options).toString());
        // repeat the error so no need to scroll back past the usage to see it
        //NOSONAR
        System.out.println("Error: " + error);
        return;
    }
    try {
        // Also initialises JMeter logging
        initializeProperties(parser);
        Thread.setDefaultUncaughtExceptionHandler((Thread t, Throwable e) -> {
            if (!(e instanceof ThreadDeath)) {
                log.error("Uncaught exception: ", e);
                //NOSONAR
                System.err.println("Uncaught Exception " + e + ". See log file for details.");
            }
        });
        if (log.isInfoEnabled()) {
            log.info(JMeterUtils.getJMeterCopyright());
            log.info("Version {}", JMeterUtils.getJMeterVersion());
            //$NON-NLS-1$ //$NON-NLS-2$
            log.info("java.version={}", System.getProperty("java.version"));
            //$NON-NLS-1$ //$NON-NLS-2$
            log.info("java.vm.name={}", System.getProperty("java.vm.name"));
            //$NON-NLS-1$ //$NON-NLS-2$
            log.info("os.name={}", System.getProperty("os.name"));
            //$NON-NLS-1$ //$NON-NLS-2$
            log.info("os.arch={}", System.getProperty("os.arch"));
            //$NON-NLS-1$ //$NON-NLS-2$
            log.info("os.version={}", System.getProperty("os.version"));
            //$NON-NLS-1$ //$NON-NLS-2$
            log.info("file.encoding={}", System.getProperty("file.encoding"));
            log.info("Max memory     ={}", Runtime.getRuntime().maxMemory());
            log.info("Available Processors ={}", Runtime.getRuntime().availableProcessors());
            log.info("Default Locale={}", Locale.getDefault().getDisplayName());
            log.info("JMeter  Locale={}", JMeterUtils.getLocale().getDisplayName());
            log.info("JMeterHome={}", JMeterUtils.getJMeterHome());
            //$NON-NLS-1$ //$NON-NLS-2$
            log.info("user.dir  ={}", System.getProperty("user.dir"));
            //$NON-NLS-1$
            log.info("PWD       ={}", new File(".").getCanonicalPath());
            log.info("IP: {} Name: {} FullName: {}", JMeterUtils.getLocalHostIP(), JMeterUtils.getLocalHostName(), JMeterUtils.getLocalHostFullName());
        }
        setProxy(parser);
        updateClassLoader();
        if (log.isDebugEnabled()) {
            // $NON-NLS-1$
            String jcp = System.getProperty("java.class.path");
            String[] bits = jcp.split(File.pathSeparator);
            log.debug("ClassPath");
            for (String bit : bits) {
                log.debug(bit);
            }
        }
        // Set some (hopefully!) useful properties
        long now = System.currentTimeMillis();
        // $NON-NLS-1$
        JMeterUtils.setProperty("START.MS", Long.toString(now));
        // so it agrees with above
        Date today = new Date(now);
        // $NON-NLS-1$ $NON-NLS-2$
        JMeterUtils.setProperty("START.YMD", new SimpleDateFormat("yyyyMMdd").format(today));
        // $NON-NLS-1$ $NON-NLS-2$
        JMeterUtils.setProperty("START.HMS", new SimpleDateFormat("HHmmss").format(today));
        if (parser.getArgumentById(VERSION_OPT) != null) {
            displayAsciiArt();
        } else if (parser.getArgumentById(HELP_OPT) != null) {
            displayAsciiArt();
            //NOSONAR $NON-NLS-1$
            System.out.println(JMeterUtils.getResourceFileAsText("org/apache/jmeter/help.txt"));
        } else if (parser.getArgumentById(OPTIONS_OPT) != null) {
            displayAsciiArt();
            //NOSONAR
            System.out.println(CLUtil.describeOptions(options).toString());
        } else if (parser.getArgumentById(SERVER_OPT) != null) {
            // Start the server
            try {
                // $NON-NLS-1$
                RemoteJMeterEngineImpl.startServer(JMeterUtils.getPropDefault("server_port", 0));
                startOptionalServers();
            } catch (Exception ex) {
                //NOSONAR
                System.err.println("Server failed to start: " + ex);
                log.error("Giving up, as server failed with:", ex);
                throw ex;
            }
        } else {
            String testFile = null;
            CLOption testFileOpt = parser.getArgumentById(TESTFILE_OPT);
            if (testFileOpt != null) {
                testFile = testFileOpt.getArgument();
                if (USE_LAST_JMX.equals(testFile)) {
                    // most recent
                    testFile = LoadRecentProject.getRecentFile(0);
                }
            }
            CLOption testReportOpt = parser.getArgumentById(REPORT_GENERATING_OPT);
            if (testReportOpt != null) {
                // generate report from existing file
                String reportFile = testReportOpt.getArgument();
                extractAndSetReportOutputFolder(parser);
                ReportGenerator generator = new ReportGenerator(reportFile, null);
                generator.generate();
            } else if (parser.getArgumentById(NONGUI_OPT) == null) {
                // not non-GUI => GUI
                startGui(testFile);
                startOptionalServers();
            } else {
                // NON-GUI must be true
                extractAndSetReportOutputFolder(parser);
                CLOption rem = parser.getArgumentById(REMOTE_OPT_PARAM);
                if (rem == null) {
                    rem = parser.getArgumentById(REMOTE_OPT);
                }
                CLOption jtl = parser.getArgumentById(LOGFILE_OPT);
                String jtlFile = null;
                if (jtl != null) {
                    // $NON-NLS-1$
                    jtlFile = processLAST(jtl.getArgument(), ".jtl");
                }
                CLOption reportAtEndOpt = parser.getArgumentById(REPORT_AT_END_OPT);
                if (reportAtEndOpt != null && jtlFile == null) {
                    throw new IllegalUserActionException("Option -" + ((char) REPORT_AT_END_OPT) + " requires -" + ((char) LOGFILE_OPT) + " option");
                }
                startNonGui(testFile, jtlFile, rem, reportAtEndOpt != null);
                startOptionalServers();
            }
        }
    } catch (IllegalUserActionException e) {
        // NOSONAR
        //NOSONAR
        System.out.println("Incorrect Usage:" + e.getMessage());
        //NOSONAR
        System.out.println(CLUtil.describeOptions(options).toString());
    } catch (Throwable e) {
        // NOSONAR
        log.error("An error occurred: ", e);
        //NOSONAR
        System.out.println("An error occurred: " + e.getMessage());
        // FIXME Should we exit here ? If we are called by Maven or Jenkins
        System.exit(1);
    }
}
Also used : CLOption(org.apache.commons.cli.avalon.CLOption) Date(java.util.Date) ConfigurationException(org.apache.jmeter.report.config.ConfigurationException) JMeterException(org.apache.jorphan.util.JMeterException) FileNotFoundException(java.io.FileNotFoundException) ConversionException(com.thoughtworks.xstream.converters.ConversionException) SocketException(java.net.SocketException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) IllegalUserActionException(org.apache.jmeter.exceptions.IllegalUserActionException) GenerationException(org.apache.jmeter.report.dashboard.GenerationException) ReportGenerator(org.apache.jmeter.report.dashboard.ReportGenerator) IllegalUserActionException(org.apache.jmeter.exceptions.IllegalUserActionException) CLArgsParser(org.apache.commons.cli.avalon.CLArgsParser) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat)

Example 2 with CLArgsParser

use of org.apache.commons.cli.avalon.CLArgsParser in project jmeter by apache.

the class HttpMirrorServer method main.

public static void main(String[] args) {
    CLArgsParser parser = new CLArgsParser(args, options);
    String error = parser.getErrorString();
    if (error != null) {
        //NOSONAR
        System.err.println("Error: " + error);
        //NOSONAR
        System.out.println("Usage");
        //NOSONAR
        System.out.println(CLUtil.describeOptions(options).toString());
        // repeat the error so no need to scroll back past the usage to see it
        //NOSONAR
        System.out.println("Error: " + error);
        return;
    }
    if (parser.getArgumentById(OPTIONS_OPT) != null) {
        //NOSONAR
        System.out.println(CLUtil.describeOptions(options).toString());
        return;
    }
    int port = HttpMirrorControl.DEFAULT_PORT;
    if (parser.getArgumentById(PORT_OPT) != null) {
        CLOption option = parser.getArgumentById(PORT_OPT);
        String value = option.getArgument(0);
        try {
            port = Integer.parseInt(value);
        } catch (NumberFormatException ignored) {
        }
    } else if (args.length > 0) {
        try {
            port = Integer.parseInt(args[0]);
        } catch (NumberFormatException ignored) {
        }
    }
    if (System.getProperty("log4j.configurationFile") == null) {
        // $NON-NLS-1$
        Configurator.setRootLevel(Level.INFO);
    }
    List<CLOption> clOptions = parser.getArguments();
    for (CLOption option : clOptions) {
        String name = option.getArgument(0);
        String value = option.getArgument(1);
        switch(option.getDescriptor().getId()) {
            case LOGLEVEL_OPT:
                if (!value.isEmpty()) {
                    // Set category
                    final Level logLevel = Level.getLevel(value);
                    if (logLevel != null) {
                        String loggerName = name;
                        if (name.startsWith("jmeter") || name.startsWith("jorphan")) {
                            // $NON-NLS-1$
                            loggerName = "org.apache." + name;
                        }
                        // $NON-NLS-1$ // $NON-NLS-2$
                        getLogger().info("Setting log level to " + value + " for " + loggerName);
                        Configurator.setAllLevels(loggerName, logLevel);
                    } else {
                        // $NON-NLS-1$ // $NON-NLS-2$
                        getLogger().warn("Invalid log level, '" + value + "' for '" + name + "'.");
                    }
                } else {
                    // Set root level
                    final Level logLevel = Level.getLevel(name);
                    if (logLevel != null) {
                        // $NON-NLS-1$
                        getLogger().info("Setting root log level to " + name);
                        Configurator.setRootLevel(logLevel);
                    } else {
                        // $NON-NLS-1$ // $NON-NLS-2$
                        getLogger().warn("Invalid log level, '" + name + "' for the root logger.");
                    }
                }
                break;
            default:
                break;
        }
    }
    HttpMirrorServer serv = new HttpMirrorServer(port);
    serv.start();
}
Also used : CLOption(org.apache.commons.cli.avalon.CLOption) Level(org.apache.logging.log4j.Level) CLArgsParser(org.apache.commons.cli.avalon.CLArgsParser)

Aggregations

CLArgsParser (org.apache.commons.cli.avalon.CLArgsParser)2 CLOption (org.apache.commons.cli.avalon.CLOption)2 ConversionException (com.thoughtworks.xstream.converters.ConversionException)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 SocketException (java.net.SocketException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 IllegalUserActionException (org.apache.jmeter.exceptions.IllegalUserActionException)1 ConfigurationException (org.apache.jmeter.report.config.ConfigurationException)1 GenerationException (org.apache.jmeter.report.dashboard.GenerationException)1 ReportGenerator (org.apache.jmeter.report.dashboard.ReportGenerator)1 JMeterException (org.apache.jorphan.util.JMeterException)1 Level (org.apache.logging.log4j.Level)1