心血来潮再把以前的程序改一改
最近在学习系统容量分析,做了次作业算是对此有点初步的概念了。
东西写的比较乱,也很菜,先发上来,以后慢慢修改
任务概要:
给予一个系统,单预处理器分割每一个服务器需求,并且使用n个服务器(e.g. n = 10)来处理这些需求,并最后整合为一个响应并返回给客户端。
操作细节:
1. 忽略各部件之间的传输时间;
2. 预处理器优先分配任务给空闲的服务器。如果无空闲服务器,则等概率随机分配给服务器缓存里;
3. 需求序列分布符合 Ak = (A1k + a2k) * |(A1k - a2k)|。其中:A1k服从伽马分布,A2k服从三角分布;
4. 预处理器处理时间服从指数分布;
5. 各个服务器处理时间服从Pareto分布;
6. 测试用序列随机数手动设定;
7. 每个服务器的数据缓存设定为无限;
在这里,我们需要对需求序列进行相应和分割,然后再对字需求序列进行处理,最后得出响应时间。基于这三个划分,这里可以使用三个类:query.java,subquery.java,和server.java去分别定义此过程,并由主类Generator.java来调用并处理数据。
任务细节写的不是很详细,过段时间再补补~
附源程序代码:
/*
* This class is used to define the content of each arrival request.
* We set a requestID to distinct each request.
* ArrivalTime, ProcessTime and DepartureTime are used to record the process time of pre-processor for each request.
*
*/
//package simulation;
public class Request {
private int requestID;
private float arrivalTime;
private float processTime;
private float departureTime;
public Request (int ID, float arrivalTime, float processTime, float departureTime){
this.requestID = ID;
this.arrivalTime = arrivalTime;
this.processTime = processTime;
this.departureTime = departureTime;
}
public void setRequestID(int ID){
this.requestID = ID;
}
public void setArrivalTime(int ID, float time){
this.requestID = ID;
this.arrivalTime = time;
}
public void setProcessTime(int ID, float time){
this.requestID = ID;
this.processTime = time;
}
public void setDepartureTime(int ID, float time){
this.requestID = ID;
this.departureTime = time;
}
public int getRequestID(){
return this.requestID;
}
public float getArrivalTime(){
return this.arrivalTime;
}
public float getProcessTime(){
return this.processTime;
}
public float getDepartureTime(){
return this.departureTime;
}
}
//package simulation;
/*
* This class is used to define the content of each sub-request.
* We set a SubrequestID to distinct each sub-request.
* ArrivalTime, ServiceTime and DepartureTime are used to record the serving time of current servers for each sub-request.
*
*/
import java.util.ArrayList;
public class Subrequest {
private int requestID;
private float arrivalTime;
private float serviceTime;
private int serverID;
private float departureTime;
public Subrequest(int requestID, float arrivalTime, float serviceTime, int serverID, float departureTime){
this.requestID = requestID;
this.arrivalTime = arrivalTime;
this.serviceTime = serviceTime;
this.serverID = serverID;
this.departureTime = departureTime;
}
public void setRequestID(int ID){
this.requestID = ID;
}
public void setArrivalTime(float time){
this.arrivalTime = time;
}
public void setServiceTime(float time){
this.serviceTime = time;
}
public void setServerID(int ID){
this.serverID = ID;
}
public void setDepartureTime(float time){
this.departureTime = time;
}
public int getRequestID(){
return this.requestID;
}
public float getArrivalTime(){
return this.arrivalTime;
}
public float getServiceTime(){
return this.serviceTime;
}
public int getServerID(){
return this.serverID;
}
public float getDepartureTime(){
return this.departureTime;
}
}
/*
* This class is used to define the content of servers.
* There are 10 servers in this simulation.
* We set a Server_ID to distinct each server.
* startTime and endTime will be used to compare and identify whether the current server is busy or vacant.
* Infinite Buffer.
*
*/
//package simulation;
public class Server {
private int serverID;
private float startTime;
private float endTime;
public Server(int ID, float startTime, float endTime){
this.serverID = ID;
this.startTime = startTime;
this.endTime = endTime;
}
public void setSeverID(int ID){
this.serverID = ID;
}
public void setStartTime(float time){
this.startTime = time;
}
public void setEndTime(float time){
this.endTime = time;
}
public int getServerID(){
return this.serverID;
}
public float getStartTime(){
return this.startTime;
}
public float getEndTime(){
return this.endTime;
}
}
/*
* This class is the main class for dealing with the arrival requests, recording pre-processor serving time,
* the largest result of serving time for each sub-request in servers and return the response time for analyzing
* the performance of current system in different number of servers.
* Random seed, results of performance, number of servers will be recorded for further system capacity analysis.
* Ignore any transmit time.
*
*/
//package simulation;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Random;
public class Generator {
public static void main(String argv[]){
final int seed = Integer.parseInt(argv[0]);
final int nb_of_Request = Integer.parseInt(argv[1]);
final int nb_of_Server = Integer.parseInt(argv[2]);
Random rand = new Random(seed);
float[] random = new float[nb_of_Request];
for(int i = 0; i < nb_of_Request; i ++){
random[i] = rand.nextFloat();
}
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(3);
// System.out.println(df.format(decimalNumber));
float[] arrivalTime = new float[nb_of_Request];
float[] processTime = new float[nb_of_Request];
float[] serviceTime = new float[nb_of_Server * nb_of_Request];
for(int i = 0; i < nb_of_Request; i ++){
if(i == 0){
arrivalTime[i] = generateArrivalTime(random[i]);
}
else{
arrivalTime[i] = generateArrivalTime(random[i]) + arrivalTime[i - 1];
}
processTime[i] = generateProcessTime(nb_of_Server, random[i]);
// System.out.println(df.format(arrivalTime[i]) + " " + df.format(processTime[i]));
}
for(int i = 0; i < nb_of_Request * nb_of_Server; i ++){
serviceTime[i] = generateServiceTime(nb_of_Server, rand);
}
// System.out.println();
ArrayList <Request> requestList = new ArrayList<Request>();
for(int i = 0; i < nb_of_Request; i ++){
requestList.add(new Request(i, arrivalTime[i], processTime[i], 0));
if(i == 0){
requestList.get(i).setDepartureTime(i, arrivalTime[i] + processTime[i]);
}
else if(arrivalTime[i] >= requestList.get(i - 1).getDepartureTime()){
requestList.get(i).setDepartureTime(i, arrivalTime[i] + processTime[i]);
}
else{
requestList.get(i).setDepartureTime(i, requestList.get(i - 1).getDepartureTime() + processTime[i]);
}
}
ArrayList <Subrequest> subrequestList = new ArrayList <Subrequest>();
ArrayList <Server> serverTimeList = new ArrayList <Server>();
ArrayList <Integer> serverList = new ArrayList <Integer>();
ArrayList <Integer> idleList = new ArrayList <Integer>();
// Initiate server list
for(int i = 1; i < nb_of_Server + 1; i++){
serverList.add(i);
}
// System.out.println(serverList.size());
// Initiate server start/end time list
for(int i = 1; i < nb_of_Server + 1; i++){
serverTimeList.add(new Server(i, 0, 0));
}
// System.out.println(serverTimeList.size());
for(int i = 0 ; i < nb_of_Server * nb_of_Request; i++){
subrequestList.add(new Subrequest(i, requestList.get(i / nb_of_Server).getDepartureTime(), serviceTime[i], 0, 0));
// System.out.println(subrequestList);
idleList = generateIdleList(serverList, serverTimeList, subrequestList.get(i), nb_of_Server);
// System.out.println(idleList);
// System.out.println(idleList.size());
int randomServer = idleList.get(rand.nextInt(idleList.size()));
// System.out.println(randomServer);
subrequestList.get(i).setServerID(randomServer);
// float serverEndTIme = getEndTimeByServerID(randomServer, serverTimeList);
// System.out.println(serverEndTIme);
if(subrequestList.get(i).getArrivalTime() < serverTimeList.get(randomServer).getEndTime()){
// System.out.println();
subrequestList.get(i).setDepartureTime(serverTimeList.get(randomServer).getEndTime() + subrequestList.get(i).getServiceTime());
// for(int j = 0 ; j < nb_of_Server; j ++){
// if(serverTimeList.get(j).getServerID() == subrequestList.get(i).getServerID()){
// serverTimeList.get(j).setStartTime(serverEndTIme);
// serverTimeList.get(j).setEndTime(subrequestList.get(i).getDepartureTime());
// System.out.println(serverTimeList.get(j).getServerID() + " : " + serverTimeList.get(j).getStartTime() + " --- " + serverTimeList.get(j).getStartTime());
// break;
// }
// }
}
else{
subrequestList.get(i).setDepartureTime(subrequestList.get(i).getArrivalTime() + subrequestList.get(i).getServiceTime());
// for(int j = 0 ; j < nb_of_Server; j ++){
// if(serverTimeList.get(j).getServerID() == subrequestList.get(i).getServerID()){
// serverTimeList.get(j).setStartTime(subrequestList.get(i).getArrivalTime());
// serverTimeList.get(j).setEndTime(subrequestList.get(i).getDepartureTime());
// System.out.println(serverTimeList.get(j).getServerID() + " " + serverTimeList.get(j).getStartTime() + " " + serverTimeList.get(j).getStartTime());
// break;
// }
// }
}
// System.out.println(subrequestList);
// break;
for(int j = 0 ; j < nb_of_Server; j ++){
if(serverTimeList.get(j).getServerID() == subrequestList.get(i).getServerID()){
serverTimeList.get(j).setStartTime(subrequestList.get(i).getArrivalTime());
serverTimeList.get(j).setEndTime(subrequestList.get(i).getDepartureTime());
break;
}
}
}
float[] departureFinal = new float[nb_of_Request];
float[] responseTime = new float[nb_of_Request];
float[] meanResponseTime = new float[nb_of_Request];
for(int i = 0; i < nb_of_Request; i++){
departureFinal[i] = maximumDepartureTime(subrequestList, i, nb_of_Server);
responseTime[i] = departureFinal[i] - requestList.get(i).getArrivalTime();
if(i != 0){
for(int j = 0; j < responseTime.length; j++){
meanResponseTime[i] += responseTime[j];//subrequestList.get(i * nb_of_Server).getArrivalTime();
}
meanResponseTime[i] /= i;
}
else{
meanResponseTime[i] = responseTime[i];
}
System.out.println(
// df.format(requestList.get(i).getArrivalTime()) + "" +
// df.format(requestList.get(i).getProcessTime()) + "" +
// df.format(requestList.get(i).getDepartureTime()) + "" +
// df.format(subrequestList.get(i * nb_of_Server).getArrivalTime()) + "" +
// df.format(departureFinal[i] - subrequestList.get(i * nb_of_Server).getArrivalTime()) + "" +
// df.format(departureFinal[i]) + "" +
// df.format(responseTime[i]) + "" +
df.format(meanResponseTime[i])
);
}
// for(int i = 0; i < nb_of_Request; i ++){
// for(int j = 0; j < nb_of_Server; j ++){
// System.out.println(df.format(requestList.get(i).getArrivalTime()) + ""
// + df.format(requestList.get(i).getProcessTime())
// + " " + df.format(requestList.get(i).getDepartureTime()) + ""
// + /*(df.format(requestList.get(i).getDepartureTime() - requestList.get(i).getArrivalTime()))
// + " " + */df.format(serviceTime[i * nb_of_Server + j]));
// }
// }
}
// private static float getEndTimeByServerID(int randomServer,
// ArrayList<Server> serverTimeList) {
// // TODO Auto-generated method stub
// float time = 0;
// for(int i = 0; i < serverTimeList.size(); i ++){
// if(serverTimeList.get(i).getServerID() == randomServer){
// time = serverTimeList.get(i).getEndTime();
// break;
// }
// }
// return time;
// }
private static float maximumDepartureTime(
ArrayList<Subrequest> subrequestList, int i, int nb_of_Server) {
// TODO Auto-generated method stub
float time = 0;
for(int j = 0; j < nb_of_Server; j++){
if(subrequestList.get(i * nb_of_Server + j).getDepartureTime() > time){
time = subrequestList.get(i * nb_of_Server + j).getDepartureTime();
}
}
return time;
}
private static ArrayList<Integer> generateIdleList(
ArrayList<Integer> serverList, ArrayList<Server> serverTimeList,
Subrequest subrequest, int nb_of_Server) {
// TODO Auto-generated method stub
ArrayList <Integer> result = new ArrayList <Integer>();
for(int i = 0; i < nb_of_Server; i++){
if(serverTimeList.get(i).getEndTime() <= subrequest.getArrivalTime()){
// System.out.println(serverTimeList.get(i).getEndTime() + " <= " + subrequest.getArrivalTime());
result.add(i);
}
}
if(result.size() == 0){
result = serverList;
}
return result;
}
private static float generateServiceTime(int nb_of_Server, Random rand) {
// TODO Auto-generated method stub
float t = rand.nextFloat();
return (float)(10.38 / (Math.pow(nb_of_Server, 1.7) * Math.pow(t, (float)1.00 / 2.08)));
// while(Math.pow(t, (float)1.00 / 2.08) > Math.pow(nb_of_Server, 1.7)){
// t = rand.nextFloat();
// }
// return (float) (10.38 / Math.pow(t, (float)1.00 / 2.08));
}
private static float generateProcessTime(int nb_of_Server, float n) {
// TODO Auto-generated method stub
return (float) ((0.1 * nb_of_Server) * (-Math.log(n)));
}
private static float generateArrivalTime(float n) {
// TODO Auto-generated method stub
float a1 = (float) (0.6 * (-Math.log(n)));
float a2;
if(n < 0.5){
a2 = (float) (0.1 + 0.7 * Math.sqrt(2 * n));
}
else{
a2 = (float) (1.5 - 0.7 * Math.sqrt(2*(1 - n)));
}
return (a1 + a2) * Math.abs(a1 - a2);
}
}