软件测试——进程调度(短作业优先调度算法+先来先服务算法)测试
Posted Starzkg
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了软件测试——进程调度(短作业优先调度算法+先来先服务算法)测试相关的知识,希望对你有一定的参考价值。
被测代码
Schedule
package net.mooctest;
import java.util.ArrayList;
import java.util.List;
public class Schedule {
public List<Double> FCFS(List<Work> works) {
double avgCircleTime = 0;
double avgCircleTimeWP = 0;
List<Double> lst = new ArrayList<Double>();
for (int i = 0; i < works.size(); i++) {
// works.get(i).getArrivalTime();
// works.get(i).getServiceTime();
if (i != 0) {
works.get(i).setBeginTime(works.get(i - 1).getEndTime());
} else {
works.get(i).setBeginTime(works.get(i).getArrivalTime());
}
works.get(i)
.setEndTime(
works.get(i).getBeginTime()
+ works.get(i).getServiceTime());
avgCircleTime += works.get(i).getCircleTime();
avgCircleTimeWP += works.get(i).getCircleWPTime();
}
avgCircleTime /= works.size();
avgCircleTimeWP /= works.size();
lst.add(avgCircleTime);
lst.add(avgCircleTimeWP);
return lst;
}
/**
* 改后的短作业优先调度算法
*
* @param works
* @return
*/
public List<Double> SJF(List<Work> works) {
List<Double> lst = new ArrayList<Double>();
double avgCircleTime = 0;
double avgCircleTimeWP = 0;
List<Work> ins = new ArrayList<Work>();
int i = 0;
int counter = 0; // 已完成作业的计数器
// 判断是否已经把所有的作业都处理完毕
while (counter < works.size()) {
// 找到所有未执行,且到达时间小于i的作业
for (int j = 0; j < works.size(); j++) {
if (works.get(j).isIn())
continue;
if (works.get(j).getArrivalTime() <= i) {
ins.add(works.get(j));
}
}
if (ins.isEmpty()) {
i++;
} else {
ins = sortByServiceTime(ins);
// 将当前已到达的作业中,服务时间最短的作业加入到CPU执行
Work now = ins.get(0);
now.setBeginTime(i);
now.setEndTime(now.getBeginTime() + now.getServiceTime());
now.setIn(); // 标记作业已调度
counter++;
i = now.getEndTime(); // 更新当前时间
ins.clear(); // 清空临时数组ins
}
}
for (int m = 0; m < works.size(); m++) {
avgCircleTime += works.get(m).getCircleTime();
avgCircleTimeWP += works.get(m).getCircleWPTime();
}
avgCircleTime = avgCircleTime / works.size();
avgCircleTimeWP = avgCircleTimeWP / works.size();
lst.add(avgCircleTime);
lst.add(avgCircleTimeWP);
return lst;
}
/**
* 对加入到系统中的作业依据服务时间多少进行排序<BR>
* 然后直接返回
*
* @param ins
* @return ins
*/
public static List<Work> sortByServiceTime(List<Work> ins) {
for (int i = 0; i < ins.size(); i++) {
for (int j = i + 1; j < ins.size(); j++) {
Work aw = ins.get(i);
int a = aw.getServiceTime();
Work bw = ins.get(j);
int b = bw.getServiceTime();
if (a > b) {
ins.remove(j);
ins.add(i, bw);
}
}
}
return ins;
}
}
Work
package net.mooctest;
public class Work {
/** 作业名*/
private String name;
/** 作业到达时间*/
private int arrivalTime;
/** 作业服务时间*/
private int serviceTime;
/** 开始执行时间*/
private int beginTime;
/** 完成时间*/
private int endTime;
/** 作业是否调入系统*/
private boolean in=false;
/**
* 作业已调入
*/
public void setIn(){
this.in=true;
}
/**
* 判断作业是否已调入系统
* @return
*/
public boolean isIn(){
return this.in;
}
/**
* Constructor
* @param name
* @param t1
* @param t2
*/
public Work(String name,int t1,int t2){
this.name=name;
this.arrivalTime=t1;
this.serviceTime=t2;
}
/**
* 设置开始执行时间
* @param t
*/
public void setBeginTime(int t){
this.beginTime=t;
}
/**
* 获取开始时间
* @return
*/
public int getBeginTime(){
return this.beginTime;
}
/**
* 设置完成时间
* @param t
*/
public void setEndTime(int t){
this.endTime=t;
}
/**
* 获取结束时间
* @return
*/
public int getEndTime(){
return this.endTime;
}
/**
* 计算“周转时间”=完成时间-到达时间
* @return int
*/
public int getCircleTime(){
return this.endTime-this.arrivalTime;
}
/**
* 计算“带权周转时间”=周转时间/服务时间
* @return
*/
public double getCircleWPTime(){
return ((double)getCircleTime())/this.serviceTime;
}
/**
* 计算"响应比"=(等待时间+要求服务时间)/要求服务时间=响应时间/要求服务时间
* @return
*/
public int getPriority(){
//TODO
return 0;
}
/**
*获取到达时间
* @return
*/
public int getArrivalTime(){
return this.arrivalTime;
}
/**
* 获取服务时间
* @return
*/
public int getServiceTime(){
return this.serviceTime;
}
}
测试代码
ScheduleTest
package net.mooctest;
import static org.junit.Assert.*;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
public class ScheduleTest {
@Test
public void testFCFS() {
Schedule s = new Schedule();
List<Work> l = new ArrayList<>();
l.add(new Work("P1",0,7));
l.add(new Work("P2",2,4));
l.add(new Work("P3",4,1));
l.add(new Work("P4",5,4));
List<Double> outcome = s.FCFS(l);
assertEquals(8.75,outcome.get(0),0.001);
assertEquals(3.5,outcome.get(1),0.001);
}
@Test
public void testSJF01(){
Schedule s = new Schedule();
List<Work> l = new ArrayList<>();
l.add(new Work("P1",1,7));
l.add(new Work("P2",2,4));
l.add(new Work("P3",4,1));
l.add(new Work("P4",5,4));
l.add(new Work("P5",1,5));
l.add(new Work("P6",2,4));
l.add(new Work("P7",4,1));
l.add(new Work("P8",5,6));
l.add(new Work("P9",3,3));
List<Double> outcome = s.SJF(l);
assertEquals(14.111,outcome.get(0),0.001);
assertEquals(3.518,outcome.get(1),0.001);
}
@Test
public void testWork(){
assertEquals(0,new Work("P1",0,5).getPriority());
}
}
测试结果
参考文章
以上是关于软件测试——进程调度(短作业优先调度算法+先来先服务算法)测试的主要内容,如果未能解决你的问题,请参考以下文章
操作系统| 作业调度算法平均周转时间平均带权周转时间先来先服务FCFS短作业优先SJF高优先权算法FPF高响应比优先算法HRRN
操作系统王道考研 p15 调度算法:先来先服务最短作业优先最高响应比优先