using System;
using System.Data;
using System.Threading;
using edu.jhu.Cas.Jobs;
using edu.jhu.Cas.Util;

namespace edu.jhu.Cas.Test {
	/// <summary>
	/// Summary description for Bench.
	/// </summary>
	public class Bench {
		private CasJobs cJobs;
		private MyDBJobs dbJobs;
		private int basePow,startPow,endPow;
		private long wsid;
		private const string tableName = "TizestTablest";
		private string[] cols;
		
		public Bench(long wsid_,int basePow_,int startPow_,int endPow_) {
			cJobs = new CasJobs();
			dbJobs = new MyDBJobs(); 
			wsid = wsid_;
			startPow = startPow_;
			endPow = endPow_;
			basePow = basePow_;
			cols = getColumns("photoObj");
		}

		public string[] getColumns(string tableName){
			DataSet ds = SqlFactory.userSelect("sp_columns_rowset photoObj",
				"Initial Catalog=bestdr1; Data Source = sdssad6; User ID = test; Password=riuzg54;");
			string[] cols = new string[ds.Tables[0].Rows.Count];
			for(int x=0;x<cols.Length;x++)
				cols[x] =  ds.Tables[0].Rows[x][3].ToString();
			return cols;
		}

		public void Run() {
	
			Console.WriteLine("Begining...");
			int c = -1,rows = -1;
			for(int x=startPow;x<=endPow;x++) {
			//	rows = (int)Math.Pow(basePow,x);
				rows = (int)Math.Pow(10,x);
				Console.WriteLine("ROW: "+rows);
				c = 0;
				for(int y = startPow;c<cols.Length;y++){
					c = (int)Math.Pow(basePow,y);
					if(c>=cols.Length)
						c = cols.Length;
					string qry = "select top "+rows+" "+cols[0];
					for(int a=1;a<c;a++)											
							qry+=" ,"+cols[a];
					qry+=" into MyDB."+tableName+" from photoObj";
					//string qry = "select top "+rows+" * from photoObj";
					Console.WriteLine("Running: "+qry);
					long jobid = cJobs.SubmitJob(wsid,qry,"BESTDR1","FAR: "+rows.ToString()+" rows "+c+" cols",2,0);
					//DataSet ds = cJobs.SubmitShortJob(wsid,qry,"BESTDR1","SHORT_FAR: "+rows.ToString()+" rows",0);
				
					int status = cJobs.StatusJob(wsid,jobid);
					while(status != QUERY_STATUS.FINISHED && status != QUERY_STATUS.FAILED){
						Thread.Sleep(5 * 1000);
						Console.Write(".");
						status = cJobs.StatusJob(wsid,jobid);
					}
					if(status == QUERY_STATUS.FINISHED)
						Console.WriteLine(" complete!");
					else
						Console.WriteLine(" FAILED!");
					Thread.Sleep(3 * 1000);
					Console.WriteLine("Dropping table\n");		
					dbJobs.DeleteTable(wsid,tableName);
					Thread.Sleep(3 * 1000);
				}
			}
			Console.ReadLine();
		}

	}
}
