home.html 83 KB

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