public void testWithData() throws Exception {
    final Timer timer = new StubTimer();

    final SampleModelImplementation sampleModelImplementation =
        new SampleModelImplementation(
            new ConsoleProperties(m_resources, m_file),
            m_statisticsServices,
            timer,
            m_resources,
            null);

    final CumulativeStatisticsTableModel model =
        new CumulativeStatisticsTableModel(
            sampleModelImplementation, m_sampleModelViews, m_resources, m_swingDispatcherFactory);

    model.newTests(null, new ModelTestIndex());

    assertEquals(1, model.getRowCount());
    assertNull(model.getForeground(0, 0));
    assertNull(model.getBackground(0, 0));

    final Test[] tests = {
      new StubTest(1, "test 1"), new StubTest(2, "test 2"),
    };

    sampleModelImplementation.registerTests(Arrays.asList(tests));

    assertEquals(3, model.getRowCount());
    assertNull(model.getForeground(0, 0));
    assertNull(model.getBackground(0, 0));
    assertEquals("t3st 1", model.getValueAt(0, 0));
    assertEquals("test 1", model.getValueAt(0, 1));
    assertEquals("0", model.getValueAt(0, 3));
    assertNull(model.getForeground(0, 3));
    assertNull(model.getForeground(2, 3));

    final StatisticsSet statistics = m_statisticsServices.getStatisticsSetFactory().create();
    statistics.addValue(m_statisticsServices.getStatisticsIndexMap().getLongIndex("errors"), 1);

    final TestStatisticsMap testStatisticsMap = new TestStatisticsMap();
    testStatisticsMap.put(tests[0], statistics);
    sampleModelImplementation.addTestReport(testStatisticsMap);

    assertEquals("1", model.getValueAt(0, 3));
    assertEquals(Color.RED, model.getForeground(0, 3));
    assertNull(model.getForeground(0, 2));
    assertEquals(Color.RED, model.getForeground(2, 3));
  }
  @Test
  public void testUpdate() {
    SingleConsole singleConsole =
        new SingleConsole(12345) {
          @Override
          public long getCurrentRunningTime() {
            return 2000;
          }

          @Override
          public Map<String, Object> getStatictisData() {
            Map<String, Object> newMap = new HashMap<String, Object>();
            Map<Object, Object> errorMap = new HashMap<Object, Object>();
            errorMap.put("Tests", new Double(testCount));
            errorMap.put("Errors", new Double(errorCount));
            newMap.put("totalStatistics", errorMap);
            return newMap;
          }

          @Override
          protected void updateStatistics(
              StatisticsSet intervalStatisticsSnapshot,
              StatisticsSet cumulatedStatisticsSnapshot) {}

          @Override
          protected Map<String, Object> getStatisticData() {
            return new HashMap<String, Object>();
          }
        };

    singleConsole.update(null, null);
    singleConsole.startSampling(0);

    SampleModelImplementationEx sampleModelMock = mock(SampleModelImplementationEx.class);

    singleConsole.setSampleModel(sampleModelMock);
    StatisticExpression exp = mock(StatisticExpression.class);
    StatisticsSet statisticMock = mock(StatisticsSet.class);
    StatisticsSet statisticCumulatedMock = mock(StatisticsSet.class);
    when(statisticMock.snapshot()).thenReturn(statisticMock);
    when(statisticCumulatedMock.snapshot()).thenReturn(statisticCumulatedMock);
    when(exp.getDoubleValue(any(StatisticsSet.class))).thenReturn(3D);
    when(sampleModelMock.getTPSExpression()).thenReturn(exp);

    singleConsole.update(statisticMock, statisticCumulatedMock);
    singleConsole.update(statisticMock, statisticCumulatedMock);
  }
Exemple #3
0
  /*
   * (non-Javadoc)
   *
   * @see
   * net.grinder.console.model.SampleListener#update(net.grinder.statistics
   * .StatisticsSet, net.grinder.statistics.StatisticsSet)
   */
  @Override
  public void update(
      final StatisticsSet intervalStatistics, final StatisticsSet cumulativeStatistics) {
    try {
      if (!capture) {
        return;
      }
      samplingCount++;
      long currentPeriod = cumulativeStatistics.getValue(getSampleModel().getPeriodIndex());
      setTpsValue(sampleModel.getTPSExpression().getDoubleValue(intervalStatistics));
      checkTooLowTps(getTpsValues());
      updateStatistics(intervalStatistics, cumulativeStatistics);

      writeIntervalCsvData(intervalStatistics);
      int interval = getSampleModel().getSampleInterval();
      long gap = 1;
      if (samplingCount == 1) {
        lastSamplingPeriod = currentPeriod;
      } else {
        lastSamplingPeriod = lastSamplingPeriod + interval;
        gap = ((currentPeriod - lastSamplingPeriod) / interval);
      }
      // Adjust sampling delay.. run write data multiple times... when it
      // takes longer than 1
      // sec.

      samplingLifeCycleListener.apply(
          new Informer<SamplingLifeCycleListener>() {
            @Override
            public void inform(SamplingLifeCycleListener listener) {
              listener.onSampling(getReportPath(), intervalStatistics, cumulativeStatistics);
            }
          });
      for (int i = 0; i < (gap + 1); i++) {
        final boolean lastCall = (samplingCount == 1 && i == 0) || (samplingCount != 1 && i == gap);
        writeIntervalSummaryData(intervalStatistics, lastCall);
        if (interval >= (MIN_SAMPLING_INTERVAL_TO_ACTIVATE_TPS_PER_TEST)) {
          writeIntervalSummaryDataPerTest(intervalStatisticMapPerTest, lastCall);
        }
        samplingLifeCycleFollowupListener.apply(
            new Informer<SamplingLifeCycleFollowUpListener>() {
              @Override
              public void inform(SamplingLifeCycleFollowUpListener listener) {
                listener.onSampling(
                    getReportPath(), intervalStatistics, cumulativeStatistics, lastCall);
              }
            });
      }
      checkTooManyError(cumulativeStatistics);
      lastSamplingPeriod = lastSamplingPeriod + (interval * gap);
    } catch (RuntimeException e) {
      LOGGER.error("Error occurred while updating the statistics : {}", e.getMessage());
      LOGGER.debug("Details : ", e);
      throw e;
    }
  }
Exemple #4
0
 /**
  * Check if too many error has been occurred. If the half of total transaction is error for the
  * last 10 secs. It notifies the {@link ConsoleShutdownListener}
  *
  * @param cumulativeStatistics accumulated Statistics
  */
 private void checkTooManyError(StatisticsSet cumulativeStatistics) {
   StatisticsIndexMap statisticsIndexMap = getStatisticsIndexMap();
   long testSum =
       cumulativeStatistics.getCount(statisticsIndexMap.getLongSampleIndex("timedTests"));
   long errors = cumulativeStatistics.getValue(statisticsIndexMap.getLongIndex("errors"));
   if (((double) (testSum + errors)) / 2 < errors) {
     if (lastMomentWhenErrorsMoreThanHalfOfTotalTPSValue == 0) {
       lastMomentWhenErrorsMoreThanHalfOfTotalTPSValue = System.currentTimeMillis();
     } else if (isOverLowTpsThreshhold()) {
       LOGGER.warn(
           "Stop the test because the count of test error is more than"
               + " half of total tps for last {} seconds.",
           TOO_MANY_ERROR_TIME / 1000);
       getListeners()
           .apply(
               new Informer<ConsoleShutdownListener>() {
                 public void inform(ConsoleShutdownListener listener) {
                   listener.readyToStop(StopReason.TOO_MANY_ERRORS);
                 }
               });
       lastMomentWhenErrorsMoreThanHalfOfTotalTPSValue = 0;
     }
   }
 }