Exemplo n.º 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;
  }
Exemplo n.º 2
0
  public boolean runTestPlan(String testPlanFilename) {

    jmeterResults = "";
    createJmeterEngine();
    File f = new File(testPlanFilename);
    if (!f.exists() || !f.isFile()) {
      jmeterResults += "Could not open " + testPlanFilename;
      System.out.println(jmeterResults);
      return false;
    }

    FileInputStream reader = null;
    try {
      reader = new FileInputStream(new File(testPlanFilename));
      currentHashTree = SaveService.loadTree(reader);
      // store log file in ./FitNesseRoot/files/testResults/testPlanFilename.log
      String logFile =
          new File(jmeterLogPath, (new File(testPlanFilename).getName() + ".log"))
              .getCanonicalPath();
      lastJmeterLog = logFile;

      @SuppressWarnings("deprecation") // Deliberate use of deprecated ctor
      JMeterTreeModel treeModel =
          new JMeterTreeModel(new Object()); // Create non-GUI version to avoid headless problems
      JMeterTreeNode root = (JMeterTreeNode) treeModel.getRoot();
      treeModel.addSubTree(currentHashTree, root);

      // Hack to resolve ModuleControllers in non GUI mode
      SearchByClass<ReplaceableController> replaceableControllers =
          new SearchByClass<ReplaceableController>(ReplaceableController.class);
      currentHashTree.traverse(replaceableControllers);
      Collection<ReplaceableController> replaceableControllersRes =
          replaceableControllers.getSearchResults();
      for (Iterator<ReplaceableController> iter = replaceableControllersRes.iterator();
          iter.hasNext(); ) {
        ReplaceableController replaceableController = iter.next();
        replaceableController.resolveReplacementSubTree(root);
      }

      // Remove the disabled items
      // For GUI runs this is done in Start.java
      JMeter.convertSubTree(currentHashTree);

      Summariser summer = null;
      String summariserName =
          JMeterUtils.getPropDefault("summariser.name", "summary"); // $NON-NLS-1$
      if (summariserName.length() > 0) {
        // log.info("Creating summariser <" + summariserName + ">");
        // println("Creating summariser <" + summariserName + ">");
        summer = new Summariser(summariserName);
      }

      if (logFile != null) {
        ResultCollector logger = new ResultCollector(summer);
        logger.setFilename(logFile);
        currentHashTree.add(currentHashTree.getArray()[0], logger);
      } else {
        // only add Summariser if it can not be shared with the ResultCollector
        if (summer != null) {
          currentHashTree.add(currentHashTree.getArray()[0], summer);
        }
      }

      // Used for remote notification of threads start/stop,see BUG 54152
      // Summariser uses this feature to compute correctly number of threads
      // when NON GUI mode is used
      currentHashTree.add(currentHashTree.getArray()[0], new RemoteThreadsListenerTestElement());

      jEngine.configure(currentHashTree);
      jEngine.runTest();
      // reader.close();
      JOrphanUtils.closeQuietly(reader);
      Util.waitForFileToExists(logFile, 5); // wait up to 5 seconds for file to exist
      String logStr = Util.fileToString(logFile);
      // logStr = logStr.replaceAll("\n", "<br/>\n");
      jmeterResults += logStr;
      jmeterResults += "Test " + testPlanFilename + " completed.";
      System.out.println("Test " + testPlanFilename + " completed.");

    } catch (Exception e) {
      e.printStackTrace();
      jmeterResults += "\r\nException: " + e.getMessage();
      return false;
    }
    return true;
  }