首页 > 代码库 > Oracle Load Testing
Oracle Load Testing
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;
import oracle.oats.scripting.modules.basic.api.ScriptService;
/**
*
*/
/**
* @author 420309
*
*/
public class MyDatabank {
private static HashMap<String,int[]> DBDesc = new HashMap<String,int[]>();
private static List<String[]> DBList = new ArrayList<String[]>();
private static String logFile = null;
private static OutputStreamWriter logger = null;
@ScriptService oracle.oats.scripting.modules.http.api.HTTPService http;
@ScriptService oracle.oats.scripting.modules.adfload.api.AdfLoadService adfload;
public static synchronized void setLog(String file)throws Exception{
if(logger != null) return;
logFile = file;
logger = new OutputStreamWriter(new FileOutputStream(file));
}
public static synchronized void log(String str)throws Exception{
if(logFile == null)return;
if(logger == null){
logger = new OutputStreamWriter(new FileOutputStream(logFile));
}
//String time = new Date().getTime();
logger.write(new Date().getTime() + ":" + str + "\r\n");
logger.flush();
}
// synchronized 防止多个线程重复加载
public static synchronized void initDatabank(String fileCSV)throws Exception{
if(DBList.size() > 0) return;//是否已经加载数据,防止多线程下多次加载
log("initDatabank:"+fileCSV);
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(fileCSV)));
String rec = null;
try {
br.readLine();//Ignore CSV Header
while ((rec = br.readLine()) != null) {
//System.out.println("line:"+rec);
String[] cells = rec.split(",");
DBList.add(cells);
}
} catch (Exception e) {
throw e;
} finally {
if (br != null) {
br.close();
}
}
}
//LINE:based-1
public static synchronized void initDatabankDesc(String fileDesc)throws Exception{
if(DBDesc.size() > 0) return;
log("initDatabankDesc:"+fileDesc);
Properties props = new Properties();
props.load(new FileInputStream(fileDesc));
Enumeration<String> enums = (Enumeration<String>)props.propertyNames();
String name = null;
String value = http://www.mamicode.com/null;
int idx = -1;
int s,e;
while(enums.hasMoreElements()){
name = enums.nextElement();
value = http://www.mamicode.com/props.getProperty(name).trim();
idx = value.indexOf(‘,‘);
if(idx == -1) throw new Exception("File Format Error,‘,‘ Not Found");
s = Integer.parseInt(value.substring(0,idx)) - 1;//based-0
e = Integer.parseInt(value.substring(idx+1)) - 1;//based-0
int[] flag = new int[]{s,e,0};//[s,e]idx-0
//System.out.println("********s:"+s+",e:"+e);
//System.out.println("********name:"+name);
DBDesc.put(name, flag);
}
}
//col:based-0
public static String getNextDatabank(String name,int col)throws Exception{
int[] flag = DBDesc.get(name);
if(flag == null) throw new Exception("Name:["+name+"] Not Found");
Object obj = new Object();
int idx = -1;
synchronized(obj){
idx = flag[0]+flag[2];
if(idx > flag[1]) {
idx = flag[0];
flag[2] = 0;
}
flag[2]++;
}
String[] cells = DBList.get(idx);
if(cells == null)throw new Exception("Name:["+name+"],Row:["+(idx + 1)+"] Not Found");
log("getNextDatabank:name["+name+"],idx["+idx+"],col["+col+"] = ["+cells[col]+"]");
return cells[col];
}
public static int getDatabankCount(String name)throws Exception{
int[] flag = DBDesc.get(name);
if(flag == null) throw new Exception("Name:["+name+"] Not Found");
int count = flag[1] - flag[0] + 1;
log("getDatabankCount:"+count);
return count;
}
}
Oracle Load Testing