From 5553d8b11634935c34e7774325eda6afd61c8f56 Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Wed, 3 Jan 2018 09:44:28 -0500 Subject: Initialize V8 once in embedder tests This CL moves the initialization of the V8 platform to happen in the GTest environment so it's only run once. This takes the CFXJSE_FormCalcContextEmbedderTest Debug time from ~25s to ~19s on my local machine. Bug: pdfium:928 Change-Id: Ie8c27606721e7056de42e7d9474b0621f1e7212f Reviewed-on: https://pdfium-review.googlesource.com/22070 Reviewed-by: Tom Sepez Commit-Queue: dsinclair --- BUILD.gn | 1 + testing/embedder_test.cpp | 51 +---------------------- testing/embedder_test.h | 3 -- testing/embedder_test_main.cpp | 94 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 53 deletions(-) create mode 100644 testing/embedder_test_main.cpp diff --git a/BUILD.gn b/BUILD.gn index 0bc8134720..50f83c8dbf 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -2913,6 +2913,7 @@ test("pdfium_embeddertests") { "fpdfsdk/pwl/cpwl_edit_embeddertest.cpp", "testing/embedder_test.cpp", "testing/embedder_test.h", + "testing/embedder_test_main.cpp", "testing/embedder_test_mock_delegate.h", "testing/embedder_test_timer_handling_delegate.h", "testing/fake_file_access.cpp", diff --git a/testing/embedder_test.cpp b/testing/embedder_test.cpp index ccc98b4dcd..baedeb3432 100644 --- a/testing/embedder_test.cpp +++ b/testing/embedder_test.cpp @@ -28,15 +28,6 @@ namespace { -const char* g_exe_path = nullptr; - -#ifdef PDF_ENABLE_V8 -#ifdef V8_USE_EXTERNAL_STARTUP_DATA -v8::StartupData* g_v8_natives = nullptr; -v8::StartupData* g_v8_snapshot = nullptr; -#endif // V8_USE_EXTERNAL_STARTUP_DATA -#endif // PDF_ENABLE_V8 - int GetBitmapBytesPerPixel(FPDF_BITMAP bitmap) { const int format = FPDFBitmap_GetFormat(bitmap); switch (format) { @@ -67,31 +58,11 @@ EmbedderTest::EmbedderTest() memset(&file_access_, 0, sizeof(file_access_)); delegate_ = default_delegate_.get(); -#ifdef PDF_ENABLE_V8 -#ifdef V8_USE_EXTERNAL_STARTUP_DATA - if (g_v8_natives && g_v8_snapshot) { - InitializeV8ForPDFium(g_exe_path, std::string(), nullptr, nullptr, - &platform_); - } else { - g_v8_natives = new v8::StartupData; - g_v8_snapshot = new v8::StartupData; - InitializeV8ForPDFium(g_exe_path, std::string(), g_v8_natives, - g_v8_snapshot, &platform_); - } -#else - InitializeV8ForPDFium(g_exe_path, &platform_); -#endif // V8_USE_EXTERNAL_STARTUP_DATA -#endif // FPDF_ENABLE_V8 FPDF_FILEWRITE::version = 1; FPDF_FILEWRITE::WriteBlock = WriteBlockCallback; } -EmbedderTest::~EmbedderTest() { -#ifdef PDF_ENABLE_V8 - v8::V8::ShutdownPlatform(); - delete platform_; -#endif // PDF_ENABLE_V8 -} +EmbedderTest::~EmbedderTest() {} void EmbedderTest::SetUp() { FPDF_LIBRARY_CONFIG config; @@ -499,23 +470,3 @@ int EmbedderTest::GetBlockFromString(void* param, memcpy(buf, new_file->data() + pos, size); return 1; } - -// Can't use gtest-provided main since we need to stash the path to the -// executable in order to find the external V8 binary data files. -int main(int argc, char** argv) { - g_exe_path = argv[0]; - testing::InitGoogleTest(&argc, argv); - testing::InitGoogleMock(&argc, argv); - int ret_val = RUN_ALL_TESTS(); - -#ifdef PDF_ENABLE_V8 -#ifdef V8_USE_EXTERNAL_STARTUP_DATA - if (g_v8_natives) - free(const_cast(g_v8_natives->data)); - if (g_v8_snapshot) - free(const_cast(g_v8_snapshot->data)); -#endif // V8_USE_EXTERNAL_STARTUP_DATA -#endif // PDF_ENABLE_V8 - - return ret_val; -} diff --git a/testing/embedder_test.h b/testing/embedder_test.h index 03155793c5..8da1643824 100644 --- a/testing/embedder_test.h +++ b/testing/embedder_test.h @@ -167,9 +167,6 @@ class EmbedderTest : public ::testing::Test, FPDF_FORMHANDLE form_handle_; FPDF_AVAIL avail_; FPDF_FILEACCESS file_access_; // must outlive avail_. -#ifdef PDF_ENABLE_V8 - v8::Platform* platform_; -#endif // PDF_ENABLE_V8 void* external_isolate_; TestLoader* loader_; size_t file_length_; diff --git a/testing/embedder_test_main.cpp b/testing/embedder_test_main.cpp new file mode 100644 index 0000000000..ade6118e64 --- /dev/null +++ b/testing/embedder_test_main.cpp @@ -0,0 +1,94 @@ +// Copyright 2018 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include + +#include "core/fxcrt/fx_memory.h" +#include "testing/gmock/include/gmock/gmock.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "testing/test_support.h" + +#ifdef PDF_ENABLE_V8 +#include "v8/include/v8-platform.h" +#include "v8/include/v8.h" +#endif // PDF_ENABLE_v8 + +namespace { + +const char* g_exe_path = nullptr; + +#ifdef PDF_ENABLE_V8 +#ifdef V8_USE_EXTERNAL_STARTUP_DATA +v8::StartupData* g_v8_natives = nullptr; +v8::StartupData* g_v8_snapshot = nullptr; +#endif // V8_USE_EXTERNAL_STARTUP_DATA +#endif // PDF_ENABLE_V8 + +// The loading time of the CFGAS_FontMgr is linear in the number of times it is +// loaded. So, if a test suite has a lot of tests that need a font manager they +// can end up executing very, very slowly. +class Environment : public testing::Environment { + public: + void SetUp() override { +#ifdef PDF_ENABLE_V8 +#ifdef V8_USE_EXTERNAL_STARTUP_DATA + if (g_v8_natives && g_v8_snapshot) { + InitializeV8ForPDFium(g_exe_path, std::string(), nullptr, nullptr, + &platform_); + } else { + g_v8_natives = new v8::StartupData; + g_v8_snapshot = new v8::StartupData; + InitializeV8ForPDFium(g_exe_path, std::string(), g_v8_natives, + g_v8_snapshot, &platform_); + } +#else + InitializeV8ForPDFium(g_exe_path, &platform_); +#endif // V8_USE_EXTERNAL_STARTUP_DATA +#endif // FPDF_ENABLE_V8 + } + + void TearDown() override { +#ifdef PDF_ENABLE_V8 + v8::V8::ShutdownPlatform(); + delete platform_; +#endif // PDF_ENABLE_V8 + } + + private: +#ifdef PDF_ENABLE_V8 + v8::Platform* platform_; +#endif // PDF_ENABLE_V8 +}; + +Environment* env_ = nullptr; + +} // namespace + +// Can't use gtest-provided main since we need to stash the path to the +// executable in order to find the external V8 binary data files. +int main(int argc, char** argv) { + g_exe_path = argv[0]; + + FXMEM_InitializePartitionAlloc(); + + env_ = new Environment(); + // The env will be deleted by gtest. + AddGlobalTestEnvironment(env_); + + testing::InitGoogleTest(&argc, argv); + testing::InitGoogleMock(&argc, argv); + + int ret_val = RUN_ALL_TESTS(); + +#ifdef PDF_ENABLE_V8 +#ifdef V8_USE_EXTERNAL_STARTUP_DATA + if (g_v8_natives) + free(const_cast(g_v8_natives->data)); + if (g_v8_snapshot) + free(const_cast(g_v8_snapshot->data)); +#endif // V8_USE_EXTERNAL_STARTUP_DATA +#endif // PDF_ENABLE_V8 + + return ret_val; +} -- cgit v1.2.3