pytest.ini: declare minimum version requirement
[metze/wireshark/wip.git] / epan / timestats.c
1 /* timestats.c
2  * routines for time statistics
3  * Copyright 2003 Lars Roland
4  *
5  * Wireshark - Network traffic analyzer
6  * By Gerald Combs <gerald@wireshark.org>
7  * Copyright 1998 Gerald Combs
8  *
9  * SPDX-License-Identifier: GPL-2.0-or-later
10  */
11
12 #include "config.h"
13
14 #include "timestats.h"
15
16 /* Initialize a timestat_t struct */
17 void
18 time_stat_init(timestat_t *stats)
19 {
20         stats->num = 0;
21         stats->min_num = 0;
22         stats->max_num = 0;
23         nstime_set_zero(&stats->min);
24         nstime_set_zero(&stats->max);
25         nstime_set_zero(&stats->tot);
26         stats->variance = 0.0;
27 }
28
29 /* Update a timestat_t struct with a new sample */
30 void
31 time_stat_update(timestat_t *stats, const nstime_t *delta, packet_info *pinfo)
32 {
33         if(stats->num==0){
34                 stats->max=*delta;
35                 stats->max_num=pinfo->num;
36                 stats->min=*delta;
37                 stats->min_num=pinfo->num;
38         }
39
40         if( (delta->secs<stats->min.secs)
41         ||( (delta->secs==stats->min.secs)
42           &&(delta->nsecs<stats->min.nsecs) ) ){
43                 stats->min=*delta;
44                 stats->min_num=pinfo->num;
45         }
46
47         if( (delta->secs>stats->max.secs)
48         ||( (delta->secs==stats->max.secs)
49           &&(delta->nsecs>stats->max.nsecs) ) ){
50                 stats->max=*delta;
51                 stats->max_num=pinfo->num;
52         }
53
54         nstime_add(&stats->tot, delta);
55
56         stats->num++;
57 }
58
59 /*
60  * get_average - function
61  *
62  * function to calculate the average
63  * returns the average as a gdouble , time base is milli seconds
64  */
65
66 gdouble get_average(const nstime_t *sum, guint32 num)
67 {
68         gdouble average;
69
70         if(num > 0) {
71                 average = (double)sum->secs*1000 + (double)sum->nsecs/1000000;
72                 average /= num;
73         }
74         else {
75                 average = 0;
76         }
77         return average;
78 }
79
80 /*
81  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
82  *
83  * Local variables:
84  * c-basic-offset: 8
85  * tab-width: 8
86  * indent-tabs-mode: t
87  * End:
88  *
89  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
90  * :indentSize=8:tabSize=8:noTabs=false:
91  */