2 Implementation class HCEProfiler for python bindings to HCE cluster node. 6 @author Alexander Vybornyh <alexander.hce.cluster@gmail.com> 7 @link: http://hierarchical-cluster-engine.com/ 8 @copyright: Copyright © 2013-2015 IOIX Ukraine 9 @license: http://hierarchical-cluster-engine.com/license/ 35 MSG_ERROR_PARSE_CMD_PARAMS =
"Error parse command line parameters." 36 MSG_ERROR_EMPTY_CONFIG_FILE_NAME =
"Config file name is empty." 37 MSG_ERROR_WRONG_CONFIG_FILE_NAME =
"Config file name is wrong" 38 MSG_ERROR_LOAD_APP_CONFIG =
"Error loading application config file." 39 MSG_ERROR_READ_LOG_CONFIG =
"Error read log config file." 40 MSG_ERROR_WRONG_CONFIG_OPTION =
"Wrong format option" 41 MSG_ERROR_WRONG__OPTION_SORTBY =
"Read wrong value of 'sortby' from config" 44 PROFILER_OPTION_LOG =
"log" 45 PROFILER_OPTION_STATUS =
"profile" 46 PROFILER_OPTION_SORTBY =
"sortby" 47 PROFILER_OPTION_LIMIT =
"limit" 48 PROFILER_OPTION_TRACEBACK =
"traceback" 49 PROFILER_OPTION_TRACEBACK_LOGGER_MODE =
"tracebackLoggerMode" 52 PROFILER_OPTION_STATUS_DEFAULT = 0
53 PROFILER_OPTION_SORTBY_DEFAULT =
"cumulative" 54 PROFILER_OPTION_LIMIT_DEFAULT = 1.0
55 PROFILER_OPTION_TRACEBACK_DEFAULT = 0
56 PROFILER_OPTION_TRACEBACK_LOGGER_MODE_DEFAULT = 1
59 PROFILER_OPTION_SORTBY_ALLOWED_LIST = [
'stdname',
'calls',
'time',
'cumulative']
62 MESSAGES_ITEMS_DELIMITER =
"," 65 tracebackOptions = {
'tracebackIdent':Utils.tracebackIdent,
66 'tracebackIdentFiller':Utils.tracebackIdentFiller,
67 'tracebackMessageCall':Utils.tracebackMessageCall,
68 'tracebackMessageExit':Utils.tracebackMessageExit,
69 'tracebackmessageDelimiter':Utils.tracebackmessageDelimiter,
70 'tracebackTimeMark':Utils.tracebackTimeMark,
71 'tracebackTimeMarkFormat':Utils.tracebackTimeMarkFormat,
72 'tracebackTimeMarkDelimiter':Utils.tracebackTimeMarkDelimiter,
73 'tracebackIncludeInternalCalls':Utils.tracebackIncludeInternalCalls,
74 'tracebackIncludeLineNumber':Utils.tracebackIncludeLineNumber,
75 'tracebackIncludeLineNumberDelimiter':Utils.tracebackIncludeLineNumberDelimiter,
76 'tracebackIncludeFileNumber':Utils.tracebackIncludeFileNumber,
77 'tracebackIncludeFileNumberDelimiter':Utils.tracebackIncludeFileNumberDelimiter,
78 'tracebackFunctionNameDelimiter':Utils.tracebackFunctionNameDelimiter,
79 'tracebackExcludeModulePath':Utils.tracebackExcludeModulePath,
80 'tracebackExcludeFunctionName':Utils.tracebackExcludeFunctionName,
81 'tracebackExcludeFunctionNameStarts':Utils.tracebackExcludeFunctionNameStarts,
82 'tracebackIncludeExitCalls':Utils.tracebackIncludeExitCalls,
83 'tracebackRecursionlimit':Utils.tracebackRecursionlimit,
84 'tracebackRecursionlimitErrorMsg':Utils.tracebackRecursionlimitErrorMsg,
85 'tracebackIncludeLocals':Utils.tracebackIncludeLocals,
86 'tracebackIncludeArg':Utils.tracebackIncludeArg,
87 'tracebackIncludeLocalsPrefix':Utils.tracebackIncludeLocalsPrefix,
88 'tracebackIncludeArgPrefix':Utils.tracebackIncludeArgPrefix,
89 'tracebackElapsedTimeDelimiter':Utils.tracebackElapsedTimeDelimiter,
90 'tracebackElapsedTimeFormat':Utils.tracebackElapsedTimeFormat,
91 'tracebackUnknownExceptionMsg':Utils.tracebackUnknownExceptionMsg}
108 self.
pr = cProfile.Profile()
112 except Exception, err:
121 sys.settrace(tracefunc)
132 opt = Utils.getConfigParameter(config, section, key, value)
134 if opt
and opt != value:
136 exec(
'Utils.' + str(key) +
'=' + str(opt))
150 config = ConfigParser.ConfigParser()
151 config.optionxform = str
153 readOk = config.read(configName)
158 if config.has_section(APP_CONSTS.CONFIG_APPLICATION_SECTION_NAME):
159 confLogFileName = str(config.get(APP_CONSTS.CONFIG_APPLICATION_SECTION_NAME, self.
PROFILER_OPTION_LOG))
161 if config.has_section(APP_CONSTS.CONFIG_PROFILER_SECTION_NAME):
169 self.
tracebackLoggerMode = int(Utils.getConfigParameter(config, APP_CONSTS.CONFIG_PROFILER_SECTION_NAME,
177 except Exception, err:
183 return confLogFileName
192 if isinstance(configName, str)
and len(configName) == 0:
195 logging.config.fileConfig(configName)
199 except Exception, err:
209 self.
parser = argparse.ArgumentParser(description=
'Process command line arguments.', add_help=
False)
210 self.
parser.add_argument(
'-c',
'--config', action=
'store', metavar=
'config_file', help=
'config ini-file')
212 args = self.
parser.parse_known_args()
214 if args
is None or args[0]
is None or args[0].config
is None:
217 configName = str(args[0].config)
219 except Exception, err:
243 s = StringIO.StringIO()
244 ps = pstats.Stats(self.
pr, stream=s).sort_stats(self.
sortby)
245 ps.print_stats(self.
limit)
251 self.
logger.debug(
"%s\n%s", msgStr, str(s.getvalue()))
252 self.
logger.debug(
"%s", APP_CONSTS.LOGGER_DELIMITER_LINE)
255 if Utils.tracebackLogger
is None:
260 self.
logger.debug(
"%s\n%s", msgStr,
"\n".
join(Utils.tracebackList))
261 self.
logger.debug(
"%s", APP_CONSTS.LOGGER_DELIMITER_LINE)
string PROFILER_OPTION_TRACEBACK_LOGGER_MODE
def __init__(self)
constructor
def __loadAppConfig(self, configName)
load application config file
string PROFILER_OPTION_LIMIT
def readConfig(self, configName)
load log config file of prifiler
def __initTrackbackOptions(self, config, section)
initialize traceback options from config file
string MSG_ERROR_READ_LOG_CONFIG
string PROFILER_OPTION_SORTBY
dictionary tracebackOptions
Constans traceback config options (key - options name, value - default.
def __parseParams(self)
parsing paramers
string PROFILER_OPTION_TRACEBACK
string PROFILER_OPTION_LOG
Constans profiler options read from config.
string MSG_ERROR_WRONG_CONFIG_FILE_NAME
string MSG_ERROR_PARSE_CMD_PARAMS
Constants error messages used in class.
int PROFILER_OPTION_STATUS_DEFAULT
Constans default values of profiler options.
string PROFILER_OPTION_STATUS
string MSG_ERROR_WRONG__OPTION_SORTBY
def start(self)
start profiling
Class which provides functionality for use profiling.
int PROFILER_OPTION_TRACEBACK_DEFAULT
int PROFILER_OPTION_TRACEBACK_LOGGER_MODE_DEFAULT
string MESSAGES_ITEMS_DELIMITER
Constans for global message list of strings in the final message for profiler records.
float PROFILER_OPTION_LIMIT_DEFAULT
string MSG_ERROR_LOAD_APP_CONFIG
list PROFILER_OPTION_SORTBY_ALLOWED_LIST
Constans allowed values of 'sortby'.
string PROFILER_OPTION_SORTBY_DEFAULT
string MSG_ERROR_WRONG_CONFIG_OPTION
string MSG_ERROR_EMPTY_CONFIG_FILE_NAME
def stop(self)
stop profiling