def test_should_sum_vm_billing_units(self):
        vms = [
            VMLog(started=0.0,
                  finished=SECS_IN_HOUR,
                  id=1,
                  price_for_billing_unit=1000.0,
                  **IRRELEVANT_VM_ATTRIBUTES),
            VMLog(started=0.0,
                  finished=SECS_IN_HOUR,
                  id=2,
                  price_for_billing_unit=1000.0,
                  **IRRELEVANT_VM_ATTRIBUTES),
            VMLog(started=0.0,
                  finished=SECS_IN_HOUR,
                  id=3,
                  price_for_billing_unit=1000.0,
                  **IRRELEVANT_VM_ATTRIBUTES)
        ]

        settings = ExperimentSettings(budget=2999.0,
                                      deadline=DEFAULT_DEADLINE,
                                      pricing_model=SIMPLE_PRICING_MODEL,
                                      billing_time_in_seconds=SECS_IN_HOUR,
                                      first_billing_time_in_seconds=None)

        result = constraints_validator.validate(vms, settings)

        self.assertFalse(result.is_valid)

        vms = [
            VMLog(started=0.0,
                  finished=SECS_IN_HOUR,
                  id=1,
                  price_for_billing_unit=10.0,
                  **IRRELEVANT_VM_ATTRIBUTES),
            VMLog(started=0.0,
                  finished=SECS_IN_HOUR,
                  id=2,
                  price_for_billing_unit=10.0,
                  **IRRELEVANT_VM_ATTRIBUTES),
            VMLog(started=0.0,
                  finished=SECS_IN_HOUR,
                  id=3,
                  price_for_billing_unit=10.0,
                  **IRRELEVANT_VM_ATTRIBUTES)
        ]

        settings = ExperimentSettings(
            budget=2999.0,
            deadline=DEFAULT_DEADLINE,
            pricing_model=GOOGLE_PRICING_MODEL,
            billing_time_in_seconds=SECS_IN_HOUR / 100,
            first_billing_time_in_seconds=SECS_IN_HOUR / 10)

        result = constraints_validator.validate(vms, settings)

        self.assertFalse(result.is_valid)
    def test_should_fail_if_vms_cost_exceeded_budget(self):
        vms = [
            VMLog(started=0.0,
                  finished=SECS_IN_HOUR,
                  id=1,
                  price_for_billing_unit=1000.0,
                  **IRRELEVANT_VM_ATTRIBUTES)
        ]

        settings = ExperimentSettings(budget=333.0,
                                      deadline=DEFAULT_DEADLINE,
                                      pricing_model=SIMPLE_PRICING_MODEL,
                                      billing_time_in_seconds=SECS_IN_HOUR,
                                      first_billing_time_in_seconds=None)

        result = constraints_validator.validate(vms, settings)

        self.assertFalse(result.is_valid)

        # first billing time is enough to exeed budget
        vms = [
            VMLog(started=0.0,
                  finished=SECS_IN_HOUR,
                  id=1,
                  price_for_billing_unit=100.0,
                  **IRRELEVANT_VM_ATTRIBUTES)
        ]

        settings = ExperimentSettings(budget=333.0,
                                      deadline=DEFAULT_DEADLINE,
                                      pricing_model=GOOGLE_PRICING_MODEL,
                                      billing_time_in_seconds=360,
                                      first_billing_time_in_seconds=1800)

        result = constraints_validator.validate(vms, settings)

        self.assertFalse(result.is_valid)

        # budget is exeeded after first_billing_time
        vms = [
            VMLog(started=0.0,
                  finished=SECS_IN_HOUR,
                  id=1,
                  price_for_billing_unit=40.0,
                  **IRRELEVANT_VM_ATTRIBUTES)
        ]

        result = constraints_validator.validate(vms, settings)

        self.assertFalse(result.is_valid)
    def test_should_count_full_hours_only(self):
        vms = [
            VMLog(started=0.0, finished=SECS_IN_HOUR + 1.0, id=1, **IRRELEVANT_VM_ATTRIBUTES)]

        settings = ExperimentSettings(budget=1000.0, vm_cost_per_hour=1000.0, deadline=DEFAULT_DEADLINE)

        result = constraints_validator.validate(vms, settings)

        self.assertFalse(result.is_valid)
    def test_should_pass_when_vms_cost_was_equal_to_budget(self):
        vms = [
            VMLog(started=0.0, finished=SECS_IN_HOUR, id=1, **IRRELEVANT_VM_ATTRIBUTES)]

        settings = ExperimentSettings(budget=1000.0, vm_cost_per_hour=1000.0, deadline=DEFAULT_DEADLINE)

        result = constraints_validator.validate(vms, settings)

        self.assertTrue(result.is_valid)
    def test_should_fail_when_vms_terminated_after_deadline(self):
        vms = [
            VMLog(started=14.0, finished=22.0, id=1, **IRRELEVANT_VM_ATTRIBUTES)]

        settings = ExperimentSettings(deadline=20.0, budget=DEFAULT_BUDGET, vm_cost_per_hour=DEFAULT_VM_COST)

        result = constraints_validator.validate(vms, settings)

        self.assertFalse(result.is_valid)
        self.assertLessEqual(1, len(result.errors))
    def test_should_pass_when_vms_terminated_equally_with_deadline(self):
        vms = [
            VMLog(started=0.0, finished=13.0, id=1, **IRRELEVANT_VM_ATTRIBUTES),
            VMLog(started=14.0, finished=20.0, id=2, **IRRELEVANT_VM_ATTRIBUTES)]

        settings = ExperimentSettings(deadline=20.0, budget=DEFAULT_BUDGET, vm_cost_per_hour=DEFAULT_VM_COST)

        result = constraints_validator.validate(vms, settings)

        self.assertTrue(result.is_valid)
        self.assertEqual([], result.errors)
    def test_should_pass_when_vms_cost_was_within_budget(self):
        vms = [
            VMLog(started=0.0,
                  finished=SECS_IN_HOUR,
                  id=1,
                  price_for_billing_unit=1000.0,
                  **IRRELEVANT_VM_ATTRIBUTES)
        ]

        settings = ExperimentSettings(budget=1234.0,
                                      deadline=DEFAULT_DEADLINE,
                                      pricing_model=SIMPLE_PRICING_MODEL,
                                      billing_time_in_seconds=SECS_IN_HOUR,
                                      first_billing_time_in_seconds=None)

        result = constraints_validator.validate(vms, settings)

        self.assertTrue(result.is_valid)

        vms = [
            VMLog(started=0.0,
                  finished=SECS_IN_HOUR,
                  id=1,
                  price_for_billing_unit=20.0,
                  **IRRELEVANT_VM_ATTRIBUTES)
        ]

        settings = ExperimentSettings(budget=1234.0,
                                      deadline=DEFAULT_DEADLINE,
                                      pricing_model=GOOGLE_PRICING_MODEL,
                                      billing_time_in_seconds=60,
                                      first_billing_time_in_seconds=600)

        result = constraints_validator.validate(vms, settings)

        self.assertTrue(result.is_valid)
    def test_should_fail_when_vms_terminated_after_deadline(self):
        vms = [
            VMLog(started=14.0,
                  finished=22.0,
                  id=1,
                  price_for_billing_unit=DEFAULT_VM_COST,
                  **IRRELEVANT_VM_ATTRIBUTES)
        ]

        settings = ExperimentSettings(deadline=20.0,
                                      budget=DEFAULT_BUDGET,
                                      pricing_model=SIMPLE_PRICING_MODEL,
                                      billing_time_in_seconds=SECS_IN_HOUR,
                                      first_billing_time_in_seconds=None)

        result = constraints_validator.validate(vms, settings)

        self.assertFalse(result.is_valid)
        self.assertLessEqual(1, len(result.errors))
def create_execution_log_from_events(events):
    log = ExecutionLog()
    settings_logs = [
        event for event in events
        if isinstance(event, ExperimentSettingsWithId)
    ]
    settings_logs = glue_fissured_events(settings_logs)
    settings = settings_logs[0]
    settings = ExperimentSettings(
        budget=settings.budget,
        deadline=settings.deadline,
        pricing_model=settings.pricing_model,
        billing_time_in_seconds=settings.billing_time_in_seconds,
        first_billing_time_in_seconds=settings.first_billing_time_in_seconds)
    log.settings = settings
    workflows = [event for event in events if isinstance(event, Workflow)]
    for workflow in workflows:
        log.add_workflow(workflow)
    task_events = [event for event in events if isinstance(event, TaskLog)]
    task_events = glue_fissured_events(task_events)
    for task_log in task_events:
        if task_log.finished is not None:
            log.add_event(EventType.TASK, task_log)
    transfer_events = [
        event for event in events if isinstance(event, TransferLog)
    ]
    transfer_events = glue_fissured_events(transfer_events)
    for transfer_log in transfer_events:
        if transfer_log.finished is not None:
            log.add_event(EventType.TRANSFER, transfer_log)
    vm_events = [event for event in events if isinstance(event, VMLog)]
    vm_events = glue_fissured_events(vm_events)
    for vm_log in vm_events:
        log.add_event(EventType.VM, vm_log)
    storage_state_events = [
        event for event in events if isinstance(event, StorageState)
    ]
    for storage_state in storage_state_events:
        log.add_event(EventType.STORAGE_STATE, storage_state)
    return log
def read_log(file_content):
    lines = file_content.splitlines()
    current_line = 0

    deadline, budget, vm_cost_per_hour = map(float,
                                             lines[current_line].split())
    current_line += 1

    settings = ExperimentSettings(deadline, budget, vm_cost_per_hour)

    vm_number = int(lines[current_line])
    current_line += 1

    vms = {}

    for i in xrange(0, vm_number):
        vm_info = lines[current_line].split()

        vm = VMLog(id=vm_info[0],
                   started=float_or_none(vm_info[1]),
                   finished=float_or_none(vm_info[2]))
        vms[vm.id] = vm

        current_line += 1

    workflows_number = int(lines[current_line])
    current_line += 1

    workflows = {}

    for i in xrange(0, workflows_number):
        workflow_info = lines[current_line].split()
        workflow = Workflow(id=workflow_info[0],
                            priority=int(workflow_info[1]),
                            filename=workflow_info[2])
        workflows[workflow.id] = workflow
        current_line += 1

    tasks_number = int(lines[current_line])
    current_line += 1

    tasks = []

    for i in xrange(0, tasks_number):
        task_info = lines[current_line].split()
        task = TaskLog(id=task_info[0],
                       workflow=task_info[1],
                       task_id=task_info[2],
                       vm=task_info[3],
                       started=float_or_none(task_info[4]),
                       finished=float_or_none(task_info[5]),
                       result=task_info[6])
        tasks.append(task)
        current_line += 1

    transfers_number = int(lines[current_line])
    current_line += 1

    transfers = []

    for i in xrange(0, transfers_number):
        transfer_info = lines[current_line].split()
        transfer = TransferLog(id=transfer_info[0],
                               vm=transfer_info[1],
                               started=float_or_none(transfer_info[2]),
                               finished=float_or_none(transfer_info[3]),
                               direction=transfer_info[4],
                               job_id=transfer_info[5],
                               file_id=transfer_info[6])
        transfers.append(transfer)
        current_line += 1

    execution_log = ExecutionLog()

    execution_log.settings = settings

    for task in tasks:
        execution_log.add_event(EventType.TASK, task)

    for transfer in transfers:
        execution_log.add_event(EventType.TRANSFER, transfer)

    for vm in vms.values():
        execution_log.add_event(EventType.VM, vm)

    for workflow in workflows.values():
        execution_log.add_workflow(workflow)

    return execution_log