package us.kbase.common.performance.sortjson;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import org.nocrala.tools.texttablefmt.Table;

import us.kbase.common.performance.PerformanceMeasurement;
import us.kbase.common.utils.sortjson.SortedKeysJsonFile;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class MeasureSortJsonSpeed {
	
	private static final ObjectMapper SORT_MAPPER = new ObjectMapper();
	static {
		SORT_MAPPER.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
	}

	public static void main(String[] args) throws Exception {
		File f = new File("src/us/kbase/common/performance/sortjson/83333.2.txt");
		int sorts = 100;
		

		JsonNode jn = new ObjectMapper().readTree(f);
		byte[] b = new ObjectMapper().writeValueAsBytes(jn);
		
		System.out.println("File read into bytes[]. Start profiler, then hit enter to continue");
		Scanner s = new Scanner(System.in);
		s.nextLine();
//		PerformanceMeasurement js = measureJsonSort(b, sorts);
		PerformanceMeasurement skfj = measureSKJFSort(b, sorts);
		renderResults(Arrays.asList(skfj));
		
	}
	
	private static void renderResults(List<PerformanceMeasurement> pms) {
		final int width = 4;
		Table tbl = new Table(width);
		tbl.addCell("Operation");
		tbl.addCell("N");
		tbl.addCell("Mean time (s)");
		tbl.addCell("Std dev (s)");
		for (PerformanceMeasurement pm: pms) {
			tbl.addCell(pm.getName());
			tbl.addCell("" + pm.getN());
			tbl.addCell(String.format("%,.4f", pm.getAverageInSec()));
			tbl.addCell(String.format("%,.4f", pm.getStdDevInSec()));
		}
		System.out.println(tbl.render());
	}

	private static PerformanceMeasurement measureSKJFSort(byte[] b, int sorts)
			throws Exception {
		List<Long> m = new LinkedList<Long>();
		for (int i = 0; i < sorts; i++) {
			long start = System.nanoTime();
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			new SortedKeysJsonFile(b).writeIntoStream(baos);
			@SuppressWarnings("unused")
			byte[] t = baos.toByteArray();
			m.add(System.nanoTime() - start);
		}
		return new PerformanceMeasurement(m, "SortedKeysJsonFile JSON sort");
	}

	private static PerformanceMeasurement measureJsonSort(byte[] b, int sorts)
			throws Exception {
		List<Long> m = new LinkedList<Long>();
		for (int i = 0; i < sorts; i++) {
			long start = System.nanoTime();
			@SuppressWarnings("unchecked")
			Map<String, Object> d = SORT_MAPPER.readValue(b, Map.class);
			@SuppressWarnings("unused")
			byte[] t = SORT_MAPPER.writeValueAsBytes(d);
			m.add(System.nanoTime() - start);
		}
		return new PerformanceMeasurement(m, "ObjectMapper JSON sort");
	}
}

@@ -905,16 +905,52 @@
  = 100;%0A
+%09%09boolean pauseForProfiler = false;%0A
 %09%09%0A%0A%09%09Js
@@ -1050,17 +1050,41 @@
 (jn);%0A%09%09
-%0A
+if (pauseForProfiler) %7B%0A%09
 %09%09System
@@ -1169,16 +1169,17 @@
 ue%22);%0A%09%09
+%09
 Scanner 
@@ -1208,16 +1208,17 @@
 .in);%0A%09%09
+%09
 s.nextLi
@@ -1227,10 +1227,12 @@
 ();%0A
-//
+%09%09%7D%0A
 %09%09Pe
@@ -1343,16 +1343,88 @@
 sorts);%0A
+//%09%09PerformanceMeasurement skfjs = measureSKJFSortStringKeys(b, sorts);%0A
 %09%09render
@@ -1445,20 +1445,36 @@
 .asList(
-skfj
+js, skfj));//, skfjs
 ));%0A%09%09%0A%09
@@ -1979,24 +1979,596 @@
 der());%0A%09%7D%0A%0A
+%09private static PerformanceMeasurement measureSKJFSortStringKeys(byte%5B%5D b, int sorts)%0A%09%09%09throws Exception %7B%0A%09%09List%3CLong%3E m = new LinkedList%3CLong%3E();%0A%09%09for (int i = 0; i %3C sorts; i++) %7B%0A%09%09%09long start = System.nanoTime();%0A%09%09%09ByteArrayOutputStream baos = new ByteArrayOutputStream();%0A%09%09%09new SortedKeysJsonFile(b).setUseStringsForKeyStoring(true)%0A%09%09%09%09%09.writeIntoStream(baos);%0A%09%09%09@SuppressWarnings(%22unused%22)%0A%09%09%09byte%5B%5D t = baos.toByteArray();%0A%09%09%09m.add(System.nanoTime() - start);%0A%09%09%7D%0A%09%09return new PerformanceMeasurement(m, %22SortedKeysJsonFile JSON sort with String keys%22);%0A%09%7D%0A%09%0A
 %09private sta
@@ -3056,24 +3056,53 @@
 sort%22);%0A%09%7D%0A%0A
+%09@SuppressWarnings(%22unused%22)%0A
 %09private sta
@@ -3401,39 +3401,8 @@
 s);%0A
-%09%09%09@SuppressWarnings(%22unused%22)%0A
 %09%09%09b

Test sort performance with String keys