diff mbox

[KVM-AUTOTEST] kvm_runtest_2.py: use pickle instead of shelve when loading/saving env

Message ID de2b74cada241e816c81e7e28cf6b6460a56dce9.1243179847.git.mgoldish@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

Michael Goldish May 24, 2009, 3:46 p.m. UTC
pickle allows more control over the load/save process. Specifically, it enables
us to dump the contents of an object to disk without having to unpickle it.
shelve, which uses pickle, seems to pickle and unpickle every time sync() is
called. This is bad for classes that need to be unpickled only once per test
(such a class will be introduced in a future patch).

Signed-off-by: Michael Goldish <mgoldish@redhat.com>
---
 client/tests/kvm_runtest_2/kvm_runtest_2.py |   29 ++++++++++++++++++++------
 1 files changed, 22 insertions(+), 7 deletions(-)

Comments

Lucas Meneghel Rodrigues May 28, 2009, 1:26 p.m. UTC | #1
On Sun, 2009-05-24 at 18:46 +0300, Michael Goldish wrote:
> pickle allows more control over the load/save process. Specifically, it enables
> us to dump the contents of an object to disk without having to unpickle it.
> shelve, which uses pickle, seems to pickle and unpickle every time sync() is
> called. This is bad for classes that need to be unpickled only once per test
> (such a class will be introduced in a future patch).

Looks good to me.

> Signed-off-by: Michael Goldish <mgoldish@redhat.com>
> ---
>  client/tests/kvm_runtest_2/kvm_runtest_2.py |   29 ++++++++++++++++++++------
>  1 files changed, 22 insertions(+), 7 deletions(-)
> 
> diff --git a/client/tests/kvm_runtest_2/kvm_runtest_2.py b/client/tests/kvm_runtest_2/kvm_runtest_2.py
> index a69951b..5f7f6ad 100644
> --- a/client/tests/kvm_runtest_2/kvm_runtest_2.py
> +++ b/client/tests/kvm_runtest_2/kvm_runtest_2.py
> @@ -3,9 +3,9 @@
>  import sys
>  import os
>  import time
> -import shelve
>  import random
>  import resource
> +import cPickle
>  
>  from autotest_lib.client.bin import test
>  from autotest_lib.client.common_lib import error
> @@ -18,6 +18,22 @@ class test_routine:
>          self.routine = None
>  
> 
> +def dump_env(obj, filename):
> +    file = open(filename, "w")
> +    cPickle.dump(obj, file)
> +    file.close()
> +
> +
> +def load_env(filename, default=None):
> +    try:
> +        file = open(filename, "r")
> +    except:
> +        return default
> +    obj = cPickle.load(file)
> +    file.close()
> +    return obj
> +
> +
>  class kvm_runtest_2(test.test):
>      version = 1
>  
> @@ -65,7 +81,7 @@ class kvm_runtest_2(test.test):
>  
>          # Open the environment file
>          env_filename = os.path.join(self.bindir, params.get("env", "env"))
> -        env = shelve.open(env_filename, writeback=True)
> +        env = load_env(env_filename, {})
>          kvm_log.debug("Contents of environment: %s" % str(env))
>  
>          try:
> @@ -87,24 +103,23 @@ class kvm_runtest_2(test.test):
>  
>                  # Preprocess
>                  kvm_preprocessing.preprocess(self, params, env)
> -                env.sync()
> +                dump_env(env, env_filename)
>                  # Run the test function
>                  routine_obj.routine(self, params, env)
> -                env.sync()
> +                dump_env(env, env_filename)
>  
>              except Exception, e:
>                  kvm_log.error("Test failed: %s" % e)
>                  kvm_log.debug("Postprocessing on error...")
>                  kvm_preprocessing.postprocess_on_error(self, params, env)
> -                env.sync()
> +                dump_env(env, env_filename)
>                  raise
>  
>          finally:
>              # Postprocess
>              kvm_preprocessing.postprocess(self, params, env)
>              kvm_log.debug("Contents of environment: %s" % str(env))
> -            env.sync()
> -            env.close()
> +            dump_env(env, env_filename)
>  
>      def postprocess(self):
>          pass
diff mbox

Patch

diff --git a/client/tests/kvm_runtest_2/kvm_runtest_2.py b/client/tests/kvm_runtest_2/kvm_runtest_2.py
index a69951b..5f7f6ad 100644
--- a/client/tests/kvm_runtest_2/kvm_runtest_2.py
+++ b/client/tests/kvm_runtest_2/kvm_runtest_2.py
@@ -3,9 +3,9 @@ 
 import sys
 import os
 import time
-import shelve
 import random
 import resource
+import cPickle
 
 from autotest_lib.client.bin import test
 from autotest_lib.client.common_lib import error
@@ -18,6 +18,22 @@  class test_routine:
         self.routine = None
 
 
+def dump_env(obj, filename):
+    file = open(filename, "w")
+    cPickle.dump(obj, file)
+    file.close()
+
+
+def load_env(filename, default=None):
+    try:
+        file = open(filename, "r")
+    except:
+        return default
+    obj = cPickle.load(file)
+    file.close()
+    return obj
+
+
 class kvm_runtest_2(test.test):
     version = 1
 
@@ -65,7 +81,7 @@  class kvm_runtest_2(test.test):
 
         # Open the environment file
         env_filename = os.path.join(self.bindir, params.get("env", "env"))
-        env = shelve.open(env_filename, writeback=True)
+        env = load_env(env_filename, {})
         kvm_log.debug("Contents of environment: %s" % str(env))
 
         try:
@@ -87,24 +103,23 @@  class kvm_runtest_2(test.test):
 
                 # Preprocess
                 kvm_preprocessing.preprocess(self, params, env)
-                env.sync()
+                dump_env(env, env_filename)
                 # Run the test function
                 routine_obj.routine(self, params, env)
-                env.sync()
+                dump_env(env, env_filename)
 
             except Exception, e:
                 kvm_log.error("Test failed: %s" % e)
                 kvm_log.debug("Postprocessing on error...")
                 kvm_preprocessing.postprocess_on_error(self, params, env)
-                env.sync()
+                dump_env(env, env_filename)
                 raise
 
         finally:
             # Postprocess
             kvm_preprocessing.postprocess(self, params, env)
             kvm_log.debug("Contents of environment: %s" % str(env))
-            env.sync()
-            env.close()
+            dump_env(env, env_filename)
 
     def postprocess(self):
         pass