def METHOD_NAME(sandbox, collect_output=False):
    """Extract statistics from a sandbox about the last ran command.
    sandbox (Sandbox): the sandbox to inspect.
    collect_output (bool): whether to collect output from the sandbox
        stdout_file and stderr_file.
    return (dict): a dictionary with statistics.
    """
    stats = {
        "execution_time": sandbox.get_execution_time(),
        "execution_wall_clock_time": sandbox.get_execution_wall_clock_time(),
        "execution_memory": sandbox.get_memory_used(),
        "exit_status": sandbox.get_exit_status(),
    }
    if stats["exit_status"] == Sandbox.EXIT_SIGNAL:
        stats["signal"] = sandbox.get_killing_signal()
    if collect_output:
        stats["stdout"] = sandbox.get_file_to_string(sandbox.stdout_file)\
            .decode("utf-8", errors="replace").strip()
        stats["stderr"] = sandbox.get_file_to_string(sandbox.stderr_file)\
            .decode("utf-8", errors="replace").strip()
    return stats