MatrixBerryCore
ProfileManager.m
Go to the documentation of this file.
1 classdef ProfileManager<handle
2  properties (Access=private)
3  profMode
4  profDir
5  end
6  methods (Static)
7  profSave(SProfileInfo, dirName)
8  activeBrowser=profView(varargin)
9  end
10  methods
11  function self=ProfileManager(profMode,profDir)
12  if nargin<2
13  profDir='';
14  if nargin<1
15  profMode='none';
16  end
17  end
18  self.profMode=profMode;
19  self.profDir=profDir;
20  end
21  function resTime=runAndProcess(self,fRun,varargin)
22  mxberry.core.checkvar(fRun,'isfunction(x)');
23  %
24  [reg,~,nRuns,useMedianTime]=...
25  mxberry.core.parseparext(varargin,...
26  {'nRuns','useMedianTime';1,false;...
27  'isreal(x)&&isscalar(x)','islogical(x)&&isscalar(x)'},...
28  [0,1],'propRetMode','separate');
29  %
30  if isempty(reg)
31  profCaseName='default';
32  else
33  profCaseName=reg{1};
34  end
35  %
36  isnDetailed=any(strcmpi(self.profMode,{'none','off'}));
37  if isnDetailed
38  if useMedianTime
39  resTimeVec=zeros(1,nRuns);
40  curProfileInfoObj=mxberry.dev.prof.ProfileInfo();
41  for iRun=1:nRuns
42  curProfileInfoObj.tic();
43  feval(fRun);
44  resTimeVec(iRun)=curProfileInfoObj.toc();
45  end
46  resTime=median(resTimeVec);
47  else
48  profileInfoObj=mxberry.dev.prof.ProfileInfo();
49  profileInfoObj.tic();
50  for iRun=1:nRuns
51  feval(fRun);
52  end
53  resTime=profileInfoObj.process();
54  end
55  else
56  profileInfoObj=mxberry.dev.prof.ProfileInfoDetailed();
57  profileInfoObj.tic();
58  try
59  for iRun=1:nRuns
60  feval(fRun);
61  end
62  catch meObj
63  profileInfoObj.toc();
64  rethrow(meObj);
65  end
66  %
67  resTime=profileInfoObj.process(...
68  profCaseName,'profileMode',self.profMode,...
69  'callerName',mxberry.core.getcallername(2),...
70  'profileDir',self.profDir);
71  end
72  end
73  end
74 end
75 function profSave(SProfileInfo, dirName)
76 import mxberry.core.throwerror;
77 if nargin < 2
78  dirName = 'profile_results';
79 end
80 %
81 pathStr = fileparts(dirName);
82 %
83 if isempty(pathStr)
84  fullDirname = fullfile(cd,dirName);
85 else
86  fullDirname = dirName;
87 end
88 %
89 if ~mxberry.io.isdir(fullDirname)
90  mxberry.io.mkdir(dirName);
91 end
92 %
93 for iFunc = 0:numel(SProfileInfo.FunctionTable)
94  htmlStr = profview(iFunc,SProfileInfo);
95 
96  htmlStr = regexprep(htmlStr,'<a href="matlab: profview\((\d+)\);">',...
97  '<a href="file$1.html">');
98  %
99  htmlStr = regexprep(htmlStr,'<a href="matlab:.*?>(.*?)</a>','$1');
100  %
101  htmlStr = regexprep(htmlStr,'<form.*?</form>','');
102  %
103  insertStr = ['<body bgcolor="#F8F8F8"><strong>',...
104  'This is a static copy of a profile report</strong><p>' ...
105  '<a href="file0.html">Home</a><p>'];
106  htmlStr = strrep(htmlStr,'<body>',insertStr);
107  %
108  fileName = fullfile(fullDirname,sprintf('file%d.html',iFunc));
109  [idFile,errMsg] = fopen(fileName,'w','n','utf8');
110  if idFile > 0
111  fprintf(idFile,'%s',htmlStr);
112  fclose(idFile);
113  else
114  throwerror('unableToOpenFile',...
115  'Unable to open file %s, reason: %s',fileName,errMsg);
116  end
117 end
118 function activeBrowser=profView(varargin)
119 import mxberry.core.throwerror;
120 persistent profInfoMap browserMap
121 [reg,prop]=mxberry.core.parseparams(varargin,...
122  {'titlePrefix','keepCache','cacheKey'});
123 nRegs=length(reg);
124 nProps=length(prop);
125 %
126 isTitlePrefixSpec=false;
127 isCacheKept=false;
128 isCacheKeySpec=false;
129 for k=1:2:nProps-1
130  switch lower(prop{k})
131  case 'titleprefix'
132  titlePrefix=prop{k+1};
133  if ~(ischar(titlePrefix)&&mxberry.core.isrow(titlePrefix))
134  throwerror('wrongInput',...
135  'titlePrefix property is expected to be a string');
136  end
137  isTitlePrefixSpec=true;
138  case 'keepcache'
139  isCacheKept=prop{k+1};
140  if ~(islogical(isCacheKept)&&numel(isCacheKept)==1)
141  throwerror('wrongInput',...
142  'cacheKept property is expected to be a logical scalar');
143  end
144  case 'cachekey'
145  cacheKey=prop{k+1};
146  if ~(ischar(cacheKey)&&mxberry.core.isrow(cacheKey))
147  throwerror('wrongInput',...
148  'cacheKey property is expected to be a string');
149  end
150  isCacheKeySpec=true;
151  end
152 end
153 %
154 if isempty(browserMap)
155  browserMap=containers.Map();
156 end
157 if isCacheKeySpec
158  if browserMap.isKey(cacheKey)
159  activeBrowser=browserMap(cacheKey);
160  else
161  throwerror('wrongInput',...
162  'Oops, we shouldn''t be here');
163  end
164 else
165  activeBrowser=createBrowser();
166  cacheKey=num2str(activeBrowser.hashCode());
167  browserMap(cacheKey)=activeBrowser;
168 end
169 %
170 if ~isTitlePrefixSpec
171  titlePrefix='';
172 end
173 %
174 if isempty(profInfoMap)
175  profInfoMap=containers.Map();
176 end
177 %
178 if (profInfoMap.isKey(cacheKey)&&~isCacheKept)||~profInfoMap.isKey(cacheKey)
179  profInfo=profile('info');
180  profInfoMap(cacheKey)=profInfo;
181 else
182  profInfo=profInfoMap(cacheKey);
183 end
184 %
185 if nRegs==0
186  reg={0,profInfo};
187 elseif nRegs==1
188  reg=[reg,{profInfo}];
189 end
190 %
191 htmlOut=profview(reg{:});
192 funcRepStr='mxberry.dev.prof.ProfileManager.profView';
193 funcRepArgStr=['''keepCache'',true,''cacheKey'',''',cacheKey,''','];
194 if isTitlePrefixSpec
195  htmlOut=strrep(htmlOut,'<title>',['<title>',titlePrefix,', ']);
196  funcRepArgStr=[funcRepArgStr,'''titlePrefix'',''',titlePrefix,''','];
197 end
198 %
199 htmlOut=strrep(htmlOut,'profview(',['profview(',funcRepArgStr]);
200 %
201 htmlOut=strrep(htmlOut,'profview',funcRepStr);
202 %
203 if nargout==0
204  activeBrowser.setHtmlText(htmlOut);
205 end
206 end
207 function activeBrowser=createBrowser()
208 activeBrowser=...
209  com.mathworks.mde.webbrowser.WebBrowser.createBrowser(true,true);
210 if isempty(activeBrowser)
211  throwerror('wrongCall',...
212  'Browser window for profiler can not be created');
213 end
214 end
function tic(in self)
TIC starts a stopwatch timer and begins profiling.
function tic(in self)
TIC starts a stopwatch timer and begins profiling.
function throwerror(in msgTag, in varargin)
THROWERROR works similarly to built-in ERROR function in case when there is no output arguments but s...
function parseparext(in args, in propNameValMat, in varargin)
PARSEPAREXT behaves in the same way as mxberry.core.parseparams but returns property values in a more...
function checkvar(in x, in typeSpec, in varargin)
CHECKVAR checks a generic condition provided by typeSpec string in the following format: &#39;isnumeric(x...
PROFILEINFO contains profiling info obtaining during exectution of some code.
Definition: ProfileInfo.m:5
function isfunction(in inpArray)
PROFILEINFODETAILED contains detailed profiling info obtaining during exectution of some code...