Skip to content

Commit 4214f77

Browse files
Add averages for Nom metrics
1 parent f9c1454 commit 4214f77

File tree

2 files changed

+109
-6
lines changed

2 files changed

+109
-6
lines changed

rust-code-analysis-web/src/web/server.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ mod tests {
650650
"n1": 2.0,
651651
"volume": 4.754_887_502_163_468},
652652
"loc": {"cloc": 1.0, "ploc": 2.0, "lloc": 1.0, "sloc": 4.0, "blank": 1.0},
653-
"nom": {"functions": 1.0, "closures": 0.0, "total": 1.0, "closures_min": 0.0, "closures_max": 0.0, "functions_min": 0.0, "functions_max": 1.0},
653+
"nom": {"functions": 1.0, "closures": 0.0, "functions_average": 0.5, "closures_average": 0.0, "total": 1.0, "average": 0.5, "closures_min": 0.0, "closures_max": 0.0, "functions_min": 0.0, "functions_max": 1.0},
654654
"mi": {"mi_original": 139.974_331_558_152_1,
655655
"mi_sei": 161.414_455_240_662_22,
656656
"mi_visual_studio": 81.856_334_244_533_39}},
@@ -677,7 +677,7 @@ mod tests {
677677
"n1": 2.0,
678678
"volume": 4.754_887_502_163_468},
679679
"loc": {"cloc": 0.0, "ploc": 2.0, "lloc": 1.0, "sloc": 2.0, "blank": 0.0},
680-
"nom": {"functions": 1.0, "closures": 0.0, "total": 1.0, "closures_min": 0.0, "closures_max": 0.0, "functions_min": 1.0, "functions_max": 1.0},
680+
"nom": {"functions": 1.0, "closures": 0.0, "functions_average": 1.0, "closures_average": 0.0, "total": 1.0, "average": 1.0, "closures_min": 0.0, "closures_max": 0.0, "functions_min": 1.0, "functions_max": 1.0},
681681
"mi": {"mi_original": 151.433_315_883_223_23,
682682
"mi_sei": 142.873_061_717_489_78,
683683
"mi_visual_studio": 88.557_494_668_551_6}},
@@ -730,7 +730,7 @@ mod tests {
730730
"n1": 2.0,
731731
"volume": 4.754_887_502_163_468},
732732
"loc": {"cloc": 0.0, "ploc": 2.0, "lloc": 1.0, "sloc": 2.0, "blank": 0.0},
733-
"nom": {"functions": 1.0, "closures": 0.0, "total": 1.0, "closures_min": 0.0, "closures_max": 0.0, "functions_min": 0.0, "functions_max": 1.0},
733+
"nom": {"functions": 1.0, "closures": 0.0, "functions_average": 0.5, "closures_average": 0.0, "total": 1.0, "average": 0.5, "closures_min": 0.0, "closures_max": 0.0, "functions_min": 0.0, "functions_max": 1.0},
734734
"mi": {"mi_original": 151.203_315_883_223_2,
735735
"mi_sei": 142.643_061_717_489_76,
736736
"mi_visual_studio": 88.422_991_744_574_97}},
@@ -779,7 +779,7 @@ mod tests {
779779
"n1": 2.0,
780780
"volume": 4.754_887_502_163_468},
781781
"loc": {"cloc": 0.0, "ploc": 2.0, "lloc": 1.0, "sloc": 2.0, "blank": 0.0},
782-
"nom": {"functions": 1.0, "closures": 0.0, "total": 1.0, "closures_min": 0.0, "closures_max": 0.0, "functions_min": 0.0, "functions_max": 1.0},
782+
"nom": {"functions": 1.0, "closures": 0.0, "functions_average": 0.5, "closures_average": 0.0, "total": 1.0, "average": 0.5, "closures_min": 0.0, "closures_max": 0.0, "functions_min": 0.0, "functions_max": 1.0},
783783
"mi": {"mi_original": 151.203_315_883_223_2,
784784
"mi_sei": 142.643_061_717_489_76,
785785
"mi_visual_studio": 88.422_991_744_574_97}},
@@ -806,7 +806,7 @@ mod tests {
806806
"n1": 2.0,
807807
"volume": 4.754_887_502_163_468},
808808
"loc": {"cloc": 0.0, "ploc": 2.0, "lloc": 1.0, "sloc": 2.0, "blank": 0.0},
809-
"nom": {"functions": 1.0, "closures": 0.0, "total": 1.0, "closures_min": 0.0, "closures_max": 0.0, "functions_min": 1.0, "functions_max": 1.0},
809+
"nom": {"functions": 1.0, "closures": 0.0, "functions_average": 1.0, "closures_average": 0.0, "total": 1.0, "average": 1.0, "closures_min": 0.0, "closures_max": 0.0, "functions_min": 1.0, "functions_max": 1.0},
810810
"mi": {"mi_original": 151.433_315_883_223_23,
811811
"mi_sei": 142.873_061_717_489_78,
812812
"mi_visual_studio": 88.557_494_668_551_6}},

src/metrics/nom.rs

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub struct Stats {
1717
functions_max: usize,
1818
closures_min: usize,
1919
closures_max: usize,
20+
space_count: usize,
2021
}
2122
impl Default for Stats {
2223
fn default() -> Self {
@@ -29,6 +30,7 @@ impl Default for Stats {
2930
functions_max: 0,
3031
closures_min: usize::MAX,
3132
closures_max: 0,
33+
space_count: 1,
3234
}
3335
}
3436
}
@@ -37,10 +39,13 @@ impl Serialize for Stats {
3739
where
3840
S: Serializer,
3941
{
40-
let mut st = serializer.serialize_struct("nom", 7)?;
42+
let mut st = serializer.serialize_struct("nom", 10)?;
4143
st.serialize_field("functions", &self.functions_sum())?;
4244
st.serialize_field("closures", &self.closures_sum())?;
45+
st.serialize_field("functions_average", &self.functions_average())?;
46+
st.serialize_field("closures_average", &self.closures_average())?;
4347
st.serialize_field("total", &self.total())?;
48+
st.serialize_field("average", &self.average())?;
4449
st.serialize_field("functions_min", &self.functions_min())?;
4550
st.serialize_field("functions_max", &self.functions_max())?;
4651
st.serialize_field("closures_min", &self.closures_min())?;
@@ -55,14 +60,20 @@ impl fmt::Display for Stats {
5560
f,
5661
"functions: {}, \
5762
closures: {}, \
63+
functions_average: {}, \
64+
closures_average: {}, \
5865
total: {} \
66+
average: {} \
5967
functions_min: {} \
6068
functions_max: {} \
6169
closures_min: {} \
6270
closures_max: {}",
6371
self.functions_sum(),
6472
self.closures_sum(),
73+
self.functions_average(),
74+
self.closures_average(),
6575
self.total(),
76+
self.average(),
6677
self.functions_min(),
6778
self.functions_max(),
6879
self.closures_min(),
@@ -80,6 +91,7 @@ impl Stats {
8091
self.closures_max = self.closures_max.max(other.closures_max);
8192
self.functions_sum += other.functions_sum;
8293
self.closures_sum += other.closures_sum;
94+
self.space_count += other.space_count;
8395
}
8496

8597
/// Counts the number of function definitions in a scope
@@ -108,6 +120,22 @@ impl Stats {
108120
self.closures_sum as f64
109121
}
110122

123+
/// Returns the average number of function definitions over all spaces
124+
#[inline(always)]
125+
pub fn functions_average(&self) -> f64 {
126+
self.functions_sum() as f64 / self.space_count as f64
127+
}
128+
/// Returns the average number of closures over all spaces
129+
#[inline(always)]
130+
pub fn closures_average(&self) -> f64 {
131+
self.closures_sum() as f64 / self.space_count as f64
132+
}
133+
/// Returns the average number of function definitions and closures over all spaces
134+
#[inline(always)]
135+
pub fn average(&self) -> f64 {
136+
self.total() as f64 / self.space_count as f64
137+
}
138+
111139
/// Counts the number of function definitions in a scope
112140
#[inline(always)]
113141
pub fn functions_min(&self) -> f64 {
@@ -202,6 +230,11 @@ mod tests {
202230
(functions_min, 0, usize),
203231
(closures_min, 0, usize),
204232
(closures_max, 1, usize),
233+
],
234+
[
235+
(functions_average, 0.75), // number of spaces = 4
236+
(closures_average, 0.25),
237+
(average, 1.0)
205238
]
206239
);
207240
}
@@ -223,6 +256,11 @@ mod tests {
223256
(functions_min, 0, usize),
224257
(closures_min, 0, usize),
225258
(closures_max, 1, usize),
259+
],
260+
[
261+
(functions_average, 0.5), // number of spaces = 4
262+
(closures_average, 0.25),
263+
(average, 0.75)
226264
]
227265
);
228266
}
@@ -246,6 +284,11 @@ mod tests {
246284
(functions_min, 0, usize),
247285
(closures_min, 0, usize),
248286
(closures_max, 0, usize),
287+
],
288+
[
289+
(functions_average, 0.5), // number of spaces = 2
290+
(closures_average, 0.0),
291+
(average, 0.5)
249292
]
250293
);
251294
}
@@ -269,6 +312,11 @@ mod tests {
269312
(functions_min, 0, usize),
270313
(closures_min, 0, usize),
271314
(closures_max, 1, usize),
315+
],
316+
[
317+
(functions_average, 0.5), // number of spaces = 4
318+
(closures_average, 0.25),
319+
(average, 0.75)
272320
]
273321
);
274322
}
@@ -300,6 +348,11 @@ mod tests {
300348
(functions_min, 0, usize),
301349
(closures_min, 0, usize),
302350
(closures_max, 1, usize),
351+
],
352+
[
353+
(functions_average, 0.6), // number of spaces = 5
354+
(closures_average, 0.2),
355+
(average, 0.8)
303356
]
304357
);
305358
}
@@ -321,6 +374,11 @@ mod tests {
321374
(functions_min, 0, usize),
322375
(closures_min, 0, usize),
323376
(closures_max, 0, usize),
377+
],
378+
[
379+
(functions_average, 0.5), // number of spaces = 2
380+
(closures_average, 0.0),
381+
(average, 0.5)
324382
]
325383
);
326384
}
@@ -340,6 +398,11 @@ mod tests {
340398
(functions_min, 0, usize),
341399
(closures_min, 0, usize),
342400
(closures_max, 0, usize),
401+
],
402+
[
403+
(functions_average, 0.5), // number of spaces = 2
404+
(closures_average, 0.0),
405+
(average, 0.5)
343406
]
344407
);
345408
}
@@ -361,6 +424,11 @@ mod tests {
361424
(functions_min, 0, usize),
362425
(closures_min, 0, usize),
363426
(closures_max, 0, usize),
427+
],
428+
[
429+
(functions_average, 0.5), // number of spaces = 2
430+
(closures_average, 0.0),
431+
(average, 0.5)
364432
]
365433
);
366434
}
@@ -382,6 +450,11 @@ mod tests {
382450
(functions_min, 0, usize),
383451
(closures_min, 0, usize),
384452
(closures_max, 0, usize),
453+
],
454+
[
455+
(functions_average, 0.5), // number of spaces = 2
456+
(closures_average, 0.0),
457+
(average, 0.5)
385458
]
386459
);
387460
}
@@ -405,6 +478,11 @@ mod tests {
405478
(functions_min, 0, usize),
406479
(closures_min, 0, usize),
407480
(closures_max, 0, usize),
481+
],
482+
[
483+
(functions_average, 0.5), // number of spaces = 2
484+
(closures_average, 0.0),
485+
(average, 0.5)
408486
]
409487
);
410488
}
@@ -428,6 +506,11 @@ mod tests {
428506
(functions_min, 0, usize),
429507
(closures_min, 0, usize),
430508
(closures_max, 1, usize),
509+
],
510+
[
511+
(functions_average, 0.0), // number of spaces = 3
512+
(closures_average, 0.6666666666666666),
513+
(average, 0.6666666666666666)
431514
]
432515
);
433516
}
@@ -449,6 +532,11 @@ mod tests {
449532
(functions_min, 0, usize),
450533
(closures_min, 0, usize),
451534
(closures_max, 1, usize),
535+
],
536+
[
537+
(functions_average, 0.3333333333333333), // number of spaces = 3
538+
(closures_average, 0.3333333333333333),
539+
(average, 0.6666666666666666)
452540
]
453541
);
454542
}
@@ -468,6 +556,11 @@ mod tests {
468556
(functions_min, 0, usize),
469557
(closures_min, 0, usize),
470558
(closures_max, 0, usize),
559+
],
560+
[
561+
(functions_average, 0.5), // number of spaces = 2
562+
(closures_average, 0.0),
563+
(average, 0.5)
471564
]
472565
);
473566
}
@@ -487,6 +580,11 @@ mod tests {
487580
(functions_min, 0, usize),
488581
(closures_min, 0, usize),
489582
(closures_max, 1, usize),
583+
],
584+
[
585+
(functions_average, 0.0), // number of spaces = 2
586+
(closures_average, 0.5),
587+
(average, 0.5)
490588
]
491589
);
492590
}
@@ -508,6 +606,11 @@ mod tests {
508606
(functions_min, 0, usize),
509607
(closures_min, 0, usize),
510608
(closures_max, 1, usize),
609+
],
610+
[
611+
(functions_average, 0.0), // number of spaces = 2
612+
(closures_average, 0.5),
613+
(average, 0.5)
511614
]
512615
);
513616
}

0 commit comments

Comments
 (0)