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): ...@@ -162,6 +162,8 @@ class BenchmarkFileLogger(BaseBenchmarkLogger):
self._logging_dir = logging_dir self._logging_dir = logging_dir
if not tf.gfile.IsDirectory(self._logging_dir): if not tf.gfile.IsDirectory(self._logging_dir):
tf.gfile.MakeDirs(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): def log_metric(self, name, value, unit=None, global_step=None, extras=None):
"""Log the benchmark metric information to local file. """Log the benchmark metric information to local file.
...@@ -179,11 +181,10 @@ class BenchmarkFileLogger(BaseBenchmarkLogger): ...@@ -179,11 +181,10 @@ class BenchmarkFileLogger(BaseBenchmarkLogger):
""" """
metric = _process_metric_to_json(name, value, unit, global_step, extras) metric = _process_metric_to_json(name, value, unit, global_step, extras)
if metric: if metric:
with tf.gfile.GFile(
os.path.join(self._logging_dir, METRIC_LOG_FILE_NAME), "a") as f:
try: try:
json.dump(metric, f) json.dump(metric, self._metric_file_handler)
f.write("\n") self._metric_file_handler.write("\n")
self._metric_file_handler.flush()
except (TypeError, ValueError) as e: except (TypeError, ValueError) as e:
tf.logging.warning("Failed to dump metric to log file: " tf.logging.warning("Failed to dump metric to log file: "
"name %s, value %s, error %s", name, value, e) "name %s, value %s, error %s", name, value, e)
...@@ -213,7 +214,8 @@ class BenchmarkFileLogger(BaseBenchmarkLogger): ...@@ -213,7 +214,8 @@ class BenchmarkFileLogger(BaseBenchmarkLogger):
e) e)
def on_finish(self, status): def on_finish(self, status):
pass self._metric_file_handler.flush()
self._metric_file_handler.close()
class BenchmarkBigQueryLogger(BaseBenchmarkLogger): 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