Esempio n. 1
0
  /**
   * Test.
   *
   * @throws IOException e
   */
  @Test
  public void testPeriodeNonTout() throws IOException {
    // counter avec période non TOUT et des requêtes
    collector.collectWithoutErrors(javaInformationsList);
    final String requestName = "test 1";
    counter.bindContext(requestName, "complete test 1", null, -1);
    sqlCounter.addRequest("sql1", 10, 10, false, -1);
    counter.addRequest(requestName, 0, 0, false, 1000);
    counter.addRequest("test2", 1000, 500, false, 1000);
    counter.addRequest("test3", 10000, 500, true, 10000);
    collector.collectWithoutErrors(javaInformationsList);
    final HtmlReport htmlReport =
        new HtmlReport(collector, null, javaInformationsList, Period.SEMAINE, writer);
    htmlReport.toHtml("message 6", null);
    assertNotEmptyAndClear(writer);

    // période personnalisée
    final HtmlReport htmlReportRange =
        new HtmlReport(
            collector,
            null,
            javaInformationsList,
            Range.createCustomRange(new Date(), new Date()),
            writer);
    htmlReportRange.toHtml("message 6", null);
    assertNotEmptyAndClear(writer);
  }
Esempio n. 2
0
  /**
   * Test.
   *
   * @throws IOException e
   */
  @Test
  public void testRootContexts() throws IOException {
    HtmlReport htmlReport;
    // addRequest pour que CounterRequestContext.getCpuTime() soit appelée
    counter.addRequest("first request", 100, 100, false, 1000);
    TestCounter.bindRootContexts("first request", counter, 3);
    sqlCounter.bindContext("sql", "sql", null, -1);
    htmlReport = new HtmlReport(collector, null, javaInformationsList, Period.TOUT, writer);
    htmlReport.toHtml("message a", null);
    assertNotEmptyAndClear(writer);

    final Counter myCounter = new Counter("http", null);
    final Collector collector2 = new Collector("test 2", Arrays.asList(myCounter));
    myCounter.bindContext("my context", "my context", null, -1);
    htmlReport = new HtmlReport(collector2, null, javaInformationsList, Period.SEMAINE, writer);
    htmlReport.toHtml("message b", null);
    assertNotEmptyAndClear(writer);

    final HtmlCounterRequestContextReport htmlCounterRequestContextReport =
        new HtmlCounterRequestContextReport(
            collector2.getRootCurrentContexts(collector2.getCounters()),
            null,
            new ArrayList<ThreadInformations>(),
            false,
            500,
            writer);
    htmlCounterRequestContextReport.toHtml();
    assertNotEmptyAndClear(writer);
  }
Esempio n. 3
0
  /**
   * Test.
   *
   * @throws IOException e
   */
  @Test
  public void testCounter() throws IOException {
    // counter avec 3 requêtes
    setProperty(Parameter.WARNING_THRESHOLD_MILLIS, "500");
    setProperty(Parameter.SEVERE_THRESHOLD_MILLIS, "1500");
    setProperty(Parameter.ANALYTICS_ID, "123456789");
    counter.addRequest("test1", 0, 0, false, 1000);
    counter.addRequest("test2", 1000, 500, false, 1000);
    counter.addRequest("test3", 100000, 50000, true, 10000);
    final StringBuilder sb = new StringBuilder();
    for (int i = 0; i < 5010; i++) {
      // HtmlCounterReport.MAX_REQUEST_LENGTH = 5000
      sb.append(i % 10);
    }
    final String longRequestName = sb.toString();
    counter.addRequest(longRequestName, 0, 0, false, 5000);
    collector.collectWithoutErrors(javaInformationsList);

    final HtmlReport htmlReport =
        new HtmlReport(collector, null, javaInformationsList, Period.TOUT, writer);
    htmlReport.toHtml("message 2", null);
    assertNotEmptyAndClear(writer);

    setProperty(Parameter.NO_DATABASE, Boolean.TRUE.toString());
    collector.collectWithoutErrors(javaInformationsList);
    htmlReport.toHtml("message 2", null);
    assertNotEmptyAndClear(writer);
    setProperty(Parameter.NO_DATABASE, Boolean.FALSE.toString());

    setProperty(Parameter.WARNING_THRESHOLD_MILLIS, "-1");
    try {
      htmlReport.toHtml("message 2", null);
    } catch (final IllegalStateException e) {
      assertNotNull("ok", e);
    }
    setProperty(Parameter.WARNING_THRESHOLD_MILLIS, null);

    // cas counterReportsByCounterName.size() == 1
    collector = new Collector("test", Arrays.asList(counter));
    final HtmlReport htmlReport2 =
        new HtmlReport(collector, null, javaInformationsList, Period.TOUT, writer);
    htmlReport2.toHtml(null, null);
  }
Esempio n. 4
0
  /**
   * Test.
   *
   * @throws IOException e
   */
  @Test
  public void testToHtmlEn() throws IOException {
    I18N.bindLocale(Locale.US);
    Locale.setDefault(Locale.US);
    try {
      assertEquals("locale en", Locale.US, I18N.getCurrentLocale());

      // counter avec 3 requêtes
      counter.addRequest("test1", 0, 0, false, 1000);
      counter.addRequest("test2", 1000, 500, false, 1000);
      counter.addRequest("test3", 10000, 5000, true, 10000);
      final HtmlReport htmlReport =
          new HtmlReport(collector, null, javaInformationsList, Period.TOUT, writer);
      htmlReport.toHtml("message", null);
      assertNotEmptyAndClear(writer);
    } finally {
      I18N.unbindLocale();
      Locale.setDefault(Locale.FRENCH);
    }
  }
Esempio n. 5
0
  Object doExecute(String requestName, Statement statement, Method method, Object[] args)
      throws IllegalAccessException, InvocationTargetException {
    assert requestName != null;
    assert statement != null;
    assert method != null;

    // on ignore les requêtes explain exécutées par DatabaseInformations
    if (!sqlCounter.isDisplayed() || requestName.startsWith("explain ")) {
      ACTIVE_CONNECTION_COUNT.incrementAndGet();
      try {
        return method.invoke(statement, args);
      } finally {
        ACTIVE_CONNECTION_COUNT.decrementAndGet();
      }
    }

    final long start = System.currentTimeMillis();
    boolean systemError = true;
    try {
      ACTIVE_CONNECTION_COUNT.incrementAndGet();

      // note perf: selon un paramètre current-sql(/requests)-disabled,
      // on pourrait ici ne pas binder un nouveau contexte à chaque requête sql
      sqlCounter.bindContext(requestName, requestName, null, -1);

      final Object result = method.invoke(statement, args);
      systemError = false;
      return result;
    } catch (final InvocationTargetException e) {
      if (e.getCause() instanceof SQLException) {
        final int errorCode = ((SQLException) e.getCause()).getErrorCode();
        if (errorCode >= 20000 && errorCode < 30000) {
          // Dans Oracle par exemple, les erreurs 20000 à 30000 sont standardisées
          // comme étant des erreurs lancées par l'application dans des procédures stockées
          // pour être traitées comme des erreurs de saisies ou comme des règles de gestion.
          // Ce ne sont donc pas des erreurs systèmes.
          systemError = false;
        }
      }
      throw e;
    } finally {
      // Rq : on n'utilise pas la création du statement et l'appel à la méthode close du statement
      // comme début et fin d'une connexion active, car en fonction de l'application
      // la méthode close du statement peut ne jamais être appelée
      // (par exemple, seule la méthode close de la connection peut être appelée ce qui ferme aussi
      // le statement)
      // Rq : pas de temps cpu pour les requêtes sql car c'est 0 ou quasiment 0
      ACTIVE_CONNECTION_COUNT.decrementAndGet();
      final long duration = Math.max(System.currentTimeMillis() - start, 0);
      sqlCounter.addRequest(requestName, duration, -1, systemError, -1);
    }
  }
Esempio n. 6
0
  /**
   * Test.
   *
   * @throws Exception e
   */
  @Test
  public void testWriteRequests() throws Exception { // NOPMD
    final HtmlReport htmlReport =
        new HtmlReport(collector, null, javaInformationsList, Period.SEMAINE, writer);
    htmlReport.writeRequestAndGraphDetail("httpHitsRate");
    assertNotEmptyAndClear(writer);

    // writeRequestAndGraphDetail avec drill-down
    collector.collectWithoutErrors(javaInformationsList);
    // si sqlCounter reste à displayed=false,
    // il ne sera pas utilisé dans writeRequestAndGraphDetail
    sqlCounter.setDisplayed(true);
    final String requestName = "test 1";
    counter.bindContext(requestName, "complete test 1", null, -1);
    servicesCounter.clear();
    servicesCounter.bindContext("myservices.service1", "service1", null, -1);
    sqlCounter.bindContext("sql1", "complete sql1", null, -1);
    sqlCounter.addRequest("sql1", 5, -1, false, -1);
    servicesCounter.addRequest("myservices.service1", 10, 10, false, -1);
    servicesCounter.bindContext("myservices.service2", "service2", null, -1);
    servicesCounter.addRequest("myservices.service2", 10, 10, false, -1);
    servicesCounter.addRequest("otherservices.service3", 10, 10, false, -1);
    servicesCounter.addRequest("otherservices", 10, 10, false, -1);
    jspCounter.addRequest("jsp1", 10, 10, false, -1);
    counter.addRequest(requestName, 0, 0, false, 1000);
    collector.collectWithoutErrors(javaInformationsList);
    final HtmlReport toutHtmlReport =
        new HtmlReport(collector, null, javaInformationsList, Period.TOUT, writer);
    for (final Counter collectorCounter : collector.getCounters()) {
      for (final CounterRequest request : collectorCounter.getRequests()) {
        toutHtmlReport.writeRequestAndGraphDetail(request.getId());
        assertNotEmptyAndClear(writer);
        toutHtmlReport.writeRequestUsages(request.getId());
        assertNotEmptyAndClear(writer);
      }
    }
    sqlCounter.setDisplayed(false);

    // writeCounterSummaryPerClass
    toutHtmlReport.writeCounterSummaryPerClass(servicesCounter.getName(), null);
    String requestId = new CounterRequest("myservices", servicesCounter.getName()).getId();
    toutHtmlReport.writeCounterSummaryPerClass(servicesCounter.getName(), requestId);
    requestId = new CounterRequest("otherservices", servicesCounter.getName()).getId();
    toutHtmlReport.writeCounterSummaryPerClass(servicesCounter.getName(), requestId);
    toutHtmlReport.writeCounterSummaryPerClass(servicesCounter.getName(), "unknown");
  }