home.html 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748
  1. <!DOCTYPE html>
  2. <!--
  3. Metrix++, Copyright 2009-2013, Metrix++ Project
  4. Link: http://metrixplusplus.sourceforge.net
  5. This file is part of Metrix++ Tool.
  6. Metrix++ is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, version 3 of the License.
  9. Metrix++ is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Metrix++. If not, see <http://www.gnu.org/licenses/>.
  15. -->
  16. <html lang="en">
  17. <head>
  18. <meta charset="utf-8">
  19. <title>Metrix++ Project</title>
  20. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  21. <meta name="description" content="">
  22. <meta name="author" content="">
  23. <!-- Le styles -->
  24. <!--
  25. <link href="../../style.css" rel="stylesheet">
  26. -->
  27. <link href="assets/css/bootstrap.css" rel="stylesheet">
  28. <link href="assets/css/bootstrap-responsive.css" rel="stylesheet">
  29. <link href="assets/css/docs.css" rel="stylesheet">
  30. <link href="assets/js/google-code-prettify/prettify.css" rel="stylesheet">
  31. <!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
  32. <!--[if lt IE 9]>
  33. <script src="assets/js/html5shiv.js"></script>
  34. <![endif]-->
  35. <!-- Le fav and touch icons -->
  36. <link rel="apple-touch-icon-precomposed" sizes="144x144" href="assets/ico/apple-touch-icon-144-precomposed.png">
  37. <link rel="apple-touch-icon-precomposed" sizes="114x114" href="assets/ico/apple-touch-icon-114-precomposed.png">
  38. <link rel="apple-touch-icon-precomposed" sizes="72x72" href="assets/ico/apple-touch-icon-72-precomposed.png">
  39. <link rel="apple-touch-icon-precomposed" href="assets/ico/apple-touch-icon-57-precomposed.png">
  40. <link rel="shortcut icon" href="assets/ico/favicon.png">
  41. <!--
  42. <script type="text/javascript">
  43. var _gaq = _gaq || [];
  44. _gaq.push(['_setAccount', 'UA-146052-10']);
  45. _gaq.push(['_trackPageview']);
  46. (function() {
  47. var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  48. ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  49. var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  50. })();
  51. </script>
  52. -->
  53. </head>
  54. <body data-spy="scroll" data-target=".bs-docs-sidebar">
  55. <!-- Navbar
  56. ================================================== -->
  57. <div class="navbar navbar-link navbar-fixed-top">
  58. <div class="navbar-inner">
  59. <div class="container">
  60. <button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
  61. <span class="icon-bar"></span>
  62. <span class="icon-bar"></span>
  63. <span class="icon-bar"></span>
  64. </button>
  65. <a class="brand" href="./index.html">Metrix++</a>
  66. <div class="nav-collapse collapse">
  67. <ul class="nav">
  68. <li class="">
  69. <a href="./index.html">Home</a>
  70. </li>
  71. </ul>
  72. </div>
  73. </div>
  74. </div>
  75. </div>
  76. <!-- Subhead
  77. ================================================== -->
  78. <header class="jumbotron" id="overview">
  79. <div id="myCarousel" class="carousel slide">
  80. <div class="carousel-inner">
  81. <div class="item active">
  82. <img src="assets/img/slide-01.jpg" alt="">
  83. <div class="container">
  84. <div class="carousel-caption">
  85. <h2>Multiple languages</h2>
  86. <p class="lead">&middot; C/C++, C# and Java.</p>
  87. <p class="lead">&middot; Recognises classes, interfaces, namespaces, functions, comments, preprocessor and much more.</p>
  88. </div>
  89. </div>
  90. </div>
  91. <div class="item">
  92. <img src="assets/img/slide-02.jpg" alt="">
  93. <div class="container">
  94. <div class="carousel-caption">
  95. <h2>Multiple metrics</h2>
  96. <p class="lead">&middot; Complexity, size and other.</p>
  97. </div>
  98. </div>
  99. </div>
  100. <div class="item">
  101. <img src="assets/img/slide-03.jpg" alt="">
  102. <div class="container">
  103. <div class="carousel-caption">
  104. <h2>High performance and scalability</h2>
  105. <p class="lead">&middot; Applicable to huge code bases: thousands of files per minute.</p>
  106. <p class="lead">&middot; Ultra-fast feedback on iterative re-run.</p>
  107. </div>
  108. </div>
  109. </div>
  110. <div class="item">
  111. <img src="assets/img/slide-01.jpg" alt="">
  112. <div class="container">
  113. <div class="carousel-caption">
  114. <h2>Effortless application to legacy code</h2>
  115. <p class="lead">&middot; Recognises legacy, modified and new code.</p>
  116. <p class="lead">&middot; Prevents from negative trends. Encourages positive.</p>
  117. </div>
  118. </div>
  119. </div>
  120. <div class="item">
  121. <img src="assets/img/slide-02.jpg" alt="">
  122. <div class="container">
  123. <div class="carousel-caption">
  124. <h2>Configurable</h2>
  125. <p class="lead">&middot; Define and apply your rules and policies.</p>
  126. <p class="lead">&middot; Integrate with your workflow.</p>
  127. </div>
  128. </div>
  129. </div>
  130. <div class="item">
  131. <img src="assets/img/slide-03.jpg" alt="">
  132. <div class="container">
  133. <div class="carousel-caption">
  134. <h2>Extendable via plugins</h2>
  135. <p class="lead">&middot; Define your custom metric.</p>
  136. <p class="lead">&middot; Add new language parser.</p>
  137. <p class="lead">&middot; Create advanced post-analysis tool.</p>
  138. </div>
  139. </div>
  140. </div>
  141. </div>
  142. <a class="left carousel-control" href="#myCarousel" data-slide="prev">&lsaquo;</a>
  143. <a class="right carousel-control" href="#myCarousel" data-slide="next">&rsaquo;</a>
  144. </div>
  145. <div class="container">
  146. <div class="row">
  147. <div class="span3">
  148. </div>
  149. <div class="span9">
  150. <h5 class="text-right">Management of source code quality is possible.</h5>
  151. <p class="text-right">
  152. <a href="https://sourceforge.net/projects/metrixplusplus/files/latest/download"
  153. ><button type="button"class="btn btn-danger">Download</button></a>
  154. <!--
  155. <button type="button"class="btn btn-warning">Donate</button>
  156. -->
  157. </p>
  158. </div>
  159. </div>
  160. </div>
  161. </header>
  162. <div class="container"><div class="row">
  163. <!-- Docs nav
  164. ================================================== -->
  165. <div class="span3 bs-docs-sidebar">
  166. <ul class="nav nav-list bs-docs-sidenav">
  167. <!--<li><img src="../../logo_project.png"/><p>&nbsp;</p></li>-->
  168. <li><a href="#overview_section"><i class="icon-chevron-right"></i> Overview</a></li>
  169. <li><a href="#download_section"><i class="icon-chevron-right"></i> Download &amp; Install</a></li>
  170. <li><a href="#workflow_collect_section"><i class="icon-chevron-right"></i> Workflow: Collect data</a></li>
  171. <li><a href="#workflow_view_section"><i class="icon-chevron-right"></i> Workflow: View data</a></li>
  172. <li><a href="#workflow_view_summary_section"><i class="icon-hand-right"></i> &middot; summary &amp; distributions</a></li>
  173. <li><a href="#workflow_view_details_section"><i class="icon-hand-right"></i> &middot; details per file/region</a></li>
  174. <li><a href="#workflow_limit_section"><i class="icon-chevron-right"></i> Workflow: Apply thresholds</a></li>
  175. <li><a href="#workflow_limit_hotspots_section"><i class="icon-hand-right"></i> &middot; hotspots</a></li>
  176. <li><a href="#workflow_limit_control_section"><i class="icon-hand-right"></i> &middot; controlling trends</a></li>
  177. <li><a href="#workflow_other_section"><i class="icon-chevron-right"></i> Workflow: Other applications</a></li>
  178. <li><a href="#extend_section"><i class="icon-chevron-right"></i> Create plugin</a></li>
  179. <li><a href="#contribute_section"><i class="icon-chevron-right"></i> Feedback &amp; Contribute</a></li>
  180. </ul>
  181. </div>
  182. <!-- Sections
  183. ================================================== -->
  184. <div class="span9">
  185. <!-- Overview
  186. ================================================== -->
  187. <section id="overview_section">
  188. <div class="page-header">
  189. <h1>Overview</h1>
  190. </div>
  191. <h3>Highlights</h3>
  192. <p>Metrix++ is a tool to collect and analyse code metrics. Any metric is useless if it is not used.
  193. Metrix++ offers ease of introduction and integration with a variety of application use cases.</p>
  194. <ul>
  195. <li>Monitoring trends (eg. on <strong>daily</strong> basis. In order to take actions or make right decisions earlier.)</li>
  196. <li>Enforcing trends (eg. on <strong>hourly</strong> basis, at every commit of code changes.)</li>
  197. <li>Automated asistance to review agains standards (eg. on <strong>per minute</strong> basis during code refactoring and code development.)</li>
  198. </ul>
  199. <p>The workflow sections below demonstarate these basic application usecases.</p>
  200. <h3>Languages supported</h3>
  201. <p>The tool can parse C/C++, C# and Java source code files. The parser identifies certain regions in the code,
  202. such as classes, functions, namespaces, interfaces, etc. It detects comments, strings and code for the preprocessor.
  203. The identified regions form a tree of nested source code blocks, which are subsequently refered to and scanned by metrics plugins.
  204. Thus the tool attributes metrics to regions, which provides fine grained data to analysis tools.
  205. The following example demonstrates the regions concept.</p>
  206. <table class="table">
  207. <thead>
  208. <tr>
  209. <th>Source code</th>
  210. <th>Regions tree [type: name: content type]</th>
  211. </tr>
  212. </thead>
  213. <tbody>
  214. <tr>
  215. <td>
  216. <pre class="prettyprint linenums">
  217. // simple C++ code
  218. #include &lt;myapi.h&gt;
  219. // I explain the following class
  220. class MyClass {
  221. public:
  222. int m_var; // some member
  223. // I explain the following function
  224. MyClass(): m_var(0) {
  225. char str[] = "unused string"
  226. // nested region for good measure
  227. struct MyStruct {};
  228. }
  229. // Do not judge ugly code below
  230. #define get_max(a,b) ((a)>(b)?(a):(b))
  231. set_max(int b) {
  232. m_var = get_max(m_var, b);
  233. }
  234. };
  235. // this is the last line
  236. </pre>
  237. </td>
  238. <td>
  239. <pre class="prettyprint linenums">
  240. file: __global__: comment
  241. file: __global__: code
  242. file: __global__: preprocessor
  243. file: __global__: code
  244. class: MyClass: comment
  245. class: MyClass: code
  246. class: MyClass: code
  247. class: MyClass: code, comment
  248. class: MyClass: code
  249. function: MyClass: comment
  250. function: MyClass: code
  251. function: MyClass: code, string
  252. function: MyClass: code
  253. struct: MyStruct: comment
  254. struct: MyStruct: code
  255. function: MyClass: code
  256. class: MyClass: code
  257. function: set_max: comment
  258. function: set_max: preprocessor
  259. function: set_max: code
  260. function: set_max: code
  261. function: set_max: code
  262. class: MyClass: code
  263. file: __global__: comment
  264. </pre>
  265. </td>
  266. </tr>
  267. </tbody>
  268. </table>
  269. <h3>Metrics</h3>
  270. <p>The metrics highlighed in blue are <strong>per file</strong> metrics. The other metrics are <strong>per region</strong> metrics.</p>
  271. <table class="table table-bordered">
  272. <thead>
  273. <tr>
  274. <th>Metric (enable option)</th>
  275. <th>Brief description</th>
  276. <th>Motivation / Potential use</th>
  277. </tr>
  278. </thead>
  279. <tbody>
  280. <tr class="info">
  281. <td>std.general.size</td>
  282. <td>Size of a file in bytes.</td>
  283. <td rowspan="4"><ul><li>Monitoring the growth of source code base.</li>
  284. <li>Normalizing other metrics.</li>
  285. <li>Preventing large files and regions (large things are difficult to maintain).</li>
  286. <li>Predicting delivery dates by comparing
  287. <a href="http://www.compaid.com/caiInternet/casestudies/kanarticle2.pdf">S-shaped code base growth / change curves</a>.</li></ul></td>
  288. </tr>
  289. <tr>
  290. <td>std.code.length.total</td>
  291. <td>The same as 'std.general.size' metric, but attributed to code regions.</td>
  292. <td></td>
  293. </tr>
  294. <tr>
  295. <td>std.code.lines.total</td>
  296. <td>Number of non-blank lines of code of any content type (exectuable, comments, etc.)</td>
  297. <td></td>
  298. </tr>
  299. <tr>
  300. <td>std.code.lines.code</td>
  301. <td>Number of non-blank lines of code excluding preprocessor and comments.</td>
  302. <td></td>
  303. </tr>
  304. <tr>
  305. <td>std.code.lines.preprocessor</td>
  306. <td>Number of non-blank lines of preprocessor code.</td>
  307. <td><ul><li>Enforcing localisation of preprocessor code in a single place.</li>
  308. <li>Encouraging usage of safer code structures instead of the preprocessor.</li></ul></td>
  309. </tr>
  310. <tr>
  311. <td>std.code.lines.comments</td>
  312. <td>Number of non-blank lines of comments.</td>
  313. <td><ul><li>Low number of comments may indicate maintainability problems.</li></ul></td>
  314. </tr>
  315. <tr>
  316. <td>std.code.complexity.cyclomatic</td>
  317. <td>McCabe cyclomatic complexity metric.</td>
  318. <td colspan="2"><ul><li>Identification of highly complex code for review and refactoring.</li>
  319. <li>Preventing complex functions (complexity is a reason of many defects and a reason of expensive maintaintenance).</li></ul></td>
  320. </tr>
  321. <tr>
  322. <td>std.code.complexity.maxindent</td>
  323. <td>Maximum level of indentation of blocks within a region. For example, the following class has got
  324. the metric equal to 1 and the function has got it equal to 2:
  325. <pre class="prettyprint">
  326. class WeekClass {
  327. int isWeekend(int day) {
  328. if (day == SATURDAY ||
  329. day == SUNDAY) {
  330. return true;
  331. }
  332. return false;
  333. }
  334. }
  335. </pre>
  336. </td>
  337. </tr>
  338. <tr>
  339. <td>std.suppress</td>
  340. <td>An option enables collection of Metrix++ suppressions and 2 metrics: 'std.suppress:count' and
  341. 'std.suppress.file:count'. The first is number of suppressions per region.
  342. The second is the same but applies for file-scope metrics.</td>
  343. <td><ul><li>Suppressing false-positives.</li>
  344. <li>Managing the amount of suppressions. There should be no false-positives to suppress with the right metric,
  345. but there could be exceptions in specific cases. Managing suppressions is about managing exceptions.
  346. If there are many exceptional cases, maybe something is wrong with a metric or the application of a metric.</li></ul></td>
  347. </tr>
  348. <tr class="info">
  349. <td>std.general.procerrors</td>
  350. <td>Number of errors detected by Metrix++ code parser.</td>
  351. <td><ul><li>Cleaning up errors to ensure reliable code scanning.</li>
  352. <li>Errors, like mismatched brackets, may result in bad identification of regions.</li>
  353. </ul></td>
  354. </tr>
  355. <tr class="info">
  356. <td>std.general.proctime</td>
  357. <td>Seconds spent on processing a file.</td>
  358. <td><ul><li>Monitor and profile Metrix++ tool's performance.</li></ul></td>
  359. </tr>
  360. </tbody>
  361. </table>
  362. </section>
  363. <section id="download_section">
  364. <div class="page-header">
  365. <h1>Download &amp; Install</h1>
  366. </div>
  367. <p>In order to get the tool working, <a href="https://sourceforge.net/projects/metrixplusplus/files/latest/download">dowload the archive</a> with the latest stable version
  368. and unpack it to some folder. The first run of the tool will trigger the installation within the folder,
  369. where it was launched.</p>
  370. <p>In order to checkout the latest released version from the <a href="https://sourceforge.net/p/metrixplusplus/code">version control system</a> use this command:</p>
  371. <pre>svn checkout <a href="https://sourceforge.net/p/metrixplusplus/code">svn://svn.code.sf.net/p/metrixplusplus/code</a>/releases/latest Metrix++</pre>
  372. <p>In order to checkout the latest version under development, use this command:</p>
  373. <pre>svn checkout <a href="https://sourceforge.net/p/metrixplusplus/code">svn://svn.code.sf.net/p/metrixplusplus/code</a>/mainline Metrix++</pre>
  374. <h4>Prerequisites</h4>
  375. <p>Python Runtime Environment (version 2.7.* or later. Version 3.* has not been tested)</p>
  376. <h4>License</h4>
  377. <p>This program is free software; you can redistribute it and/or modify it
  378. under the terms of the GNU General Public License as published by the
  379. Free Software Foundation; version 3 of the License.</p>
  380. <p>This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  381. without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  382. See the GNU General Public License for more details.</p>
  383. <p>You should have received a copy of the GNU General Public License along with the Metrix++;
  384. if not, contact <a href="mailto:avkonst@users.sourceforge.net?subject=Metrix%2B%2B License">Project Administrator</a></p>
  385. </section>
  386. <div class="page-header">
  387. <h1>Workflow</h1>
  388. </div>
  389. <p>The workflow and application usecases are demonstrated using source code from the
  390. <a href="http://www.boost.org/doc/libs/1_54_0/doc/html/interprocess.html">boost/interprocess library</a>.
  391. Boost versions 1.52 and 1.54 are used and refered below as the 'previous' version and the 'current' version accordingly.</p>
  392. <section id="workflow_collect_section">
  393. <h2>Collect data</h2>
  394. <p>The first step is to collect the data.
  395. The 'collect' tool has got multiple options to enable various metrics plugins.
  396. Let's collect the number of lines of code and cyclomatic complexity metrics
  397. for the previous (1.52.0 version) boost interprocess library. Assuming that 2 versions of boost library
  398. are unpacked in the current working directory:</p>
  399. <pre>
  400. &gt; cd boost_1_52_0
  401. &gt; python "/path/to/metrix++.py" collect --std.code.lines.code --std.code.complexity.cyclomatic -- boost/interprocess
  402. &gt; cd ../ # return back to working directory
  403. </pre>
  404. <p>The list of arguments after '--' enumerates the paths to read the source files.
  405. As a result of execution of this command, a file metrixpp.db will be written in the current working directory.
  406. It can be redefined using the --db-file option.</p>
  407. <p>Metrix++ can compare code bases which reduces processing scope to the modified or new code.
  408. So, let's collect the same data for the current (1.54.0 version) boost interprocess library.</p>
  409. <pre>
  410. &gt; cd boost_1_54_0
  411. &gt; python "/path/to/metrix++.py" collect --std.code.lines.code --std.code.complexity.cyclomatic -- boost/interprocess --db-file-prev=../boost_1_52_0/metrixpp.db
  412. &gt; cd ../ # return back to working directory
  413. </pre>
  414. <p>The option --db-file-prev points to the file with the data collected in the previous step.
  415. So, eventually it executed iterative collection. It can speed up the exectuion significantly,
  416. depending on amount of changes between two version.</p>
  417. <p>Check other options of the collect tool by executing:</p>
  418. <pre>
  419. &gt; python "/path/to/metrix++.py" collect --help
  420. </pre>
  421. </section>
  422. <section id="workflow_view_section">
  423. <h2>View data</h2>
  424. </section>
  425. <section id="workflow_view_summary_section">
  426. <h3>Summary metrics and distribution tables/graphs</h3>
  427. <p>It is time to look at the data files collected (step above). The command:</p>
  428. <pre>
  429. &gt; python "/path/to/metrix++.py" view --db-file=boost_1_54_0/metrixpp.db
  430. </pre>
  431. <p>prints summary metrics, like minimum/maximum, and distribution/frequency tables:</p>
  432. <pre>
  433. :: info: Overall metrics for 'std.code.complexity:cyclomatic' metric
  434. Average : 0.652902698283
  435. Minimum : 0
  436. Maximum : 37
  437. Total : 1597.0
  438. Distribution : 2446 regions in total (including 0 suppressed)
  439. Metric value : Ratio : R-sum : Number of regions
  440. 0 : 0.771 : 0.771 : 1886 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  441. 1 : 0.110 : 0.881 : 268 |||||||||||
  442. 2 : 0.044 : 0.925 : 108 ||||
  443. 3 : 0.025 : 0.949 : 60 ||
  444. 4 : 0.016 : 0.966 : 40 ||
  445. 5 : 0.007 : 0.973 : 18 |
  446. 6 : 0.006 : 0.979 : 14 |
  447. 7 : 0.004 : 0.983 : 10
  448. 8 : 0.003 : 0.986 : 8
  449. 9 : 0.002 : 0.988 : 4
  450. 10 : 0.004 : 0.991 : 9
  451. 11 : 0.002 : 0.993 : 4
  452. 12 : 0.001 : 0.994 : 3
  453. 13 : 0.001 : 0.995 : 2
  454. 14 : 0.001 : 0.996 : 2
  455. 15-16 : 0.001 : 0.997 : 3
  456. 17-18 : 0.001 : 0.998 : 3
  457. 20 : 0.000 : 0.999 : 1
  458. 23-25 : 0.001 : 1.000 : 2
  459. 37 : 0.000 : 1.000 : 1
  460. :: info: Overall metrics for 'std.code.lines:code' metric
  461. Average : 6.64356984479
  462. Minimum : 0
  463. Maximum : 201
  464. Total : 23970.0
  465. Distribution : 3608 regions in total (including 0 suppressed)
  466. Metric value : Ratio : R-sum : Number of regions
  467. 0-1 : 0.088 : 0.088 : 319 |||||||||
  468. 2 : 0.320 : 0.409 : 1155 ||||||||||||||||||||||||||||||||
  469. 3 : 0.108 : 0.517 : 390 |||||||||||
  470. 4 : 0.081 : 0.598 : 294 ||||||||
  471. 5 : 0.080 : 0.678 : 290 ||||||||
  472. 6 : 0.061 : 0.739 : 220 ||||||
  473. 7 : 0.049 : 0.788 : 176 |||||
  474. 8 : 0.030 : 0.818 : 109 |||
  475. 9 : 0.025 : 0.843 : 89 ||
  476. 10-11 : 0.032 : 0.876 : 117 |||
  477. 12-13 : 0.020 : 0.895 : 71 ||
  478. 14 : 0.012 : 0.907 : 43 |
  479. 15-16 : 0.017 : 0.924 : 61 ||
  480. 17-19 : 0.015 : 0.939 : 55 ||
  481. 20-22 : 0.013 : 0.952 : 46 |
  482. 23-26 : 0.011 : 0.963 : 40 |
  483. 27-30 : 0.009 : 0.972 : 33 |
  484. 31-39 : 0.009 : 0.981 : 33 |
  485. 40-65 : 0.009 : 0.991 : 34 |
  486. 66-201 : 0.009 : 1.000 : 33 |
  487. :: info: Directory content:
  488. Directory : .
  489. </pre>
  490. <p>The same command with --db-file-prev option enables comparision and change trends are shown in [] brackets:</p>
  491. <pre>
  492. &gt; python "/path/to/metrix++.py" view --db-file=boost_1_54_0/metrixpp.db --db-file-prev=boost_1_52_0/metrixpp.db
  493. </pre>
  494. <pre>
  495. :: info: Overall metrics for 'std.code.complexity:cyclomatic' metric
  496. Average : 0.652902698283 [+0.00362138411453]
  497. Minimum : 0 [+0]
  498. Maximum : 37 [+1]
  499. Total : 1597.0 [+16.0]
  500. Distribution : 2446 [+11] regions in total (including 0 [+0] suppressed)
  501. Metric value : Ratio : R-sum : Number of regions
  502. 0 : 0.771 : 0.771 : 1886 [+5 ] |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  503. 1 : 0.110 : 0.881 : 268 [+4 ] |||||||||||
  504. 2 : 0.044 : 0.925 : 108 [+0 ] ||||
  505. 3 : 0.025 : 0.949 : 60 [+2 ] ||
  506. 4 : 0.016 : 0.966 : 40 [-1 ] ||
  507. 5 : 0.007 : 0.973 : 18 [-1 ] |
  508. 6 : 0.006 : 0.979 : 14 [+1 ] |
  509. 7 : 0.004 : 0.983 : 10 [+1 ]
  510. 8 : 0.003 : 0.986 : 8 [+1 ]
  511. 9 : 0.002 : 0.988 : 4 [+0 ]
  512. 10 : 0.004 : 0.991 : 9 [-2 ]
  513. 11 : 0.002 : 0.993 : 4 [+1 ]
  514. 12 : 0.001 : 0.994 : 3 [+0 ]
  515. 13 : 0.001 : 0.995 : 2 [+0 ]
  516. 14 : 0.001 : 0.996 : 2 [+0 ]
  517. 15-16 : 0.001 : 0.997 : 3 [-1 ]
  518. 17-18 : 0.001 : 0.998 : 3 [+1 ]
  519. 20 : 0.000 : 0.999 : 1 [+0 ]
  520. 23-25 : 0.001 : 1.000 : 2 [+0 ]
  521. 36-37 : 0.000 : 1.000 : 1 [+0 ]
  522. :: info: Overall metrics for 'std.code.lines:code' metric
  523. Average : 6.64356984479 [+0.012181964309]
  524. Minimum : 0 [+0]
  525. Maximum : 201 [+4]
  526. Total : 23970.0 [+223.0]
  527. Distribution : 3608 [+27] regions in total (including 0 [+0] suppressed)
  528. Metric value : Ratio : R-sum : Number of regions
  529. 0-1 : 0.088 : 0.088 : 319 [+3 ] |||||||||
  530. 2 : 0.320 : 0.409 : 1155 [+9 ] ||||||||||||||||||||||||||||||||
  531. 3 : 0.108 : 0.517 : 390 [-3 ] |||||||||||
  532. 4 : 0.081 : 0.598 : 294 [+7 ] ||||||||
  533. 5 : 0.080 : 0.678 : 290 [+7 ] ||||||||
  534. 6 : 0.061 : 0.739 : 220 [-1 ] ||||||
  535. 7 : 0.049 : 0.788 : 176 [-2 ] |||||
  536. 8 : 0.030 : 0.818 : 109 [-1 ] |||
  537. 9 : 0.025 : 0.843 : 89 [+4 ] ||
  538. 10-11 : 0.032 : 0.876 : 117 [+9 ] |||
  539. 12-13 : 0.020 : 0.895 : 71 [-9 ] ||
  540. 14 : 0.012 : 0.907 : 43 [+0 ] |
  541. 15-16 : 0.017 : 0.924 : 61 [+0 ] ||
  542. 17-19 : 0.015 : 0.939 : 55 [+6 ] ||
  543. 20-22 : 0.013 : 0.952 : 46 [-3 ] |
  544. 23-26 : 0.011 : 0.963 : 40 [+2 ] |
  545. 27-30 : 0.009 : 0.972 : 33 [-3 ] |
  546. 31-39 : 0.009 : 0.981 : 33 [+0 ] |
  547. 40-65 : 0.009 : 0.991 : 34 [+1 ] |
  548. 66-201 : 0.009 : 1.000 : 33 [+1 ] |
  549. :: info: Directory content:
  550. Directory : .
  551. </pre>
  552. <h4>Reducing analysis scope</h4>
  553. <p>There are two ways to reduce the analysis scope for the view tool. The first is to enumerate paths of interest.
  554. For example, the following command reduces scope to the 'allocators' sub-directory within the processed code.</p>
  555. <pre>
  556. &gt; python "/path/to/metrix++.py" view --db-file=boost_1_54_0/metrixpp.db -- ./boost/interprocess/allocators
  557. </pre>
  558. <p>The second is to specify the --scope-mode option, which instructs the tool to process only modified and/or new files/regions.
  559. For example, to view the summary metrics for all modified and new regions:</p>
  560. <pre>
  561. &gt; python "/path/to/metrix++.py" view --db-file=boost_1_54_0/metrixpp.db --db-file-prev=boost_1_52_0/metrixpp.db --scope-mode=touched
  562. </pre>
  563. <pre>
  564. :: info: Overall metrics for 'std.code.complexity:cyclomatic' metric
  565. Average : 1.84924623116 [-0.0230941943761]
  566. Minimum : 0 [+0]
  567. Maximum : 37 [+1]
  568. Total : 368.0 [+16.0]
  569. Distribution : 199 [+11] regions in total (including 0 [+0] suppressed)
  570. Metric value : Ratio : R-sum : Number of regions
  571. 0 : 0.608 : 0.608 : 121 [+5 ] |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  572. 1 : 0.131 : 0.739 : 26 [+4 ] |||||||||||||
  573. 2 : 0.070 : 0.809 : 14 [+0 ] |||||||
  574. 3 : 0.060 : 0.869 : 12 [+2 ] ||||||
  575. 4 : 0.015 : 0.884 : 3 [-1 ] ||
  576. 5 : 0.015 : 0.899 : 3 [-1 ] ||
  577. 6 : 0.005 : 0.905 : 1 [+1 ] |
  578. 7 : 0.015 : 0.920 : 3 [+1 ] ||
  579. 8 : 0.015 : 0.935 : 3 [+1 ] ||
  580. 9 : 0.010 : 0.945 : 2 [+0 ] |
  581. 10 : 0.010 : 0.955 : 2 [-2 ] |
  582. 11 : 0.005 : 0.960 : 1 [+1 ] |
  583. 12 : 0.005 : 0.965 : 1 [+0 ] |
  584. 13 : 0.005 : 0.970 : 1 [+0 ] |
  585. 16 : 0.005 : 0.975 : 1 [-1 ] |
  586. 17 : 0.005 : 0.980 : 1 [+0 ] |
  587. 18 : 0.010 : 0.990 : 2 [+1 ] |
  588. 20 : 0.005 : 0.995 : 1 [+0 ] |
  589. 36-37 : 0.005 : 1.000 : 1 [+0 ] |
  590. :: info: Overall metrics for 'std.code.lines:code' metric
  591. Average : 15.9645390071 [-0.815853149771]
  592. Minimum : 0 [+0]
  593. Maximum : 201 [+6]
  594. Total : 4502.0 [+223.0]
  595. Distribution : 282 [+27] regions in total (including 0 [+0] suppressed)
  596. Metric value : Ratio : R-sum : Number of regions
  597. 0-1 : 0.053 : 0.053 : 15 [+3 ] |||||
  598. 2 : 0.124 : 0.177 : 35 [+9 ] ||||||||||||
  599. 3 : 0.053 : 0.230 : 15 [-3 ] |||||
  600. 4 : 0.060 : 0.291 : 17 [+7 ] ||||||
  601. 5 : 0.089 : 0.379 : 25 [+7 ] |||||||||
  602. 6 : 0.060 : 0.440 : 17 [-1 ] ||||||
  603. 7 : 0.050 : 0.489 : 14 [-2 ] |||||
  604. 8-9 : 0.074 : 0.564 : 21 [+3 ] |||||||
  605. 10 : 0.035 : 0.599 : 10 [+5 ] ||||
  606. 11 : 0.082 : 0.681 : 23 [+4 ] ||||||||
  607. 12-13 : 0.043 : 0.723 : 12 [-9 ] ||||
  608. 14-15 : 0.039 : 0.762 : 11 [-1 ] ||||
  609. 16-18 : 0.028 : 0.791 : 8 [+4 ] |||
  610. 19-22 : 0.039 : 0.830 : 11 [+0 ] ||||
  611. 23-26 : 0.039 : 0.869 : 11 [+2 ] ||||
  612. 27-32 : 0.028 : 0.897 : 8 [-3 ] |||
  613. 38-50 : 0.025 : 0.922 : 7 [+0 ] ||
  614. 51-69 : 0.025 : 0.947 : 7 [+1 ] ||
  615. 71-100 : 0.032 : 0.979 : 9 [+2 ] |||
  616. 101-201 : 0.021 : 1.000 : 6 [-1 ] ||
  617. :: info: Directory content:
  618. Directory : .
  619. </pre>
  620. </section>
  621. <section id="workflow_view_details_section">
  622. <h3>Detailed metrics per file/region</h3>
  623. <p>The same view tool can print detailed metrics per file and per every region in the specified file.
  624. In order to get detailed metrics, enumerate files of interest after '--'. For example:</p>
  625. <pre>
  626. &gt; python "/path/to/metrix++.py" view --db-file=boost_1_54_0/metrixpp.db --db-file-prev=boost_1_52_0/metrixpp.db -- ./boost/interprocess/detail/managed_open_or_create_impl.hpp
  627. </pre>
  628. <p>produces output similar to this (truncated to make the page shorter):</p>
  629. <pre>
  630. ./interprocess/detail/managed_open_or_create_impl.hpp:302: info: Metrics per 'priv_open_or_create' region
  631. Region name : priv_open_or_create
  632. Region type : function
  633. Offsets : 8314-14526
  634. Line numbers : 301-467
  635. Modified : True
  636. std.code.complexity:cyclomatic: 37 [+1]
  637. std.code.lines:code: 148 [+4]
  638. </pre>
  639. </section>
  640. <section>
  641. <h3>More about the viewer</h3>
  642. <p>The 'view' command has got an option to alter the output format. It is possible to get the same data
  643. in xml or python dictionary formats. This can be particularly useful for integration of the tool with
  644. other applications. For example, an editor may re-collect and show context based metrics when a file is saved.</p>
  645. <pre>
  646. &gt; python "/path/to/metrix++.py" view --db-file=boost_1_54_0/metrixpp.db --format=xml
  647. </pre>
  648. <p>Check other options of the view tool by executing:</p>
  649. <pre>
  650. &gt; python "/path/to/metrix++.py" view --help
  651. </pre>
  652. </section>
  653. <section id="workflow_limit_section">
  654. <h2>Apply thresholds</h2>
  655. <p>The viewer shows (above) that there are functions with quite large cyclomatic complexities.
  656. Growth of this metric can be considered as negative trend. The Metrix++ 'limit' tool offers the capability
  657. to manage control over trends by applying limits to metric values.
  658. Exceeded limits could be raised as alarms by quality management and control.</p>
  659. </section>
  660. <section id="workflow_limit_hotspots_section">
  661. <h3>Hotspots</h3>
  662. <p>The hotspots mode of the limit tool helps to identify top files/regions exceeding a metric threshold.
  663. Let's identify the top 3 functions in the boost interprocess library, which exceed a limit of 15 points of
  664. cyclomatic complexity:</p>
  665. <pre>
  666. &gt; python "/path/to/metrix++.py" limit --db-file=boost_1_54_0/metrixpp.db --max-limit=std.code.complexity:cyclomatic:15 --hotspots=3
  667. </pre>
  668. <pre>
  669. ./interprocess/detail/managed_open_or_create_impl.hpp:302: warning: Metric 'std.code.complexity:cyclomatic' for region 'priv_open_or_create' exceeds the limit.
  670. Metric name : std.code.complexity:cyclomatic
  671. Region name : priv_open_or_create
  672. Metric value : 37
  673. Modified : None
  674. Change trend : None
  675. Limit : 15.0
  676. Suppressed : False
  677. ./interprocess/streams/vectorstream.hpp:284: warning: Metric 'std.code.complexity:cyclomatic' for region 'seekoff' exceeds the limit.
  678. Metric name : std.code.complexity:cyclomatic
  679. Region name : seekoff
  680. Metric value : 25
  681. Modified : None
  682. Change trend : None
  683. Limit : 15.0
  684. Suppressed : False
  685. ./interprocess/streams/bufferstream.hpp:174: warning: Metric 'std.code.complexity:cyclomatic' for region 'seekoff' exceeds the limit.
  686. Metric name : std.code.complexity:cyclomatic
  687. Region name : seekoff
  688. Metric value : 23
  689. Modified : None
  690. Change trend : None
  691. Limit : 15.0
  692. Suppressed : False
  693. </pre>
  694. </section>
  695. <section id="workflow_limit_control_section">
  696. <h3>Controlling trends</h3>
  697. <p>The exit code of the 'limit' tool is equal to the number of warnings printed. This supports use of the tool
  698. as a static analysis tool during the software build process. In this case, a non-zero exit code means
  699. that there are violations to the agreed standards and it may fail the build. The same command
  700. without --hotspots option will print all regions/files exceeding the specified limit:</p>
  701. <pre>
  702. &gt; python "/path/to/metrix++.py" limit --db-file=boost_1_54_0/metrixpp.db --max-limit=std.code.complexity:cyclomatic:15
  703. </pre>
  704. <h4>Modes to exclude old code from the considiration</h4>
  705. <p>However, it is likely there are many warnings printed in this mode, especially if very old or legacy code is profiled
  706. against new metrics and coding rules. Although all warnings can be removed
  707. by re-factoring as a big task force activity, it is where many tools are rejected,
  708. because it is difficult to justify the initial cost of applying and integrating them.
  709. The Metrix++ 'limit' tool has got an option --warn-mode, which helps to overcome this problem.</p>
  710. <p>--warn-mode=touched encourages re-factoring only for new and modified regions. It enables
  711. continuous refactoring. It does not matter how late the rule is applied or the
  712. coding standard is modified. It is possible to do it anytime with zero initial investment.
  713. For example, applying it to the boost interprocess library for a changes between 1.52 and 1.54 versions
  714. results in only 6 warnings:</p>
  715. <pre>
  716. &gt; python "/path/to/metrix++.py" limit --db-file=boost_1_54_0/metrixpp.db --db-file-prev=boost_1_52_0/metrixpp.db --max-limit=std.code.complexity:cyclomatic:15 --warn-mode=touched
  717. </pre>
  718. <pre>
  719. ./interprocess/detail/managed_open_or_create_impl.hpp:302: warning: Metric 'std.code.complexity:cyclomatic' for region 'priv_open_or_create' exceeds the limit.
  720. Metric name : std.code.complexity:cyclomatic
  721. Region name : priv_open_or_create
  722. Metric value : 37
  723. Modified : True
  724. Change trend : +1
  725. Limit : 15.0
  726. Suppressed : False
  727. ./interprocess/ipc/message_queue.hpp:375: warning: Metric 'std.code.complexity:cyclomatic' for region 'insert_at' exceeds the limit.
  728. Metric name : std.code.complexity:cyclomatic
  729. Region name : insert_at
  730. Metric value : 16
  731. Modified : True
  732. Change trend : 0
  733. Limit : 15.0
  734. Suppressed : False
  735. ./interprocess/mapped_region.hpp:575: warning: Metric 'std.code.complexity:cyclomatic' for region 'mapped_region' exceeds the limit.
  736. Metric name : std.code.complexity:cyclomatic
  737. Region name : mapped_region
  738. Metric value : 18
  739. Modified : True
  740. Change trend : +2
  741. Limit : 15.0
  742. Suppressed : False
  743. ./interprocess/mem_algo/detail/mem_algo_common.hpp:452: warning: Metric 'std.code.complexity:cyclomatic' for region 'priv_allocate_many' exceeds the limit.
  744. Metric name : std.code.complexity:cyclomatic
  745. Region name : priv_allocate_many
  746. Metric value : 20
  747. Modified : True
  748. Change trend : 0
  749. Limit : 15.0
  750. Suppressed : False
  751. ./interprocess/mem_algo/rbtree_best_fit.hpp:787: warning: Metric 'std.code.complexity:cyclomatic' for region 'priv_expand_both_sides' exceeds the limit.
  752. Metric name : std.code.complexity:cyclomatic
  753. Region name : priv_expand_both_sides
  754. Metric value : 17
  755. Modified : True
  756. Change trend : 0
  757. Limit : 15.0
  758. Suppressed : False
  759. ./interprocess/sync/windows/named_sync.hpp:98: warning: Metric 'std.code.complexity:cyclomatic' for region 'open_or_create' exceeds the limit.
  760. Metric name : std.code.complexity:cyclomatic
  761. Region name : open_or_create
  762. Metric value : 18
  763. Modified : True
  764. Change trend : 0
  765. Limit : 15.0
  766. Suppressed : False
  767. </pre>
  768. <p>If it is challenging or of little benefit to refactor everything touched,
  769. --warn-mode=trends simplifies the control over modified regions and only makes sure
  770. that there are no regressions in modified code. In other words, a warning is printed about a modified region/file
  771. only if a metric exceeds the specified limit and the value of the metric has got a negative trend due to the modification.
  772. It is possible to apply it anytime with zero initial investment and almost zero on-going investment around old code.
  773. For example, applying it to the boost interprocess library for a changes between 1.52 and 1.54 versions
  774. results in only 2 warnings:</p>
  775. <pre>
  776. &gt; python "/path/to/metrix++.py" limit --db-file=boost_1_54_0/metrixpp.db --db-file-prev=boost_1_52_0/metrixpp.db --max-limit=std.code.complexity:cyclomatic:15 --warn-mode=trend
  777. </pre>
  778. <pre>
  779. ./interprocess/detail/managed_open_or_create_impl.hpp:302: warning: Metric 'std.code.complexity:cyclomatic' for region 'priv_open_or_create' exceeds the limit.
  780. Metric name : std.code.complexity:cyclomatic
  781. Region name : priv_open_or_create
  782. Metric value : 37
  783. Modified : True
  784. Change trend : +1
  785. Limit : 15.0
  786. Suppressed : False
  787. ./interprocess/mapped_region.hpp:575: warning: Metric 'std.code.complexity:cyclomatic' for region 'mapped_region' exceeds the limit.
  788. Metric name : std.code.complexity:cyclomatic
  789. Region name : mapped_region
  790. Metric value : 18
  791. Modified : True
  792. Change trend : +2
  793. Limit : 15.0
  794. Suppressed : False
  795. </pre>
  796. <p>--warn-mode=new ignores existing code and ensures that warnings are only about new code.
  797. For example, applying it to the boost interprocess library for a changes between 1.52 and 1.54 versions
  798. results in 0 warnings, so it shows that the new code is totally compliant with the standard required in the example.</p>
  799. <pre>
  800. &gt; python "/path/to/metrix++.py" limit --db-file=boost_1_54_0/metrixpp.db --db-file-prev=boost_1_52_0/metrixpp.db --max-limit=std.code.complexity:cyclomatic:15 --warn-mode=new
  801. </pre>
  802. <h4>Suppressions</h4>
  803. <p>It is possible to suppress warnings. Suppressions are collected from comments in code
  804. and used by the 'limit' tool to filter out suppressed warnings.
  805. It supports fine grained control over false-positive warnings, if there are any.</p>
  806. <p>In order to suppress a warning:</p>
  807. <ul>
  808. <li>per region metrics: put the metrix++ instruction in the comments before the region, for example:</li>
  809. <pre class="prettyprint linenums">
  810. // This function returns string typed
  811. // representation of a name of a color,
  812. // requested by color's id
  813. // metrix++: suppress std.code.complexity:cyclomatic
  814. std::string getColorName(int color_id)
  815. {
  816. switch (color_id)
  817. {
  818. case COLOR_RED:
  819. return std::string("red")
  820. case COLOR_GREEN:
  821. return std::string("green")
  822. case COLOR_BLUE:
  823. return std::string("blue")
  824. /* and so on */
  825. }
  826. }
  827. </pre>
  828. <li>per file metrics: put the metrix++ instruction in the comments at the beginning of a file, for example:</li>
  829. <pre class="prettyprint linenums">
  830. //
  831. // This file does processing of colors and brushes
  832. // Copyright is my company, 2013
  833. //
  834. // However, it is too long and big file, and there is no time
  835. // to split it into multiple file, so shut up the metrix++ warnings:
  836. // metrix++: suppress std.general:size
  837. //
  838. std::string getColorName(int color_id)
  839. {
  840. ...
  841. ...
  842. </pre>
  843. <li>activate collection of suppressions:</li>
  844. <pre>
  845. &gt; python "/path/to/metrix++.py" collect --std.suppress
  846. </pre>
  847. <li>run the 'limit' tool WITHOUT --disable-suppressions option:</li>
  848. <pre>
  849. &gt; python "/path/to/metrix++.py" limit ...
  850. </pre>
  851. </ul>
  852. <h5>Important notice:</h5>
  853. <ul><li>The --std.suppress option enables collection of 2 metrics as well: 'std.suppress:count' and
  854. 'std.suppress.file:count'. The first is number of suppressions per region.
  855. The second is the same but applies to file-scope metrics.
  856. It supports management of the number of suppressions.
  857. Usually there are no false-positives to suppress with the <strong>right</strong> metric,
  858. but there could be exceptions in specific cases. Managing suppressions is about managing exceptions.
  859. If there are many exceptional cases, maybe something is wrong with a metric or the application of a metric.
  860. Two code examples about colors above do not demonstrate the technically exceptional case,
  861. they likely demonstrate a case of a process exception, like "there is no time to do it properly now", or
  862. a case of the wrong application of a metric, like "shut up the useless tool". So, be careful.
  863. The 'view' tool shows the number of suppressions and its change trends on a per metric basis.</li></ul>
  864. </section>
  865. <section id="workflow_other_section">
  866. <h2>Other applications</h2>
  867. <h3>Checking data file properties</h3>
  868. <p>The Metrix++ 'info' tool is helpful to check the properties of a data file, like the settings used to write it,
  869. collected metrics and files processed. For example:</p>
  870. <pre>
  871. &gt; python "/path/to/metrix++.py" info --db-file=boost_1_54_0/metrixpp.db
  872. </pre>
  873. <pre>
  874. boost_1_54_0/metrixpp.db:: info: Created using plugins and settings:
  875. version : 1.0
  876. std.code.complexity:version: 1.1
  877. std.code.cpp:version: 1.1
  878. std.code.cpp:files: *.c,*.cc,*.cpp,*.cxx,*.h,*.hh,*.hpp,*.hxx
  879. std.code.cs:version: 1.0
  880. std.code.cs:files: *.cs
  881. std.code.java:version: 1.1
  882. std.code.java:files: *.java
  883. std.code.lines:version: 1.1
  884. test_workflow.db:: info: Collected metrics:
  885. std.code.complexity:cyclomatic:
  886. std.code.lines:code:
  887. :: info: Processed files and checksums:
  888. ./interprocess/allocators/detail/node_pool.hpp: 0xb099a7c3
  889. ./interprocess/allocators/detail/node_tools.hpp: 0xaaf5044d
  890. ./interprocess/anonymous_shared_memory.hpp: 0x2bf06cb0
  891. ./interprocess/containers/allocation_type.hpp: 0x8e95cda0
  892. ./interprocess/containers/containers_fwd.hpp: 0xa4d0d9f7
  893. ./interprocess/containers/deque.hpp: 0x6dbb77af
  894. ./interprocess/containers/flat_map.hpp: 0x6750338c
  895. ...
  896. </pre>
  897. <h3>Exporting results</h3>
  898. <p>The Metrix++ 'export' tool exports data files to csv formated files. For example:</p>
  899. <pre>
  900. &gt; python "/path/to/metrix++.py" export --db-file=boost_1_54_0/metrixpp.db > boost_1_54_0/metrixpp.csv
  901. </pre>
  902. <pre>
  903. file,region,type,modified,line start,line end,std.code.complexity:cyclomatic,std.code.lines:code
  904. ./interprocess/allocators/detail/node_pool.hpp,__global__,global,,1,110,,0
  905. ./interprocess/allocators/detail/node_pool.hpp,boost,namespace,,33,105,,2
  906. ./interprocess/allocators/detail/node_pool.hpp,interprocess,namespace,,34,104,,2
  907. ./interprocess/allocators/detail/node_pool.hpp,ipcdetail,namespace,,35,103,,4
  908. ./interprocess/allocators/detail/node_pool.hpp,SegmentManager,class,,39,72,,16
  909. ...
  910. </pre>
  911. <p>Files with csv format can be opened by applications, like Microsoft Office Excel, with advanced analysis capabilities.
  912. For example, to draw this distribution graph:</p>
  913. <p align="center"><img src="assets/img/piechart.png"/></p>
  914. <p>It is not recommended to use the export tool to implement custom post-analysis Metrix++ extensions.
  915. The main reason is that granted backward compatibility support for csv columns is not granted.
  916. Another main reason is that
  917. exporting is relatively slow process. It is recommended to use Metrix++ extensions API instead.</p>
  918. </section>
  919. <section id="extend_section">
  920. <div class="page-header">
  921. <h1>Create plugin</h1>
  922. <p>There are 3 types of plugins considered in this chapter:</p>
  923. <ul>
  924. <li>Metric plugin</li>
  925. <li>Language parser</li>
  926. <li>Post-processing / Analysis tool</li>
  927. </ul>
  928. <p>Tutorial for metric plugin is generic at the beginning and large portion of this is applied to
  929. all other plugins. You need to know python and python regular expressions library to write Metrix++ extensions.</p>
  930. </div>
  931. <h2>Metric plugin</h2>
  932. <p>The tutorial will explain how to create a plugin to count magic numbers in source code.
  933. It will be relatively simple at first and will be extended with additional configuration
  934. options and smarter counting logic.</p>
  935. <h4>Create placeholder for new plugin</h4>
  936. <ol>
  937. <li>All plugins are loaded by Metrix++ from standard places within the tool installation directory and
  938. from custom places specified in the METRIXPLUSPLUS_PATH environment variable.
  939. METRIXPLUSPLUS_PATH has got the same format as system PATH environment variable.
  940. So, the first step in plugin development is to set the METRIXPLUSPLUS_PATH to point out to
  941. the directory (or directories) where plugin is located.</li>
  942. <li>Create new python package 'myext', python lib 'magic.py' and 'magic.ini' file.</li>
  943. <pre>
  944. + working_directory (set in METRIXPLUSPLUS_PATH variable)
  945. \--+ myext
  946. \--- __init__.py
  947. \--- magic.py
  948. \--- magic.ini
  949. </pre>
  950. <li>__init__.py is empty file to make myext considered by python as a package.</li>
  951. <li>Edit magic.py to have the following content:
  952. <pre class="prettyprint linenums">
  953. import mpp.api
  954. class Plugin(mpp.api.Plugin):
  955. def initialize(self):
  956. print "Hello world"
  957. </pre>
  958. mpp.api package include Metrix++ API classes. mpp.api.Plugin is the base class, which can be loaded
  959. by Metrix++ engine and does nothing by default. In the code sample above it is extended to print
  960. "Hello world" on initialization.</li>
  961. <li>Edit magic.ini to have the following content:
  962. <pre class="prettyprint linenums">
  963. [Plugin]
  964. version: 1.0
  965. package: myext
  966. module: magic
  967. class: Plugin
  968. depends: None
  969. actions: collect
  970. enabled: True
  971. </pre>
  972. This file is a manifest for Metrix++ plugin loader. The fields in Plugin section are:
  973. <dl class="dl-horizontal">
  974. <dt>version</dt>
  975. <dd>a string representing the version, step up it every time when behaviour of a plugin
  976. or backward compatibility in api or data scope is changed</dd>
  977. <dt>package</dt>
  978. <dd>python package name where to load from</dd>
  979. <dt>module</dt>
  980. <dd>python module name (filename of *.py file) to load</dd>
  981. <dt>class</dt>
  982. <dd>name of a plugin class to instanciate</dd>
  983. <dt>depends</dt>
  984. <dd>list of plugin names to load, if it this plugin is loaded</dd>
  985. <dt>actions</dt>
  986. <dd>list of Metrix++ actions affected by this plugin</dd>
  987. <dt>enabled</dt>
  988. <dd>True or False, working status of a plugin</dd>
  989. </dl>
  990. </li>
  991. <li>Now run Metrix++ to see how this new plugin works:</li>
  992. <pre>&gt; python "/path/to/metrix++.py" collect</pre>
  993. <pre>Hello world</pre>
  994. </ol>
  995. <h4>Toogle option for the plugin</h4>
  996. <ol>
  997. <li>It is recommended to follow the convention for all plugins: 'run only if enabled'.
  998. So, let's extend the magic.py file to make it configurable
  999. <pre class="prettyprint linenums">
  1000. import mpp.api
  1001. class Plugin(mpp.api.Plugin,
  1002. # make this instance configurable...
  1003. mpp.api.IConfigurable):
  1004. # ... and implement 2 interfaces
  1005. def declare_configuration(self, parser):
  1006. parser.add_option("--myext.magic.numbers", "--mmn",
  1007. action="store_true", default=False,
  1008. help="Enables collection of magic numbers metric [default: %default]")
  1009. def configure(self, options):
  1010. self.is_active_numbers = options.__dict__['myext.magic.numbers']
  1011. def initialize(self):
  1012. # use configuration option here
  1013. if self.is_active_numbers == True:
  1014. print "Hello world"
  1015. </pre>
  1016. parser argument is an instance of optparse.OptionParser class. It has got an extension to
  1017. accept multiple options of the same argument. Check std.tools.limit to see how to declare multiopt options, if you need.</li>
  1018. <li>Now run Metrix++ to see how this works:</li>
  1019. <pre>&gt; python "/path/to/metrix++.py" collect --myext.magic.numbers</pre>
  1020. <pre>Hello world</pre>
  1021. </ol>
  1022. <h4>Subscribe to notifications from parent plugins (or code parsers)</h4>
  1023. <ol>
  1024. <li>Every plugin works in a callback functions called by parent plugins.
  1025. Callback receives a reference to parent plugin, data object where to store metrics data,
  1026. and a flag indicating if there are changes in file or parent's settings since the previous collection.</li>
  1027. <pre class="prettyprint linenums">
  1028. import mpp.api
  1029. class Plugin(mpp.api.Plugin,
  1030. mpp.api.IConfigurable,
  1031. # declare that it can subscribe on notifications
  1032. mpp.api.Child):
  1033. def declare_configuration(self, parser):
  1034. parser.add_option("--myext.magic.numbers", "--mmn",
  1035. action="store_true", default=False,
  1036. help="Enables collection of magic numbers metric [default: %default]")
  1037. def configure(self, options):
  1038. self.is_active_numbers = options.__dict__['myext.magic.numbers']
  1039. def initialize(self):
  1040. if self.is_active_numbers == True:
  1041. # subscribe to notifications from all code parsers
  1042. self.subscribe_by_parents_interface(mpp.api.ICode, 'callback')
  1043. # parents (code parsers) will call the callback declared
  1044. def callback(self, parent, data, is_updated):
  1045. print parent.get_name(), data.get_path(), is_updated
  1046. </pre>
  1047. <li>Now run Metrix++ to see how this works. Try to do iterative scans (--db-file-prev option) to see how the
  1048. state of arguments is changed</li>
  1049. <pre>&gt; python "/path/to/metrix++.py" collect --myext.magic.numbers</pre>
  1050. <pre>std.code.cpp ./test.cpp True</pre>
  1051. </ol>
  1052. <h4>Implement simple metric based on regular expression pattern</h4>
  1053. <ol>
  1054. <li>Callback may execute counting, searcing and additional parsing and store results, using data argument.
  1055. 'data' argument is an instance of mpp.api.FileData class.
  1056. However, most metrics can be implemented
  1057. simplier, if mpp.api.MetricPluginMixin routines are used. MetricPluginMixin implements
  1058. declarative style for metrics based on searches by regular expression. It
  1059. cares about initialisation of database fields and properties.
  1060. It implements default callback which counts number of matches by regular expression for all
  1061. active declared metrics. So, let's utilise that:
  1062. </li>
  1063. <pre class="prettyprint linenums">
  1064. import mpp.api
  1065. import re
  1066. class Plugin(mpp.api.Plugin,
  1067. mpp.api.IConfigurable,
  1068. mpp.api.Child,
  1069. # reuse by inheriting standard metric facilities
  1070. mpp.api.MetricPluginMixin):
  1071. def declare_configuration(self, parser):
  1072. parser.add_option("--myext.magic.numbers", "--mmn",
  1073. action="store_true", default=False,
  1074. help="Enables collection of magic numbers metric [default: %default]")
  1075. def configure(self, options):
  1076. self.is_active_numbers = options.__dict__['myext.magic.numbers']
  1077. def initialize(self):
  1078. # declare metric rules
  1079. self.declare_metric(
  1080. self.is_active_numbers, # to count if active in callback
  1081. self.Field('numbers', int), # field name and type in the database
  1082. re.compile(r'''[0-9]+'''), # pattern to search
  1083. marker_type_mask=mpp.api.Marker.T.CODE, # search in code
  1084. region_type_mask=mpp.api.Region.T.ANY) # search in all types of regions
  1085. # use superclass facilities to initialize everything from declared fields
  1086. super(Plugin, self).initialize(fields=self.get_fields())
  1087. # subscribe to all code parsers if at least one metric is active
  1088. if self.is_active() == True:
  1089. self.subscribe_by_parents_interface(mpp.api.ICode)
  1090. </pre>
  1091. <li>Now run Metrix++ to count numbers in code files.</li>
  1092. <pre>&gt; python "/path/to/metrix++.py" collect --myext.magic.numbers</pre>
  1093. <li>Now view the results. At this stage it is fully working simple metric.</li>
  1094. <pre>&gt; python "/path/to/metrix++.py" view</pre>
  1095. <pre>
  1096. :: info: Overall metrics for 'myext.magic:numbers' metric
  1097. Average : 2.75
  1098. Minimum : 0
  1099. Maximum : 7
  1100. Total : 11.0
  1101. Distribution : 4 regions in total (including 0 suppressed)
  1102. Metric value : Ratio : R-sum : Number of regions
  1103. 0 : 0.250 : 0.250 : 1 |||||||||||||||||||||||||
  1104. 1 : 0.250 : 0.500 : 1 |||||||||||||||||||||||||
  1105. 3 : 0.250 : 0.750 : 1 |||||||||||||||||||||||||
  1106. 7 : 0.250 : 1.000 : 1 |||||||||||||||||||||||||
  1107. :: info: Directory content:
  1108. Directory : .
  1109. </pre>
  1110. </ol>
  1111. <h4>Extend regular expression incremental counting by smarter logic</h4>
  1112. <ol>
  1113. <li>At this stage the metric counts every number in source code.
  1114. However, we indent to spot only 'magic' numbers. Declared constant
  1115. is not a magic number, so it is better to exclude constants from counting.
  1116. It is easy to change default counter behaviour by implementing
  1117. a function with name '_&lt;metric_name&gt;_count'. </li>
  1118. <pre class="prettyprint linenums">
  1119. import mpp.api
  1120. import re
  1121. class Plugin(mpp.api.Plugin,
  1122. mpp.api.IConfigurable,
  1123. mpp.api.Child,
  1124. mpp.api.MetricPluginMixin):
  1125. def declare_configuration(self, parser):
  1126. parser.add_option("--myext.magic.numbers", "--mmn",
  1127. action="store_true", default=False,
  1128. help="Enables collection of magic numbers metric [default: %default]")
  1129. def configure(self, options):
  1130. self.is_active_numbers = options.__dict__['myext.magic.numbers']
  1131. def initialize(self):
  1132. # improve pattern to find declarations of constants
  1133. pattern_to_search = re.compile(
  1134. r'''(const\s+([_a-zA-Z][_a-zA-Z0-9]*\s+)+[=]\s*)?[-+]?[0-9]+''')
  1135. self.declare_metric(self.is_active_numbers,
  1136. self.Field('numbers', int),
  1137. pattern_to_search, # and use it here
  1138. marker_type_mask=mpp.api.Marker.T.CODE,
  1139. region_type_mask=mpp.api.Region.T.ANY)
  1140. super(Plugin, self).initialize(fields=self.get_fields())
  1141. if self.is_active() == True:
  1142. self.subscribe_by_parents_interface(mpp.api.ICode)
  1143. # implement custom counter behavior:
  1144. # increments counter by 1 only if single number spotted,
  1145. # but not declaration of a constant
  1146. def _numbers_count(self, alias, data, region, marker, match, count, counter_data):
  1147. if match.group(0).startswith('const'):
  1148. return count
  1149. return count + 1
  1150. </pre>
  1151. <li>Initialy counter is initialized by zero, but it is possible to
  1152. change it as well by implementing a function with name '_&lt;metric_name&gt;_count_initialize'.
  1153. Plugin we are implementing does not require this.</li>
  1154. <li>Now run Metrix++ to collect and view the results.</li>
  1155. <pre>&gt; python "/path/to/metrix++.py" collect --myext.magic.numbers</pre>
  1156. <pre>&gt; python "/path/to/metrix++.py" view</pre>
  1157. </ol>
  1158. <h4>Language specific regular expressions</h4>
  1159. <ol>
  1160. <li>In the previous step we added matching of constants assuming that identifiers
  1161. may have symbols '_', 'a-z', 'A-Z' and '0-9'. It is true for C++ but it is not complete for Java.
  1162. Java identifier may have '$' symbol in the identifier. So, let's add language specific pattern
  1163. in the declaration of the metric:</li>
  1164. <pre class="prettyprint linenums">
  1165. import mpp.api
  1166. import re
  1167. class Plugin(mpp.api.Plugin,
  1168. mpp.api.IConfigurable,
  1169. mpp.api.Child,
  1170. mpp.api.MetricPluginMixin):
  1171. def declare_configuration(self, parser):
  1172. parser.add_option("--myext.magic.numbers", "--mmn",
  1173. action="store_true", default=False,
  1174. help="Enables collection of magic numbers metric [default: %default]")
  1175. def configure(self, options):
  1176. self.is_active_numbers = options.__dict__['myext.magic.numbers']
  1177. def initialize(self):
  1178. # specialized pattern for java
  1179. pattern_to_search_java = re.compile(
  1180. r'''(const\s+([_$a-zA-Z][_$a-zA-Z0-9]*\s+)+[=]\s*)?[-+]?[0-9]+''')
  1181. # pattern for C++ and C# languages
  1182. pattern_to_search_cpp_cs = re.compile(
  1183. r'''(const\s+([_a-zA-Z][_a-zA-Z0-9]*\s+)+[=]\s*)?[-+]?[0-9]+''')
  1184. # pattern for all other languages
  1185. pattern_to_search = re.compile(
  1186. r'''[0-9]+''')
  1187. self.declare_metric(self.is_active_numbers,
  1188. self.Field('numbers', int),
  1189. # dictionary of patterns instead of a single one
  1190. {
  1191. 'std.code.java': pattern_to_search_java,
  1192. 'std.code.cpp': pattern_to_search_cpp_cs,
  1193. 'std.code.cs': pattern_to_search_cpp_cs,
  1194. '*': pattern_to_search
  1195. },
  1196. marker_type_mask=mpp.api.Marker.T.CODE,
  1197. region_type_mask=mpp.api.Region.T.ANY)
  1198. super(Plugin, self).initialize(fields=self.get_fields())
  1199. if self.is_active() == True:
  1200. self.subscribe_by_parents_interface(mpp.api.ICode)
  1201. def _numbers_count(self, alias, data, region, marker, match, count, counter_data):
  1202. if match.group(0).startswith('const'):
  1203. return count
  1204. return count + 1
  1205. </pre>
  1206. <li>Keys in the dictionary of patterns are names of parent plugins (references to code parsers).
  1207. The key '*' refers to any parser.</li>
  1208. <li>Now run Metrix++ to collect and view the results.</li>
  1209. <pre>&gt; python "/path/to/metrix++.py" collect --myext.magic.numbers</pre>
  1210. <pre>&gt; python "/path/to/metrix++.py" view</pre>
  1211. </ol>
  1212. <h4>Store only non-zero metric values</h4>
  1213. <ol>
  1214. <li>Most functions have the metric, which we are implemneting, equal to zero.
  1215. However, we are interested in finding code blocks having this metric greater than zero.
  1216. Zeros consumes the space in the data file. So, we can optimise the size of a data file,
  1217. if we exclude zero metric values. Let's declare this behavior for the metric.</li>
  1218. <pre class="prettyprint linenums">
  1219. import mpp.api
  1220. import re
  1221. class Plugin(mpp.api.Plugin,
  1222. mpp.api.IConfigurable,
  1223. mpp.api.Child,
  1224. mpp.api.MetricPluginMixin):
  1225. def declare_configuration(self, parser):
  1226. parser.add_option("--myext.magic.numbers", "--mmn",
  1227. action="store_true", default=False,
  1228. help="Enables collection of magic numbers metric [default: %default]")
  1229. def configure(self, options):
  1230. self.is_active_numbers = options.__dict__['myext.magic.numbers']
  1231. def initialize(self):
  1232. pattern_to_search_java = re.compile(
  1233. r'''(const\s+([_$a-zA-Z][_$a-zA-Z0-9]*\s+)+[=]\s*)?[-+]?[0-9]+''')
  1234. pattern_to_search_cpp_cs = re.compile(
  1235. r'''(const\s+([_a-zA-Z][_a-zA-Z0-9]*\s+)+[=]\s*)?[-+]?[0-9]+''')
  1236. pattern_to_search = re.compile(
  1237. r'''[0-9]+''')
  1238. self.declare_metric(self.is_active_numbers,
  1239. self.Field('numbers', int,
  1240. # optimize the size of datafile:
  1241. # store only non-zero results
  1242. non_zero=True),
  1243. {
  1244. 'std.code.java': pattern_to_search_java,
  1245. 'std.code.cpp': pattern_to_search_cpp_cs,
  1246. 'std.code.cs': pattern_to_search_cpp_cs,
  1247. '*': pattern_to_search
  1248. },
  1249. marker_type_mask=mpp.api.Marker.T.CODE,
  1250. region_type_mask=mpp.api.Region.T.ANY)
  1251. super(Plugin, self).initialize(fields=self.get_fields())
  1252. if self.is_active() == True:
  1253. self.subscribe_by_parents_interface(mpp.api.ICode)
  1254. def _numbers_count(self, alias, data, region, marker, match, count, counter_data):
  1255. if match.group(0).startswith('const'):
  1256. return count
  1257. return count + 1
  1258. </pre>
  1259. <li>Now run Metrix++ to collect and view the results.</li>
  1260. <pre>&gt; python "/path/to/metrix++.py" collect --myext.magic.numbers</pre>
  1261. <pre>&gt; python "/path/to/metrix++.py" view</pre>
  1262. </ol>
  1263. <h4>Additional per metric configuration options</h4>
  1264. <ol>
  1265. <li>It is typical that most numbers counted by the metric are equal to 0, -1 or 1.
  1266. They are not necessary magic numbers. 0 or 1 are typical variable initializers.
  1267. -1 is a typical negative return code. So, let's implement simplified version of the metric,
  1268. which does not count 0, -1 and 1, if the specific new option is set.</li>
  1269. <pre class="prettyprint linenums">
  1270. import mpp.api
  1271. import re
  1272. class Plugin(mpp.api.Plugin,
  1273. mpp.api.IConfigurable,
  1274. mpp.api.Child,
  1275. mpp.api.MetricPluginMixin):
  1276. def declare_configuration(self, parser):
  1277. parser.add_option("--myext.magic.numbers", "--mmn",
  1278. action="store_true", default=False,
  1279. help="Enables collection of magic numbers metric [default: %default]")
  1280. # Add new option
  1281. parser.add_option("--myext.magic.numbers.simplier", "--mmns",
  1282. action="store_true", default=False,
  1283. help="Is set, 0, -1 and 1 numbers are not counted [default: %default]")
  1284. def configure(self, options):
  1285. self.is_active_numbers = options.__dict__['myext.magic.numbers']
  1286. # remember the option here
  1287. self.is_active_numbers_simplier = options.__dict__['myext.magic.numbers.simplier']
  1288. def initialize(self):
  1289. pattern_to_search_java = re.compile(
  1290. r'''(const\s+([_$a-zA-Z][_$a-zA-Z0-9]*\s+)+[=]\s*)?[-+]?[0-9]+''')
  1291. pattern_to_search_cpp_cs = re.compile(
  1292. r'''(const\s+([_a-zA-Z][_a-zA-Z0-9]*\s+)+[=]\s*)?[-+]?[0-9]+''')
  1293. pattern_to_search = re.compile(
  1294. r'''[0-9]+''')
  1295. self.declare_metric(self.is_active_numbers,
  1296. self.Field('numbers', int,
  1297. non_zero=True),
  1298. {
  1299. 'std.code.java': pattern_to_search_java,
  1300. 'std.code.cpp': pattern_to_search_cpp_cs,
  1301. 'std.code.cs': pattern_to_search_cpp_cs,
  1302. '*': pattern_to_search
  1303. },
  1304. marker_type_mask=mpp.api.Marker.T.CODE,
  1305. region_type_mask=mpp.api.Region.T.ANY)
  1306. super(Plugin, self).initialize(fields=self.get_fields(),
  1307. # remember option settings in data file properties
  1308. # in order to detect changes in settings on iterative re-run
  1309. properties=[self.Property('number.simplier', self.is_active_numbers_simplier)])
  1310. if self.is_active() == True:
  1311. self.subscribe_by_parents_interface(mpp.api.ICode)
  1312. def _numbers_count(self, alias, data, region, marker, match, count, counter_data):
  1313. if (match.group(0).startswith('const') or
  1314. # use new option to exclude 0 and 1 numbers
  1315. (self.is_active_numbers_simplier == True and
  1316. match.group(0) in ['0', '1', '-1', '+1'])):
  1317. return count
  1318. return count + 1
  1319. </pre>
  1320. <li>Now run Metrix++ to collect and view the results.</li>
  1321. <pre>&gt; python "/path/to/metrix++.py" collect --myext.magic.numbers</pre>
  1322. <pre>&gt; python "/path/to/metrix++.py" view</pre>
  1323. <pre>
  1324. :: info: Overall metrics for 'myext.magic:numbers' metric
  1325. Average : 2.5 (excluding zero metric values)
  1326. Minimum : 2
  1327. Maximum : 3
  1328. Total : 5.0
  1329. Distribution : 2 regions in total (including 0 suppressed)
  1330. Metric value : Ratio : R-sum : Number of regions
  1331. 2 : 0.500 : 0.500 : 1 ||||||||||||||||||||||||||||||||||||||||||||||||||
  1332. 3 : 0.500 : 1.000 : 1 ||||||||||||||||||||||||||||||||||||||||||||||||||
  1333. :: info: Directory content:
  1334. Directory : .
  1335. </pre>
  1336. </ol>
  1337. <h4>Summary</h4>
  1338. <p>We have finished with the tutorial. The tutorial explained how to implement simple and advanced metric plugins.
  1339. We used built-in Metrix++ base classes. If you need to more advanced plugin capabilities,
  1340. override in your plugin class functions inherited from mpp.api base classes. Check code of standard plugins
  1341. to learn more techniques.</p>
  1342. <p></p>
  1343. <h2>Analysis tool plugin</h2>
  1344. <p>This tutorial will explain how to build custom Metrix++ command, which is bound to custom post-analysis tool.
  1345. We will implement the tool, which identifies all new and changed regions and counts number of added lines.
  1346. We skip calculating number of deleted lines, but it is easy to extend from what we get finally in the tutorial.</p>
  1347. <h4>New Metrix++ command / action</h4>
  1348. <ol>
  1349. <li>As in the tutorial for metric plugin, set the environment and
  1350. create new python package 'myext', python lib 'compare.py' and 'compare.ini' file.</li>
  1351. <pre>
  1352. + working_directory (set in METRIXPLUSPLUS_PATH variable)
  1353. \--+ myext
  1354. \--- __init__.py
  1355. \--- compare.py
  1356. \--- compare.ini
  1357. </pre>
  1358. <li>__init__.py is empty file to make myext considered by python as a package.</li>
  1359. <li>Edit compare.py to have the following content:
  1360. <pre class="prettyprint linenums">
  1361. import mpp.api
  1362. class Plugin(mpp.api.Plugin, mpp.api.IRunable):
  1363. def run(self, args):
  1364. print args
  1365. return 0
  1366. </pre>
  1367. Inheritance from mpp.api.IRunable declares that the plugin is runable and requires implementation of 'run' interface.</li>
  1368. <li>Edit compare.ini to have the following content:
  1369. <pre class="prettyprint linenums">
  1370. [Plugin]
  1371. version: 1.0
  1372. package: myext
  1373. module: compare
  1374. class: Plugin
  1375. depends: None
  1376. actions: compare
  1377. enabled: True
  1378. </pre>
  1379. This file is a manifest for Metrix++ plugin loader. Actions field has got new value 'compare'.
  1380. Metrix++ engine will automatically pick this action and will add it to the list of available commands.
  1381. This plugin will be loaded on 'compare' action.
  1382. </li>
  1383. <li>Now run Metrix++ to see how this new plugin works:</li>
  1384. <pre>&gt; python "/path/to/metrix++.py" compare -- path1 path2 path3</pre>
  1385. <pre>["path1", "path2", "path3"]</pre>
  1386. </ol>
  1387. <h4>Access data file loader and its' interfaces</h4>
  1388. <ol>
  1389. <li>By default, all post-analysis tools have got --db-file and --db-file-prev options. It is
  1390. because 'mpp.dbf' plugin is loaded for any action, including our new one 'compare'. In order to continue
  1391. the tutorial, we need to have 2 data files with 'std.code.lines:total' metric collected.
  1392. So, write to files by running:</li>
  1393. <pre>cd my_project_version_1
  1394. &gt; python "/path/to/metrix++.py" collect --std.code.lines.total</pre>
  1395. <pre>cd my_project_version_2
  1396. &gt; python "/path/to/metrix++.py" collect --std.code.lines.total</pre>
  1397. <li>Edit compare.py file to get the loader and iterate collected file paths:</li>
  1398. <pre class="prettyprint linenums">
  1399. import mpp.api
  1400. # load common utils for post processing tools
  1401. import mpp.utils
  1402. class Plugin(mpp.api.Plugin, mpp.api.IRunable):
  1403. def run(self, args):
  1404. # get data file reader using standard metrix++ plugin
  1405. loader = self.get_plugin('mpp.dbf').get_loader()
  1406. # iterate and print file length for every path in args
  1407. exit_code = 0
  1408. for path in (args if len(args) > 0 else [""]):
  1409. file_iterator = loader.iterate_file_data(path)
  1410. if file_iterator == None:
  1411. mpp.utils.report_bad_path(path)
  1412. exit_code += 1
  1413. continue
  1414. for file_data in file_iterator:
  1415. print file_data.get_path()
  1416. return exit_code
  1417. </pre>
  1418. <li>Now run Metrix++ to see how it works:</li>
  1419. <pre>&gt; python "/path/to/metrix++.py" compare --db-file=my_project_version_2/metrixpp.db --db-file-prev=my_project_version_1/metrixpp.db</pre>
  1420. </ol>
  1421. <h4>Identify added, modified files/regions and read metric data</h4>
  1422. <ol>
  1423. <li>Let's extend the logic of the tool to compare files and regions, read 'std.code.lines:total' metric
  1424. and calcuate the summary of number of added lines. mpp.utils.FileRegionsMatcher is helper class
  1425. which does matching and comparison of regions for 2 given mpp.api.FileData objects.</li>
  1426. <pre class="prettyprint linenums">
  1427. import mpp.api
  1428. import mpp.utils
  1429. import mpp.cout
  1430. class Plugin(mpp.api.Plugin, mpp.api.IRunable):
  1431. def run(self, args):
  1432. loader = self.get_plugin('mpp.dbf').get_loader()
  1433. # get previous db file loader
  1434. loader_prev = self.get_plugin('mpp.dbf').get_loader_prev()
  1435. exit_code = 0
  1436. for path in (args if len(args) > 0 else [""]):
  1437. added_lines = 0
  1438. file_iterator = loader.iterate_file_data(path)
  1439. if file_iterator == None:
  1440. mpp.utils.report_bad_path(path)
  1441. exit_code += 1
  1442. continue
  1443. for file_data in file_iterator:
  1444. added_lines += self._compare_file(file_data, loader, loader_prev)
  1445. mpp.cout.notify(path, '', mpp.cout.SEVERITY_INFO,
  1446. "Change trend report",
  1447. [('Added lines', added_lines)])
  1448. return exit_code
  1449. def _compare_file(self, file_data, loader, loader_prev):
  1450. # compare file with previous and return number of new lines
  1451. file_data_prev = loader_prev.load_file_data(file_data.get_path())
  1452. if file_data_prev == None:
  1453. return self._sum_file_regions_lines(file_data)
  1454. elif file_data.get_checksum() != file_data_prev.get_checksum():
  1455. return self._compare_file_regions(file_data, file_data_prev)
  1456. def _sum_file_regions_lines(self, file_data):
  1457. # just sum up the metric for all regions
  1458. result = 0
  1459. for region in file_data.iterate_regions():
  1460. result += region.get_data('std.code.lines', 'total')
  1461. def _compare_file_regions(self, file_data, file_data_prev):
  1462. # compare every region with previous and return number of new lines
  1463. matcher = mpp.utils.FileRegionsMatcher(file_data, file_data_prev)
  1464. result = 0
  1465. for region in file_data.iterate_regions():
  1466. if matcher.is_matched(region.get_id()) == False:
  1467. # if added region, just add the lines
  1468. result += region.get_data('std.code.lines', 'total')
  1469. elif matcher.is_modified(region.get_id()):
  1470. # if modified, add the difference in lines
  1471. region_prev = file_data_prev.get_region(
  1472. matcher.get_prev_id(region.get_id()))
  1473. result += (region.get_data('std.code.lines', 'total') -
  1474. region_prev.get_data('std.code.lines', 'total'))
  1475. return result
  1476. </pre>
  1477. <li>Now run Metrix++ to see how it works:</li>
  1478. <pre>&gt; python "/path/to/metrix++.py" compare --db-file=my_project_version_2/metrixpp.db --db-file-prev=my_project_version_1/metrixpp.db</pre>
  1479. <pre>
  1480. :: info: Change trend report
  1481. Added lines : 7
  1482. </pre>
  1483. </ol>
  1484. <h4>Summary</h4>
  1485. <p>We have finished with the tutorial. The tutorial explained how to read Metrix++ data files and
  1486. implement custom post-processing tools. Even if some existing Metrix++ code requires clean-up and refactoring,
  1487. check code of standard tool plugins to learn more techniques.</p>
  1488. <h2>Language parser plugin</h2>
  1489. <p>Unfortunately, there is no good documentation at this stage for this part.
  1490. Briefly, if metric plugin counts and stores data into FileData object,
  1491. tool plugin reads this data, language plugin construct the original structure of
  1492. FileData object. The orginal structure includes regions (like functions, classes, etc.)
  1493. and markers (like comments, strings, preprocessor, etc.).
  1494. Check code of existing parsers.</p>
  1495. <ul>
  1496. <li>a language parser plugin is registered in the same way as a metric plugin</li>
  1497. <li>it registers parser's callback in 'std.tools.collect' plugin</li>
  1498. <li>parses a file in a callback, called by 'std.tools.collect'</li>
  1499. <li>parser needs to identify markers and regions
  1500. and tell about this to file data object passed as an
  1501. argument for the callback.</li>
  1502. </ul>
  1503. <p>There are useful options and tools avaialble for
  1504. trobuleshooting purposes during development:</p>
  1505. <ul>
  1506. <li>metrix++.py debug generates html code showing parsed code structures and their boundaries</li>
  1507. <li>--nest-regions for view tool forces the viewer to indent subregions.</li>
  1508. <li>--general.log-level option is available for any command and is helpful to trace execution.</li>
  1509. </ul>
  1510. <p>Finally, if there are any questions or enquires, please,
  1511. feel free to <a href="https://sourceforge.net/p/metrixplusplus/tickets/new/">submit new question</a>.</p>
  1512. </section>
  1513. <section id="contribute_section">
  1514. <div class="page-header">
  1515. <h1>Feeback and contribute</h1>
  1516. </div>
  1517. <p>Now it is your turn. There are multiple ways how you can contribute and help to improve and progress Metrix++ project:</p>
  1518. <ul>
  1519. <li>Try Metrix++ and <a href="https://sourceforge.net/projects/metrixplusplus/reviews/new">post review</a></li>
  1520. <li><a href="https://sourceforge.net/p/metrixplusplus/tickets/new/">Submit new feature request or bug report</a></li>
  1521. <li><a href="https://sourceforge.net/p/metrixplusplus/tickets/new/">Ask a question</a></li>
  1522. <li>Share your patch files and ideas, colloborate by email to
  1523. <a href="mailto:avkonst@users.sourceforge.net?subject=Metrix%2B%2B Project Request">project administrator</a></li>
  1524. <li>Create and publish your plugin.
  1525. <a href="mailto:avkonst@users.sourceforge.net?subject=Metrix%2B%2B Plugin Reference">Request to refer</a>
  1526. to it from Metrix++ project space.</li>
  1527. <li><a href="mailto:avkonst@users.sourceforge.net?subject=Metrix%2B%2B Plugin Submission">Submit your plugin</a>
  1528. to include to the standard set</li>
  1529. <li>... and consider to
  1530. <a href="mailto:avkonst@users.sourceforge.net?subject=Metrix%2B%2B Join Request">join the project</a>!</li>
  1531. </ul>
  1532. </section>
  1533. </div> <!-- end for sections -->
  1534. </div></div> <!-- end for row and container -->
  1535. <!-- Footer
  1536. ================================================== -->
  1537. <footer class="footer">
  1538. <div class="container">
  1539. <div class="row">
  1540. <div class="span3">
  1541. <p><a href="http://sourceforge.net/projects/metrixplusplus/"><img src="http://sflogo.sourceforge.net/sflogo.php?group_id=275605&amp;type=3"
  1542. alt="Get Metrix++ at SourceForge.net. Fast, secure and Free Open Source software downloads" border="0"></a></p>
  1543. <p>&middot;</p>
  1544. <p>&middot; &middot;<script type="text/javascript" src="http://www.ohloh.net/p/485947/widgets/project_users_logo.js"></script></p>
  1545. <p><a href="http://freecode.com/projects/metrix"><img src="assets/img/fm_logo.png" width="130"></a></p>
  1546. <p>&middot;</p>
  1547. </div>
  1548. <div class="span9">
  1549. <p>Copyright <strong>&copy;</strong> 2009 - 2013, <a href="mailto:avkonst@users.sourceforge.net"><span class="normalImportance">Metrix++</span> Project</a></p>
  1550. <p>Code licensed under <a href="http://www.gnu.org/licenses/gpl.txt" target="_blank">GPL 3.0</a>, documentation under <a href="http://creativecommons.org/licenses/by/3.0/">CC BY 3.0</a>.</p>
  1551. <ul class="footer-links">
  1552. <li><a href="https://sourceforge.net/p/metrixplusplus/tickets/new/">Ask question</a></li>
  1553. <li class="muted">&middot;</li>
  1554. <li><a href="https://sourceforge.net/p/metrixplusplus/tickets/new/">Report defect</a></li>
  1555. <li class="muted">&middot;</li>
  1556. <li><a href="https://sourceforge.net/p/metrixplusplus/tickets/new/">Feature request</a></li>
  1557. <li class="muted">&middot;</li>
  1558. <li><a href="https://sourceforge.net/p/metrixplusplus/tickets/search/?q=%21status%3Awont-fix+%26%26+%21status%3Aclosed">Open issues</a></li>
  1559. <li class="muted">&middot;</li>
  1560. <li><a href="https://sourceforge.net/p/metrixplusplus/wiki/ChangeLog/">Changelog</a></li>
  1561. </ul>
  1562. </div>
  1563. </div>
  1564. </div>
  1565. </footer>
  1566. <!-- Le javascript
  1567. ================================================== -->
  1568. <!-- Placed at the end of the document so the pages load faster -->
  1569. <script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
  1570. <script src="assets/js/jquery.js"></script>
  1571. <script src="assets/js/bootstrap-transition.js"></script>
  1572. <script src="assets/js/bootstrap-alert.js"></script>
  1573. <script src="assets/js/bootstrap-modal.js"></script>
  1574. <script src="assets/js/bootstrap-dropdown.js"></script>
  1575. <script src="assets/js/bootstrap-scrollspy.js"></script>
  1576. <script src="assets/js/bootstrap-tab.js"></script>
  1577. <script src="assets/js/bootstrap-tooltip.js"></script>
  1578. <script src="assets/js/bootstrap-popover.js"></script>
  1579. <script src="assets/js/bootstrap-button.js"></script>
  1580. <script src="assets/js/bootstrap-collapse.js"></script>
  1581. <script src="assets/js/bootstrap-carousel.js"></script>
  1582. <script src="assets/js/bootstrap-typeahead.js"></script>
  1583. <script src="assets/js/bootstrap-affix.js"></script>
  1584. <script>
  1585. !function ($) {
  1586. $(function(){
  1587. // carousel demo
  1588. $('#myCarousel').carousel()
  1589. })
  1590. }(window.jQuery)
  1591. </script>
  1592. <script src="assets/js/holder/holder.js"></script>
  1593. <script src="assets/js/google-code-prettify/prettify.js"></script>
  1594. <script src="assets/js/application.js"></script>
  1595. <script>
  1596. </script>
  1597. <!-- Analytics
  1598. ================================================== -->
  1599. <!--
  1600. <script>
  1601. var _gauges = _gauges || [];
  1602. (function() {
  1603. var t = document.createElement('script');
  1604. t.type = 'text/javascript';
  1605. t.async = true;
  1606. t.id = 'gauges-tracker';
  1607. t.setAttribute('data-site-id', '4f0dc9fef5a1f55508000013');
  1608. t.src = '//secure.gaug.es/track.js';
  1609. var s = document.getElementsByTagName('script')[0];
  1610. s.parentNode.insertBefore(t, s);
  1611. })();
  1612. </script>
  1613. -->
  1614. </body>
  1615. </html>