diff options
author | toyoshim@chromium.org <toyoshim@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-04-26 13:15:29 +0000 |
---|---|---|
committer | toyoshim@chromium.org <toyoshim@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-04-26 13:15:29 +0000 |
commit | e0be0099cd7ca92687a2c7a3b79b737c5eedee6d (patch) | |
tree | 5a4e8ab5933b6d953e2952956c59172b276e2ab5 /chrome/renderer | |
parent | 44e9a2fe0aa46ac1b8f2d7a861aa211bba252096 (diff) | |
download | chromium_src-e0be0099cd7ca92687a2c7a3b79b737c5eedee6d.zip chromium_src-e0be0099cd7ca92687a2c7a3b79b737c5eedee6d.tar.gz chromium_src-e0be0099cd7ca92687a2c7a3b79b737c5eedee6d.tar.bz2 |
Translate: implement UMAs for monitoring performance
Expose JavaScript performance counts and reports them as following UMAs.
- Translate.TimeToLoad
- Translate.TimeToBeReady
- Translate.TimeToTranslate
BUG=235429
TEST=none
NOTRY=true
Review URL: https://codereview.chromium.org/14358038
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@196714 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/renderer')
-rw-r--r-- | chrome/renderer/translate/translate_helper.cc | 125 | ||||
-rw-r--r-- | chrome/renderer/translate/translate_helper.h | 33 | ||||
-rw-r--r-- | chrome/renderer/translate/translate_helper_browsertest.cc | 26 |
3 files changed, 127 insertions, 57 deletions
diff --git a/chrome/renderer/translate/translate_helper.cc b/chrome/renderer/translate/translate_helper.cc index 6621a24..43e35f9 100644 --- a/chrome/renderer/translate/translate_helper.cc +++ b/chrome/renderer/translate/translate_helper.cc @@ -180,6 +180,67 @@ base::TimeDelta TranslateHelper::AdjustDelay(int delayInMs) { return base::TimeDelta::FromMilliseconds(delayInMs); } +void TranslateHelper::ExecuteScript(const std::string& script) { + WebFrame* main_frame = GetMainFrame(); + if (main_frame) + main_frame->executeScript(WebScriptSource(ASCIIToUTF16(script))); +} + +bool TranslateHelper::ExecuteScriptAndGetBoolResult(const std::string& script, + bool fallback) { + WebFrame* main_frame = GetMainFrame(); + if (!main_frame) + return fallback; + + v8::HandleScope handle_scope; + v8::Handle<v8::Value> v = main_frame->executeScriptAndReturnValue( + WebScriptSource(ASCIIToUTF16(script))); + if (v.IsEmpty() || !v->IsBoolean()) { + NOTREACHED(); + return fallback; + } + + return v->BooleanValue(); +} + +std::string TranslateHelper::ExecuteScriptAndGetStringResult( + const std::string& script) { + WebFrame* main_frame = GetMainFrame(); + if (!main_frame) + return std::string(); + + v8::HandleScope handle_scope; + v8::Handle<v8::Value> v = main_frame->executeScriptAndReturnValue( + WebScriptSource(ASCIIToUTF16(script))); + if (v.IsEmpty() || !v->IsString()) { + NOTREACHED(); + return std::string(); + } + + v8::Local<v8::String> v8_str = v->ToString(); + int length = v8_str->Utf8Length() + 1; + scoped_ptr<char[]> str(new char[length]); + v8_str->WriteUtf8(str.get(), length); + return std::string(str.get()); +} + +double TranslateHelper::ExecuteScriptAndGetDoubleResult( + const std::string& script) { + WebFrame* main_frame = GetMainFrame(); + if (!main_frame) + return 0.0; + + v8::HandleScope handle_scope; + v8::Handle<v8::Value> v = main_frame->executeScriptAndReturnValue( + WebScriptSource(ASCIIToUTF16(script))); + if (v.IsEmpty() || !v->IsNumber()) { + NOTREACHED(); + return 0.0; + } + + return v->NumberValue(); +} + //////////////////////////////////////////////////////////////////////////////// // TranslateHelper, private: // @@ -391,6 +452,13 @@ void TranslateHelper::CheckTranslateStatus() { translation_pending_ = false; + // Check JavaScript performance counters for UMA reports. + double time_to_translate = + ExecuteScriptAndGetDoubleResult("cr.googleTranslate.translationTime"); + UMA_HISTOGRAM_MEDIUM_TIMES( + "Translate.TimeToTranslate", + base::TimeDelta::FromMicroseconds(time_to_translate * 1000.0)); + // Notify the browser we are done. render_view()->Send(new ChromeViewHostMsg_PageTranslated( render_view()->GetRoutingID(), render_view()->GetPageId(), @@ -406,50 +474,6 @@ void TranslateHelper::CheckTranslateStatus() { AdjustDelay(kTranslateStatusCheckDelayMs)); } -void TranslateHelper::ExecuteScript(const std::string& script) { - WebFrame* main_frame = GetMainFrame(); - if (main_frame) - main_frame->executeScript(WebScriptSource(ASCIIToUTF16(script))); -} - -bool TranslateHelper::ExecuteScriptAndGetBoolResult(const std::string& script, - bool fallback) { - WebFrame* main_frame = GetMainFrame(); - if (!main_frame) - return fallback; - - v8::HandleScope handle_scope; - v8::Handle<v8::Value> v = main_frame->executeScriptAndReturnValue( - WebScriptSource(ASCIIToUTF16(script))); - if (v.IsEmpty() || !v->IsBoolean()) { - NOTREACHED(); - return fallback; - } - - return v->BooleanValue(); -} - -std::string TranslateHelper::ExecuteScriptAndGetStringResult( - const std::string& script) { - WebFrame* main_frame = GetMainFrame(); - if (!main_frame) - return std::string(); - - v8::HandleScope handle_scope; - v8::Handle<v8::Value> v = main_frame->executeScriptAndReturnValue( - WebScriptSource(ASCIIToUTF16(script))); - if (v.IsEmpty() || !v->IsString()) { - NOTREACHED(); - return std::string(); - } - - v8::Local<v8::String> v8_str = v->ToString(); - int length = v8_str->Utf8Length() + 1; - scoped_ptr<char[]> str(new char[length]); - v8_str->WriteUtf8(str.get(), length); - return std::string(str.get()); -} - void TranslateHelper::TranslatePageImpl(int count) { DCHECK_LT(count, kMaxTranslateInitCheckAttempts); if (page_id_ != render_view()->GetPageId() || !render_view()->GetWebView()) @@ -470,6 +494,19 @@ void TranslateHelper::TranslatePageImpl(int count) { return; } + // The library is loaded, and ready for translation now. + // Check JavaScript performance counters for UMA reports. + double time_to_load = + ExecuteScriptAndGetDoubleResult("cr.googleTranslate.loadTime"); + double time_to_be_ready = + ExecuteScriptAndGetDoubleResult("cr.googleTranslate.readyTime"); + UMA_HISTOGRAM_MEDIUM_TIMES( + "Translate.TimeToLoad", + base::TimeDelta::FromMicroseconds(time_to_load * 1000.0)); + UMA_HISTOGRAM_MEDIUM_TIMES( + "Translate.TimeToBeReady", + base::TimeDelta::FromMicroseconds(time_to_be_ready * 1000.0)); + if (!StartTranslation()) { NotifyBrowserTranslationFailed(TranslateErrors::TRANSLATION_ERROR); return; diff --git a/chrome/renderer/translate/translate_helper.h b/chrome/renderer/translate/translate_helper.h index 0a37818..5ef43dc 100644 --- a/chrome/renderer/translate/translate_helper.h +++ b/chrome/renderer/translate/translate_helper.h @@ -66,6 +66,26 @@ class TranslateHelper : public content::RenderViewObserver { // immediately by returning 0. virtual base::TimeDelta AdjustDelay(int delayInMs); + // Executes the JavaScript code in |script| in the main frame of RenderView. + virtual void ExecuteScript(const std::string& script); + + // Executes the JavaScript code in |script| in the main frame of RenderView, + // and returns the boolean returned by the script evaluation if the script was + // run successfully. Otherwise, returns |fallback| value. + virtual bool ExecuteScriptAndGetBoolResult(const std::string& script, + bool fallback); + + // Executes the JavaScript code in |script| in the main frame of RenderView, + // and returns the string returned by the script evaluation if the script was + // run successfully. Otherwise, returns empty string. + virtual std::string ExecuteScriptAndGetStringResult( + const std::string& script); + + // Executes the JavaScript code in |script| in the main frame of RenderView. + // and returns the number returned by the script evaluation if the script was + // run successfully. Otherwise, returns 0.0. + virtual double ExecuteScriptAndGetDoubleResult(const std::string& script); + private: FRIEND_TEST_ALL_PREFIXES(TranslateHelperTest, LanguageCodeTypoCorrection); FRIEND_TEST_ALL_PREFIXES(TranslateHelperTest, LanguageCodeSynonyms); @@ -111,19 +131,6 @@ class TranslateHelper : public content::RenderViewObserver { // posts a task to check again later. void CheckTranslateStatus(); - // Executes the JavaScript code in |script| in the main frame of RenderView. - void ExecuteScript(const std::string& script); - - // Executes the JavaScript code in |script| in the main frame of RenderView, - // and returns the boolean returned by the script evaluation if the script was - // run successfully. Otherwise, returns |fallback| value. - bool ExecuteScriptAndGetBoolResult(const std::string& script, bool fallback); - - // Executes the JavaScript code in |script| in the main frame of RenderView, - // and returns the string returned by the script evaluation if the script was - // run successfully. Otherwise, returns empty string. - std::string ExecuteScriptAndGetStringResult(const std::string& script); - // Called by TranslatePage to do the actual translation. |count| is used to // limit the number of retries. void TranslatePageImpl(int count); diff --git a/chrome/renderer/translate/translate_helper_browsertest.cc b/chrome/renderer/translate/translate_helper_browsertest.cc index 781094a..f969339 100644 --- a/chrome/renderer/translate/translate_helper_browsertest.cc +++ b/chrome/renderer/translate/translate_helper_browsertest.cc @@ -14,6 +14,7 @@ using testing::AtLeast; using testing::Return; +using testing::_; class TestTranslateHelper : public TranslateHelper { public: @@ -40,6 +41,11 @@ class TestTranslateHelper : public TranslateHelper { MOCK_METHOD0(HasTranslationFailed, bool()); MOCK_METHOD0(GetOriginalPageLanguage, std::string()); MOCK_METHOD0(StartTranslation, bool()); + MOCK_METHOD1(ExecuteScript, void(const std::string&)); + MOCK_METHOD2(ExecuteScriptAndGetBoolResult, bool(const std::string&, bool)); + MOCK_METHOD1(ExecuteScriptAndGetStringResult, + std::string(const std::string&)); + MOCK_METHOD1(ExecuteScriptAndGetDoubleResult, double(const std::string&)); private: DISALLOW_COPY_AND_ASSIGN(TestTranslateHelper); @@ -136,6 +142,10 @@ TEST_F(TranslateHelperBrowserTest, TranslateSuccess) { .WillOnce(Return(false)) .WillOnce(Return(true)); + // V8 call for performance monitoring should be ignored. + EXPECT_CALL(*translate_helper_, + ExecuteScriptAndGetDoubleResult(_)).Times(3); + std::string original_lang("en"); std::string target_lang("fr"); translate_helper_->TranslatePage( @@ -181,6 +191,10 @@ TEST_F(TranslateHelperBrowserTest, TranslateFailure) { .Times(AtLeast(1)) .WillRepeatedly(Return(false)); + // V8 call for performance monitoring should be ignored. + EXPECT_CALL(*translate_helper_, + ExecuteScriptAndGetDoubleResult(_)).Times(2); + translate_helper_->TranslatePage( view_->GetPageId(), "en", "fr", std::string()); MessageLoop::current()->RunUntilIdle(); @@ -214,6 +228,10 @@ TEST_F(TranslateHelperBrowserTest, UndefinedSourceLang) { .Times(AtLeast(1)) .WillRepeatedly(Return(true)); + // V8 call for performance monitoring should be ignored. + EXPECT_CALL(*translate_helper_, + ExecuteScriptAndGetDoubleResult(_)).Times(3); + translate_helper_->TranslatePage(view_->GetPageId(), chrome::kUnknownLanguageCode, "fr", std::string()); @@ -249,6 +267,10 @@ TEST_F(TranslateHelperBrowserTest, MultipleSimilarTranslations) { EXPECT_CALL(*translate_helper_, HasTranslationFinished()) .WillOnce(Return(true)); + // V8 call for performance monitoring should be ignored. + EXPECT_CALL(*translate_helper_, + ExecuteScriptAndGetDoubleResult(_)).Times(3); + std::string original_lang("en"); std::string target_lang("fr"); translate_helper_->TranslatePage( @@ -287,6 +309,10 @@ TEST_F(TranslateHelperBrowserTest, MultipleDifferentTranslations) { EXPECT_CALL(*translate_helper_, HasTranslationFinished()) .WillOnce(Return(true)); + // V8 call for performance monitoring should be ignored. + EXPECT_CALL(*translate_helper_, + ExecuteScriptAndGetDoubleResult(_)).Times(5); + std::string original_lang("en"); std::string target_lang("fr"); translate_helper_->TranslatePage( |