summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBin Yang <bin.yang@windriver.com>2018-11-26 13:10:30 +0000
committerBin Yang <bin.yang@windriver.com>2018-11-27 13:08:25 +0000
commitc74a6b2a253671bb0ade2838b473939f35bc8586 (patch)
tree00644e65b8849131a5009857a50f64511bf4a228
parentd60ccadfb84bbdb18e480f6550d27c51f0c21661 (diff)
Cache AAI data
Add profiler decorator Change-Id: I280b5799fa876d881dc2331b44dd34344437eba3 Issue-ID: MULTICLOUD-417 Signed-off-by: Bin Yang <bin.yang@windriver.com> (cherry picked from commit 2c3a49d866ebd62210f2fbcd3a1681e58f794efa)
-rw-r--r--share/common/msapi/extsys.py9
-rw-r--r--share/newton_base/util.py30
2 files changed, 39 insertions, 0 deletions
diff --git a/share/common/msapi/extsys.py b/share/common/msapi/extsys.py
index e1534523..9b65072e 100644
--- a/share/common/msapi/extsys.py
+++ b/share/common/msapi/extsys.py
@@ -12,6 +12,7 @@
import json
import logging
import re
+from django.core.cache import cache
from common.exceptions import VimDriverNewtonException
from common.utils import restcall
@@ -22,6 +23,12 @@ logger = logging.getLogger(__name__)
def get_vim_by_id(vim_id):
+ # try to load from cache
+ cachedviminfostr = cache.get("VIMINFOCACHE_"+vim_id)
+ if cachedviminfostr:
+ viminfo = json.loads(cachedviminfostr)
+ return viminfo
+
cloud_owner,cloud_region_id = decode_vim_id(vim_id)
if cloud_owner and cloud_region_id:
@@ -74,6 +81,8 @@ def get_vim_by_id(vim_id):
viminfo['openstack_region_id'] = tmp_viminfo.get("cloud-epa-caps") \
if tmp_viminfo.get("cloud-epa-caps") else cloud_region_id
+ # cache the viminfo for 24 hour
+ cache.set("VIMINFOCACHE_"+vim_id, json.dumps(viminfo), 3600*24)
return viminfo
return None
diff --git a/share/newton_base/util.py b/share/newton_base/util.py
index e925d99a..aa03b65b 100644
--- a/share/newton_base/util.py
+++ b/share/newton_base/util.py
@@ -22,6 +22,11 @@ from keystoneauth1 import session
from common.msapi import extsys
+# profiler decoration
+import cProfile
+import pstats
+import os
+
logger = logging.getLogger(__name__)
@@ -147,3 +152,28 @@ class VimDriverUtils(object):
def replace_key_by_mapping(dict_obj, mapping, reverse=False):
for k in mapping:
VimDriverUtils._replace_a_key(dict_obj, k, reverse)
+
+ # profiler decoration
+ @staticmethod
+ def do_cprofile(filename):
+ """
+ Decorator for function profiling.
+ """
+ def wrapper(func):
+ def profiled_func(*args, **kwargs):
+ # Flag for do profiling or not.
+ DO_PROF = True # os.getenv("PROFILING")
+ if DO_PROF:
+ profile = cProfile.Profile()
+ profile.enable()
+ result = func(*args, **kwargs)
+ profile.disable()
+ # Sort stat by internal time.
+ sortby = "tottime"
+ ps = pstats.Stats(profile).sort_stats(sortby)
+ ps.dump_stats(filename)
+ else:
+ result = func(*args, **kwargs)
+ return result
+ return profiled_func
+ return wrapper