Commit fcee1735 authored by Caleb Raitto's avatar Caleb Raitto Committed by Commit Bot

[Cronet] Add metrics util to produce native date objects.

Bug: 879208
Change-Id: Ie57908edc90261202f7bfb82a9963403358df7b8
Reviewed-on: https://chromium-review.googlesource.com/c/1423351
Commit-Queue: Caleb Raitto <caraitto@chromium.org>
Reviewed-by: default avatarMisha Efimov <mef@chromium.org>
Reviewed-by: default avatarPaul Jensen <pauljensen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#635741}
parent ccb3cb18
......@@ -41,6 +41,7 @@ source_set("cronet_native_impl") {
"//base",
"//components/cronet:cronet_common",
"//components/cronet:cronet_version_header",
"//components/cronet:metrics_util",
"//components/grpc_support:grpc_support",
"//net",
]
......@@ -57,6 +58,8 @@ source_set("cronet_native_impl") {
"engine.h",
"io_buffer_with_cronet_buffer.cc",
"io_buffer_with_cronet_buffer.h",
"native_metrics_util.cc",
"native_metrics_util.h",
"runnables.cc",
"runnables.h",
"upload_data_sink.cc",
......@@ -77,6 +80,7 @@ source_set("cronet_native_unittests") {
deps = [
":cronet_native_impl",
"//base/test:test_support",
"//components/cronet/native/test:cronet_native_testutil",
"//net:test_support",
"//testing/gtest",
......@@ -85,6 +89,7 @@ source_set("cronet_native_unittests") {
configs += [ ":cronet_native_include_config" ]
sources = [
"native_metrics_util_test.cc",
"runnables_unittest.cc",
# Generated from cronet.idl.
......
// Copyright 2019 The Chromium 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 "components/cronet/native/native_metrics_util.h"
#include "components/cronet/metrics_util.h"
namespace cronet {
namespace native_metrics_util {
void ConvertTime(const base::TimeTicks& ticks,
const base::TimeTicks& start_ticks,
const base::Time& start_time,
base::Optional<Cronet_DateTime>* out) {
Cronet_DateTime date_time;
date_time.value = metrics_util::ConvertTime(ticks, start_ticks, start_time);
if (date_time.value == metrics_util::kNullTime) {
(*out).reset();
return;
}
(*out).emplace(date_time);
}
} // namespace native_metrics_util
} // namespace cronet
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_CRONET_NATIVE_NATIVE_METRICS_UTIL_H_
#define COMPONENTS_CRONET_NATIVE_NATIVE_METRICS_UTIL_H_
#include "base/optional.h"
#include "base/time/time.h"
#include "components/cronet/native/generated/cronet.idl_impl_struct.h"
namespace cronet {
namespace native_metrics_util {
// Converts timing metrics stored as TimeTicks into the format expected by the
// native layer: a base::Optional<Cronet_DateTime> (which may be valueless if
// either |ticks| or |start_ticks| is null) -- this is returned via |out|. An
// out parameter is used because Cronet IDL structs like Cronet_DateTime aren't
// assignable.
//
// By calculating time values using a base (|start_ticks|, |start_time|) pair,
// time values are normalized. This allows time deltas between pairs of events
// to be accurately computed, even if the system clock changed between those
// events, as long as times for both events were calculated using the same
// (|start_ticks|, |start_time|) pair.
//
// Args:
//
// ticks: The ticks value corresponding to the time of the event -- the returned
// time corresponds to this event.
//
// start_ticks: Ticks measurement at some base time -- the ticks equivalent of
// start_time. Should be smaller than ticks.
//
// start_time: Time measurement at some base time -- the time equivalent of
// start_ticks. Must not be null.
//
// out: The output of the function -- the existing pointee object is mutated to
// either hold the new Cronet_DateTime or nothing (if either |ticks| or
// |start_ticks| is null).
void ConvertTime(const base::TimeTicks& ticks,
const base::TimeTicks& start_ticks,
const base::Time& start_time,
base::Optional<Cronet_DateTime>* out);
} // namespace native_metrics_util
} // namespace cronet
#endif // COMPONENTS_CRONET_NATIVE_NATIVE_METRICS_UTIL_H_
// Copyright 2019 The Chromium 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 "components/cronet/native/native_metrics_util.h"
#include "base/test/gtest_util.h"
#include "base/time/time.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace cronet {
namespace native_metrics_util {
namespace {
TEST(NativeMetricsUtilTest, ConvertsTimes) {
constexpr auto start_delta = base::TimeDelta::FromMilliseconds(20);
constexpr auto event_delta = base::TimeDelta::FromMilliseconds(30);
base::Optional<Cronet_DateTime> converted;
ConvertTime(base::TimeTicks::UnixEpoch() + event_delta,
base::TimeTicks::UnixEpoch() + start_delta,
base::Time::UnixEpoch() + start_delta, &converted);
ASSERT_TRUE(converted.has_value());
EXPECT_EQ(converted->value, 30);
}
TEST(NativeMetricsUtilTest, OverwritesOldOutParam) {
constexpr auto start_delta = base::TimeDelta::FromMilliseconds(20);
constexpr auto event_delta = base::TimeDelta::FromMilliseconds(30);
base::Optional<Cronet_DateTime> converted;
converted.emplace();
converted->value = 60;
ConvertTime(base::TimeTicks::UnixEpoch() + event_delta,
base::TimeTicks::UnixEpoch() + start_delta,
base::Time::UnixEpoch() + start_delta, &converted);
ASSERT_TRUE(converted.has_value());
EXPECT_EQ(converted->value, 30);
}
TEST(NativeMetricsUtilTest, NullTicks) {
constexpr auto start_delta = base::TimeDelta::FromMilliseconds(20);
base::Optional<Cronet_DateTime> converted;
ConvertTime(base::TimeTicks(), base::TimeTicks::UnixEpoch() + start_delta,
base::Time::UnixEpoch() + start_delta, &converted);
ASSERT_FALSE(converted.has_value());
}
TEST(NativeMetricsUtilTest, NullStartTicks) {
constexpr auto start_delta = base::TimeDelta::FromMilliseconds(20);
constexpr auto event_delta = base::TimeDelta::FromMilliseconds(30);
base::Optional<Cronet_DateTime> converted;
ConvertTime(base::TimeTicks::UnixEpoch() + event_delta, base::TimeTicks(),
base::Time::UnixEpoch() + start_delta, &converted);
ASSERT_FALSE(converted.has_value());
}
TEST(NativeMetricsUtilTest, NullStartTime) {
constexpr auto start_delta = base::TimeDelta::FromMilliseconds(20);
constexpr auto event_delta = base::TimeDelta::FromMilliseconds(30);
base::Optional<Cronet_DateTime> converted;
EXPECT_DCHECK_DEATH(ConvertTime(base::TimeTicks::UnixEpoch() + event_delta,
base::TimeTicks::UnixEpoch() + start_delta,
base::Time(), &converted));
}
} // namespace
} // namespace native_metrics_util
} // namespace cronet
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment