Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / net / tools / flip_server / loadtime_measurement.h
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.
4
5 #ifndef NET_TOOLS_FLIP_SERVER_LOADTIME_MEASUREMENT_H_
6 #define NET_TOOLS_FLIP_SERVER_LOADTIME_MEASUREMENT_H_
7
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <stdio.h>
11 #include <sys/types.h>
12 #include <unistd.h>
13
14 #include <map>
15 #include <string>
16 #include <vector>
17
18 #include "base/files/file_util.h"
19 #include "base/strings/string_split.h"
20
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     base::SplitString(urls_string, '\n', &urls_);
34     num_urls_ = urls_.size();
35   }
36
37   // This is the entry function for all the loadtime measure related urls
38   // It handles the request to html file, get_total_iteration to get number
39   // of urls in the urls file, get each url, report the loadtime for
40   // each url, and the test is completed.
41   void ProcessRequest(const std::string& uri, std::string& output) {
42     // remove "/testing/" from uri to get the action
43     std::string action = uri.substr(9);
44     if (pageload_html_file_.find(action) != std::string::npos) {
45       base::ReadFileToString(pageload_html_file_, &output);
46       return;
47     }
48     if (action.find("get_total_iteration") == 0) {
49       char buffer[16];
50       snprintf(buffer, sizeof(buffer), "%d", num_urls_);
51       output.append(buffer, strlen(buffer));
52       return;
53     }
54     if (action.find("geturl") == 0) {
55       size_t b = action.find_first_of('=');
56       if (b != std::string::npos) {
57         int num = atoi(action.substr(b + 1).c_str());
58         if (num < num_urls_) {
59           output.append(urls_[num]);
60         }
61       }
62       return;
63     }
64     if (action.find("test_complete") == 0) {
65       for (std::map<std::string, int>::const_iterator it = loadtimes_.begin();
66            it != loadtimes_.end();
67            ++it) {
68         LOG(INFO) << it->first << " " << it->second;
69       }
70       loadtimes_.clear();
71       output.append("OK");
72       return;
73     }
74     if (action.find("record_page_load") == 0) {
75       std::vector<std::string> query;
76       base::SplitString(action, '?', &query);
77       std::vector<std::string> params;
78       base::SplitString(query[1], '&', &params);
79       std::vector<std::string> url;
80       std::vector<std::string> loadtime;
81       base::SplitString(params[1], '=', &url);
82       base::SplitString(params[2], '=', &loadtime);
83       loadtimes_[url[1]] = atoi(loadtime[1].c_str());
84       output.append("OK");
85       return;
86     }
87   }
88
89  private:
90   int num_urls_;
91   std::vector<std::string> urls_;
92   std::map<std::string, int> loadtimes_;
93   const std::string pageload_html_file_;
94 };
95
96 #endif  // NET_TOOLS_FLIP_SERVER_LOADTIME_MEASUREMENT_H_