Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2011 JBoss Inc
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.drools.planner.benchmark.statistic.memoryuse;
 
 import java.io.File;
 import java.io.Writer;
 import java.util.List;
 import java.util.Map;
 
 import  org.jfree.chart.JFreeChart;
 import  org.jfree.chart.axis.NumberAxis;
 import  org.jfree.chart.plot.PlotOrientation;
 import  org.jfree.chart.plot.XYPlot;
 import  org.jfree.chart.renderer.xy.XYAreaRenderer2;
 import  org.jfree.chart.renderer.xy.XYItemRenderer;
 import  org.jfree.data.xy.XYSeries;
 import  org.jfree.data.xy.XYSeriesCollection;
 
 public class MemoryUseStatistic extends AbstractSolverStatistic {
 
     private List<StringconfigNameList = new ArrayList<String>();
     // key is the configName
             = new LinkedHashMap<StringMemoryUseStatisticListener>();
 
     public void addListener(Solver solverString configName) {
         if (.contains(configName)) {
             throw new IllegalArgumentException("Cannot add a listener with the same configName (" + configName
                     + ") twice.");
         }
         .add(configName);
         MemoryUseStatisticListener statisticListener = new MemoryUseStatisticListener();
         ((DefaultSolversolver).addSolverPhaseLifecycleListener(statisticListener);
         .put(configNamestatisticListener);
     }
 
     public void removeListener(Solver solverString configName) {
         MemoryUseStatisticListener statisticListener = .get(configName);
         ((DefaultSolversolver).removeSolverPhaseLifecycleListener(statisticListener);
     }
 
     // ************************************************************************
     // Write methods
     // ************************************************************************
 
     public CharSequence writeStatistic(File solverStatisticFilesDirectoryString baseName) {
         StringBuilder htmlFragment = new StringBuilder();
         htmlFragment.append(writeCsvStatistic(solverStatisticFilesDirectorybaseName));
         htmlFragment.append(writeGraphStatistic(solverStatisticFilesDirectorybaseName));
         return htmlFragment;
     }
 
     private List<MemoryUseScvLineextractCsvLineList() {
         Map<LongMemoryUseScvLinetimeToBestScoresLineMap = new HashMap<LongMemoryUseScvLine>();
         for (Map.Entry<StringMemoryUseStatisticListenerlistenerEntry : .entrySet()) {
             String configName = listenerEntry.getKey();
             List<MemoryUseStatisticPointstatisticPointList = listenerEntry.getValue().getStatisticPointList();
             for (MemoryUseStatisticPoint statisticPoint : statisticPointList) {
                 long timeMillisSpend = statisticPoint.getTimeMillisSpend();
                 MemoryUseScvLine line = timeToBestScoresLineMap.get(timeMillisSpend);
                 if (line == null) {
                     line = new MemoryUseScvLine(timeMillisSpend);
                     timeToBestScoresLineMap.put(timeMillisSpendline);
                 }
                 line.getConfigNameToMemoryUseMeasurementMap().put(configNamestatisticPoint.getMemoryUseMeasurement());
             }
         }
         List<MemoryUseScvLinecsvLineList = new ArrayList<MemoryUseScvLine>(timeToBestScoresLineMap.values());
         Collections.sort(csvLineList);
        return csvLineList;
    }
    protected class MemoryUseScvLine implements Comparable<MemoryUseScvLine> {
        private long timeMillisSpend;
        public MemoryUseScvLine(long timeMillisSpend) {
            this. = timeMillisSpend;
        }
        public long getTimeMillisSpend() {
            return ;
        }
            return ;
        }
        public int compareTo(MemoryUseScvLine other) {
            return  < other.timeMillisSpend ? -1 : ( > other.timeMillisSpend ? 1 : 0);
        }
    }
    private CharSequence writeCsvStatistic(File solverStatisticFilesDirectoryString baseName) {
        List<MemoryUseScvLinecsvLineList = extractCsvLineList();
        File csvStatisticFile = new File(solverStatisticFilesDirectorybaseName + "MemoryUseStatistic.csv");
        Writer writer = null;
        try {
            writer = new OutputStreamWriter(new FileOutputStream(csvStatisticFile), "utf-8");
            writer.append("\"TimeMillisSpend\"");
            for (String configName : ) {
                writer.append(",\"").append(configName.replaceAll("\\\"""\\\"")).append(" used\"");
                writer.append(",\"").append(configName.replaceAll("\\\"""\\\"")).append(" max\"");
            }
            writer.append("\n");
            for (MemoryUseScvLine line : csvLineList) {
                writer.write(Long.toString(line.getTimeMillisSpend()));
                for (String configName : ) {
                    writer.append(",");
                    MemoryUseMeasurement memoryUseMeasurement = line.getConfigNameToMemoryUseMeasurementMap()
                            .get(configName);
                    if (memoryUseMeasurement != null) {
                        writer.append(Long.toString(memoryUseMeasurement.getUsedMemory()));
                        writer.append(",");
                        writer.append(Long.toString(memoryUseMeasurement.getMaxMemory()));
                    }
                }
                writer.append("\n");
            }
        } catch (IOException e) {
            throw new IllegalArgumentException("Problem writing csvStatisticFile: " + csvStatisticFilee);
        } finally {
            IOUtils.closeQuietly(writer);
        }
        return "  <p><a href=\"" + csvStatisticFile.getName() + "\">CVS file</a></p>\n";
    }
    private CharSequence writeGraphStatistic(File solverStatisticFilesDirectoryString baseName) {
        XYSeriesCollection seriesCollection = new XYSeriesCollection();
        for (Map.Entry<StringMemoryUseStatisticListenerlistenerEntry : .entrySet()) {
            String configName = listenerEntry.getKey();
            XYSeries usedSeries = new XYSeries(configName + " used");
            XYSeries maxSeries = new XYSeries(configName + " max");
            List<MemoryUseStatisticPointstatisticPointList = listenerEntry.getValue().getStatisticPointList();
            for (MemoryUseStatisticPoint statisticPoint : statisticPointList) {
                long timeMillisSpend = statisticPoint.getTimeMillisSpend();
                MemoryUseMeasurement memoryUseMeasurement = statisticPoint.getMemoryUseMeasurement();
                usedSeries.add(timeMillisSpendmemoryUseMeasurement.getUsedMemory());
                maxSeries.add(timeMillisSpendmemoryUseMeasurement.getMaxMemory());
            }
            seriesCollection.addSeries(usedSeries);
            seriesCollection.addSeries(maxSeries);
        }
        NumberAxis xAxis = new NumberAxis("Time millis spend");
        xAxis.setNumberFormatOverride(new MillisecondsSpendNumberFormat());
        NumberAxis yAxis = new NumberAxis("Memory");
        yAxis.setAutoRangeIncludesZero(false);
        XYItemRenderer renderer = new XYAreaRenderer2();
        XYPlot plot = new XYPlot(seriesCollectionxAxisyAxisrenderer);
        plot.setOrientation(PlotOrientation.VERTICAL);
        JFreeChart chart = new JFreeChart(baseName + " memory use statistic",
                JFreeChart.DEFAULT_TITLE_FONT, plottrue);
        BufferedImage chartImage = chart.createBufferedImage(1024, 768);
        File graphStatisticFile = new File(solverStatisticFilesDirectorybaseName + "MemoryUseStatistic.png");
        OutputStream out = null;
        try {
            out = new FileOutputStream(graphStatisticFile);
            ImageIO.write(chartImage"png"out);
        } catch (IOException e) {
            throw new IllegalArgumentException("Problem writing graphStatisticFile: " + graphStatisticFilee);
        } finally {
            IOUtils.closeQuietly(out);
        }
        return "  <img src=\"" + graphStatisticFile.getName() + "\"/>\n";
    }
New to GrepCode? Check out our FAQ X