MCPcopy Create free account
hub / github.com/apache/trafficserver / loadFile

Function loadFile

plugins/experimental/rate_limit/iprep_simu.cc:146–198  ·  view source on GitHub ↗

////////////////////////////////////////////////////////////////////////// Load a configuration file, and populate the two structures with the list of IPs (and their status) as well as the full sequence of requests. Returns a tuple with the number of good requests and bad requests, respectively.

Source from the content-addressed store, hash-verified

144// Returns a tuple with the number of good requests and bad requests, respectively.
145//
146std::tuple<uint32_t, uint32_t>
147loadFile(const std::string &fname, IpMap &all_ips, IpList &ips)
148{
149 std::ifstream infile(fname);
150
151 float timestamp; // The timestamp from the request(relative)
152 std::string ip; // The IP
153 bool status; // Bad (false) or Good (true) request?
154
155 uint32_t good_ips = 0;
156 uint32_t bad_ips = 0;
157 uint32_t good_requests = 0;
158 uint32_t bad_requests = 0;
159
160 // Load in the entire file, and fill the request vector as well as the IP lookup table (state)
161 while (infile >> timestamp >> ip >> status) {
162 auto ip_hash = IpReputation::SieveLru::hasher(ip, ip.find(':') != std::string::npos ? AF_INET6 : AF_INET);
163 auto it = all_ips.find(ip_hash);
164
165 if (!status) {
166 ++good_requests;
167 } else {
168 ++bad_requests;
169 }
170
171 if (all_ips.end() != it) {
172 auto &[key, data] = *it;
173 auto &[count, d_status] = data;
174
175 ++count;
176 ips.push_back(it);
177 } else {
178 all_ips[ip_hash] = {0, status};
179 ips.push_back(all_ips.find(ip_hash));
180 if (!status) {
181 ++good_ips;
182 } else {
183 ++bad_ips;
184 }
185 }
186 }
187
188 std::cout << std::setprecision(3);
189 std::cout << "Total number of requests: " << ips.size() << "\n";
190 std::cout << "\tGood requests: " << good_requests << " (" << 100.0 * good_requests / ips.size() << "%)\n";
191 std::cout << "\tBad requests: " << bad_requests << " (" << 100.0 * bad_requests / ips.size() << "%)\n";
192 std::cout << "Unique IPs in set: " << all_ips.size() << "\n";
193 std::cout << "\tGood IPs: " << good_ips << " (" << 100.0 * good_ips / all_ips.size() << "%)\n";
194 std::cout << "\tBad IPs: " << bad_ips << " (" << 100.0 * bad_ips / all_ips.size() << "%)\n";
195 std::cout << "\n";
196
197 return {good_requests, bad_requests};
198}
199
200int
201main(int argc, char *argv[])

Callers 2

loadSecretMethod · 0.85
mainFunction · 0.85

Calls 4

findMethod · 0.45
endMethod · 0.45
push_backMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected