Bug 1662123 - Measure the time it takes to initialize FOG. r=chutten

Differential Revision: https://phabricator.services.mozilla.com/D90850
This commit is contained in:
Jan-Erik Rediger 2020-09-28 10:09:28 +00:00
Родитель 375f13589a
Коммит c503da69ce
11 изменённых файлов: 121 добавлений и 27 удалений

8
Cargo.lock сгенерированный
Просмотреть файл

@ -1546,6 +1546,13 @@ dependencies = [
"xpcom",
]
[[package]]
name = "fog-gtest"
version = "0.1.0"
dependencies = [
"fog",
]
[[package]]
name = "fog_control"
version = "0.1.0"
@ -1945,6 +1952,7 @@ name = "gkrust-gtest"
version = "0.1.0"
dependencies = [
"bench-collections-gtest",
"fog-gtest",
"gecko-fuzz-targets",
"gkrust-shared",
"mozglue-static",

Просмотреть файл

@ -9,7 +9,7 @@ An often-overlooked first line of testing is "what do the logs say?".
To turn on logging for FOG, use any of the following:
* Run Firefox with `RUST_LOG="fog_control,fog,glean_core"`.
* On some platforms this will use terminal colours to indicate log level.
* Run Firefox with `MOZ_LOG="timestamp,glean::*:5,fog::*:5,glean_core::*:5"`.
* Run Firefox with `MOZ_LOG="timestamp,glean::*:5,fog::*:5,fog_control::*:5,glean_core::*:5"`.
* Set the following prefs:
* `logging.config.timestamp` to `true`
* `logging.fog_control::*` to `5`
@ -79,6 +79,12 @@ Because Gecko symbols aren't built for the
any test that is written for code that uses Gecko symbols should be written as a
[`gtest`](https://github.com/google/googletest)
in `toolkit/components/glean/gtest/`.
You can write the actual test code in Rust.
It needs to be accompanied by a C++ GTest that calls a C FFI-exported Rust function.
See [Testing & Debugging Rust Code](/testing-rust-code/) for more.
See [`toolkit/components/glean/gtest/TestFog.cpp`](https://searchfox.org/mozilla-central/source/toolkit/components/glean/gtest/TestFog.cpp)
and [`toolkit/components/glean/gtest/test.rs`](https://searchfox.org/mozilla-central/source/toolkit/components/glean/gtest/test.rs)
for an example.
By necessity these can only be integration tests against the compiled crate.

Просмотреть файл

@ -0,0 +1,12 @@
[package]
name = "fog-gtest"
version = "0.1.0"
authors = ["glean-team@mozilla.com"]
license = "MPL-2.0"
description = "Tests for the FOG crate"
[dependencies]
fog = { path = "../api" }
[lib]
path = "test.rs"

Просмотреть файл

@ -0,0 +1,26 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "gtest/gtest.h"
#include "nsString.h"
extern "C" {
// This function is called by the rust code in test.rs if a non-fatal test
// failure occurs.
void GTest_FOG_ExpectFailure(const char* aMessage) {
EXPECT_STREQ(aMessage, "");
}
nsresult fog_init();
}
// Initialize FOG exactly once.
// This needs to be the first test to run!
TEST(FOG, FogInitDoesntCrash)
{ ASSERT_EQ(NS_OK, fog_init()); }
extern "C" void Rust_MeasureInitializeTime();
TEST(FOG, TestMeasureInitializeTime)
{ Rust_MeasureInitializeTime(); }

Просмотреть файл

@ -1,25 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "gtest/gtest.h"
#include "nsString.h"
extern "C" {
nsresult fog_init(const nsACString* dataPath, const nsACString* buildId,
const nsACString* appDisplayVersion, const char* channel,
const nsACString* osVersion, const nsACString* architecture);
}
TEST(FOG, FogInitDoesntCrash)
{
nsAutoCString dataPath;
nsAutoCString buildID;
nsAutoCString appVersion;
const char* channel = "";
nsAutoCString osVersion;
nsAutoCString architecture;
ASSERT_EQ(NS_OK, fog_init(&dataPath, &buildID, &appVersion, channel,
&osVersion, &architecture));
}

Просмотреть файл

@ -6,7 +6,7 @@
if CONFIG['MOZ_GLEAN']:
UNIFIED_SOURCES += [
'TestFogInit.cpp',
'TestFog.cpp',
'TestFogIPC.cpp',
]

Просмотреть файл

@ -0,0 +1,44 @@
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
fn nonfatal_fail(msg: String) {
extern "C" {
fn GTest_FOG_ExpectFailure(message: *const ::std::os::raw::c_char);
}
unsafe {
let msg = ::std::ffi::CString::new(msg).unwrap();
GTest_FOG_ExpectFailure(msg.as_ptr());
}
}
/// This macro checks if the expression evaluates to true,
/// and causes a non-fatal GTest test failure if it doesn't.
macro_rules! expect {
($x:expr) => {
match (&$x) {
true => {}
false => nonfatal_fail(format!(
"check failed: (`{}`) at {}:{}",
stringify!($x),
file!(),
line!()
)),
}
};
}
#[no_mangle]
pub extern "C" fn Rust_MeasureInitializeTime() {
// At this point FOG is already initialized.
// We still need for it to finish, as it is running in a separate thread.
let metric = &*fog::metrics::fog::initialization;
while metric.test_get_value("metrics").is_none() {
// We _know_ this value is recorded early, so let's just yield
// and try again quickly.
std::thread::yield_now();
}
let value = metric.test_get_value("metrics").unwrap();
expect!(value > 0);
}

Просмотреть файл

@ -13,3 +13,20 @@
---
$schema: moz://mozilla.org/schemas/glean/metrics/1-0-0
fog:
initialization:
type: timespan
time_unit: nanosecond
description: |
Time the FOG initialization takes.
bugs:
- https://bugzilla.mozilla.org/show_bug.cgi?id=1662123
data_reviews:
- https://bugzilla.mozilla.org/show_bug.cgi?id=1662123#c3
data_sensitivity:
- technical
notification_emails:
- jrediger@mozilla.com
- glean-team@mozilla.com
expires: never

Просмотреть файл

@ -49,6 +49,8 @@ mod core_metrics;
/// Glean instance.
#[no_mangle]
pub unsafe extern "C" fn fog_init() -> nsresult {
fog::metrics::fog::initialization.start();
log::debug!("Initializing FOG.");
let data_path = match get_data_path() {
@ -119,6 +121,8 @@ pub unsafe extern "C" fn fog_init() -> nsresult {
if let Err(e) = fog::flush_init() {
log::error!("Failed to flush pre-init buffer due to {:?}", e);
}
fog::metrics::fog::initialization.stop();
});
}

Просмотреть файл

@ -43,6 +43,7 @@ nsstring-gtest = { path = "../../../../xpcom/rust/gtest/nsstring" }
xpcom-gtest = { path = "../../../../xpcom/rust/gtest/xpcom" }
gkrust-shared = { path = "../../rust/shared" }
gecko-fuzz-targets = { path = "../../../../tools/fuzzing/rust", optional = true }
fog-gtest = { path = "../../../components/glean/gtest" }
# Workarounds for https://github.com/rust-lang/rust/issues/58393
mozglue-static = { path = "../../../../mozglue/static/rust" }

Просмотреть файл

@ -3,6 +3,7 @@
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
extern crate bench_collections_gtest;
extern crate fog_gtest;
#[cfg(feature = "libfuzzer")]
extern crate gecko_fuzz_targets;
extern crate gkrust_shared;