Esempio n. 1
0
 private void setJMeterPropertyFile(JMeterTest jMeterTest) throws IOException {
   if (jMeterTest.getJMeterPropertyFile() == null) {
     log.info("Loading default jmeter.properties...");
     jmeterProps = JMeterInstallationProvider.getInstance().getJMeterPropertyFile();
     System.setProperty(
         "jmeter_properties", File.separator + "bin" + File.separator + "jmeter.properties");
   } else {
     log.info(
         "Loading custom jmeter.properties from "
             + jMeterTest.getJMeterPropertyFile().getCanonicalPath());
     jmeterProps = jMeterTest.getJMeterPropertyFile();
     System.setProperty("jmeter_properties", jmeterProps.getCanonicalPath());
   }
 }
Esempio n. 2
0
  private void addLogFile(String fileName) throws IOException {

    jmeterLogFile =
        new File(
            JMeterInstallationProvider.getInstance().getLogDir().getCanonicalPath()
                + File.separator
                + fileName.substring(0, fileName.lastIndexOf("."))
                + "-"
                + fmt.format(new Date())
                + ".log");
    if (!jmeterLogFile.createNewFile()) {
      log.error("unable to create log file");
    }
    try {
      System.setProperty("log_file", jmeterLogFile.getCanonicalPath());
    } catch (IOException e) {
      throw new IOException("Can't get canonical path for log file", e);
    }
  }
Esempio n. 3
0
  public void runTest(JMeterTest jMeterTest) throws Exception {
    JMeterResult results;

    // Init JMeter
    jmeterHome = JMeterInstallationProvider.getInstance().getJMeterHome();

    testFile = jMeterTest.getTestFile();
    // setting jmeter.properties file parameter
    setJMeterPropertyFile(jMeterTest);

    if (jMeterTest.getLogLevel() != null) {
      jmeterLogLevel = jMeterTest.getLogLevel();
    }

    results = executeMe();
    //        checkForErrors();
    log.info("for more info. " + results.getFileName());
    if (results.getErrorCount() > 0) {
      throw new Exception(
          "Test Failed. "
              + results.getErrorCount()
              + " Error/s Found.\n"
              + results.getErrorList().toString()
              + "\nRefer "
              + results.getFileName()
              + " for test result");
    }

    if (results.getFailureCount() > 0) {
      throw new AssertionError(
          "Test Failed. "
              + results.getFailureCount()
              + " Assertion Failure/s.\n"
              + results.getAssertList().toString()
              + "\nRefer "
              + results.getFileName()
              + " for test result");
    }
  }
Esempio n. 4
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;
  }