Unverified Commit add588d6 authored by Qianli Scott Zhu's avatar Qianli Scott Zhu Committed by GitHub
Browse files

Improve the BenchmarkFileLogger performance. (#4474)

* Improve the BenchmarkFileLogger performance.

Open and preserve the file handler of the metric log file during
init, which reduce the overhead of open/close the file for each
log_metric call.

* Address review comment.
parent 3ae33b4d
......@@ -162,6 +162,8 @@ class BenchmarkFileLogger(BaseBenchmarkLogger):
self._logging_dir = logging_dir
if not tf.gfile.IsDirectory(self._logging_dir):
tf.gfile.MakeDirs(self._logging_dir)
self._metric_file_handler = tf.gfile.GFile(
os.path.join(self._logging_dir, METRIC_LOG_FILE_NAME), "a")
def log_metric(self, name, value, unit=None, global_step=None, extras=None):
"""Log the benchmark metric information to local file.
......@@ -179,14 +181,13 @@ class BenchmarkFileLogger(BaseBenchmarkLogger):
"""
metric = _process_metric_to_json(name, value, unit, global_step, extras)
if metric:
with tf.gfile.GFile(
os.path.join(self._logging_dir, METRIC_LOG_FILE_NAME), "a") as f:
try:
json.dump(metric, f)
f.write("\n")
except (TypeError, ValueError) as e:
tf.logging.warning("Failed to dump metric to log file: "
"name %s, value %s, error %s", name, value, e)
try:
json.dump(metric, self._metric_file_handler)
self._metric_file_handler.write("\n")
self._metric_file_handler.flush()
except (TypeError, ValueError) as e:
tf.logging.warning("Failed to dump metric to log file: "
"name %s, value %s, error %s", name, value, e)
def log_run_info(self, model_name, dataset_name, run_params, test_id=None):
"""Collect most of the TF runtime information for the local env.
......@@ -213,7 +214,8 @@ class BenchmarkFileLogger(BaseBenchmarkLogger):
e)
def on_finish(self, status):
pass
self._metric_file_handler.flush()
self._metric_file_handler.close()
class BenchmarkBigQueryLogger(BaseBenchmarkLogger):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment