Beispiel #1
0
  private String executeTest(File test) throws Exception {
    String reportFileName;
    String reportFileFullPath;
    JMeter jmeterInstance = new JMeter();
    try {
      log.info("Executing test: " + test.getCanonicalPath());
      reportFileName =
          test.getName().substring(0, test.getName().lastIndexOf("."))
              + "-"
              + fmt.format(new Date())
              + ".jmeterResult"
              + ".jtl";

      File reportDir = JMeterInstallationProvider.getInstance().getReportDir();
      reportFileFullPath = reportDir.toString() + File.separator + reportFileName;
      List<String> argsTmp =
          Arrays.asList(
              "-n",
              "-t",
              test.getCanonicalPath(),
              "-l",
              reportDir.toString() + File.separator + reportFileName,
              "-p",
              jmeterProps.toString(),
              "-d",
              jmeterHome.getCanonicalPath(),
              "-L",
              "jorphan=" + jmeterLogLevel,
              "-L",
              "jmeter.util=" + jmeterLogLevel);

      List<String> args = new ArrayList<String>();

      args.addAll(argsTmp);

      SecurityManager oldManager = System.getSecurityManager();

      UncaughtExceptionHandler oldHandler = Thread.getDefaultUncaughtExceptionHandler();
      Thread.setDefaultUncaughtExceptionHandler(
          new UncaughtExceptionHandler() {

            public void uncaughtException(Thread t, Throwable e) {
              if (e instanceof ExitException && ((ExitException) e).getCode() == 0) {
                return; // Ignore
              }
              log.error("Error in thread " + t.getName());
            }
          });

      try {
        logParamsAndProps(args);

        jmeterInstance.start(args.toArray(new String[] {}));

        BufferedReader in = new BufferedReader(new FileReader(jmeterLogFile));
        while (!checkForEndOfTest(in)) {
          try {
            Thread.sleep(1000);
          } catch (InterruptedException e) {
            break;
          }
        }

      } catch (ExitException e) {
        if (e.getCode() != 0) {
          throw new Exception("Test failed", e);
        }
      } catch (Exception e) {
        log.error(e);

      } finally {
        System.setSecurityManager(oldManager);
        Thread.setDefaultUncaughtExceptionHandler(oldHandler);
      }
    } catch (IOException e) {
      throw new Exception("Can't execute test", e);
    }
    return reportFileFullPath;
  }