Beispiel #1
0
 @Override
 public void handleMatch(P1 obj1, P2 obj2, Object auxiliaryData) {
   handledMatches.add(Util.tuple(obj1, obj2));
 }
public class ParametricMonitorLogger {

  protected final BindingStore bindingStore;
  protected final NodeManager nodeManager;

  private long timestamp;

  public ParametricMonitorLogger(BindingStore bindingStore, NodeManager nodeManager) {
    super();
    this.bindingStore = bindingStore;
    this.nodeManager = nodeManager;
    memStats = new SummaryStatistics();
    logMemoryConsumption();
  }

  private final Logger logger =
      getFileLogger(Util.getSystemProperty("prm4j.outputfile", "logs/prm4j-stats.log"));

  private SummaryStatistics memStats;

  private String experimentName = Util.getSystemProperty("prm4j.experimentName", "");

  public void log(long timestamp) {
    this.timestamp = timestamp;
    if (timestamp % 100 == 0) {
      logMemoryConsumption();
    }
  }

  private void logMemoryConsumption() {
    double memoryConsumption =
        (((double) (Runtime.getRuntime().totalMemory() / 1024) / 1024)
            - ((double) (Runtime.getRuntime().freeMemory() / 1024) / 1024));
    // filter NaNs
    if (!Double.isNaN(memoryConsumption)) {
      memStats.addValue(memoryConsumption);
    }
  }

  public void reset() {
    logMemoryConsumption();
    logger.log(Level.INFO, String.format("%s EVENTS (totalCount) %d", experimentName, timestamp));
    logger.log(
        Level.INFO,
        String.format("%s MATCHES (totalCount) %d", experimentName, MatchHandler.getMatchCount()));
    logger.log(
        Level.INFO,
        String.format(
            "%s MEMORY (mean/max) %f %f", experimentName, memStats.getMean(), memStats.getMax()));
    logger.log(
        Level.INFO,
        String.format(
            "%s BINDINGS (created/collected/stored) %d %d %d",
            experimentName,
            bindingStore.getCreatedBindingsCount(),
            bindingStore.getCollectedBindingsCount(),
            bindingStore.size()));
    logger.log(
        Level.INFO,
        String.format("%s NODES (created) %d", experimentName, nodeManager.getCreatedCount()));
    logger.log(
        Level.INFO,
        String.format(
            "%s MONITORS (createdAlive/updated/orphaned/collected/createdDead) %d %d %d %d %d",
            experimentName,
            AbstractMonitor.getCreatedMonitorsCount(),
            AbstractMonitor.getUpdateddMonitorsCount(),
            nodeManager.getOrphanedMonitorsCount(),
            nodeManager.getCollectedMonitorsCount(),
            DeadMonitor.getCreatedMonitorsCount()));
    memStats.clear();
  }

  /**
   * A simple file logger which outputs only the message.
   *
   * @param fileName path to the output file
   * @return the logger
   */
  private static Logger getFileLogger(String fileName) {
    // make sure parent directories exist
    new File(fileName).getParentFile().mkdirs();
    final Logger logger = Logger.getLogger(fileName);
    try {
      logger.setUseParentHandlers(false);
      Handler handler = new FileHandler(fileName, true);
      handler.setFormatter(
          new Formatter() {
            @Override
            public String format(LogRecord record) {
              return record.getMessage() + "\n";
            }
          });
      logger.addHandler(handler);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
    return logger;
  }
}