-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMetrics.cpp
More file actions
192 lines (163 loc) · 4.31 KB
/
Metrics.cpp
File metadata and controls
192 lines (163 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* Metrics.cpp
*
* Object that captures all the metrics
*/
#include "Metrics.h"
/**
* Function: addCustomer
* Description: Add a customer every time it is finished
* a transaction with a teller. Updates the
* service time, wait time, and num of customers
*
* @param c A copy of a customer object.
* @threadsafe
*/
void Metrics::addCustomer(Customer c){
pthread_mutex_lock(&mutex);
if( c.getWaitTime() > maxCustWaitTime ){
maxCustWaitTime = c.getWaitTime();
}
if( c.getServiceTime() > maxServiceTime ){
maxServiceTime = c.getServiceTime();
}
// Update Aggregate Wait Time:
totalCustWaitTime += c.getWaitTime();
// Update Aggregate Service Time:
totalServiceTime += c.getServiceTime();
totalNumCustomers++;
// Release Mutex:
pthread_mutex_unlock(&mutex);
}// addCustomer()
/**
* Function: updateMaxDepth
* Description: Updates the max depth of the queue.
*
* @param size int The current size of the queue.
* @threadsafe
*/
void Metrics::updateMaxDepth(int size){
while(pthread_mutex_trylock(&mutex) != 0){
usleep(1); // to not lock CPU
}
if( maxWaitQueueSize < size ){
maxWaitQueueSize = size;
}
// Release Mutex:
pthread_mutex_unlock(&mutex);
}// updateMaxDepth()
/**
* Function: addTellerWaitTime
* Description: Adds the teller wait time. Updates the max and total teller wait tims
*
* @param wt The teller wait time.
* @threadsafe
*/
void Metrics::addTellerWaitTime(int wt){
while(pthread_mutex_trylock(&mutex) != 0){
usleep(1); // to not lock CPU
}
if( wt > maxTellerWait ){
maxTellerWait = wt;
}
totalTellerWait += wt;
// Release Mutex:
pthread_mutex_unlock(&mutex);
}// addTellerWaitTime()
/**
* Function: getNumCustomers
* Description: A getter for getting the totally number of customers.
*
* @return int the total number of customers seen.
*/
int Metrics::getNumCustomers(){
return totalNumCustomers;
}// getNumCustomers()
/**
* Function: getMaxDepth
* Description: A getter for the max depth of the queue
*
* @return int max depth of the queue
*/
int Metrics::getMaxDepth(){
return maxWaitQueueSize;
}// getMaxDepth()
/**
* Function: getTotalCustWaitTIme
* Description: A getter for the total customer wait time
*
* @return int total customer wait time of all customers seen.
*/
int Metrics::getTotalCustWaitTime(){
return totalCustWaitTime;
}// getTotalCustWaitTime()
/**
* Function: getAvgCustWaitTime
* Description: A getter for the average customer wait time
*
* @return float the average customer wait time of all customers seen.
*/
float Metrics::getAvgCustWaitTime(){
return ((float)totalCustWaitTime)/((float)totalNumCustomers);
}// getAvgCustWaitTime()
/**
* Function: getMaxCustWaitTime
* Description: A getter for the max cusomer wait time
*
* @return int the max customer wait time of all customers seen
*/
int Metrics::getMaxCustWaitTime(){
return maxCustWaitTime;
}// getMaxCustWaitTime()
/**
* Function: getAvgServiceTime
* Description: A getter for the average service time for all customers.
* This is the average time the customer is spent with the teller.
*
* @return float the average service time of all customers.
*/
float Metrics::getAvgServiceTime(){
return (float)((totalServiceTime)/(totalNumCustomers));
}// getAvgServiceTime()
/**
* Function: getMaxServiceTime
* Description: A getter for the max service time for all customers.
* This is the most time a customer spent with a teller.
*
* @return int the max time a customer spent with a teller
*/
int Metrics::getMaxServiceTime(){
return maxServiceTime;
}// getMaxServiceTime()
/**
* Function: getAvgTellerWaitTime
* Description: Getter for the average teller wait time
*
* @return float the average teller wait time.
*/
float Metrics::getAvgTellerWaitTime(){
return (float)((totalTellerWait)/(NUM_TELLERS));
}// getAvgTellerWaitTime()
/**
* Function: getMaxTellerWaitTime
* Description: A getter for the max teller wait time
*
* @return int the max teller wait time.
*/
int Metrics::getMaxTellerWaitTime(){
return maxTellerWait;
}// getMaxTellerWaitTime()
Metrics::Metrics() {
pthread_mutex_init(&mutex, NULL);
totalCustWaitTime = 0;
maxCustWaitTime = 0;
totalServiceTime = 0;
maxServiceTime = 0;
totalTellerWait = 0;
maxTellerWait = 0;
totalNumCustomers = 0;
maxWaitQueueSize = 0;
}
Metrics::~Metrics() {
// TODO Auto-generated destructor stub
}