-
Notifications
You must be signed in to change notification settings - Fork 537
Description
Replace Data Value with Object:
CloudDataCenterBroker.java
Replace primitive fields like cloudletsSubmitted, vmsRequested, vmsAcks, vmsDestroyed with dedicated classes or objects
Benefits: Encapsulation or additional behavior.
// Class for CloudletSubmission
class CloudletSubmission {
private int count;
public CloudletSubmission() {
count = 0;
}
public void increment() {
count++;
}
public int getCount() {
return count;
}
}
// Class for VmRequest
class VmRequest {
private int count;
public VmRequest() {
count = 0;
}
public void increment() {
count++;
}
public int getCount() {
return count;
}
}
Inline Method
Problem Description The getSeekTime(in size) method is a simple method that is used as a getter twice.
Benefits Consider writing the expression on a single line to avoid using the temporary variable seekTime.
public HarddriveStorage(double capacity) throws ParameterException {
obj = fileList.get(index);
double transferTime = getTransferTime(obj.getSize());
obj.setTransactionTime(getSeekTime(size)+transferTime);
return obj;
}
Move method
CloudSim Host.java
Issue Description
Methods related to VM migration can be moved to a dedicated VmMigrationManager class.
Benefits: Helps with the single responsibility principle by balancing the load of the main class into helper classes.
public class MigratingVms{
public void addMigratingInVm(Vm vm){}
public void removeMigratingVm(Vm vm){}
public void reallocateMigratingInVms(){}
}
Extract method
Problem Description: We apply Extract method because it breaks down the long and complex ‘main’ into smaller methods like initializeCloudSim, createAndSubmitVm, and createAndSubmitCloudlet, making the program flow clearer and easier to follow.
Benefits:
Maintainability: Allows modifying specific parts of the configuration (like VMs or Cloudlets) without affecting the rest of the code, simplifying management and updating.
Modularity: Facilitates reusing methods in other contexts or tests, reducing code duplication.
Debugging: Helps identify and fix bugs more easily by isolating logic into separate methods.
Previous Design:
From line 51 to 157 the main method. More than 100 lines.
Paragraph locked by Anonymous User
public class CloudSimExample1 {
private static List<Vm> vmlist;
private static List<Cloudlet> cloudletList;
public static void main(String[] args) {
Log.printLine("Starting CloudSimExample1...");
try {
initializeCloudSim();
Datacenter datacenter0 = createDatacenter("Datacenter_0");
DatacenterBroker broker = createBroker();
createAndSubmitVm(broker);
createAndSubmitCloudlet(broker);
runSimulation();
printResults(broker);
Log.printLine("CloudSimExample1 finished!");
} catch (Exception e) {
e.printStackTrace();
Log.printLine("Unwanted errors happened");
}
}
private static void initializeCloudSim() {
int num_user = 1; // number of cloud users
Calendar calendar = Calendar.getInstance(); // Calendar whose fields have been initialized with the current date and time.
boolean trace_flag = false; // trace events
CloudSim.init(num_user, calendar, trace_flag);
}
private static Datacenter createDatacenter(String name) {
// Create and return a Datacenter instance
return new Datacenter(name, ...); // Provide necessary parameters for Datacenter creation
}
private static DatacenterBroker createBroker() {
// Create and return a DatacenterBroker instance
return new DatacenterBroker("Broker");
}
private static void createAndSubmitVm(DatacenterBroker broker) {
vmlist = new ArrayList<>();
int vmid = 0;
int mips = 1000;
long size = 10000; // image size (MB)
int ram = 512; // vm memory (MB)
long bw = 1000;
int pesNumber = 1; // number of cpus
String vmm = "Xen"; // VMM name
Vm vm = new Vm(vmid, broker.getId(), mips, pesNumber, ram, bw, size, vmm, new CloudletSchedulerTimeShared());
vmlist.add(vm);
broker.submitVmList(vmlist);
}
private static void createAndSubmitCloudlet(DatacenterBroker broker) {
cloudletList = new ArrayList<>();
int id = 0;
long length = 400000;
long fileSize = 300;
long outputSize = 300;
UtilizationModel utilizationModel = new UtilizationModelFull();
Cloudlet cloudlet = new Cloudlet(id, length, 1, fileSize, outputSize, utilizationModel, utilizationModel, utilizationModel);
cloudlet.setUserId(broker.getId());
cloudlet.setVmId(0); // Assuming VM id is 0
cloudletList.add(cloudlet);
broker.submitCloudletList(cloudletList);
}
private static void runSimulation() {
CloudSim.startSimulation();
CloudSim.stopSimulation();
}
private static void printResults(DatacenterBroker broker) {
List<Cloudlet> newList = broker.getCloudletReceivedList();
printCloudletList(newList);
}
private static void printCloudletList(List<Cloudlet> list) {
// Print the Cloudlet list
for (Cloudlet cloudlet : list) {
Log.printLine("Cloudlet ID: " + cloudlet.getCloudletId() +
" Status: " + cloudlet.getStatus() +
" Result: " + cloudlet.getActualCPUTime());
}
}
}
Extract class
Problem Description: The constructor of the cloudlet class has more than three parameters in the constructor thus violating long parameter list. The solution is as follows: Introduce a parameter object. Create a dedicated class to encapsulate the related parameters. Pass an instance of this class as a single parameter to the method.
Benefits: Improved readability, maintainability, and testability.
class CloudletNumParam{
int id;
int length;
int pesNumber;
CloudletNumParam(int id, int length, int pesNumber){
this.id = id;
this.length = length;
this.pesNumber = pesNumber;
}
}
class CloudletSizeParam{
int fileSize;
int outputSize;
CloudletSizeParam(int fileSize, int outputSize){
this.fileSize = fileSize;
this.outputSize = outputSize;
}
}
class CloudletUtilizationModelParam{
UtilizationModel um1;
UtilizationModel um2;
UtilizationModel um3;
CloudletUtilizationModel(UtilizationModel um1, UtilizationModel um2, UtilizationModel um3){
this.um1 = um1;
this.um2 = um2;
this.um3 = um3;
}
}
class Cloudlet{
int id;
int length;
int pesNumber;
int fileSize;
int outputSize;
UtilizationModel um1;
UtilizationModel um2;
UtilizationModel um3;
Cloudlet(CloudletNumParam cnp, CloudletSizeParam csp, CloudletUtilizationModelParam cump){
this.id = cnp.id;
this.length = cnp.length;
this.pesNumber = cnp.pesNumber;
this.fileSize = csp.fileSize;
this.outputSize = csp.outputSize;
this.um1 = cump.um1;
this.um2 = cump.um2;
this.um3 = cump.um3;
}
}
class UtilizationModel{}
Cloudlet cloudlet = new Cloudlet(new CloudletNumParam(1, 2, 3), new CloudletSizeParam(4, 5), new CloudletUtilizationModelParam(new UtilizationModel(), new UtilizationModel()));