def test_context(set_memory): memory_profiling.start() set_memory(1000) set_memory(100) memory_profiling.set_context("a") set_memory(200) set_memory(100) memory_profiling.unset_context("a") memory_profiling.stop() result = memory_profiling.get_result() assert result == {"a": 200}
async def evaluate(body: EvaluateBody): challenge = state_keeper.challenge_keeper.get_challenge(body.challenge) response = {"entities": {}} for variant in challenge.variants or [None]: room = state_keeper.challenge_keeper.get_challenge( body.challenge).create_room(variant=variant) if body.profile_time: time_profiling.start() if body.profile_memory: memory_profiling.start() room.step(steps=body.duration) if body.profile_time: time_profiling.stop() time_result = time_profiling.get_result() if "processTime" not in response: response["processTime"] = 0 response["processTime"] += time_result["process_time"] if body.profile_memory: memory_profiling.stop() memory_result = memory_profiling.get_result() scores = room.get_entity_scores() for entity_id in room.list_entities(): entity = room.get_entity(entity_id) if entity.label is None: continue label = (entity.label if variant is None else f"{variant}:{entity.label}") if entity_id in scores: score = scores[entity_id] else: score = 0 response["entities"][label] = {"score": score} if body.profile_time: if entity.label in time_result["contexts"]: ai_time = time_result["contexts"][entity.label] else: ai_time = 0 response["entities"][label]["time"] = format_prefix( ai_time, "s") if body.profile_memory: if entity.label in memory_result: ai_memory = memory_result[entity.label] else: ai_memory = 0 response["entities"][label]["memory"] = format_prefix( ai_memory, "B") if body.profile_time: response["processTime"] = format_prefix(response["processTime"], "s") return response
def test_do_profiling(): system = ActionSystem() random_generator = random.Random(123) ai_components = {"a": MockAI(DoNothing())} percepts = {} actions_components = {} score_components = {} label_components = LabelDict({"a": "player"}) time_profiling.start() memory_profiling.start() system.get_actions(ai_components, percepts, actions_components, score_components, label_components, random_generator) time_profiling.stop() memory_profiling.stop() assert "player" in time_profiling.get_result()["contexts"] assert "player" in memory_profiling.get_result()
def test_only_one_context_allowed(set_memory): memory_profiling.start() memory_profiling.set_context("a") with pytest.raises(RuntimeError): memory_profiling.set_context("b") memory_profiling.stop()