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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <memory>
#include <sstream>
#include "ada.h"
#include "performancecounters/event_counter.h"
event_collector collector;
bool file_exists(const char *filename) {
namespace fs = std::filesystem;
std::filesystem::path f{filename};
if (std::filesystem::exists(filename)) {
return true;
} else {
std::cout << " file missing: " << filename << std::endl;
return false;
}
}
std::string read_file(std::string filename) {
constexpr size_t read_size = 4096;
std::ifstream stream(filename.c_str());
stream.exceptions(std::ios_base::badbit);
std::string out;
std::string buf(read_size, '\0');
while (stream.read(&buf[0], read_size)) {
out.append(buf, 0, size_t(stream.gcount()));
}
out.append(buf, 0, size_t(stream.gcount()));
return out;
}
std::vector<std::string> split_string(const std::string &str) {
auto result = std::vector<std::string>{};
std::stringstream ss{str};
for (std::string line; std::getline(ss, line, '\n');) {
std::string_view view = line;
// Some parsers like boost/url will refuse to parse a URL with trailing
// whitespace.
while (!view.empty() && std::isspace(view.back())) {
view.remove_suffix(1);
}
while (!view.empty() && std::isspace(view.front())) {
view.remove_prefix(1);
}
if (!view.empty()) {
result.emplace_back(view);
}
}
return result;
}
struct stat_numbers {
std::string url_string{};
std::string href{};
ada::url_components components{};
event_aggregate counters{};
bool is_valid = true;
bool has_port = false;
bool has_credentials = false;
bool has_fragment = false;
bool has_search = false;
};
size_t count_ascii_bytes(const std::string &s) {
size_t counter = 0;
for (uint8_t c : s) {
if (c < 128) {
counter++;
}
}
return counter;
}
template <class result_type = ada::url_aggregator>
std::vector<stat_numbers> collect_values(
const std::vector<std::string> &url_examples, size_t trials) {
std::vector<stat_numbers> numbers(url_examples.size());
for (size_t i = 0; i < url_examples.size(); i++) {
numbers[i].url_string = url_examples[i];
ada::result<result_type> url = ada::parse<result_type>(url_examples[i]);
if (url) {
numbers[i].is_valid = true;
numbers[i].href = url->get_href();
numbers[i].components = url->get_components();
numbers[i].has_port = url->has_port();
numbers[i].has_credentials = url->has_credentials();
numbers[i].has_fragment = url->has_hash();
numbers[i].has_search = url->has_search();
} else {
numbers[i].is_valid = false;
}
}
volatile size_t href_size = 0;
for (size_t i = 0; i < trials; i++) {
for (stat_numbers &n : numbers) {
std::atomic_thread_fence(std::memory_order_acquire);
collector.start();
ada::result<result_type> url = ada::parse<result_type>(n.url_string);
if (url) {
href_size += url->get_href().size();
}
std::atomic_thread_fence(std::memory_order_release);
event_count allocate_count = collector.end();
n.counters << allocate_count;
}
}
return numbers;
}
#ifdef ADA_URL_FILE
const char *default_file = ADA_URL_FILE;
#else
const char *default_file = nullptr;
#endif
std::vector<std::string> init_data(const char *input = default_file) {
std::vector<std::string> input_urls;
if (input == nullptr) {
return input_urls;
}
if (!file_exists(input)) {
std::cout << "File not found !" << input << std::endl;
return input_urls;
} else {
std::cout << "# Loading " << input << std::endl;
input_urls = split_string(read_file(input));
}
return input_urls;
}
void print(const stat_numbers &n) {
std::cout << std::setw(15) << n.url_string.size() << ",";
std::cout << std::setw(15) << n.counters.best.cycles() << "," << std::setw(15)
<< size_t(n.counters.cycles()) << ",";
std::cout << std::setw(15) << n.counters.best.instructions() << ","
<< std::setw(15) << n.counters.instructions() << ",";
std::cout << std::setw(15) << n.is_valid << ",";
// hash size
std::cout << std::setw(15) << n.href.size() << ",";
size_t end = n.href.size();
if (n.components.hash_start != ada::url_components::omitted) {
std::cout << std::setw(15) << (end - n.components.hash_start) << ",";
end = n.components.hash_start;
} else {
std::cout << std::setw(15) << 0 << ",";
}
// search size
if (n.components.search_start != ada::url_components::omitted) {
std::cout << std::setw(15) << (end - n.components.search_start) << ",";
end = n.components.search_start;
} else {
std::cout << std::setw(15) << 0 << ",";
}
// path size
std::cout << std::setw(15) << (end - n.components.pathname_start) << ",";
end = n.components.pathname_start;
// port size
std::cout << std::setw(15) << (end - n.components.host_end) << ",";
end = n.components.host_end;
// host size
std::cout << std::setw(15) << (end - n.components.host_start) << ",";
end = n.components.host_start;
// user/pass size
std::cout << std::setw(15) << (end - n.components.protocol_end) << ",";
end = n.components.protocol_end;
// protocol type
ada::result<ada::url_aggregator> url =
ada::parse<ada::url_aggregator>(n.url_string);
if (url) {
std::cout << std::setw(15) << int(url->type);
} else {
std::cout << std::setw(15) << -1;
}
std::cout << ",";
std::cout << std::setw(15) << n.has_port << ",";
std::cout << std::setw(15) << n.has_credentials << ",";
std::cout << std::setw(15) << n.has_fragment << ",";
std::cout << std::setw(15) << n.has_search << ",";
std::cout << std::setw(15)
<< (n.url_string.size() - count_ascii_bytes(n.url_string)) << ",";
std::cout << std::setw(15) << (n.href.size() - count_ascii_bytes(n.href))
<< ",";
std::cout << std::setw(15)
<< (count_ascii_bytes(n.url_string) == n.url_string.size()) << ",";
std::cout << std::setw(15) << (n.href == n.url_string);
}
void print(const std::vector<stat_numbers> numbers) {
std::cout << std::setw(15) << "input_size"
<< ",";
std::cout << std::setw(15) << "best_cycles"
<< ",";
std::cout << std::setw(15) << "mean_cycles"
<< ",";
std::cout << std::setw(15) << "best_instr"
<< ",";
std::cout << std::setw(15) << "mean_instr"
<< ",";
std::cout << std::setw(15) << "is_valid"
<< ",";
std::cout << std::setw(15) << "href_size"
<< ",";
std::cout << std::setw(15) << "hash_size"
<< ",";
std::cout << std::setw(15) << "search_size"
<< ",";
std::cout << std::setw(15) << "path_size"
<< ",";
std::cout << std::setw(15) << "port_size"
<< ",";
std::cout << std::setw(15) << "host_size"
<< ",";
std::cout << std::setw(15) << "credential_size"
<< ",";
std::cout << std::setw(15) << "protocol_type"
<< ",";
std::cout << std::setw(15) << "has_port"
<< ",";
std::cout << std::setw(15) << "has_authority"
<< ",";
std::cout << std::setw(15) << "has_fragment"
<< ",";
std::cout << std::setw(15) << "has_search"
<< ",";
std::cout << std::setw(15) << "non_ascii_bytes"
<< ",";
std::cout << std::setw(15) << "href_non_ascii_bytes"
<< ",";
std::cout << std::setw(15) << "is_ascii"
<< ",";
std::cout << std::setw(15) << "input_is_href";
std::cout << std::endl;
for (const stat_numbers &n : numbers) {
print(n);
std::cout << std::endl;
}
}
int main(int argc, char **argv) {
std::vector<std::string> input_urls;
if (argc == 1) {
input_urls = init_data();
} else {
input_urls = init_data(argv[1]);
}
if (input_urls.empty()) {
std::cout << "pass the path to a file containing a list of URL (one per "
"line) as a parameter."
<< std::endl;
return EXIT_FAILURE;
}
if (!collector.has_events()) {
std::cout << "We require access to performance counters. (Try sudo.)"
<< std::endl;
return EXIT_FAILURE;
}
std::string empty;
// We always start with a null URL for calibration.
input_urls.insert(input_urls.begin(), empty);
bool use_ada_url = (getenv("USE_URL") != nullptr);
size_t trials = 100;
std::cout << "# trials " << trials << std::endl;
if (use_ada_url) {
std::cout << "# ada::url" << std::endl;
print(collect_values<ada::url>(input_urls, trials));
} else {
std::cout << "# ada::url_aggregator" << std::endl;
print(collect_values<ada::url_aggregator>(input_urls, trials));
}
return EXIT_SUCCESS;
}
|