package org.spbu.benchmark.web;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

import org.spbu.benchmark.*;
import org.spbu.benchmark.web.HttpAction.Method;

public class Test extends LoggingSupport {
	public void run(){
		log.debug("Testing scenario");		
		ThreadingSettings settings = new ThreadingSettings();
		settings.setLaunchInterval(1);
		settings.setTotalThreads(100);
		//settings.setMaxConcurrentThreads(40);
		List<Scenario> sList = new ArrayList<Scenario>();
		int[] empList = {2106,2476,2533,2566,3437,3656,3659,3833,3837,1093,1094,1123,1124,1089,1099,1117,1125,1103,1128,1140,1142,1095,1139,1148,1120,1519,1523,1524,1531,1522,1525,1734,1904,1944,2100,2347,2536,2704,2716,2718,3081,3225,3830,3831,3838,3840,3841,1149,1109,1092,2054,1113,1110,1102,1101,1096,1625,1699,1517,2070,2071,1131,3661,1105,1104,2720,3501,1112,1133,1144,1116,1121,1130,1920,1921,1145,1901};
		String url = "http://localhost:8080/scripts/wsisa.dll/WService=wsbroker/";
		int division = 101;
		for(int i =0;i<empList.length;i++)
		{
		HttpScenario s = new HttpScenario();
		s.addAction(new HttpAction(url + "index.html?s="+division,Method.GET));
		s.addAction(new HttpAction(url + "index.html",Method.POST)
		.addParameter("s", division)
		.addParameter("empCode", empList[i]));
		s.addAction(new HttpAction(url + "frames.html",Method.POST)
		.addParameter("s", division)
		.addParameter("empCode", empList[i]));		
		s.addAction(new HttpAction(url + "workhours.html",Method.POST)
		.addParameter("s", division)
		.addParameter("empCode", empList[i]));		
		s.addAction(new HttpAction(url + "workhours.html",Method.POST)
		.addParameter("s", division)
		.addParameter("empCode", empList[i])
		.addParameter("action", "arrival"));		
		s.addAction(new HttpAction(url + "workhours.html",Method.POST)
		.addParameter("s", division)
		.addParameter("empCode", empList[i])
		.addParameter("action","departure"));				
		sList.add(s);
		}
		Benchmark b = new DefaultBenchmark(sList, settings);		
		try{
			BenchmarkResult bresult = b.start();
			NumberFormat f = new DecimalFormat("00.00000");
			results.info("Benchmark success\n"+bresult.succeeded()+" scenarios succeeded, " + bresult.failed()+" scenarios failed\n"+
					"Response time: "+f.format((float)bresult.avgResponse()/1000)+" average, "+
					f.format((float)bresult.maxResponse()/1000)+ " max, "+
					f.format((float)bresult.minResponse()/1000)+ " min\n"+
					"Average throughput: "+f.format(bresult.throughput()) + " requests/second");
		}catch(BenchmarkException be){
			results.error("Error:", be);
		}
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Test t = new Test();
		t.run();

	}

}
