def test_stop_unsetting_context(advance_timer):
    time_profiling.start()
    time_profiling.set_context("a")
    advance_timer(1)
    time_profiling.stop()
    result = time_profiling.get_result()
    assert result == {"process_time": 1, "contexts": {"a": 1}}
def test_overlapping_contexts(advance_timer):
    time_profiling.start()
    time_profiling.set_context("a")
    advance_timer(1)
    time_profiling.set_context("b")
    advance_timer(1)
    time_profiling.unset_context("a")
    time_profiling.unset_context("b")
    time_profiling.stop()
    result = time_profiling.get_result()
    assert result == {"process_time": 2, "contexts": {"a": 2, "b": 1}}
Esempio n. 3
0
    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
Esempio n. 4
0
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_process_time(advance_timer):
    time_profiling.start()
    advance_timer(2)
    time_profiling.stop()
    result = time_profiling.get_result()
    assert result == {"process_time": 2, "contexts": {}}