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
, 16, "%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(); ++it
) {
64 LOG(INFO
) << it
->first
<< " " << it
->second
;
70 if (action
.find("record_page_load") == 0) {
71 std::vector
<std::string
> query
;
72 split_string(action
, '?', &query
);
73 std::vector
<std::string
> params
;
74 split_string(query
[1], '&', ¶ms
);
75 std::vector
<std::string
> url
;
76 std::vector
<std::string
> loadtime
;
77 split_string(params
[1], '=', &url
);
78 split_string(params
[2], '=', &loadtime
);
79 loadtimes_
[url
[1]] = atoi(loadtime
[1].c_str());
86 void read_file_to_string(const char* filename
, std::string
* output
) {
88 int fd
= open(filename
, 0, "r");
92 ssize_t read_status
= read(fd
, buffer
, sizeof(buffer
));
93 while (read_status
> 0) {
94 output
->append(buffer
, static_cast<size_t>(read_status
));
96 read_status
= read(fd
, buffer
, sizeof(buffer
));
97 } while (read_status
<= 0 && errno
== EINTR
);
102 void split_string(std::string
& str
, char sepa
,
103 std::vector
<std::string
>* sub_strs
) {
105 size_t e
= str
.find_first_of(sepa
, b
);
106 while (e
!= std::string::npos
&& e
> b
) {
107 sub_strs
->push_back(str
.substr(b
, e
- b
));
109 e
= str
.find_first_of(sepa
, b
);
111 if (b
< str
.size()) {
112 sub_strs
->push_back(str
.substr(b
));
117 std::vector
<std::string
> urls_
;
118 std::map
<std::string
, int> loadtimes_
;
119 const std::string pageload_html_file_
;
122 #endif // NET_TOOLS_FLIP_SERVER_LOADTIME_MEASUREMENT_H__