2008-07-26 Keishi Hattori <casey.hattori@gmail.com>
authormrowe@apple.com <mrowe@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sun, 27 Jul 2008 05:44:30 +0000 (05:44 +0000)
committermrowe@apple.com <mrowe@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sun, 27 Jul 2008 05:44:30 +0000 (05:44 +0000)
        Reviewed by Adam Roben.

        Add console.time/timeEnd.
        https://bugs.webkit.org/show_bug.cgi?id=19159

        * manual-tests/inspector/console-time.html: Added. Test cases for console.time/timeEnd.
        * page/Console.cpp:
        (WebCore::Console::time): Added.
        (WebCore::Console::timeEnd): Added.
        * page/Console.h:
        * page/Console.idl: Added console.time/timeEnd.
        * page/InspectorController.cpp:
        (WebCore::InspectorController::startTiming): Added.
        (WebCore::InspectorController::stopTiming): Added.
        * page/InspectorController.h:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@35392 268f45cc-cd09-0410-ab3c-d52691b4dbfc

WebCore/ChangeLog
WebCore/manual-tests/inspector/console-time.html [new file with mode: 0644]
WebCore/page/Console.cpp
WebCore/page/Console.h
WebCore/page/Console.idl
WebCore/page/InspectorController.cpp
WebCore/page/InspectorController.h

index a70fed0..b916cec 100644 (file)
@@ -1,3 +1,21 @@
+2008-07-26  Keishi Hattori  <casey.hattori@gmail.com>
+
+        Reviewed by Adam Roben.
+
+        Add console.time/timeEnd.
+        https://bugs.webkit.org/show_bug.cgi?id=19159
+
+        * manual-tests/inspector/console-time.html: Added. Test cases for console.time/timeEnd.
+        * page/Console.cpp:
+        (WebCore::Console::time): Added.
+        (WebCore::Console::timeEnd): Added.
+        * page/Console.h:
+        * page/Console.idl: Added console.time/timeEnd.
+        * page/InspectorController.cpp:
+        (WebCore::InspectorController::startTiming): Added.
+        (WebCore::InspectorController::stopTiming): Added.
+        * page/InspectorController.h:
+
 2008-07-26  Dirk Schulze  <vbs85@gmx.de>
 
         Reviewed by Nikolas Zimmermann.
diff --git a/WebCore/manual-tests/inspector/console-time.html b/WebCore/manual-tests/inspector/console-time.html
new file mode 100644 (file)
index 0000000..6c24373
--- /dev/null
@@ -0,0 +1,33 @@
+<script>
+function test() {
+    // Test 1: call timeEnd before time
+    console.log(">>>console.timeEnd(\"1\")");
+    console.timeEnd("1");
+    // Test 2: call time two times before timeEnd
+    console.log(">>>console.time(\"2\")");
+    console.time("2");
+    setTimeout(function() {
+        console.log(">>>console.time(\"2\")");
+        console.time("2");
+        console.log(">>>console.timeEnd(\"2\")");
+        console.timeEnd("2");
+        console.log("2: If it says ~1000ms the first time is not overwritten.");
+    }, 1000);
+    // Test 3: call timeEnd two times
+    console.log(">>>console.time(\"3\")");
+    console.time("3");
+    console.log(">>>console.timeEnd(\"3\")");
+    console.timeEnd("3");
+    console.log(">>>console.timeEnd(\"3\")");
+    console.timeEnd("3");
+    // Test 4: call time/timeEnd without title parameter
+    console.log(">>>console.time()");
+    console.time();
+    console.log(">>>console.timeEnd()");
+    console.timeEnd();
+};
+
+</script>
+<p>Test for <a href="https://bugs.webkit.org/show_bug.cgi?id=19159">Bug 19159: Inspector should support console.time/console.timeEnd</a>.</p>
+<p>Open the Inspector (right-click and choose "Inspect Element"), then click the "Run Tests" button.</p>
+<button onclick="test()">Run Tests</button>
\ No newline at end of file
index bc1db41..adc96e4 100644 (file)
@@ -267,6 +267,42 @@ void Console::profileEnd(ExecState* exec, const ArgList& args)
     Profiler::profiler()->stopProfiling(exec, title);
 }
 
+void Console::time(const UString& title)
+{
+    if (title.isNull())
+        return;
+    
+    if (!m_frame)
+        return;
+    
+    Page* page = m_frame->page();
+    if (!page)
+        return;
+    
+    page->inspectorController()->startTiming(title);
+}
+
+void Console::timeEnd(const UString& title)
+{
+    if (title.isNull())
+        return;
+    
+    if (!m_frame)
+        return;
+    
+    Page* page = m_frame->page();
+    if (!page)
+        return;
+    
+    double elapsed;
+    if (!page->inspectorController()->stopTiming(title, elapsed))
+        return;
+    
+    String message = String(title) + String::format(": %.0fms", elapsed);
+    // FIXME: <https://bugs.webkit.org/show_bug.cgi?id=19791> We should pass in the real sourceURL here so that the Inspector can show it.
+    page->inspectorController()->addMessageToConsole(JSMessageSource, LogMessageLevel, message, 0, String());
+}
+
 void Console::finishedProfiling(PassRefPtr<Profile> prpProfile)
 {
     if (Page* page = m_frame->page())
index babe622..047ac5a 100644 (file)
@@ -77,6 +77,8 @@ namespace WebCore {
         void assertCondition(bool condition, KJS::ExecState*, const KJS::ArgList& arguments);
         void profile(KJS::ExecState*, const KJS::ArgList& arguments);
         void profileEnd(KJS::ExecState*, const KJS::ArgList& arguments);
+        void time(const KJS::UString& title);
+        void timeEnd(const KJS::UString& title);
 
         void finishedProfiling(PassRefPtr<KJS::Profile>);
 
index 77cf828..f8a20e1 100644 (file)
@@ -38,6 +38,8 @@ module window {
 
         [Custom] void profile(in DOMString title);
         [Custom] void profileEnd();
+        void time(in [ConvertUndefinedOrNullToNullString] DOMString title);
+        void timeEnd(in [ConvertUndefinedOrNullToNullString] DOMString title);
     };
 
 }
index a88d43c..67118fe 100644 (file)
@@ -2449,6 +2449,24 @@ void InspectorController::drawNodeHighlight(GraphicsContext& context) const
     drawHighlightForBoxes(context, lineBoxRects, contentBox, paddingBox, borderBox, marginBox);
 }
 
+void InspectorController::startTiming(const UString& title)
+{
+    m_times.add(title, currentTime() * 1000);
+}
+
+bool InspectorController::stopTiming(const UString& title, double& elapsed)
+{
+    HashMap<String, double>::iterator it = m_times.find(title);
+    if (it == m_times.end())
+        return false;
+
+    double startTime = it->second;
+    m_times.remove(it);
+    
+    elapsed = currentTime() * 1000 - startTime;
+    return true;
+}
+
 bool InspectorController::handleException(JSContextRef context, JSValueRef exception, unsigned lineNumber) const
 {
     if (!exception)
index 428d9b5..d8be159 100644 (file)
@@ -169,6 +169,9 @@ public:
     void stepOutOfFunctionInDebugger();
 
     void drawNodeHighlight(GraphicsContext&) const;
+    
+    void startTiming(const KJS::UString& title);
+    bool stopTiming(const KJS::UString& title, double& elapsed);
 
 private:
     void focusNode();
@@ -220,6 +223,7 @@ private:
     FrameResourcesMap m_frameResources;
     Vector<ConsoleMessage*> m_consoleMessages;
     Vector<RefPtr<KJS::Profile> > m_profiles;
+    HashMap<String, double> m_times;
 #if ENABLE(DATABASE)
     DatabaseResourcesSet m_databaseResources;
 #endif