Ejemplo n.º 1
0
    async def test_same_score(self):
        coeffs = [0.001, 0.002, 0.1]
        offer1 = OfferProposalFactory(coeffs=coeffs)
        offer2 = OfferProposalFactory(coeffs=coeffs)

        assert await self.strategy.score_offer(
            offer1) == await self.strategy.score_offer(offer2)
Ejemplo n.º 2
0
    async def test_same_score(self):
        coeffs = [0.001, 0.002, 0.1]
        offer1 = OfferProposalFactory(
            **{"proposal__proposal__properties__linear_coeffs": coeffs})
        offer2 = OfferProposalFactory(
            **{"proposal__proposal__properties__linear_coeffs": coeffs})

        assert await self.strategy.score_offer(
            offer1) == await self.strategy.score_offer(offer2)
Ejemplo n.º 3
0
async def test_LeastExpensiveLinearPauyuMS_score(expected_time):
    prices = [0.0, 0.01, 0.03, 0.1, 0.3, 1.0, 3.0]

    triples = list(product(
        prices,
        repeat=3))  # get triples of (cpu_price, time_price, fixed_price)

    strategy = LeastExpensiveLinearPayuMS(expected_time_secs=expected_time)

    def cost(coeffs):
        return round(
            coeffs[0] * expected_time + coeffs[1] * expected_time + coeffs[2],
            11)

    scores = {
        coeffs: round(
            await strategy.score_offer(OfferProposalFactory(coeffs=coeffs)),
            11,
        )
        for coeffs in triples
    }

    # Sort coeffs by cost, from the most expensive to the least expensive
    triples_by_cost = sorted(triples,
                             key=lambda coeffs: (cost(coeffs), coeffs))
    # Sort coeffs by strategy score, from the lowest (worst) to the highest (best)
    triples_by_score = sorted(triples,
                              key=lambda coeffs: (-scores[coeffs], coeffs))

    assert triples_by_cost == triples_by_score
    assert all(SCORE_NEUTRAL < score < SCORE_TRUSTED
               for score in scores.values())
Ejemplo n.º 4
0
 async def test_score_unknown_price(self):
     offer = OfferProposalFactory(
         **{
             "proposal__proposal__properties__defined_usages": [
                 Counter.MAXMEM.value,
                 Counter.TIME.value,
             ]
         })
     assert await self.strategy.score_offer(offer) == SCORE_REJECTED
Ejemplo n.º 5
0
async def test_restricted_providers(bad_providers):
    """Test if the strategy restricts correct providers"""
    strategy = ProviderFilter(
        Always6(), lambda provider_id: provider_id not in bad_providers)

    for provider_id in (1, 2, 3):
        offer = OfferProposalFactory(provider_id=provider_id)
        expected_score = SCORE_REJECTED if provider_id in bad_providers else 6
        assert expected_score == await strategy.score_offer(offer)
Ejemplo n.º 6
0
async def test_default_strategy_update(dummy_yagna_engine):
    """Test if default strategy extended with ProviderFilter works as expected"""
    golem = Golem(budget=1, app_key="NOT_A_REAL_APPKEY")
    golem.strategy = ProviderFilter(golem.strategy,
                                    lambda provider_id: provider_id == 2)

    async with golem:
        for provider_id in (1, 2, 3):
            offer = OfferProposalFactory(provider_id=provider_id)
            expected_score = DEFAULT_OFFER_SCORE if provider_id == 2 else SCORE_REJECTED
            assert expected_score == await golem._engine._strategy.score_offer(
                offer)
Ejemplo n.º 7
0
async def test_respond_to_provider_offer(offer_props, expiration_secs, expected_props):
    strategy = GoodStrategy()
    demand = DemandBuilder()
    expiration = datetime.now() + timedelta(seconds=expiration_secs)
    demand.add(Activity(expiration=expiration))
    offer_kwargs = {"proposal__proposal__properties": offer_props}
    offer = OfferProposalFactory(**offer_kwargs)

    updated_demand = await strategy.respond_to_provider_offer(demand, offer)
    del updated_demand.properties["golem.srv.comp.expiration"]

    assert updated_demand.properties == expected_props
Ejemplo n.º 8
0
async def test_dynamic_change():
    """Test if changes in the is_allowed function are reflected in scores"""
    something_happened = False

    def is_allowed(provider_id):
        if something_happened:
            return False
        return True

    strategy = ProviderFilter(Always6(), is_allowed)

    for i in range(0, 5):
        offer = OfferProposalFactory(provider_id=1)
        expected_score = SCORE_REJECTED if i > 3 else 6
        assert expected_score == await strategy.score_offer(offer)

        if i == 3:
            something_happened = True
Ejemplo n.º 9
0
async def test_LeastExpensiveLinearPayuMS_price_caps():
    """Test if LeastExpenciveLinearPayuMS correctly handles price caps."""

    prices = [Decimal(p) for p in [0.0, 0.01, 1.0, 100.0]]
    epsilon = Decimal(0.0000001)

    triples = list(product(
        prices,
        repeat=3))  # get triples of (cpu_price, time_price, fixed_price)

    for cpu_price, time_price, fixed_price in triples:

        offer = OfferProposalFactory(
            **{
                "proposal__proposal__properties__linear_coeffs": (
                    cpu_price,
                    time_price,
                    fixed_price,
                )
            })

        async def _test_strategy(strategy, cpu_price_cap, time_price_cap,
                                 fixed_price_cap):
            score = await strategy.score_offer(offer)
            should_reject = (
                (cpu_price_cap is not None and cpu_price > cpu_price_cap)
                or (time_price_cap is not None and time_price > time_price_cap)
                or (fixed_price_cap is not None
                    and fixed_price > fixed_price_cap))
            assert should_reject == (score == SCORE_REJECTED), (
                f"failed for cpu_price_cap = {cpu_price_cap}, cpu_price = {cpu_price}, "
                f"time_price_cap = {time_price_cap}, time_price = {time_price}, "
                f"fixed_price_cap = {fixed_price_cap}, fixed_price = {fixed_price}, "
                f"score = {score}")

        for cpu_price_cap, time_price_cap, fixed_price_cap in product(
            (None, cpu_price - epsilon, cpu_price, cpu_price + epsilon),
            (None, time_price - epsilon, time_price, time_price + epsilon),
            (None, fixed_price - epsilon, fixed_price, fixed_price + epsilon),
        ):
            if cpu_price_cap == time_price_cap == fixed_price_cap == None:
                strategies = (
                    LeastExpensiveLinearPayuMS(),
                    LeastExpensiveLinearPayuMS(max_price_for={}),
                )
            elif cpu_price_cap == time_price_cap == None:
                strategies = (
                    LeastExpensiveLinearPayuMS(
                        max_fixed_price=fixed_price_cap),
                    LeastExpensiveLinearPayuMS(max_fixed_price=fixed_price_cap,
                                               max_price_for={}),
                )
            else:
                counter_caps = {}
                if cpu_price_cap is not None:
                    counter_caps[Counter.CPU] = cpu_price_cap
                if time_price_cap is not None:
                    counter_caps[Counter.TIME] = time_price_cap

                if fixed_price_cap is None:
                    strategies = (
                        LeastExpensiveLinearPayuMS(max_price_for=counter_caps),
                        # Also add a mock cap for an unrelated counter
                        LeastExpensiveLinearPayuMS(
                            max_price_for={
                                **counter_caps, Counter.STORAGE: Decimal(0.0)
                            }),
                    )
                else:
                    strategies = (LeastExpensiveLinearPayuMS(
                        max_fixed_price=fixed_price_cap,
                        max_price_for=counter_caps), )

            for strategy in strategies:
                await _test_strategy(strategy, cpu_price_cap, time_price_cap,
                                     fixed_price_cap)
Ejemplo n.º 10
0
 async def test_score_negative_coeff(self, coeffs):
     offer = OfferProposalFactory(
         **{"proposal__proposal__properties__linear_coeffs": coeffs})
     assert await self.strategy.score_offer(offer) == SCORE_REJECTED
Ejemplo n.º 11
0
 async def test_score_negative_coeff(self, coeffs):
     offer = OfferProposalFactory(coeffs=coeffs)
     assert await self.strategy.score_offer(offer) == SCORE_REJECTED