def replay(args): """ Starts a replay environment for the given replay directory, including setting up interfaces, running a DNS server, and configuring and running an nginx server to serve the requests """ policy = None cert_path = os.path.abspath(args.cert_path) if args.cert_path else None key_path = os.path.abspath(args.key_path) if args.key_path else None per_resource_latency = os.path.abspath( args.per_resource_latency) if args.per_resource_latency else None if args.policy: log.debug("reading policy", push_policy=args.policy) with open(args.policy, "r") as policy_file: policy_dict = json.load(policy_file) policy = Policy.from_dict(policy_dict) # handle sigterm gracefully signal.signal(signal.SIGTERM, sigterm_handler) with start_server(args.replay_dir, cert_path, key_path, policy, per_resource_latency, cache_time=args.cache_time, extract_critical_requests=args.extract_critical_requests, enable_http2=args.enable_http2): while True: time.sleep(86400)
def replay(args): """ Starts a replay environment for the given replay directory, including setting up interfaces, running a DNS server, and configuring and running an nginx server to serve the requests """ policy = None cert_path = os.path.abspath(args.cert_path) if args.cert_path else None key_path = os.path.abspath(args.key_path) if args.key_path else None if args.policy: log.debug("reading policy", push_policy=args.policy) with open(args.policy, "r") as policy_file: policy_dict = json.load(policy_file) policy = Policy.from_dict(policy_dict) with start_server( args.replay_dir, cert_path, key_path, policy, cache_time=args.cache_time, extract_critical_requests=args.extract_critical_requests, ): while True: time.sleep(86400)
def test_create_push_policy(self): ps = PolicyService(self.saved_model) policy = ps.create_policy(self.page) assert policy assert isinstance(policy, policy_service_pb2.Policy) push_pairs = convert_push_groups_to_push_pairs(self.push_groups) push_pairs = [(s.url, p.url) for (s, p) in push_pairs] p = Policy.from_dict(json.loads(policy.policy)) for (source, push_res) in p.push: for push in push_res: assert (source.url, push.url) in push_pairs
def test_from_dict(self): policy_dict = { "push": { "A": [{ "url": "B", "type": "SCRIPT" }, { "url": "C", "type": "IMAGE" }] }, "preload": { "B": [{ "url": "D", "type": "IMAGE" }], "G": [{ "url": "E", "type": "CSS" }, { "url": "F", "type": "FONT" }], }, } policy = Policy.from_dict(policy_dict) assert policy.total_actions == 0 assert not policy.action_space for (source, deps) in policy.push: assert isinstance(source, Resource) assert all(isinstance(push, Resource) for push in deps) assert [p["url"] for p in policy_dict["push"][source.url] ] == sorted([push.url for push in deps]) for push in deps: assert policy.push_to_source[push] == source assert push.url in [ p["url"] for p in policy_dict["push"][source.url] ] for (source, deps) in policy.preload: assert isinstance(source, Resource) assert all(isinstance(push, Resource) for push in deps) assert [p["url"] for p in policy_dict["preload"][source.url] ] == sorted([push.url for push in deps]) for push in deps: assert policy.preload_to_source[push] == source assert push.url in [ p["url"] for p in policy_dict["preload"][source.url] ] assert len(policy.source_to_push) == len(policy_dict["push"]) assert len(policy.source_to_preload) == len(policy_dict["preload"])
def get_policy(self, url: str, client_env: ClientEnvironment, manifest: EnvironmentConfig) -> Policy: """ Queries the policy service for a push policy for the given configuration """ page = policy_service_pb2.Page( url=url, bandwidth_kbps=client_env.bandwidth, latency_ms=client_env.latency, cpu_slowdown=client_env.cpu_slowdown, manifest=manifest.serialize(), ) policy_res = self.stub.GetPolicy(page) return Policy.from_dict(json.loads(policy_res.policy))
def page_load_time(args): """ Captures a webpage and calculates the median page load time for a given website in a fast, no-latency Mahimahi shell. Then simulates the load based on profiling the page in the same Mahimahi shell. """ # Validate the arguments if args.latency is not None and args.latency < 0: log.critical("provided latency must be greater or equal to 0") sys.exit(1) if args.bandwidth is not None and args.bandwidth <= 0: log.critical("provided bandwidth must be greater than 0") sys.exit(1) if args.cpu_slowdown is not None and args.cpu_slowdown not in {1, 2, 4}: log.critical("provided cpu slodown must be 1, 2, or 4") sys.exit(1) # Setup the client environment default_client_env = get_default_client_environment() client_env = get_client_environment_from_parameters( args.bandwidth or default_client_env.bandwidth, args.latency or default_client_env.latency, args.cpu_slowdown or default_client_env.cpu_slowdown, ) # If a push/preload policy was specified, read it policy = None if args.policy: log.debug("reading policy", push_policy=args.policy) with open(args.policy, "r") as policy_file: policy_dict = json.load(policy_file) policy = Policy.from_dict(policy_dict) env_config = EnvironmentConfig.load_file(args.from_manifest) config = get_config(env_config) log.info("calculating page load time", manifest=args.from_manifest, url=env_config.request_url) plt, orig_plt = 0, 0 if not args.only_simulator: if not args.speed_index: orig_plt, *_ = get_page_load_time_in_replay_server( request_url=config.env_config.request_url, client_env=client_env, config=config, cache_time=args.cache_time, user_data_dir=args.user_data_dir, ) if policy: plt, *_ = get_page_load_time_in_replay_server( request_url=config.env_config.request_url, client_env=client_env, config=config, policy=policy, cache_time=args.cache_time, user_data_dir=args.user_data_dir, ) else: orig_plt = get_speed_index_in_replay_server( request_url=config.env_config.request_url, client_env=client_env, config=config, cache_time=args.cache_time, user_data_dir=args.user_data_dir, ) if policy: plt = get_speed_index_in_replay_server( request_url=config.env_config.request_url, client_env=client_env, config=config, policy=policy, cache_time=args.cache_time, user_data_dir=args.user_data_dir, ) log.debug("running simulator...") sim = Simulator(env_config) orig_sim_plt = sim.simulate_load_time(client_env) sim_plt = sim.simulate_load_time(client_env, policy) print( json.dumps( { "client_env": client_env._asdict(), "metric": "speed_index" if args.speed_index else "plt", "cache": "warm" if args.user_data_dir else "cold", "cache_time": args.cache_time, "replay_server": { "with_policy": plt, "without_policy": orig_plt }, "simulator": { "with_policy": sim_plt, "without_policy": orig_sim_plt }, }, indent=4, ))