Title: [219624] trunk
Revision
219624
Author
[email protected]
Date
2017-07-18 13:21:31 -0700 (Tue, 18 Jul 2017)

Log Message

Web Inspector: Add memoryCost to Inspector Protocol objects
https://bugs.webkit.org/show_bug.cgi?id=174478

Reviewed by Joseph Pecoraro.

Source/_javascript_Core:

For non-array and non-object InspectorValue, calculate memoryCost as the sizeof the object,
plus the memoryCost of the data if it is a string.

For array InspectorValue, calculate memoryCost as the sum of the memoryCost of all items.

For object InspectorValue, calculate memoryCost as the sum of the memoryCost of the string
key plus the memoryCost of the InspectorValue for each entry.

Test: TestWebKitAPI/Tests/_javascript_Core/InspectorValue.cpp

* inspector/InspectorValues.h:
* inspector/InspectorValues.cpp:
(Inspector::InspectorValue::memoryCost):
(Inspector::InspectorObjectBase::memoryCost):
(Inspector::InspectorArrayBase::memoryCost):

Tools:

* TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
* TestWebKitAPI/Tests/_javascript_Core/InspectorValue.cpp: Added.
(TestWebKitAPI::TEST(InspectorValue, MemoryCostNull)):
(TestWebKitAPI::TEST(InspectorValue, MemoryCostBoolean)):
(TestWebKitAPI::TEST(InspectorValue, MemoryCostDouble)):
(TestWebKitAPI::TEST(InspectorValue, MemoryCostInteger)):
(TestWebKitAPI::TEST(InspectorValue, MemoryCostString)):
(TestWebKitAPI::TEST(InspectorValue, MemoryCostStringEmpty)):
(TestWebKitAPI::TEST(InspectorValue, MemoryCostStringNull)):
(TestWebKitAPI::TEST(InspectorValue, MemoryCostStringGrowing)):
(TestWebKitAPI::TEST(InspectorValue, MemoryCostStringUnicode)):
(TestWebKitAPI::TEST(InspectorValue, MemoryCostObject)):
(TestWebKitAPI::TEST(InspectorValue, MemoryCostObjectEmpty)):
(TestWebKitAPI::TEST(InspectorValue, MemoryCostObjectGrowing)):
(TestWebKitAPI::TEST(InspectorValue, MemoryCostArray)):
(TestWebKitAPI::TEST(InspectorValue, MemoryCostArrayEmpty)):
(TestWebKitAPI::TEST(InspectorValue, MemoryCostArrayGrowing)):

Modified Paths

Added Paths

Diff

Modified: trunk/Source/_javascript_Core/ChangeLog (219623 => 219624)


--- trunk/Source/_javascript_Core/ChangeLog	2017-07-18 20:14:31 UTC (rev 219623)
+++ trunk/Source/_javascript_Core/ChangeLog	2017-07-18 20:21:31 UTC (rev 219624)
@@ -1,3 +1,26 @@
+2017-07-18  Devin Rousso  <[email protected]>
+
+        Web Inspector: Add memoryCost to Inspector Protocol objects
+        https://bugs.webkit.org/show_bug.cgi?id=174478
+
+        Reviewed by Joseph Pecoraro.
+
+        For non-array and non-object InspectorValue, calculate memoryCost as the sizeof the object,
+        plus the memoryCost of the data if it is a string.
+
+        For array InspectorValue, calculate memoryCost as the sum of the memoryCost of all items.
+
+        For object InspectorValue, calculate memoryCost as the sum of the memoryCost of the string
+        key plus the memoryCost of the InspectorValue for each entry.
+
+        Test: TestWebKitAPI/Tests/_javascript_Core/InspectorValue.cpp
+
+        * inspector/InspectorValues.h:
+        * inspector/InspectorValues.cpp:
+        (Inspector::InspectorValue::memoryCost):
+        (Inspector::InspectorObjectBase::memoryCost):
+        (Inspector::InspectorArrayBase::memoryCost):
+
 2017-07-18  Andy Estes  <[email protected]>
 
         [Xcode] Enable CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING

Modified: trunk/Source/_javascript_Core/inspector/InspectorValues.cpp (219623 => 219624)


--- trunk/Source/_javascript_Core/inspector/InspectorValues.cpp	2017-07-18 20:14:31 UTC (rev 219623)
+++ trunk/Source/_javascript_Core/inspector/InspectorValues.cpp	2017-07-18 20:21:31 UTC (rev 219624)
@@ -681,6 +681,14 @@
     }
 }
 
+size_t InspectorValue::memoryCost() const
+{
+    size_t memoryCost = sizeof(this);
+    if (m_type == Type::String && m_value.string)
+        memoryCost += m_value.string->sizeInBytes();
+    return memoryCost;
+}
+
 InspectorObjectBase::~InspectorObjectBase()
 {
 }
@@ -700,6 +708,17 @@
     return static_cast<InspectorObject*>(this);
 }
 
+size_t InspectorObjectBase::memoryCost() const
+{
+    size_t memoryCost = InspectorValue::memoryCost();
+    for (const auto& entry : m_map) {
+        memoryCost += entry.key.sizeInBytes();
+        if (entry.value)
+            memoryCost += entry.value->memoryCost();
+    }
+    return memoryCost;
+}
+
 bool InspectorObjectBase::getBoolean(const String& name, bool& output) const
 {
     RefPtr<InspectorValue> value;
@@ -818,4 +837,14 @@
     return adoptRef(*new InspectorArray);
 }
 
+size_t InspectorArrayBase::memoryCost() const
+{
+    size_t memoryCost = InspectorValue::memoryCost();
+    for (const auto& item : m_map) {
+        if (item)
+            memoryCost += item->memoryCost();
+    }
+    return memoryCost;
+}
+
 } // namespace Inspector

Modified: trunk/Source/_javascript_Core/inspector/InspectorValues.h (219623 => 219624)


--- trunk/Source/_javascript_Core/inspector/InspectorValues.h	2017-07-18 20:14:31 UTC (rev 219623)
+++ trunk/Source/_javascript_Core/inspector/InspectorValues.h	2017-07-18 20:21:31 UTC (rev 219624)
@@ -1,6 +1,7 @@
 /*
  * Copyright (C) 2009 Google Inc. All rights reserved.
  * Copyright (C) 2014 University of Washington. All rights reserved.
+ * Copyright (C) 2017 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
@@ -108,6 +109,8 @@
     String toJSONString() const;
     virtual void writeJSON(StringBuilder& output) const;
 
+    virtual size_t memoryCost() const;
+
 protected:
     InspectorValue()
         : m_type(Type::Null) { }
@@ -169,6 +172,8 @@
 
     InspectorObject* openAccessors();
 
+    size_t memoryCost() const final;
+
 protected:
     virtual ~InspectorObjectBase();
 
@@ -235,6 +240,7 @@
 
     using InspectorObjectBase::asObject;
 
+    // This class expected non-cyclic values, as we cannot serialize cycles in JSON.
     using InspectorObjectBase::setBoolean;
     using InspectorObjectBase::setInteger;
     using InspectorObjectBase::setDouble;
@@ -268,6 +274,8 @@
 
     unsigned length() const { return static_cast<unsigned>(m_map.size()); }
 
+    size_t memoryCost() const final;
+
 protected:
     virtual ~InspectorArrayBase();
 
@@ -303,6 +311,7 @@
 
     using InspectorArrayBase::asArray;
 
+    // This class expected non-cyclic values, as we cannot serialize cycles in JSON.
     using InspectorArrayBase::pushBoolean;
     using InspectorArrayBase::pushInteger;
     using InspectorArrayBase::pushDouble;

Modified: trunk/Tools/ChangeLog (219623 => 219624)


--- trunk/Tools/ChangeLog	2017-07-18 20:14:31 UTC (rev 219623)
+++ trunk/Tools/ChangeLog	2017-07-18 20:21:31 UTC (rev 219624)
@@ -1,3 +1,28 @@
+2017-07-18  Devin Rousso  <[email protected]>
+
+        Web Inspector: Add memoryCost to Inspector Protocol objects
+        https://bugs.webkit.org/show_bug.cgi?id=174478
+
+        Reviewed by Joseph Pecoraro.
+
+        * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
+        * TestWebKitAPI/Tests/_javascript_Core/InspectorValue.cpp: Added.
+        (TestWebKitAPI::TEST(InspectorValue, MemoryCostNull)):
+        (TestWebKitAPI::TEST(InspectorValue, MemoryCostBoolean)):
+        (TestWebKitAPI::TEST(InspectorValue, MemoryCostDouble)):
+        (TestWebKitAPI::TEST(InspectorValue, MemoryCostInteger)):
+        (TestWebKitAPI::TEST(InspectorValue, MemoryCostString)):
+        (TestWebKitAPI::TEST(InspectorValue, MemoryCostStringEmpty)):
+        (TestWebKitAPI::TEST(InspectorValue, MemoryCostStringNull)):
+        (TestWebKitAPI::TEST(InspectorValue, MemoryCostStringGrowing)):
+        (TestWebKitAPI::TEST(InspectorValue, MemoryCostStringUnicode)):
+        (TestWebKitAPI::TEST(InspectorValue, MemoryCostObject)):
+        (TestWebKitAPI::TEST(InspectorValue, MemoryCostObjectEmpty)):
+        (TestWebKitAPI::TEST(InspectorValue, MemoryCostObjectGrowing)):
+        (TestWebKitAPI::TEST(InspectorValue, MemoryCostArray)):
+        (TestWebKitAPI::TEST(InspectorValue, MemoryCostArrayEmpty)):
+        (TestWebKitAPI::TEST(InspectorValue, MemoryCostArrayGrowing)):
+
 2017-07-18  Andy Estes  <[email protected]>
 
         [Xcode] Enable CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING

Modified: trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj (219623 => 219624)


--- trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj	2017-07-18 20:14:31 UTC (rev 219623)
+++ trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj	2017-07-18 20:21:31 UTC (rev 219624)
@@ -376,6 +376,7 @@
 		7CCE7EC11A411A7E00447C4C /* HTMLCollectionNamedItem.mm in Sources */ = {isa = PBXBuildFile; fileRef = 9B4F8FA3159D52B1002D9F94 /* HTMLCollectionNamedItem.mm */; };
 		7CCE7EC21A411A7E00447C4C /* HTMLFormCollectionNamedItem.mm in Sources */ = {isa = PBXBuildFile; fileRef = 9B26FC6B159D061000CC3765 /* HTMLFormCollectionNamedItem.mm */; };
 		7CCE7EC31A411A7E00447C4C /* InspectorBar.mm in Sources */ = {isa = PBXBuildFile; fileRef = C507E8A614C6545B005D6B3B /* InspectorBar.mm */; };
+		95646E5B1F1DB60E00DE0EB9 /* InspectorValue.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 953F47911F1DB40300E3D1E3 /* InspectorValue.cpp */; };
 		7CCE7EC41A411A7E00447C4C /* JSWrapperForNodeInWebFrame.mm in Sources */ = {isa = PBXBuildFile; fileRef = 4BB4160116815B2600824238 /* JSWrapperForNodeInWebFrame.mm */; };
 		7CCE7EC51A411A7E00447C4C /* MemoryCacheDisableWithinResourceLoadDelegate.mm in Sources */ = {isa = PBXBuildFile; fileRef = E1220D9F155B25480013E2FC /* MemoryCacheDisableWithinResourceLoadDelegate.mm */; };
 		7CCE7EC61A411A7E00447C4C /* MemoryCachePruneWithinResourceLoadDelegate.mm in Sources */ = {isa = PBXBuildFile; fileRef = 517E7DFB15110EA600D0B008 /* MemoryCachePruneWithinResourceLoadDelegate.mm */; };
@@ -1538,6 +1539,7 @@
 		C2CF975916CEC69E0054E99D /* JSContextBackForwardCache2.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = JSContextBackForwardCache2.html; sourceTree = "<group>"; };
 		C2EB2DD116CAC7AC009B52EE /* WebViewDidCreateJavaScriptContext.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = WebViewDidCreateJavaScriptContext.mm; sourceTree = "<group>"; };
 		C507E8A614C6545B005D6B3B /* InspectorBar.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = InspectorBar.mm; sourceTree = "<group>"; };
+		953F47911F1DB40300E3D1E3 /* InspectorValue.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = InspectorValue.cpp; sourceTree = "<group>"; };
 		C5101C4E176B8BB900EE9B15 /* findRanges.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = findRanges.html; sourceTree = "<group>"; };
 		C51AFB98169F49FF009CCF66 /* FindMatches.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = FindMatches.mm; sourceTree = "<group>"; };
 		C540F775152E4DA000A40C8C /* SimplifyMarkup.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = SimplifyMarkup.mm; sourceTree = "<group>"; };
@@ -2721,6 +2723,7 @@
 		FE217ECB1640A54A0052988B /* _javascript_Core */ = {
 			isa = PBXGroup;
 			children = (
+				953F47911F1DB40300E3D1E3 /* InspectorValue.cpp */,
 			);
 			path = _javascript_Core;
 			sourceTree = "<group>";
@@ -3103,6 +3106,7 @@
 				3FCC4FE51EC4E8520076E37C /* PictureInPictureDelegate.mm in Sources */,
 				7C83E0B81D0A64BD00FEBCF3 /* InjectedBundleMakeAllShadowRootsOpen.cpp in Sources */,
 				7CCE7EC31A411A7E00447C4C /* InspectorBar.mm in Sources */,
+				95646E5B1F1DB60E00DE0EB9 /* InspectorValue.cpp in Sources */,
 				7CCE7EDA1A411A8700447C4C /* InstanceMethodSwizzler.mm in Sources */,
 				5C726D6F1D3EE06E00C5E1A1 /* InstanceMethodSwizzler.mm in Sources */,
 				2DB0232F1E4E871800707123 /* InteractionDeadlockAfterCrash.mm in Sources */,

Added: trunk/Tools/TestWebKitAPI/Tests/_javascript_Core/InspectorValue.cpp (0 => 219624)


--- trunk/Tools/TestWebKitAPI/Tests/_javascript_Core/InspectorValue.cpp	                        (rev 0)
+++ trunk/Tools/TestWebKitAPI/Tests/_javascript_Core/InspectorValue.cpp	2017-07-18 20:21:31 UTC (rev 219624)
@@ -0,0 +1,185 @@
+/*
+ * Copyright (C) 2017 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+
+#include "Test.h"
+#include <_javascript_Core/InspectorValues.h>
+
+using namespace Inspector;
+
+namespace TestWebKitAPI {
+
+TEST(InspectorValue, MemoryCostNull)
+{
+    Ref<InspectorValue> value = InspectorValue::null();
+    size_t memoryCost = value->memoryCost();
+    EXPECT_GT(memoryCost, 0U);
+    EXPECT_LE(memoryCost, 8U);
+}
+
+TEST(InspectorValue, MemoryCostBoolean)
+{
+    Ref<InspectorValue> value = InspectorValue::create(true);
+    size_t memoryCost = value->memoryCost();
+    EXPECT_GT(memoryCost, 0U);
+    EXPECT_LE(memoryCost, 8U);
+}
+
+TEST(InspectorValue, MemoryCostDouble)
+{
+    Ref<InspectorValue> value = InspectorValue::create(1.0);
+    size_t memoryCost = value->memoryCost();
+    EXPECT_GT(memoryCost, 0U);
+    EXPECT_LE(memoryCost, 8U);
+}
+
+TEST(InspectorValue, MemoryCostInteger)
+{
+    Ref<InspectorValue> value = InspectorValue::create(1);
+    size_t memoryCost = value->memoryCost();
+    EXPECT_GT(memoryCost, 0U);
+    EXPECT_LE(memoryCost, 8U);
+}
+
+TEST(InspectorValue, MemoryCostString)
+{
+    Ref<InspectorValue> value = InspectorValue::create("test");
+    size_t memoryCost = value->memoryCost();
+    EXPECT_GT(memoryCost, 0U);
+    EXPECT_LE(memoryCost, 36U);
+}
+
+TEST(InspectorValue, MemoryCostStringEmpty)
+{
+    Ref<InspectorValue> value = InspectorValue::create("");
+    size_t memoryCost = value->memoryCost();
+    EXPECT_GT(memoryCost, 0U);
+    EXPECT_LE(memoryCost, 32U);
+}
+
+TEST(InspectorValue, MemoryCostStringNull)
+{
+    Ref<InspectorValue> value = InspectorValue::create(String());
+    size_t memoryCost = value->memoryCost();
+    EXPECT_GT(memoryCost, 0U);
+    EXPECT_LE(memoryCost, 8U);
+}
+
+TEST(InspectorValue, MemoryCostStringGrowing)
+{
+    Ref<InspectorValue> valueA = InspectorValue::create("t");
+    Ref<InspectorValue> valueB = InspectorValue::create("te");
+    Ref<InspectorValue> valueC = InspectorValue::create("tes");
+    Ref<InspectorValue> valueD = InspectorValue::create("test");
+    EXPECT_LT(valueA->memoryCost(), valueB->memoryCost());
+    EXPECT_LT(valueB->memoryCost(), valueC->memoryCost());
+    EXPECT_LT(valueC->memoryCost(), valueD->memoryCost());
+}
+
+TEST(InspectorValue, MemoryCostStringUnicode)
+{
+    Ref<InspectorValue> valueA = InspectorValue::create("t");
+    Ref<InspectorValue> valueB = InspectorValue::create("😀");
+    EXPECT_LT(valueA->memoryCost(), valueB->memoryCost());
+}
+
+TEST(InspectorValue, MemoryCostObject)
+{
+    Ref<InspectorObject> value = InspectorObject::create();
+    value->setValue("test", InspectorValue::null());
+    size_t memoryCost = value->memoryCost();
+    EXPECT_GT(memoryCost, 0U);
+    EXPECT_LE(memoryCost, 20U);
+}
+
+TEST(InspectorValue, MemoryCostObjectEmpty)
+{
+    Ref<InspectorObject> value = InspectorObject::create();
+    size_t memoryCost = value->memoryCost();
+    EXPECT_GT(memoryCost, 0U);
+    EXPECT_LE(memoryCost, 8U);
+}
+
+TEST(InspectorValue, MemoryCostObjectGrowing)
+{
+    Ref<InspectorObject> value = InspectorObject::create();
+
+    value->setValue("1", InspectorValue::null());
+    size_t memoryCost1 = value->memoryCost();
+
+    value->setValue("2", InspectorValue::null());
+    size_t memoryCost2 = value->memoryCost();
+
+    value->setValue("3", InspectorValue::null());
+    size_t memoryCost3 = value->memoryCost();
+
+    value->setValue("4", InspectorValue::null());
+    size_t memoryCost4 = value->memoryCost();
+
+    EXPECT_LT(memoryCost1, memoryCost2);
+    EXPECT_LT(memoryCost2, memoryCost3);
+    EXPECT_LT(memoryCost3, memoryCost4);
+}
+
+TEST(InspectorValue, MemoryCostArray)
+{
+    Ref<InspectorArray> value = InspectorArray::create();
+    value->pushValue(InspectorValue::null());
+    size_t memoryCost = value->memoryCost();
+    EXPECT_GT(memoryCost, 0U);
+    EXPECT_LE(memoryCost, 16U);
+}
+
+TEST(InspectorValue, MemoryCostArrayEmpty)
+{
+    Ref<InspectorArray> value = InspectorArray::create();
+    size_t memoryCost = value->memoryCost();
+    EXPECT_GT(memoryCost, 0U);
+    EXPECT_LE(memoryCost, 8U);
+}
+
+TEST(InspectorValue, MemoryCostArrayGrowing)
+{
+    Ref<InspectorArray> value = InspectorArray::create();
+
+    value->pushValue(InspectorValue::null());
+    size_t memoryCost1 = value->memoryCost();
+
+    value->pushValue(InspectorValue::null());
+    size_t memoryCost2 = value->memoryCost();
+
+    value->pushValue(InspectorValue::null());
+    size_t memoryCost3 = value->memoryCost();
+
+    value->pushValue(InspectorValue::null());
+    size_t memoryCost4 = value->memoryCost();
+
+    EXPECT_LT(memoryCost1, memoryCost2);
+    EXPECT_LT(memoryCost2, memoryCost3);
+    EXPECT_LT(memoryCost3, memoryCost4);
+}
+
+} // namespace TestWebKitAPI
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to