Add infrastructure to dump resource load statistics
authorcommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 5 Sep 2018 21:25:06 +0000 (21:25 +0000)
committercommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 5 Sep 2018 21:25:06 +0000 (21:25 +0000)
https://bugs.webkit.org/show_bug.cgi?id=189213

Patch by Woodrow Wang <woodrow_wang@apple.com> on 2018-09-05
Source/WebCore:

Reviewed by Daniel Bates.

The dumping functionality is not currently used, but will be included in tests for
<https://bugs.webkit.org/show_bug.cgi?id=187773>.

* loader/ResourceLoadStatistics.h:

Source/WebKit:

Reviewed by Daniel Bates.

The dumping functionality is not currently used, but will be included in tests for
<https://bugs.webkit.org/show_bug.cgi?id=187773>.

* UIProcess/API/C/WKWebsiteDataStoreRef.cpp:
(WKWebsiteDataStoreDumpResourceLoadStatistics):
* UIProcess/API/C/WKWebsiteDataStoreRef.h:
* UIProcess/ResourceLoadStatisticsMemoryStore.cpp:
(WebKit::ResourceLoadStatisticsMemoryStore::dumpResourceLoadStatistics const):
* UIProcess/ResourceLoadStatisticsMemoryStore.h:
* UIProcess/WebResourceLoadStatisticsStore.cpp:
(WebKit::WebResourceLoadStatisticsStore::dumpResourceLoadStatistics):
* UIProcess/WebResourceLoadStatisticsStore.h:

Tools:

Reviewed by Daniel Bates.

The dumping functionality is not currently used, but will be included in tests for
<https://bugs.webkit.org/show_bug.cgi?id=187773>.

* WebKitTestRunner/InjectedBundle/Bindings/TestRunner.idl:
* WebKitTestRunner/InjectedBundle/TestRunner.cpp:
(WTR::TestRunner::setDumpResourceLoadStatistics):
* WebKitTestRunner/InjectedBundle/TestRunner.h:
* WebKitTestRunner/TestController.cpp:
(WTR::resourceStatisticsStringResultCallback):
(WTR::TestController::dumpResourceLoadStatistics):
* WebKitTestRunner/TestController.h:
* WebKitTestRunner/TestInvocation.cpp:
(WTR::TestInvocation::dumpResults):
(WTR::TestInvocation::didReceiveSynchronousMessageFromInjectedBundle):
(WTR::TestInvocation::setDumpResourceLoadStatistics):
* WebKitTestRunner/TestInvocation.h:

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

17 files changed:
Source/WebCore/ChangeLog
Source/WebCore/loader/ResourceLoadStatistics.h
Source/WebKit/ChangeLog
Source/WebKit/UIProcess/API/C/WKWebsiteDataStoreRef.cpp
Source/WebKit/UIProcess/API/C/WKWebsiteDataStoreRef.h
Source/WebKit/UIProcess/ResourceLoadStatisticsMemoryStore.cpp
Source/WebKit/UIProcess/ResourceLoadStatisticsMemoryStore.h
Source/WebKit/UIProcess/WebResourceLoadStatisticsStore.cpp
Source/WebKit/UIProcess/WebResourceLoadStatisticsStore.h
Tools/ChangeLog
Tools/WebKitTestRunner/InjectedBundle/Bindings/TestRunner.idl
Tools/WebKitTestRunner/InjectedBundle/TestRunner.cpp
Tools/WebKitTestRunner/InjectedBundle/TestRunner.h
Tools/WebKitTestRunner/TestController.cpp
Tools/WebKitTestRunner/TestController.h
Tools/WebKitTestRunner/TestInvocation.cpp
Tools/WebKitTestRunner/TestInvocation.h

index b82b893..3b5844f 100644 (file)
@@ -1,3 +1,15 @@
+2018-09-05  Woodrow Wang  <woodrow_wang@apple.com>
+
+        Add infrastructure to dump resource load statistics
+        https://bugs.webkit.org/show_bug.cgi?id=189213
+
+        Reviewed by Daniel Bates. 
+
+        The dumping functionality is not currently used, but will be included in tests for
+        <https://bugs.webkit.org/show_bug.cgi?id=187773>.
+
+        * loader/ResourceLoadStatistics.h:
+
 2018-09-05  Ryan Haddad  <ryanhaddad@apple.com>
 
         Unreviewed, fix the build with recent SDKs.
index 8c9ed4c..27a17ff 100644 (file)
@@ -58,7 +58,7 @@ struct ResourceLoadStatistics {
     WEBCORE_EXPORT void encode(KeyedEncoder&) const;
     WEBCORE_EXPORT bool decode(KeyedDecoder&, unsigned modelVersion);
 
-    String toString() const;
+    WEBCORE_EXPORT String toString() const;
 
     WEBCORE_EXPORT void merge(const ResourceLoadStatistics&);
 
index e620e71..6fb4b05 100644 (file)
@@ -1,3 +1,23 @@
+2018-09-05  Woodrow Wang  <woodrow_wang@apple.com>
+
+        Add infrastructure to dump resource load statistics
+        https://bugs.webkit.org/show_bug.cgi?id=189213
+
+        Reviewed by Daniel Bates.
+
+        The dumping functionality is not currently used, but will be included in tests for
+        <https://bugs.webkit.org/show_bug.cgi?id=187773>.
+
+        * UIProcess/API/C/WKWebsiteDataStoreRef.cpp:
+        (WKWebsiteDataStoreDumpResourceLoadStatistics):
+        * UIProcess/API/C/WKWebsiteDataStoreRef.h:
+        * UIProcess/ResourceLoadStatisticsMemoryStore.cpp:
+        (WebKit::ResourceLoadStatisticsMemoryStore::dumpResourceLoadStatistics const):
+        * UIProcess/ResourceLoadStatisticsMemoryStore.h:
+        * UIProcess/WebResourceLoadStatisticsStore.cpp:
+        (WebKit::WebResourceLoadStatisticsStore::dumpResourceLoadStatistics):
+        * UIProcess/WebResourceLoadStatisticsStore.h:
+
 2018-09-05  David Kilzer  <ddkilzer@apple.com>
 
         REGRESSION (r235006): Let Xcode have its way with the WebKit project
index 802d1cd..68a1658 100644 (file)
@@ -30,6 +30,7 @@
 #include "APIWebsiteDataStore.h"
 #include "WKAPICast.h"
 #include "WKSecurityOriginRef.h"
+#include "WKString.h"
 #include "WebResourceLoadStatisticsStore.h"
 #include "WebResourceLoadStatisticsTelemetry.h"
 #include "WebsiteData.h"
@@ -143,6 +144,19 @@ void WKWebsiteDataStoreSetStatisticsVeryPrevalentResource(WKWebsiteDataStoreRef
         });
 }
 
+void WKWebsiteDataStoreDumpResourceLoadStatistics(WKWebsiteDataStoreRef dataStoreRef, void* context, WKWebsiteDataStoreDumpResourceLoadStatisticsFunction callback)
+{
+    auto* store = WebKit::toImpl(dataStoreRef)->websiteDataStore().resourceLoadStatistics();
+    if (!store) {
+        callback(WebKit::toAPI(emptyString().impl()), context);
+        return;
+    }
+
+    store->dumpResourceLoadStatistics([context, callback] (const String& resourceLoadStatistics) {
+        callback(WebKit::toAPI(resourceLoadStatistics.impl()), context);
+    });
+}
+
 void WKWebsiteDataStoreIsStatisticsPrevalentResource(WKWebsiteDataStoreRef dataStoreRef, WKStringRef host, void* context, WKWebsiteDataStoreIsStatisticsPrevalentResourceFunction callback)
 {
     auto* store = WebKit::toImpl(dataStoreRef)->websiteDataStore().resourceLoadStatistics();
index 0662dcf..02e7bce 100644 (file)
@@ -49,6 +49,8 @@ typedef void (*WKWebsiteDataStoreStatisticsPrevalentResourceFunction)(void* func
 WK_EXPORT void WKWebsiteDataStoreSetStatisticsPrevalentResource(WKWebsiteDataStoreRef dataStoreRef, WKStringRef host, bool value, void* context, WKWebsiteDataStoreStatisticsPrevalentResourceFunction completionHandler);
 typedef void (*WKWebsiteDataStoreStatisticsVeryPrevalentResourceFunction)(void* functionContext);
 WK_EXPORT void WKWebsiteDataStoreSetStatisticsVeryPrevalentResource(WKWebsiteDataStoreRef dataStoreRef, WKStringRef host, bool value, void* context, WKWebsiteDataStoreStatisticsVeryPrevalentResourceFunction completionHandler);
+typedef void (*WKWebsiteDataStoreDumpResourceLoadStatisticsFunction)(WKStringRef resourceLoadStatisticsRepresentation, void* functionContext);
+WK_EXPORT void WKWebsiteDataStoreDumpResourceLoadStatistics(WKWebsiteDataStoreRef dataStoreRef, void* context, WKWebsiteDataStoreDumpResourceLoadStatisticsFunction callback);
 typedef void (*WKWebsiteDataStoreIsStatisticsPrevalentResourceFunction)(bool isPrevalentResource, void* functionContext);
 WK_EXPORT void WKWebsiteDataStoreIsStatisticsPrevalentResource(WKWebsiteDataStoreRef dataStoreRef, WKStringRef host, void* context, WKWebsiteDataStoreIsStatisticsPrevalentResourceFunction callback);
 WK_EXPORT void WKWebsiteDataStoreIsStatisticsVeryPrevalentResource(WKWebsiteDataStoreRef dataStoreRef, WKStringRef host, void* context, WKWebsiteDataStoreIsStatisticsPrevalentResourceFunction callback);
index 17ce7bd..b545adf 100644 (file)
@@ -671,6 +671,17 @@ void ResourceLoadStatisticsMemoryStore::setPrevalentResource(WebCore::ResourceLo
         mapEntry->value.isPrevalentResource = true;
     }
 }
+    
+String ResourceLoadStatisticsMemoryStore::dumpResourceLoadStatistics() const
+{
+    ASSERT(!RunLoop::isMain());
+
+    StringBuilder result;
+    result.appendLiteral("Resource load statistics:\n\n");
+    for (auto& mapEntry : m_resourceStatisticsMap.values())
+        result.append(mapEntry.toString());
+    return result.toString();
+}
 
 bool ResourceLoadStatisticsMemoryStore::isPrevalentResource(const String& primaryDomain) const
 {
index b79f0fe..634b82d 100644 (file)
@@ -83,6 +83,7 @@ public:
     bool isRegisteredAsRedirectingTo(const String& hostRedirectedFromPrimaryDomain, const String& hostRedirectedToPrimaryDomain) const;
 
     void clearPrevalentResource(const String& primaryDomain);
+    String dumpResourceLoadStatistics() const;
     bool isPrevalentResource(const String& primaryDomain) const;
     bool isVeryPrevalentResource(const String& primaryDomain) const;
     void setPrevalentResource(const String& primaryDomain);
index 6cd9dc4..6c5089a 100644 (file)
@@ -493,6 +493,18 @@ void WebResourceLoadStatisticsStore::setVeryPrevalentResource(const URL& url, Co
         postTaskReply(WTFMove(completionHandler));
     });
 }
+    
+void WebResourceLoadStatisticsStore::dumpResourceLoadStatistics(CompletionHandler<void(const String&)>&& completionHandler)
+{
+    ASSERT(RunLoop::isMain());
+
+    postTask([this, completionHandler = WTFMove(completionHandler)] () mutable {
+        String result = m_memoryStore ? m_memoryStore->dumpResourceLoadStatistics() : emptyString();
+        postTaskReply([result = result.isolatedCopy(), completionHandler = WTFMove(completionHandler)] () mutable {
+            completionHandler(result);
+        });
+    });
+}
 
 void WebResourceLoadStatisticsStore::isPrevalentResource(const URL& url, CompletionHandler<void (bool)>&& completionHandler)
 {
index fb24408..f0f50a8 100644 (file)
@@ -91,6 +91,7 @@ public:
     void setLastSeen(const WebCore::URL&, Seconds, CompletionHandler<void()>&&);
     void setPrevalentResource(const WebCore::URL&, CompletionHandler<void()>&&);
     void setVeryPrevalentResource(const WebCore::URL&, CompletionHandler<void()>&&);
+    void dumpResourceLoadStatistics(CompletionHandler<void(const String&)>&&);
     void isPrevalentResource(const WebCore::URL&, CompletionHandler<void(bool)>&&);
     void isVeryPrevalentResource(const WebCore::URL&, CompletionHandler<void(bool)>&&);
     void isRegisteredAsSubresourceUnder(const WebCore::URL& subresource, const WebCore::URL& topFrame, CompletionHandler<void(bool)>&&);
index 46133fc..4607bcb 100644 (file)
@@ -1,3 +1,27 @@
+2018-09-05  Woodrow Wang  <woodrow_wang@apple.com>
+
+        Add infrastructure to dump resource load statistics
+        https://bugs.webkit.org/show_bug.cgi?id=189213
+
+        Reviewed by Daniel Bates.
+
+        The dumping functionality is not currently used, but will be included in tests for
+        <https://bugs.webkit.org/show_bug.cgi?id=187773>.
+
+        * WebKitTestRunner/InjectedBundle/Bindings/TestRunner.idl:
+        * WebKitTestRunner/InjectedBundle/TestRunner.cpp:
+        (WTR::TestRunner::setDumpResourceLoadStatistics):
+        * WebKitTestRunner/InjectedBundle/TestRunner.h:
+        * WebKitTestRunner/TestController.cpp:
+        (WTR::resourceStatisticsStringResultCallback):
+        (WTR::TestController::dumpResourceLoadStatistics):
+        * WebKitTestRunner/TestController.h:
+        * WebKitTestRunner/TestInvocation.cpp:
+        (WTR::TestInvocation::dumpResults):
+        (WTR::TestInvocation::didReceiveSynchronousMessageFromInjectedBundle):
+        (WTR::TestInvocation::setDumpResourceLoadStatistics):
+        * WebKitTestRunner/TestInvocation.h:
+
 2018-09-05  Wenson Hsieh  <wenson_hsieh@apple.com>
 
         [macOS] DragAndDropTests.ExposeMultipleURLsInDataTransfer fails on macOS versions prior to Mojave
index 2981aba..227bf10 100644 (file)
@@ -56,6 +56,7 @@ interface TestRunner {
     void dumpDatabaseCallbacks();
     void dumpDOMAsWebArchive();
     void dumpPolicyDelegateCallbacks();
+    void dumpResourceLoadStatistics();
 
     void clearDOMCaches();
     void clearDOMCache(DOMString origin);
index 344d5af..a3ed22b 100644 (file)
@@ -1462,6 +1462,12 @@ void TestRunner::statisticsCallDidSetVeryPrevalentResourceCallback()
 {
     callTestRunnerCallback(SetStatisticsVeryPrevalentResourceCallbackID);
 }
+    
+void TestRunner::dumpResourceLoadStatistics()
+{
+    WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("dumpResourceLoadStatistics"));
+    WKBundlePagePostSynchronousMessageForTesting(InjectedBundle::singleton().page()->page(), messageName.get(), nullptr, nullptr);
+}
 
 bool TestRunner::isStatisticsPrevalentResource(JSStringRef hostName)
 {
index bfd928e..95d596a 100644 (file)
@@ -91,6 +91,7 @@ public:
     void dumpDatabaseCallbacks() { m_dumpDatabaseCallbacks = true; }
     void dumpDOMAsWebArchive() { setWhatToDump(WhatToDump::DOMAsWebArchive); }
     void dumpPolicyDelegateCallbacks() { m_dumpPolicyCallbacks = true; }
+    void dumpResourceLoadStatistics();
 
     void setShouldDumpFrameLoadCallbacks(bool value);
     void setShouldDumpProgressFinishedCallback(bool value) { m_dumpProgressFinishedCallback = value; }
index cff8d6a..9ee06be 100644 (file)
@@ -2816,7 +2816,16 @@ struct ResourceStatisticsCallbackContext {
     TestController& testController;
     bool done { false };
     bool result { false };
+    WKRetainPtr<WKStringRef> resourceLoadStatisticsRepresentation;
 };
+    
+static void resourceStatisticsStringResultCallback(WKStringRef resourceLoadStatisticsRepresentation, void* userData)
+{
+    auto* context = static_cast<ResourceStatisticsCallbackContext*>(userData);
+    context->resourceLoadStatisticsRepresentation = resourceLoadStatisticsRepresentation;
+    context->done = true;
+    context->testController.notifyDone();
+}
 
 static void resourceStatisticsVoidResultCallback(void* userData)
 {
@@ -2877,6 +2886,15 @@ void TestController::setStatisticsVeryPrevalentResource(WKStringRef host, bool v
     runUntil(context.done, noTimeout);
     m_currentInvocation->didSetVeryPrevalentResource();
 }
+    
+String TestController::dumpResourceLoadStatistics()
+{
+    auto* dataStore = WKContextGetWebsiteDataStore(platformContext());
+    ResourceStatisticsCallbackContext context(*this);
+    WKWebsiteDataStoreDumpResourceLoadStatistics(dataStore, &context, resourceStatisticsStringResultCallback);
+    runUntil(context.done, noTimeout);
+    return toWTFString(context.resourceLoadStatisticsRepresentation.get());
+}
 
 bool TestController::isStatisticsPrevalentResource(WKStringRef host)
 {
index cf75d66..a1fe510 100644 (file)
@@ -191,6 +191,7 @@ public:
     void setStatisticsLastSeen(WKStringRef hostName, double seconds);
     void setStatisticsPrevalentResource(WKStringRef hostName, bool value);
     void setStatisticsVeryPrevalentResource(WKStringRef hostName, bool value);
+    String dumpResourceLoadStatistics();
     bool isStatisticsPrevalentResource(WKStringRef hostName);
     bool isStatisticsVeryPrevalentResource(WKStringRef hostName);
     bool isStatisticsRegisteredAsSubresourceUnder(WKStringRef subresourceHost, WKStringRef topFrameHost);
index 13b21be..550f9d3 100644 (file)
@@ -250,6 +250,9 @@ void TestInvocation::forceRepaintDoneCallback(WKErrorRef error, void* context)
 
 void TestInvocation::dumpResults()
 {
+    if (m_shouldDumpResourceLoadStatistics)
+        m_textOutput.append(m_savedResourceLoadStatistics.isNull() ? TestController::singleton().dumpResourceLoadStatistics() : m_savedResourceLoadStatistics);
+
     if (m_textOutput.length() || !m_audioResult)
         dump(m_textOutput.toString().utf8().data());
     else
@@ -1069,6 +1072,11 @@ WKRetainPtr<WKTypeRef> TestInvocation::didReceiveSynchronousMessageFromInjectedB
         return nullptr;
     }
     
+    if (WKStringIsEqualToUTF8CString(messageName, "dumpResourceLoadStatistics")) {
+        dumpResourceLoadStatistics();
+        return nullptr;
+    }
+
     if (WKStringIsEqualToUTF8CString(messageName, "IsStatisticsPrevalentResource")) {
         ASSERT(WKGetTypeID(messageBody) == WKStringGetTypeID());
 
@@ -1351,6 +1359,8 @@ WKRetainPtr<WKTypeRef> TestInvocation::didReceiveSynchronousMessageFromInjectedB
     }
     
     if (WKStringIsEqualToUTF8CString(messageName, "StatisticsResetToConsistentState")) {
+        if (m_shouldDumpResourceLoadStatistics)
+            m_savedResourceLoadStatistics = TestController::singleton().dumpResourceLoadStatistics();
         TestController::singleton().statisticsResetToConsistentState();
         return nullptr;
     }
@@ -1580,4 +1590,9 @@ void TestInvocation::didRemoveAllSessionCredentials()
     WKPagePostMessageToInjectedBundle(TestController::singleton().mainWebView()->page(), messageName.get(), 0);
 }
 
+void TestInvocation::dumpResourceLoadStatistics()
+{
+    m_shouldDumpResourceLoadStatistics = true;
+}
+
 } // namespace WTR
index 3c050ed..3fb4bb7 100644 (file)
@@ -83,6 +83,8 @@ public:
 
     void didRemoveAllSessionCredentials();
     
+    void dumpResourceLoadStatistics();
+    
 private:
     WKRetainPtr<WKMutableDictionaryRef> createTestSettingsDictionary();
 
@@ -129,9 +131,11 @@ private:
     bool m_dumpFrameLoadCallbacks { false };
     bool m_dumpPixels { false };
     bool m_pixelResultIsPending { false };
+    bool m_shouldDumpResourceLoadStatistics { false };
     WhatToDump m_whatToDump { WhatToDump::RenderTree };
 
     StringBuilder m_textOutput;
+    String m_savedResourceLoadStatistics;
     WKRetainPtr<WKDataRef> m_audioResult;
     WKRetainPtr<WKImageRef> m_pixelResult;
     WKRetainPtr<WKArrayRef> m_repaintRects;