Commit 3cfbcc72 authored by Erik Luo's avatar Erik Luo Committed by Commit Bot

Do not reset timer for console.time calls with the same label

Similar to Firefox and Safari, calling console.time() repeatedly with
the same label will now produce a console warning indicating that the
label already exists.  Similarly for console.timeEnd() as well.

Bug: chromium:727514
Cq-Include-Trybots: master.tryserver.blink:linux_trusty_blink_rel
Change-Id: Id644ee107b09e7f4686fff44c5f32d31c88371ad
Reviewed-on: https://chromium-review.googlesource.com/794345
Commit-Queue: Erik Luo <luoe@chromium.org>
Reviewed-by: 's avatarAleksey Kozyatinskiy <kozyatinskiy@chromium.org>
Cr-Commit-Position: refs/heads/master@{#49803}
parent 983ca744
......@@ -528,6 +528,11 @@ double V8ConsoleMessageStorage::timeEnd(int contextId, const String16& id) {
return elapsed;
}
bool V8ConsoleMessageStorage::hasTimer(int contextId, const String16& id) {
const std::map<String16, double>& time = m_data[contextId].m_time;
return time.find(id) != time.end();
}
void V8ConsoleMessageStorage::contextDestroyed(int contextId) {
m_estimatedSize = 0;
for (size_t i = 0; i < m_messages.size(); ++i) {
......
......@@ -120,6 +120,7 @@ class V8ConsoleMessageStorage {
int count(int contextId, const String16& id);
void time(int contextId, const String16& id);
double timeEnd(int contextId, const String16& id);
bool hasTimer(int contextId, const String16& id);
private:
V8InspectorImpl* m_inspector;
......
......@@ -354,10 +354,16 @@ static void timeFunction(const v8::debug::ConsoleCallArguments& info,
ConsoleHelper helper(info, consoleContext, inspector);
String16 protocolTitle = helper.firstArgToString("default", false);
if (timelinePrefix) protocolTitle = "Timeline '" + protocolTitle + "'";
const String16& timerId =
protocolTitle + "@" + consoleContextToString(consoleContext);
if (helper.consoleMessageStorage()->hasTimer(helper.contextId(), timerId)) {
helper.reportCallWithArgument(
ConsoleAPIType::kWarning,
"Timer '" + protocolTitle + "' already exists");
return;
}
inspector->client()->consoleTime(toStringView(protocolTitle));
helper.consoleMessageStorage()->time(
helper.contextId(),
protocolTitle + "@" + consoleContextToString(consoleContext));
helper.consoleMessageStorage()->time(helper.contextId(), timerId);
}
static void timeEndFunction(const v8::debug::ConsoleCallArguments& info,
......@@ -366,6 +372,14 @@ static void timeEndFunction(const v8::debug::ConsoleCallArguments& info,
ConsoleHelper helper(info, consoleContext, inspector);
String16 protocolTitle = helper.firstArgToString("default", false);
if (timelinePrefix) protocolTitle = "Timeline '" + protocolTitle + "'";
const String16& timerId =
protocolTitle + "@" + consoleContextToString(consoleContext);
if (!helper.consoleMessageStorage()->hasTimer(helper.contextId(), timerId)) {
helper.reportCallWithArgument(
ConsoleAPIType::kWarning,
"Timer '" + protocolTitle + "' does not exist");
return;
}
inspector->client()->consoleTimeEnd(toStringView(protocolTitle));
double elapsed = helper.consoleMessageStorage()->timeEnd(
helper.contextId(),
......
Checks that repeated console.time do not reset
{
method : Runtime.consoleAPICalled
params : {
args : [
[0] : {
type : string
value : Timer 'a' already exists
}
]
executionContextId : <executionContextId>
stackTrace : {
callFrames : [
[0] : {
columnNumber : 8
functionName :
lineNumber : 0
scriptId : <scriptId>
url :
}
]
}
timestamp : <timestamp>
type : warning
}
}
{
method : Runtime.consoleAPICalled
params : {
args : [
[0] : {
type : string
value : a: 2ms
}
]
executionContextId : <executionContextId>
stackTrace : {
callFrames : [
[0] : {
columnNumber : 8
functionName :
lineNumber : 0
scriptId : <scriptId>
url :
}
]
}
timestamp : <timestamp>
type : timeEnd
}
}
{
method : Runtime.consoleAPICalled
params : {
args : [
[0] : {
type : string
value : Timer 'a' does not exist
}
]
executionContextId : <executionContextId>
stackTrace : {
callFrames : [
[0] : {
columnNumber : 8
functionName :
lineNumber : 0
scriptId : <scriptId>
url :
}
]
}
timestamp : <timestamp>
type : warning
}
}
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
let {session, contextGroup, Protocol} = InspectorTest.start('Checks that repeated console.time do not reset');
Protocol.Runtime.onConsoleAPICalled(InspectorTest.logMessage);
Protocol.Runtime.enable();
(async function() {
utils.setCurrentTimeMSForTest(0.0);
await Protocol.Runtime.evaluate({expression: `console.time('a')`});
utils.setCurrentTimeMSForTest(1.0);
await Protocol.Runtime.evaluate({expression: `console.time('a')`});
utils.setCurrentTimeMSForTest(2.0);
await Protocol.Runtime.evaluate({expression: `console.timeEnd('a')`});
utils.setCurrentTimeMSForTest(5.0);
await Protocol.Runtime.evaluate({expression: `console.timeEnd('a')`});
InspectorTest.completeTest();
})();
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment