perf record: Move existing write_output into helper function