Home | History | Annotate | Download | only in perf
      1 // Copyright (c) 2012 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 #include "base/files/file_path.h"
      6 #include "base/path_service.h"
      7 #include "base/strings/utf_string_conversions.h"
      8 #include "base/test/test_timeouts.h"
      9 #include "chrome/common/automation_constants.h"
     10 #include "chrome/common/chrome_paths.h"
     11 #include "chrome/test/automation/tab_proxy.h"
     12 #include "chrome/test/perf/perf_test.h"
     13 #include "chrome/test/ui/javascript_test_util.h"
     14 #include "chrome/test/ui/ui_perf_test.h"
     15 #include "net/base/net_util.h"
     16 #include "url/gurl.h"
     17 
     18 namespace {
     19 
     20 static const base::FilePath::CharType kStartFile[] =
     21     FILE_PATH_LITERAL("perf_test.html");
     22 
     23 class IndexedDBTest : public UIPerfTest {
     24  public:
     25   typedef std::map<std::string, std::string> ResultsMap;
     26 
     27   IndexedDBTest() : reference_(false) {
     28     dom_automation_enabled_ = true;
     29     show_window_ = true;
     30   }
     31 
     32   void RunTest() {
     33     base::FilePath::StringType start_file(kStartFile);
     34     base::FilePath test_path = GetIndexedDBTestDir();
     35     test_path = test_path.Append(start_file);
     36     GURL test_url(net::FilePathToFileURL(test_path));
     37 
     38     scoped_refptr<TabProxy> tab(GetActiveTab());
     39     ASSERT_TRUE(tab.get());
     40     ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_SUCCESS, tab->NavigateToURL(test_url));
     41 
     42     // Wait for the test to finish.
     43     ASSERT_TRUE(WaitUntilTestCompletes(tab.get(), test_url));
     44 
     45     PrintResults(tab.get());
     46   }
     47 
     48  protected:
     49   bool reference_;  // True if this is a reference build.
     50 
     51  private:
     52   // Return the path to the IndexedDB test directory on the local filesystem.
     53   base::FilePath GetIndexedDBTestDir() {
     54     base::FilePath test_dir;
     55     PathService::Get(chrome::DIR_TEST_DATA, &test_dir);
     56     return test_dir.AppendASCII("indexeddb");
     57   }
     58 
     59   bool WaitUntilTestCompletes(TabProxy* tab, const GURL& test_url) {
     60     return WaitUntilCookieValue(tab, test_url, "__done",
     61                                 TestTimeouts::large_test_timeout(), "1");
     62   }
     63 
     64   bool GetResults(TabProxy* tab, ResultsMap* results) {
     65     std::wstring json_wide;
     66     bool succeeded = tab->ExecuteAndExtractString(
     67         std::wstring(),
     68         L"window.domAutomationController.send("
     69         L"    JSON.stringify(automation.getResults()));",
     70         &json_wide);
     71 
     72     EXPECT_TRUE(succeeded);
     73     if (!succeeded)
     74       return false;
     75 
     76     std::string json = WideToUTF8(json_wide);
     77     return JsonDictionaryToMap(json, results);
     78   }
     79 
     80   void PrintResults(TabProxy* tab) {
     81     ResultsMap results;
     82     ASSERT_TRUE(GetResults(tab, &results));
     83 
     84     std::string trace_name = reference_ ? "t_ref" : "t";
     85 
     86     ResultsMap::const_iterator it = results.begin();
     87     for (; it != results.end(); ++it)
     88       perf_test::PrintResultList(
     89           it->first, std::string(), trace_name, it->second, "ms", false);
     90   }
     91 
     92   DISALLOW_COPY_AND_ASSIGN(IndexedDBTest);
     93 };
     94 
     95 class IndexedDBReferenceTest : public IndexedDBTest {
     96  public:
     97   IndexedDBReferenceTest() : IndexedDBTest() {
     98     reference_ = true;
     99   }
    100 
    101   virtual void SetUp() {
    102     UseReferenceBuild();
    103     IndexedDBTest::SetUp();
    104   }
    105 };
    106 
    107 TEST_F(IndexedDBTest, Perf) {
    108 
    109   RunTest();
    110 }
    111 
    112 TEST_F(IndexedDBReferenceTest, Perf) {
    113 
    114   RunTest();
    115 }
    116 
    117 }  // namespace
    118