QUIC: Update IETF draft URL (draft-08)
[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  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 #include "config.h"
25
26 #include "timestats.h"
27
28 /* Initialize a timestat_t struct */
29 void
30 time_stat_init(timestat_t *stats)
31 {
32         stats->num = 0;
33         stats->min_num = 0;
34         stats->max_num = 0;
35         nstime_set_zero(&stats->min);
36         nstime_set_zero(&stats->max);
37         nstime_set_zero(&stats->tot);
38         stats->variance = 0.0;
39 }
40
41 /* Update a timestat_t struct with a new sample */
42 void
43 time_stat_update(timestat_t *stats, const nstime_t *delta, packet_info *pinfo)
44 {
45         if(stats->num==0){
46                 stats->max=*delta;
47                 stats->max_num=pinfo->num;
48                 stats->min=*delta;
49                 stats->min_num=pinfo->num;
50         }
51
52         if( (delta->secs<stats->min.secs)
53         ||( (delta->secs==stats->min.secs)
54           &&(delta->nsecs<stats->min.nsecs) ) ){
55                 stats->min=*delta;
56                 stats->min_num=pinfo->num;
57         }
58
59         if( (delta->secs>stats->max.secs)
60         ||( (delta->secs==stats->max.secs)
61           &&(delta->nsecs>stats->max.nsecs) ) ){
62                 stats->max=*delta;
63                 stats->max_num=pinfo->num;
64         }
65
66         nstime_add(&stats->tot, delta);
67
68         stats->num++;
69 }
70
71 /*
72  * get_average - function
73  *
74  * function to calculate the average
75  * returns the average as a gdouble , time base is milli seconds
76  */
77
78 gdouble get_average(const nstime_t *sum, guint32 num)
79 {
80         gdouble average;
81
82         if(num > 0) {
83                 average = (double)sum->secs*1000 + (double)sum->nsecs/1000000;
84                 average /= num;
85         }
86         else {
87                 average = 0;
88         }
89         return average;
90 }
91
92 /*
93  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
94  *
95  * Local variables:
96  * c-basic-offset: 8
97  * tab-width: 8
98  * indent-tabs-mode: t
99  * End:
100  *
101  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
102  * :indentSize=8:tabSize=8:noTabs=false:
103  */