[M120 Migration]Fix for crash during chrome exit
[platform/framework/web/chromium-efl.git] / chrome / browser / load_library_perf_test.cc
1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <stddef.h>
6 #include <stdint.h>
7
8 #include "base/files/file_path.h"
9 #include "base/files/file_util.h"
10 #include "base/native_library.h"
11 #include "base/path_service.h"
12 #include "base/scoped_native_library.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/time/time.h"
15 #include "media/media_buildflags.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "testing/perf/perf_result_reporter.h"
18 #include "third_party/widevine/cdm/buildflags.h"
19 #include "third_party/widevine/cdm/widevine_cdm_common.h"
20
21 #if BUILDFLAG(ENABLE_LIBRARY_CDMS)
22 #include "media/cdm/cdm_paths.h"
23 #include "media/cdm/clear_key_cdm_common.h"
24 #endif
25
26 namespace {
27
28 #if BUILDFLAG(ENABLE_LIBRARY_CDMS)
29
30 constexpr char kMetricLibrarySizeBytes[] = "library_size";
31 constexpr char kMetricTimeToLoadLibraryMs[] = "time_to_load_library";
32
33 perf_test::PerfResultReporter SetUpReporter(const std::string& story) {
34   perf_test::PerfResultReporter reporter("", story);
35   reporter.RegisterImportantMetric(kMetricLibrarySizeBytes, "bytes");
36   reporter.RegisterImportantMetric(kMetricTimeToLoadLibraryMs, "ms");
37   return reporter;
38 }
39
40 // Measures the size (bytes) and time to load (sec) of a native library.
41 // |library_relative_dir| is the relative path based on DIR_MODULE.
42 void MeasureSizeAndTimeToLoadNativeLibrary(
43     const base::FilePath& library_relative_dir,
44     const base::FilePath& library_name) {
45   // External ClearKey is a loadable_module used only in tests, and the Widevine
46   // CDM is copied to the output directory. Both can be considered generated
47   // test data even though one is production code.
48   base::FilePath output_dir;
49   ASSERT_TRUE(
50       base::PathService::Get(base::DIR_OUT_TEST_DATA_ROOT, &output_dir));
51   output_dir = output_dir.Append(library_relative_dir);
52   base::FilePath library_path = output_dir.Append(library_name);
53   ASSERT_TRUE(base::PathExists(library_path)) << library_path.value();
54
55   int64_t size = 0;
56   ASSERT_TRUE(base::GetFileSize(library_path, &size));
57   auto reporter = SetUpReporter(library_name.AsUTF8Unsafe());
58   reporter.AddResult(kMetricLibrarySizeBytes, static_cast<size_t>(size));
59
60   base::NativeLibraryLoadError error;
61   base::TimeTicks start = base::TimeTicks::Now();
62   base::NativeLibrary native_library =
63       base::LoadNativeLibrary(library_path, &error);
64   double delta = (base::TimeTicks::Now() - start).InMillisecondsF();
65   ASSERT_TRUE(native_library) << "Error loading library: " << error.ToString();
66   base::UnloadNativeLibrary(native_library);
67   reporter.AddResult(kMetricTimeToLoadLibraryMs, delta);
68 }
69
70 void MeasureSizeAndTimeToLoadCdm(const std::string& cdm_base_dir,
71                                  const std::string& cdm_name) {
72   MeasureSizeAndTimeToLoadNativeLibrary(
73       media::GetPlatformSpecificDirectory(cdm_base_dir),
74       base::FilePath::FromUTF8Unsafe(cdm_name));
75 }
76
77 #endif  // BUILDFLAG(ENABLE_LIBRARY_CDMS)
78
79 }  // namespace
80
81 #if BUILDFLAG(ENABLE_LIBRARY_CDMS)
82
83 #if BUILDFLAG(ENABLE_WIDEVINE)
84 TEST(LoadCDMPerfTest, Widevine) {
85   MeasureSizeAndTimeToLoadCdm(
86       kWidevineCdmBaseDirectory,
87       base::GetNativeLibraryName(kWidevineCdmLibraryName));
88 }
89 #endif  // BUILDFLAG(ENABLE_WIDEVINE)
90
91 TEST(LoadCDMPerfTest, ExternalClearKey) {
92   MeasureSizeAndTimeToLoadCdm(
93       media::kClearKeyCdmBaseDirectory,
94       base::GetLoadableModuleName(media::kClearKeyCdmLibraryName));
95 }
96
97 #endif  // BUILDFLAG(ENABLE_LIBRARY_CDMS)