home.html 83 KB

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