import json
import logging
import re
+from django.core.cache import cache
from common.exceptions import VimDriverNewtonException
from common.utils import restcall
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:
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
from common.msapi import extsys
+# profiler decoration
+import cProfile
+import pstats
+import os
+
logger = logging.getLogger(__name__)
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