Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / net / tools / flip_server / loadtime_measurement.h
blobd1a8e07fdb267a0eb2cafb610293adb01f6298c8
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_
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <stdio.h>
11 #include <sys/types.h>
12 #include <unistd.h>
14 #include <map>
15 #include <string>
16 #include <vector>
18 #include "base/files/file_util.h"
19 #include "base/strings/string_split.h"
21 // Class to handle loadtime measure related urls, which all start with testing
22 // The in memory server has a singleton object of this class. It includes a
23 // html file containing javascript to go through a list of urls and upload the
24 // loadtime. The users can modify urls.txt to define the urls they want to
25 // measure and start with downloading the html file from browser.
26 class LoadtimeMeasurement {
27 public:
28 LoadtimeMeasurement(const std::string& urls_file,
29 const std::string& pageload_html_file)
30 : num_urls_(0), pageload_html_file_(pageload_html_file) {
31 std::string urls_string;
32 base::ReadFileToString(urls_file, &urls_string);
33 urls_ = base::SplitString(urls_string, "\n", base::TRIM_WHITESPACE,
34 base::SPLIT_WANT_ALL);
35 num_urls_ = urls_.size();
38 // This is the entry function for all the loadtime measure related urls
39 // It handles the request to html file, get_total_iteration to get number
40 // of urls in the urls file, get each url, report the loadtime for
41 // each url, and the test is completed.
42 void ProcessRequest(const std::string& uri, std::string& output) {
43 // remove "/testing/" from uri to get the action
44 std::string action = uri.substr(9);
45 if (pageload_html_file_.find(action) != std::string::npos) {
46 base::ReadFileToString(pageload_html_file_, &output);
47 return;
49 if (action.find("get_total_iteration") == 0) {
50 char buffer[16];
51 snprintf(buffer, sizeof(buffer), "%d", num_urls_);
52 output.append(buffer, strlen(buffer));
53 return;
55 if (action.find("geturl") == 0) {
56 size_t b = action.find_first_of('=');
57 if (b != std::string::npos) {
58 int num = atoi(action.substr(b + 1).c_str());
59 if (num < num_urls_) {
60 output.append(urls_[num]);
63 return;
65 if (action.find("test_complete") == 0) {
66 for (std::map<std::string, int>::const_iterator it = loadtimes_.begin();
67 it != loadtimes_.end();
68 ++it) {
69 LOG(INFO) << it->first << " " << it->second;
71 loadtimes_.clear();
72 output.append("OK");
73 return;
75 if (action.find("record_page_load") == 0) {
76 std::vector<std::string> query = base::SplitString(
77 action, "?", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
78 std::vector<std::string> params = base::SplitString(
79 query[1], "&", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
80 std::vector<std::string> url = base::SplitString(
81 params[1], "=", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
82 std::vector<std::string> loadtime = base::SplitString(
83 params[2], "=", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
84 loadtimes_[url[1]] = atoi(loadtime[1].c_str());
85 output.append("OK");
86 return;
90 private:
91 int num_urls_;
92 std::vector<std::string> urls_;
93 std::map<std::string, int> loadtimes_;
94 const std::string pageload_html_file_;
97 #endif // NET_TOOLS_FLIP_SERVER_LOADTIME_MEASUREMENT_H_