#ifndef NET_TOOLS_FLIP_SERVER_LOADTIME_MEASUREMENT_H_
#define NET_TOOLS_FLIP_SERVER_LOADTIME_MEASUREMENT_H_
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <map>
#include <string>
#include <vector>
#include "base/strings/string_split.h"
class LoadtimeMeasurement {
public:
LoadtimeMeasurement(const std::string& urls_file,
const std::string& pageload_html_file)
: num_urls_(0), pageload_html_file_(pageload_html_file) {
std::string urls_string;
read_file_to_string(urls_file.c_str(), &urls_string);
base::SplitString(urls_string, '\n', &urls_);
num_urls_ = urls_.size();
}
void ProcessRequest(const std::string& uri, std::string& output) {
std::string action = uri.substr(9);
if (pageload_html_file_.find(action) != std::string::npos) {
read_file_to_string(pageload_html_file_.c_str(), &output);
return;
}
if (action.find("get_total_iteration") == 0) {
char buffer[16];
snprintf(buffer, sizeof(buffer), "%d", num_urls_);
output.append(buffer, strlen(buffer));
return;
}
if (action.find("geturl") == 0) {
size_t b = action.find_first_of('=');
if (b != std::string::npos) {
int num = atoi(action.substr(b + 1).c_str());
if (num < num_urls_) {
output.append(urls_[num]);
}
}
return;
}
if (action.find("test_complete") == 0) {
for (std::map<std::string, int>::const_iterator it = loadtimes_.begin();
it != loadtimes_.end();
++it) {
LOG(INFO) << it->first << " " << it->second;
}
loadtimes_.clear();
output.append("OK");
return;
}
if (action.find("record_page_load") == 0) {
std::vector<std::string> query;
base::SplitString(action, '?', &query);
std::vector<std::string> params;
base::SplitString(query[1], '&', ¶ms);
std::vector<std::string> url;
std::vector<std::string> loadtime;
base::SplitString(params[1], '=', &url);
base::SplitString(params[2], '=', &loadtime);
loadtimes_[url[1]] = atoi(loadtime[1].c_str());
output.append("OK");
return;
}
}
private:
void read_file_to_string(const char* filename, std::string* output) {
output->clear();
int fd = open(filename, 0, "r");
if (fd == -1)
return;
char buffer[4096];
ssize_t read_status = read(fd, buffer, sizeof(buffer));
while (read_status > 0) {
output->append(buffer, static_cast<size_t>(read_status));
do {
read_status = read(fd, buffer, sizeof(buffer));
} while (read_status <= 0 && errno == EINTR);
}
close(fd);
}
int num_urls_;
std::vector<std::string> urls_;
std::map<std::string, int> loadtimes_;
const std::string pageload_html_file_;
};
#endif