Roll src/third_party/WebKit 1cd8cc5:4e01097 (svn 202507:202508)
[chromium-blink-merge.git] / chrome / browser / load_library_perf_test.cc
blob7f400b41d46872502176ed07be0e177ba0427d1e
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/files/file_path.h"
6 #include "base/files/file_util.h"
7 #include "base/path_service.h"
8 #include "base/scoped_native_library.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/time/time.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "testing/perf/perf_test.h"
13 #include "widevine_cdm_version.h" // In SHARED_INTERMEDIATE_DIR.
15 // Measures the size (bytes) and time to load (sec) of a native library.
16 void MeasureSizeAndTimeToLoadNativeLibrary(const base::FilePath& library_name) {
17 base::FilePath output_dir;
18 ASSERT_TRUE(PathService::Get(base::DIR_MODULE, &output_dir));
19 base::FilePath library_path = output_dir.Append(library_name);
20 ASSERT_TRUE(base::PathExists(library_path)) << library_path.value();
22 int64 size = 0;
23 ASSERT_TRUE(base::GetFileSize(library_path, &size));
24 perf_test::PrintResult("library_size",
25 "",
26 library_name.AsUTF8Unsafe(),
27 static_cast<size_t>(size),
28 "bytes",
29 true);
31 base::NativeLibraryLoadError error;
32 base::TimeTicks start = base::TimeTicks::Now();
33 base::NativeLibrary native_library =
34 base::LoadNativeLibrary(library_path, &error);
35 double delta = (base::TimeTicks::Now() - start).InMillisecondsF();
36 ASSERT_TRUE(native_library) << "Error loading library: " << error.ToString();
37 base::UnloadNativeLibrary(native_library);
38 perf_test::PrintResult("time_to_load_library",
39 "",
40 library_name.AsUTF8Unsafe(),
41 delta,
42 "ms",
43 true);
46 // Use the base name of the library to dynamically get the platform specific
47 // name. See base::GetNativeLibraryName() for details.
48 void MeasureSizeAndTimeToLoadNativeLibraryByBaseName(
49 const std::string& base_library_name) {
50 MeasureSizeAndTimeToLoadNativeLibrary(base::FilePath::FromUTF16Unsafe(
51 base::GetNativeLibraryName(base::ASCIIToUTF16(base_library_name))));
54 #if defined(ENABLE_PEPPER_CDMS)
55 #if defined(WIDEVINE_CDM_AVAILABLE)
56 TEST(LoadCDMPerfTest, Widevine) {
57 MeasureSizeAndTimeToLoadNativeLibrary(
58 base::FilePath::FromUTF8Unsafe(kWidevineCdmFileName));
61 TEST(LoadCDMPerfTest, WidevineAdapter) {
62 MeasureSizeAndTimeToLoadNativeLibrary(
63 base::FilePath::FromUTF8Unsafe(kWidevineCdmAdapterFileName));
65 #endif // defined(WIDEVINE_CDM_AVAILABLE)
67 TEST(LoadCDMPerfTest, ExternalClearKey) {
68 #if defined(OS_MACOSX)
69 MeasureSizeAndTimeToLoadNativeLibrary(
70 base::FilePath::FromUTF8Unsafe("libclearkeycdm.dylib"));
71 #else
72 MeasureSizeAndTimeToLoadNativeLibraryByBaseName("clearkeycdm");
73 #endif // defined(OS_MACOSX)
76 TEST(LoadCDMPerfTest, ExternalClearKeyAdapter) {
77 #if defined(OS_MACOSX)
78 MeasureSizeAndTimeToLoadNativeLibrary(
79 base::FilePath::FromUTF8Unsafe("clearkeycdmadapter.plugin"));
80 #else
81 MeasureSizeAndTimeToLoadNativeLibraryByBaseName("clearkeycdmadapter");
82 #endif // defined(OS_MACOSX)
84 #endif // defined(ENABLE_PEPPER_CDMS)