Beispiel #1
0
    def test_view_manifest_only_trainable(self):
        json = get_har_json()
        har = har_from_json(json)
        res_list = har_entries_to_resources(har)
        push_groups = resource_list_to_push_groups(
            res_list, train_domain_globs=["*reddit.com"])
        config = EnvironmentConfig(replay_dir="",
                                   request_url="https://www.reddit.com/",
                                   push_groups=push_groups,
                                   har_resources=res_list)
        with mock.patch("builtins.print") as mock_print:
            with tempfile.NamedTemporaryFile() as config_file:
                config.save_file(config_file.name)
                view_manifest(["--trainable", config_file.name])
        assert mock_print.call_count > 5

        printed_text = "\n".join(call[0][0]
                                 for call in mock_print.call_args_list
                                 if call[0])
        assert config.replay_dir in printed_text
        assert config.request_url in printed_text
        assert all(group.name in printed_text for group in config.push_groups
                   if group.trainable)

        pre_graph_text = printed_text.split("Execution Graph")[0]
        assert not any(group.name in pre_graph_text
                       for group in config.push_groups if not group.trainable)
Beispiel #2
0
    def test_simulator(self):
        har_json = get_har_json()
        har = har_from_json(har_json)
        res_list = har_entries_to_resources(har)
        push_groups = resource_list_to_push_groups(res_list)
        env_config = EnvironmentConfig(replay_dir="",
                                       request_url="https://www.reddit.com/",
                                       push_groups=push_groups,
                                       har_resources=res_list)

        client_env = get_fast_mobile_client_environment()

        simulator = Simulator(env_config)
        time_ms = simulator.simulate_load_time(client_env)
        assert time_ms > 0
Beispiel #3
0
def generate_har():
    har_file = har_from_json(get_har_json())
    har_entries = []
    # randomly drop about 3% of entries
    for entry in har_file.log.entries:
        if np.random.random() > 0.03 or entry.request.url == "https://www.reddit.com/":
            har_entries.append(entry)

    # randomly swap some entries with 3% chance
    # don't swap the first few though
    for i in range(10, len(har_entries)):  # pylint: disable=consider-using-enumerate
        if np.random.random() < 0.03:
            swap_index = max(0, i - np.random.geometric(0.30))
            har_entries[i], har_entries[swap_index] = har_entries[swap_index], har_entries[i]

    # rewrite startedDateTime so that the entries are in order
    last_date = datetime.datetime.now()
    for (i, entry) in enumerate(har_entries):
        last_date += datetime.timedelta(milliseconds=np.random.randint(0, 1000))
        har_entries[i] = HarEntry(
            started_date_time=last_date, request=entry.request, response=entry.response, critical=False
        )
    return Har(log=HarLog(entries=har_entries), timings=har_file.timings)
Beispiel #4
0
    def test_view_manifest(self):
        har = har_from_json(get_har_json())
        res_list = har_entries_to_resources(har)
        push_groups = resource_list_to_push_groups(res_list)
        config = EnvironmentConfig(replay_dir="",
                                   request_url="https://www.reddit.com/",
                                   push_groups=push_groups,
                                   har_resources=res_list)

        with mock.patch("builtins.print") as mock_print:
            with tempfile.NamedTemporaryFile() as config_file:
                config.save_file(config_file.name)
                view_manifest([config_file.name])
        assert mock_print.call_count > 5

        printed_text = "\n".join(call[0][0]
                                 for call in mock_print.call_args_list
                                 if call[0])
        assert config.replay_dir in printed_text
        assert config.request_url in printed_text
        assert all(group.name in printed_text for group in config.push_groups)
        assert all(
            Url.parse(res.url).resource[:61] in printed_text
            for group in config.push_groups for res in group.resources)
Beispiel #5
0
 def setup(self):
     self.config = get_config()
     self.har = har_from_json(get_har_json())
Beispiel #6
0
 def setup(self):
     self.client_env = get_default_client_environment()
     self.har_json = get_har_json()
     self.har = har_from_json(self.har_json)