1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef NET_TOOLS_FLIP_SERVER_LOADTIME_MEASUREMENT_H__
6 #define NET_TOOLS_FLIP_SERVER_LOADTIME_MEASUREMENT_H__
11 #include <sys/types.h>
18 // Class to handle loadtime measure related urls, which all start with testing
19 // The in memory server has a singleton object of this class. It includes a
20 // html file containing javascript to go through a list of urls and upload the
21 // loadtime. The users can modify urls.txt to define the urls they want to
22 // measure and start with downloading the html file from browser.
23 class LoadtimeMeasurement {
25 LoadtimeMeasurement(const std::string& urls_file,
26 const std::string& pageload_html_file)
27 : num_urls_(0), pageload_html_file_(pageload_html_file) {
28 std::string urls_string;
29 read_file_to_string(urls_file.c_str(), &urls_string);
30 split_string(urls_string, '\n', &urls_);
31 num_urls_ = urls_.size();
34 // This is the entry function for all the loadtime measure related urls
35 // It handles the request to html file, get_total_iteration to get number
36 // of urls in the urls file, get each url, report the loadtime for
37 // each url, and the test is completed.
38 void ProcessRequest(const std::string& uri, std::string& output) {
39 // remove "/testing/" from uri to get the action
40 std::string action = uri.substr(9);
41 if (pageload_html_file_.find(action) != std::string::npos) {
42 read_file_to_string(pageload_html_file_.c_str(), &output);
45 if (action.find("get_total_iteration") == 0) {
47 snprintf(buffer, sizeof(buffer), "%d", num_urls_);
48 output.append(buffer, strlen(buffer));
51 if (action.find("geturl") == 0) {
52 size_t b = action.find_first_of('=');
53 if (b != std::string::npos) {
54 int num = atoi(action.substr(b + 1).c_str());
55 if (num < num_urls_) {
56 output.append(urls_[num]);
61 if (action.find("test_complete") == 0) {
62 for (std::map<std::string, int>::const_iterator it = loadtimes_.begin();
63 it != loadtimes_.end();
65 LOG(INFO) << it->first << " " << it->second;
71 if (action.find("record_page_load") == 0) {
72 std::vector<std::string> query;
73 split_string(action, '?', &query);
74 std::vector<std::string> params;
75 split_string(query[1], '&', ¶ms);
76 std::vector<std::string> url;
77 std::vector<std::string> loadtime;
78 split_string(params[1], '=', &url);
79 split_string(params[2], '=', &loadtime);
80 loadtimes_[url[1]] = atoi(loadtime[1].c_str());
87 void read_file_to_string(const char* filename, std::string* output) {
89 int fd = open(filename, 0, "r");
93 ssize_t read_status = read(fd, buffer, sizeof(buffer));
94 while (read_status > 0) {
95 output->append(buffer, static_cast<size_t>(read_status));
97 read_status = read(fd, buffer, sizeof(buffer));
98 } while (read_status <= 0 && errno == EINTR);
103 void split_string(const std::string& str,
105 std::vector<std::string>* sub_strs) {
107 size_t e = str.find_first_of(sepa, b);
108 while (e != std::string::npos && e > b) {
109 sub_strs->push_back(str.substr(b, e - b));
111 e = str.find_first_of(sepa, b);
113 if (b < str.size()) {
114 sub_strs->push_back(str.substr(b));
119 std::vector<std::string> urls_;
120 std::map<std::string, int> loadtimes_;
121 const std::string pageload_html_file_;
124 #endif // NET_TOOLS_FLIP_SERVER_LOADTIME_MEASUREMENT_H__