From 80415fd5f2f2397865f1a3679d523f1e51897023 Mon Sep 17 00:00:00 2001 From: David Beach Date: Fri, 23 Nov 2018 15:53:23 -0700 Subject: [PATCH 1/2] Keep track of compute time for entries in cache Include per-entry compute time in cache metadata and in summary table. --- cache_magic/__init__.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/cache_magic/__init__.py b/cache_magic/__init__.py index 69a4fa3..7d14fee 100644 --- a/cache_magic/__init__.py +++ b/cache_magic/__init__.py @@ -118,7 +118,11 @@ def _create_new_value(self, shell, var_folder_path, var_data_path, var_info_path # calculate the new Value in user-context cmd = self._reconstruct_expression(var_name, var_value) + + # time the shell command + start_time = time.time() result = shell.run_cell(cmd) + compute_time = time.time() - start_time if not result.success: raise CacheCallException( @@ -131,7 +135,8 @@ def _create_new_value(self, shell, var_folder_path, var_data_path, var_info_path info = dict(expression_hash=self.hash_line(var_value), store_date=datetime.datetime.now(), - version=version) + version=version, + compute_time=compute_time) with open(var_info_path, 'wb') as fp: pickle.dump(info, fp) @@ -153,12 +158,14 @@ def _show_all(base_dir): try: info = CacheCall.get_from_file(var_info_path) - vars.append([var_name, size, info["store_date"], info["version"], info["expression_hash"]]) + vars.append([var_name, size, info["store_date"], "%.1f" % info["compute_time"], + info["version"], info["expression_hash"]]) except IOError: print("Warning: failed to read info variable '" + var_name + "'") - display(HTML(tabulate(vars, headers=["var name", "size(byte)", "stored at date", "version", "expression(hash)"], + display(HTML(tabulate(vars, headers=["var name", "size(byte)", "stored at date", + "time(s)", "version", "expression(hash)"], tablefmt="html"))) @staticmethod From 58bb00251500aa94c9fb425c6ae3d9ac5fc69697 Mon Sep 17 00:00:00 2001 From: "David J. C. Beach" Date: Fri, 23 Nov 2018 16:04:32 -0700 Subject: [PATCH 2/2] Fix bug, add backward compatability. --- cache_magic/__init__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cache_magic/__init__.py b/cache_magic/__init__.py index 7d14fee..3f17d53 100644 --- a/cache_magic/__init__.py +++ b/cache_magic/__init__.py @@ -3,6 +3,7 @@ import pickle import os +import time import hashlib import datetime import shutil @@ -158,7 +159,8 @@ def _show_all(base_dir): try: info = CacheCall.get_from_file(var_info_path) - vars.append([var_name, size, info["store_date"], "%.1f" % info["compute_time"], + vars.append([var_name, size, info["store_date"], + "%.1f" % info.get("compute_time", 0.0), info["version"], info["expression_hash"]]) except IOError: