StyloParsingBench.cpp (4144B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "ExampleStylesheet.h" 8 #include "ReferrerInfo.h" 9 #include "ServoBindings.h" 10 #include "gtest/MozGTestBench.h" 11 #include "gtest/gtest.h" 12 #include "mozilla/Encoding.h" 13 #include "mozilla/NullPrincipal.h" 14 #include "mozilla/Utf8.h" 15 #include "mozilla/css/SheetParsingMode.h" 16 #include "nsCSSValue.h" 17 #include "nsString.h" 18 19 using namespace mozilla; 20 using namespace mozilla::css; 21 using namespace mozilla::dom; 22 using namespace mozilla::net; 23 24 // Bug 1436018 - Disable Stylo microbenchmark on Windows 25 #if !defined(_WIN32) && !defined(_WIN64) 26 27 # define PARSING_REPETITIONS 20 28 # define SETPROPERTY_REPETITIONS (1000 * 1000) 29 # define GETPROPERTY_REPETITIONS (1000 * 1000) 30 31 static void ServoParsingBench() { 32 auto css = AsBytes(MakeStringSpan(EXAMPLE_STYLESHEET)); 33 nsCString cssStr; 34 cssStr.Append(css); 35 ASSERT_EQ(Encoding::UTF8ValidUpTo(css), css.Length()); 36 37 RefPtr<nsIURI> uri = NullPrincipal::CreateURI(); 38 nsCOMPtr<nsIReferrerInfo> referrerInfo = new ReferrerInfo(nullptr); 39 RefPtr<URLExtraData> data = 40 new URLExtraData(uri.forget(), referrerInfo.forget(), 41 NullPrincipal::CreateWithoutOriginAttributes()); 42 for (int i = 0; i < PARSING_REPETITIONS; i++) { 43 RefPtr<StyleStylesheetContents> stylesheet = 44 Servo_StyleSheet_FromUTF8Bytes( 45 nullptr, nullptr, nullptr, &cssStr, eAuthorSheetFeatures, data, 46 eCompatibility_FullStandards, nullptr, StyleAllowImportRules::Yes, 47 StyleSanitizationKind::None, nullptr) 48 .Consume(); 49 } 50 } 51 52 static constexpr auto STYLE_RULE = StyleCssRuleType::Style; 53 54 static void ServoSetPropertyByIdBench(const nsACString& css) { 55 RefPtr<StyleLockedDeclarationBlock> block = 56 Servo_DeclarationBlock_CreateEmpty().Consume(); 57 RefPtr<nsIURI> uri = NullPrincipal::CreateURI(); 58 nsCOMPtr<nsIReferrerInfo> referrerInfo = new ReferrerInfo(nullptr); 59 RefPtr<URLExtraData> data = 60 new URLExtraData(uri.forget(), referrerInfo.forget(), 61 NullPrincipal::CreateWithoutOriginAttributes()); 62 ASSERT_TRUE(IsUtf8(css)); 63 64 for (int i = 0; i < SETPROPERTY_REPETITIONS; i++) { 65 Servo_DeclarationBlock_SetPropertyById( 66 block, eCSSProperty_width, &css, 67 /* is_important = */ false, data, StyleParsingMode::DEFAULT, 68 eCompatibility_FullStandards, nullptr, STYLE_RULE, {}); 69 } 70 } 71 72 static void ServoGetPropertyValueByNonCustomId() { 73 RefPtr<StyleLockedDeclarationBlock> block = 74 Servo_DeclarationBlock_CreateEmpty().Consume(); 75 76 RefPtr<nsIURI> uri = NullPrincipal::CreateURI(); 77 nsCOMPtr<nsIReferrerInfo> referrerInfo = new ReferrerInfo(nullptr); 78 RefPtr<URLExtraData> data = 79 new URLExtraData(uri.forget(), referrerInfo.forget(), 80 NullPrincipal::CreateWithoutOriginAttributes()); 81 constexpr auto css_ = "10px"_ns; 82 const nsACString& css = css_; 83 Servo_DeclarationBlock_SetPropertyById( 84 block, eCSSProperty_width, &css, 85 /* is_important = */ false, data, StyleParsingMode::DEFAULT, 86 eCompatibility_FullStandards, nullptr, STYLE_RULE, {}); 87 88 for (int i = 0; i < GETPROPERTY_REPETITIONS; i++) { 89 nsAutoCString value; 90 Servo_DeclarationBlock_GetPropertyValueByNonCustomId( 91 block, eCSSProperty_width, &value); 92 ASSERT_TRUE(value.EqualsLiteral("10px")); 93 } 94 } 95 96 MOZ_GTEST_BENCH(Stylo, Servo_StyleSheet_FromUTF8Bytes_Bench, 97 [] { ServoParsingBench(); }); 98 99 MOZ_GTEST_BENCH(Stylo, Servo_DeclarationBlock_SetPropertyById_Bench, 100 [] { ServoSetPropertyByIdBench("10px"_ns); }); 101 102 MOZ_GTEST_BENCH(Stylo, 103 Servo_DeclarationBlock_SetPropertyById_WithInitialSpace_Bench, 104 [] { ServoSetPropertyByIdBench(" 10px"_ns); }); 105 106 MOZ_GTEST_BENCH(Stylo, Servo_DeclarationBlock_GetPropertyById_Bench, 107 ServoGetPropertyValueByNonCustomId); 108 109 #endif