<commit_before>package us.kbase.common.performance.sortjson;

import java.awt.Color;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.LegendItem;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.block.Block;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.title.LegendTitle;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;


public class MeasureSortRunner {
	
	final static boolean HEADLESS = true;
	
	final static int NUM_SORTS = 500;
	final static int TIME_INTERVAL = 100; //ms
	final static String FILE = "src/us/kbase/common/performance/sortjson/83333.2.txt";
	final static String SORTER = "SortedJsonBytes";
	
	final static List<String> SORTERS = new ArrayList<String>();
	static {
		SORTERS.add("Jackson");
		SORTERS.add("SortedJsonBytes");
		SORTERS.add("SortedJsonFile-bytes");
		SORTERS.add("SortedJsonFile-file");
	}
	
	final static List<String> JARS = new ArrayList<String>();
	static {
		JARS.add("../jars/lib/jars/jackson/jackson-annotations-2.2.3.jar");
		JARS.add("../jars/lib/jars/jackson/jackson-core-2.2.3.jar");
		JARS.add("../jars/lib/jars/jackson/jackson-databind-2.2.3.jar");
	}
	final static String CODE_ROOT = "src";
	final static String CLASSPATH;
	static {
		String classpath = CODE_ROOT;
		for (String j: JARS) {
			classpath += ":" + j;
		}
		CLASSPATH = classpath;
	}
	
	final static String MEAS_CLASS_FILE =
			"us.kbase.common.performance.sortjson.MeasureSortJsonMem";
	
	final static String MEAS_JAVA_FILE =
			CODE_ROOT + "/" + MEAS_CLASS_FILE.replace(".", "/");
	
	public static void main(String[] args) throws Exception {
		
		
		compileMeasureSort();
		
		int numSorts = NUM_SORTS;
		int interval = TIME_INTERVAL;
		String file = FILE;
		
		Map<String, List<Double>> mems = new HashMap<String, List<Double>>();
		for (String sorter: SORTERS) {
			System.out.println("Running sorter: " + sorter);
			mems.put(sorter, runMeasureSort(numSorts, interval, file, sorter));
		}
		
		String title = "Title";//TODO info
		final JFreeChart chart = saveChart(new File("output.png"), mems, title);

		

		final FooFrame demo = new FooFrame("Title", chart);
		demo.pack();
		RefineryUtilities.centerFrameOnScreen(demo);
		demo.setVisible(true);
	}

	private static JFreeChart saveChart(File f, Map<String, List<Double>> mems,
			String title) throws IOException {
		XYSeriesCollection xyc = new XYSeriesCollection();
		for (String sorter: SORTERS) {
			XYSeries s = new XYSeries(sorter, false, false);
			double count = 1;
			for (Double mem: mems.get(sorter)) {
				s.add(++count, mem);
			}
			xyc.addSeries(s);
		}
		
		final JFreeChart chart = ChartFactory.createXYLineChart(
				title, 
				"Measurement #",
				"Used Memory (MB)",
				xyc,
				PlotOrientation.VERTICAL,
				true, // include legend
				true, // tooltips
				false // urls
				);
		
		chart.setBackgroundPaint(Color.white);

		final LegendTitle legend = chart.getLegend();
		for (Object li: legend.getItemContainer().getBlocks()) {
			((LegendItem)li).setShapeVisible(true);
		}

		final XYPlot plot = chart.getXYPlot();
		plot.setBackgroundPaint(Color.white);
		plot.setDomainGridlinePaint(Color.gray);
		plot.setRangeGridlinePaint(Color.gray);

		final ValueAxis domainAxis = plot.getDomainAxis();
		domainAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		
		ChartUtilities.saveChartAsPNG(f, chart, 700, 500);
		return chart;
	}
	
	private static class FooFrame extends ApplicationFrame {
		<commit_msg>Save text data<commit_after>package us.kbase.common.performance.sortjson;

import java.awt.Color;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.LegendItem;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.title.LegendTitle;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;


public class MeasureSortRunner {
	
	final static boolean HEADLESS = true;
	
	final static int NUM_SORTS = 500;
	final static int TIME_INTERVAL = 100; //ms
	final static String FILE = "src/us/kbase/common/performance/sortjson/83333.2.txt";
	
	final static List<String> SORTERS = new ArrayList<String>();
	static {
		SORTERS.add("Jackson");
		SORTERS.add("SortedJsonBytes");
		SORTERS.add("SortedJsonFile-bytes");
		SORTERS.add("SortedJsonFile-file");
	}
	
	final static List<String> JARS = new ArrayList<String>();
	static {
		JARS.add("../jars/lib/jars/jackson/jackson-annotations-2.2.3.jar");
		JARS.add("../jars/lib/jars/jackson/jackson-core-2.2.3.jar");
		JARS.add("../jars/lib/jars/jackson/jackson-databind-2.2.3.jar");
	}
	final static String CODE_ROOT = "src";
	final static String CLASSPATH;
	static {
		String classpath = CODE_ROOT;
		for (String j: JARS) {
			classpath += ":" + j;
		}
		CLASSPATH = classpath;
	}
	
	final static String MEAS_CLASS_FILE =
			"us.kbase.common.performance.sortjson.MeasureSortJsonMem";
	
	final static String MEAS_JAVA_FILE =
			CODE_ROOT + "/" + MEAS_CLASS_FILE.replace(".", "/");
	
	public static void main(String[] args) throws Exception {
		
		
		compileMeasureSort();
		
		int numSorts = NUM_SORTS;
		int interval = TIME_INTERVAL;
		String file = FILE;
		String title = "Title";//TODO info
		
		Map<String, List<Double>> mems = new LinkedHashMap<String, List<Double>>();
		for (String sorter: SORTERS) {
			System.out.println("Running sorter: " + sorter);
			mems.put(sorter, runMeasureSort(numSorts, interval, file, sorter));
		}
		
		String params = String.format(
				"Sorts: %s, Interval (ms): %s, file: %s, size (MB): %,.2f",
				numSorts, interval, file, new File(file).length() / 1000000.0);
		final JFreeChart chart = saveChart(new File("output.png"), mems, title, params);
		saveData(new File("output.txt"), mems, title, params);

		

		final FooFrame demo = new FooFrame("Title", chart);
		demo.pack();
		RefineryUtilities.centerFrameOnScreen(demo);
		demo.setVisible(true);
	}

	private static void saveData(File file, Map<String, List<Double>> mems,
			String title, String params) throws IOException {
		BufferedWriter bw = new BufferedWriter(new FileWriter(file));
		bw.write(title + "\n");
		bw.write(params + "\n");
		for (String s: mems.keySet()) {
			bw.write(s + "\n");
			for (Double m: mems.get(s)) {
				bw.write(Double.toString(m) + "\n");
			}
			bw.write("\n");
		}
		bw.flush();
		bw.close();
	}

	private static JFreeChart saveChart(File f, Map<String, List<Double>> mems,
			String title, String params) throws IOException {
		XYSeriesCollection xyc = new XYSeriesCollection();
		for (String sorter: SORTERS) {
			XYSeries s = new XYSeries(sorter, false, false);
			double count = 1;
			for (Double mem: mems.get(sorter)) {
				s.add(++count, mem);
			}
			xyc.addSeries(s);
		}
		
		final JFreeChart chart = ChartFactory.createXYLineChart(
				title + "\n" + params, 
				"Measurement #",
				"Used Memory (MB)",
				xyc,
				PlotOrientation.VERTICAL,
				true, // include legend
				false, // tooltips
				false // urls
				);
		
		chart.setBackgroundPaint(Color.white);

		final LegendTitle legend = chart.getLegend();
		for (Object li: legend.getItemContainer().getBlocks()) {
			((LegendItem)li).setShapeVisible(true);
		}

		final XYPlot plot = chart.getXYPlot();
		plot.setBackgroundPaint(Color.white);
		plot.setDomainGridlinePaint(Color.gray);
		plot.setRangeGridlinePaint(Color.gray);

		final ValueAxis domainAxis = plot.getDomainAxis();
		domainAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		
		ChartUtilities.saveChartAsPNG(f, chart, 700, 500);
		return chart;
	}
	
	private static class FooFrame extends ApplicationFrame {
		
{'fraction_non_alphanumeric': 0.1333409376069351, 'fraction_numerical': 0.007414166761720087, 'mean_word_length': 4.266066066066066, 'pattern_counts': {'":': 2, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6815599', 'n_tokens_mistral': 3337, 'n_tokens_neox': 2953, 'n_words': 624}