Example #1
0
  /**
   * This method generates the time series for the methods and puts it in an HTML string. The
   * methods are sorted according to the greatest average time for each method.
   *
   * @param dataBase the database to aggregate the statistics for
   * @return the html representing the series of timings for the method
   */
  @SuppressWarnings({"unchecked", "rawtypes"})
  protected String methodSeries(final IDataBase dataBase) {
    Comparator<Method> comparator =
        new Comparator<Method>() {
          public int compare(Method o1, Method o2) {
            Double o1Average = calculator.averageMethodTime(o1);
            Double o2Average = calculator.averageMethodTime(o2);
            // We want a descending table, i.e. the most expensive at the top
            return o2Average.compareTo(o1Average);
          }
        };
    Set<Method> sortedMethods = new TreeSet<>(comparator);
    List<Method> methods = dataBase.find(Method.class);
    sortedMethods.addAll(methods);

    List<Snapshot<?, ?>> snapshots =
        methods.size() > 0 ? methods.get(0).getSnapshots() : new ArrayList<Snapshot<?, ?>>();
    Element tableElement = tableElement(snapshots);

    for (Method method : sortedMethods) {
      Class<?, ?> klass = (Class<?, ?>) method.getParent();
      String className = klass.getName();
      String methodName = method.getName();

      Element rowElement = addElement(tableElement, "tr", null);
      addElement(rowElement, "td", className);
      addElement(rowElement, "td", methodName);
      addElement(rowElement, "td", Double.toString(calculator.averageMethodTime(method)));
      addElement(rowElement, "td", Double.toString(calculator.averageMethodNetTime(method)));
      addElement(rowElement, "td", Double.toString(calculator.totalMethodTime(method)));
      addElement(rowElement, "td", Double.toString(calculator.totalNetMethodTime(method)));
      addElement(rowElement, "td", Integer.toString(method.getInvocations()));

      Element dataElement = addElement(rowElement, "td", null);
      Element imageElement = addElement(dataElement, "img", null);
      // Add the method series graph for the average and total time for the method
      List<Double> methodSeries = calculator.methodSeries(method);
      String url = buildGraph(IConstants.METHOD_SERIES, method, methodSeries);
      addAttributes(imageElement, new String[] {"src"}, new String[] {url});

      dataElement = addElement(rowElement, "td", null);
      imageElement = addElement(dataElement, "img", null);
      // Add the method change graph, i.e. the change in the average time for the method
      List<Double> methodChangeSeries = calculator.methodChangeSeries(method);
      url = buildGraph(IConstants.METHOD_CHANGE_SERIES, method, methodChangeSeries);
      addAttributes(imageElement, new String[] {"src"}, new String[] {url});
    }

    Document document = tableElement.getDocument();
    return prettyPrint(document);
  }