xref: /btstack/tool/metrics/metrics-lizard.py (revision 38e38f23346dd994332952caf7440f1475b74340)
1#!/usr/bin/env python3
2
3# requires https://github.com/terryyin/lizard
4
5import lizard
6import os
7import sys
8
9folders = [
10'src',
11'src/ble',
12'src/ble/gatt-service',
13'src/classic',
14]
15
16metrics = {}
17targets = {}
18
19targets['CCN']   = 10
20targets['PARAM'] = 7
21
22def metric_count(name):
23    global metrics
24    value = 0
25    if name in metrics:
26        value = metrics[name]
27    metrics[name] = value + 1
28
29
30def metric_list(name, item):
31    global metrics
32    value = []
33    if name in metrics:
34        value = metrics[name]
35    value.append(item)
36    metrics[name] = value
37
38def metric_max(name, max):
39    global metrics
40    if name in metrics:
41        if metrics[name] > max:
42            return
43    metrics[name] = max
44
45def metric_measure(metric_name, functino_name, actual):
46    metric_max(metric_name + '_MAX', actual)
47    if metric_name in targets:
48        if actual > targets[metric_name]:
49            metric_count(metric_name + '_DEVIATIONS')
50            metric_list(metric_name + '_LIST', functino_name)
51
52def analyze_file(path):
53    l = lizard.analyze_file(path)
54    for f in l.function_list:
55        metric_count('FUNC')
56        metric_measure('PARAM', f.name, f.parameter_count)
57        metric_measure('CCN',   f.name, f.cyclomatic_complexity )
58
59def analyze_folder(folder_path):
60    for file in sorted(os.listdir(folder_path)):
61        if file.endswith(".c"):
62            analyze_file(folder_path+'/'+file)
63
64# find root
65btstack_root = os.path.abspath(os.path.dirname(sys.argv[0]) + '/../..')
66
67print ("Targets:")
68for key,value in sorted(targets.items()):
69    print ('- %-20s: %u' % (key, value))
70
71print ("\nAnalyzing:")
72for path in folders:
73    print('- %s' % path)
74    analyze_folder(btstack_root + "/" + path)
75
76print ("\nResult:")
77
78for key,value in sorted(metrics.items()):
79    if key.endswith('LIST'):
80        continue
81    print ('- %-20s: %4u' % (key, value))
82
83for key,value in sorted(metrics.items()):
84    if not key.endswith('LIST'):
85        continue
86    print ("\n%s" % key)
87    print ('\n'.join(value))
88
89