예제 #1
0
  public void crunchJobFileCounter(String jobId, File jobFile) {
    List<String> fileContentLines = readFileContentAsList(jobFile);

    if (fileContentLines.size() > 0) {
      String newFile = jobFile.getAbsolutePath().replace("cumulative", FILE_EXTENSION);
      BufferedWriter bw = null;
      try {
        bw = FileHelper.bufferedWriter(newFile, true);

        LastPoint lastPoint = this.fileLastCrunchPointMap.get(jobFile.getAbsolutePath());
        if (lastPoint == null) {
          String firstContentLine = fileContentLines.remove(0);
          String[] tokens = firstContentLine.split(",");
          lastPoint = new LastPoint(Long.parseLong(tokens[0]), Long.parseLong(tokens[1]));
        }

        while (fileContentLines.size() > 0) {
          String currentContentLine = fileContentLines.remove(0);
          String[] tokens = currentContentLine.split(",");
          long currentContentTimeMS = Long.parseLong(tokens[0]);
          long currentContentCount = Long.parseLong(tokens[1]);
          long opsDone = currentContentCount - lastPoint.count;
          long timeTakenMS = currentContentTimeMS - lastPoint.time;
          float timeTakenSec = (float) timeTakenMS / MathConstant.THOUSAND;
          float tps = opsDone / timeTakenSec;

          CounterStatsInstance counterStatsInstance =
              new CounterStatsInstance()
                  .setTime(Clock.dateFromMS(currentContentTimeMS))
                  .setCount(currentContentCount)
                  .setThroughput(tps);
          bw.write(objectMapper.writeValueAsString(counterStatsInstance) + "\n");
          bw.flush();

          BufferedWriter bwLast = FileHelper.bufferedWriter(newFile + ".last", false);
          bwLast.write(objectMapper.writeValueAsString(counterStatsInstance) + "\n");
          bwLast.flush();

          lastPoint = new LastPoint(currentContentTimeMS, currentContentCount);
          this.fileLastCrunchPointMap.put(jobFile.getAbsolutePath(), lastPoint);
        }

      } catch (FileNotFoundException e) {
        e
            .printStackTrace(); // To change body of catch statement use File | Settings | File
                                // Templates.
      } catch (IOException e) {
        e
            .printStackTrace(); // To change body of catch statement use File | Settings | File
                                // Templates.
      } finally {
        try {
          FileHelper.close(bw);
        } catch (IOException e) {
          e
              .printStackTrace(); // To change body of catch statement use File | Settings | File
                                  // Templates.
        }
      }
    }
  }