Commit 399c4114 authored by Vincent Pelletier's avatar Vincent Pelletier

erp5_administration: Add profiling scripts.

Guarded with Manager role, as they allow exporting entire source files & scripts.
parent 16b7eb0d
Pipeline #6460 failed with stage
in 0 seconds
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="ExternalMethod" module="Products.ExternalMethod.ExternalMethod"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_function</string> </key>
<value> <string>getProfiler</string> </value>
</item>
<item>
<key> <string>_module</string> </key>
<value> <string>pprofile</string> </value>
</item>
<item>
<key> <string>guard</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAI=</string> </persistent>
</value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>ERP5Site_getProfiler</string> </value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string></string> </value>
</item>
</dictionary>
</pickle>
</record>
<record id="2" aka="AAAAAAAAAAI=">
<pickle>
<global name="Guard" module="Products.DCWorkflow.Guard"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>roles</string> </key>
<value>
<tuple>
<string>Manager</string>
</tuple>
</value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="ExternalMethod" module="Products.ExternalMethod.ExternalMethod"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_function</string> </key>
<value> <string>getStatisticalProfilerAndThread</string> </value>
</item>
<item>
<key> <string>_module</string> </key>
<value> <string>pprofile</string> </value>
</item>
<item>
<key> <string>guard</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAI=</string> </persistent>
</value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>ERP5Site_getStatisticalProfilerAndThread</string> </value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string></string> </value>
</item>
</dictionary>
</pickle>
</record>
<record id="2" aka="AAAAAAAAAAI=">
<pickle>
<global name="Guard" module="Products.DCWorkflow.Guard"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>roles</string> </key>
<value>
<tuple>
<string>Manager</string>
</tuple>
</value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
"""
Profile the whole execution of a callable.
Always raises, in order to allow profiling repeatedly in the same initial conditions code which would otherwise modify databases.
Writes result to response directly.
args (tuple)
kw (dict)
Positional and keyword arguments to pass to profiled callable.
func (None, callable)
Profiled callable, or context if not provided.
statistic (bool)
Whether to use deterministic or statistic profiling.
Deterministic is more detailed and easier to read, but has a very large performance cost (10x slower). It is best suited for profiling code which normally runs in a few seconds.
Statistic is less detailed and harder to read, but has an extremely small performance cost. It is best suited for profiling code which normally takes at least 10s to run.
base_name (None, str):
Value to include in the name of result file, to help categorise your own results.
When None, produced file names are of the format: {cachegrind.out.|statistical_}YYYYmmddHHMMSS{|.zip}
Otherwise, produced file names are of the format: {cachegrind.out.|statistical_}${base_name}_YYYYmmddHHMMSS{|.zip}
zipfile (bool)
When true, the result is a zip file containing profiling result along with the python code (and, when not possible, the disassembled bytecode) of all files which appear in the profiling result.
When false, the result is a bare profiling result (cachegrind file format).
"""
from StringIO import StringIO
portal = context.getPortalObject()
if statistic:
profiler, retriever = portal.ERP5Site_getStatisticalProfilerAndThread(single=True)
else:
profiler = retriever = portal.ERP5Site_getProfiler()
kw = dict(kw)
if func is None:
func = context
with retriever:
func(*args, **kw)
response = context.REQUEST.RESPONSE
filename = DateTime().strftime('%Y%m%d%H%M%S')
if base_name:
filename = base_name + '_' + filename
if zipfile:
data, content_type = profiler.asZip()
filename = 'statistical_' + filename + '.zip'
else:
out = StringIO()
profiler.callgrind(out, relative_path=True)
data = out.getvalue()
content_type = 'application/x-kcachegrind'
filename = 'cachegrind.out.' + filename
response.setHeader('content-type', content_type)
response.setHeader('content-disposition', 'attachment; filename="' + filename + '"')
response.write(data)
raise Exception('profiling')
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="PythonScript" module="Products.PythonScripts.PythonScript"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>Script_magic</string> </key>
<value> <int>3</int> </value>
</item>
<item>
<key> <string>_bind_names</string> </key>
<value>
<object>
<klass>
<global name="NameAssignments" module="Shared.DC.Scripts.Bindings"/>
</klass>
<tuple/>
<state>
<dictionary>
<item>
<key> <string>_asgns</string> </key>
<value>
<dictionary>
<item>
<key> <string>name_container</string> </key>
<value> <string>container</string> </value>
</item>
<item>
<key> <string>name_context</string> </key>
<value> <string>context</string> </value>
</item>
<item>
<key> <string>name_m_self</string> </key>
<value> <string>script</string> </value>
</item>
<item>
<key> <string>name_subpath</string> </key>
<value> <string>traverse_subpath</string> </value>
</item>
</dictionary>
</value>
</item>
</dictionary>
</state>
</object>
</value>
</item>
<item>
<key> <string>_params</string> </key>
<value> <string>args=(), kw=(), func=None, statistic=False, base_name=None, zipfile=True</string> </value>
</item>
<item>
<key> <string>guard</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAI=</string> </persistent>
</value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>ERP5Site_profileContext</string> </value>
</item>
</dictionary>
</pickle>
</record>
<record id="2" aka="AAAAAAAAAAI=">
<pickle>
<global name="Guard" module="Products.DCWorkflow.Guard"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>roles</string> </key>
<value>
<tuple>
<string>Manager</string>
</tuple>
</value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
"""
Profile all threads from current process.
Spawns an extra thread (which is ignored in the profiling) to periodically take snapshots of all threads' call stacks.
The source code line of each innermost stack entry gets one hit, and all its callers are remembered as such.
Returns result and sets some response headers.
duration (number):
How long to sample for.
Larger values will produce more detailed results.
Smaller values will produce more noisy results.
base_name (None, str):
Value to include in the name of result file, to help categorise your own results.
When None, produced file names are of the format: {cachegrind.out.|statistical_}YYYYmmddHHMMSS{|.zip}
Otherwise, produced file names are of the format: {cachegrind.out.|statistical_}${base_name}_YYYYmmddHHMMSS{|.zip}
zipfile (bool)
When true, the result is a zip file containing profiling result along with the python code (and, when not possible, the disassembled bytecode) of all files which appear in the profiling result.
When false, the result is a bare profiling result (cachegrind file format).
"""
from time import sleep
from StringIO import StringIO
profiler, thread = context.ERP5Site_getStatisticalProfilerAndThread(single=False)
with thread:
sleep(duration)
response = context.REQUEST.RESPONSE
filename = DateTime().strftime('%Y%m%d%H%M%S')
if base_name:
filename = base_name + '_' + filename
if zipfile:
data, content_type = profiler.asZip()
filename = 'statistical_' + filename + '.zip'
else:
out = StringIO()
profiler.callgrind(out, relative_path=True)
data = out.getvalue()
content_type = 'application/x-kcachegrind'
filename = 'cachegrind.out.' + filename
response.setHeader('content-type', content_type)
response.setHeader('content-disposition', 'attachment; filename="' + filename + '"')
return data
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="PythonScript" module="Products.PythonScripts.PythonScript"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>Script_magic</string> </key>
<value> <int>3</int> </value>
</item>
<item>
<key> <string>_bind_names</string> </key>
<value>
<object>
<klass>
<global name="NameAssignments" module="Shared.DC.Scripts.Bindings"/>
</klass>
<tuple/>
<state>
<dictionary>
<item>
<key> <string>_asgns</string> </key>
<value>
<dictionary>
<item>
<key> <string>name_container</string> </key>
<value> <string>container</string> </value>
</item>
<item>
<key> <string>name_context</string> </key>
<value> <string>context</string> </value>
</item>
<item>
<key> <string>name_m_self</string> </key>
<value> <string>script</string> </value>
</item>
<item>
<key> <string>name_subpath</string> </key>
<value> <string>traverse_subpath</string> </value>
</item>
</dictionary>
</value>
</item>
</dictionary>
</state>
</object>
</value>
</item>
<item>
<key> <string>_params</string> </key>
<value> <string>duration=60, base_name=None, zipfile=True</string> </value>
</item>
<item>
<key> <string>guard</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAI=</string> </persistent>
</value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>ERP5Site_profileProcess</string> </value>
</item>
</dictionary>
</pickle>
</record>
<record id="2" aka="AAAAAAAAAAI=">
<pickle>
<global name="Guard" module="Products.DCWorkflow.Guard"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>roles</string> </key>
<value>
<tuple>
<string>Manager</string>
</tuple>
</value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment