Beispiel #1
0
    def _generator(env_config: EnvironmentConfig) -> Policy:
        push_groups = env_config.push_groups
        # Collect all resources and group them by type
        all_resources = sorted(
            [res for group in push_groups for res in group.resources],
            key=lambda res: res.order)
        res_by_type = collections.defaultdict(list)
        for res in all_resources:
            # Only consider non-cached objects in the push resource type distribution
            if res.type in dist and res.url not in cached_urls:
                res_by_type[res.type].append(res)

        # choose the number of resources to push/preload
        total = sum(map(len, res_by_type.values()))
        if total <= 1:
            return Policy()

        n = random.randint(1, total)
        # choose the weight factor between push and preload
        weight = push_weight if push_weight is not None else random.random()

        # Choose n resources based on the resource type distribution without replacement
        log.debug("generating push-preload policy",
                  num_resources=len(all_resources),
                  total_size=n,
                  push_weight=weight)
        res = []
        for _ in range(n):
            g, r, s = _choose_with_dist(res_by_type, dist)
            res_by_type[g].pop(r)
            res.append(s)

        policy = Policy()

        for r in res:
            if r.source_id == 0 or r.order == 0:
                continue
            push = random.random() < weight
            policy.steps_taken += 1
            if push:
                source = random.randint(0, r.source_id - 1)
                policy.add_default_push_action(
                    push_groups[r.group_id].resources[source], r)
            else:
                source = random.randint(0, r.order - 1)
                policy.add_default_preload_action(all_resources[source], r)

        return policy
Beispiel #2
0
 def _generator(env_config: EnvironmentConfig) -> Policy:
     push_groups = env_config.push_groups
     # Collect all resources and group them by type
     all_resources = sorted(
         [res for group in push_groups for res in group.resources],
         key=lambda res: res.order)
     # choose the weight factor between push and preload
     main_domain = urllib.parse.urlparse(env_config.request_url)
     policy = Policy()
     for r in all_resources:
         if r.source_id == 0 or r.order == 0:
             continue
         request_domain = urllib.parse.urlparse(r.url)
         push = request_domain.netloc == main_domain.netloc
         policy.steps_taken += 1
         if push:
             source = random.randint(0, r.source_id - 1)
             policy.add_default_push_action(
                 push_groups[r.group_id].resources[source], r)
         else:
             source = random.randint(0, r.order - 1)
             policy.add_default_preload_action(all_resources[source], r)
     return policy