Commit 814da6c8 authored by alanlxl's avatar alanlxl Committed by Commit Bot

Add unittest for SmartDimMlAgent

This CL adds unittest for ml_agent.h/cc and ml_agent_util.h/cc.
It's very like model_unittest.cc, as ml_agent.h/cc is supposed to
replace current model_impl.h/cc and ml_service_client.h/cc in the
future.

Bug: 1018065
Test: run this unittest and pass
Change-Id: I9fda48f1235ca24220e8422a9de84e94537d0899
Cq-Depend: chromium:1903168
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1905044
Commit-Queue: Xinglong Luan <alanlxl@chromium.org>
Reviewed-by: default avatarJia Meng <jiameng@chromium.org>
Reviewed-by: default avatarAndrew Moylan <amoylan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#743890}
parent ca1693d9
...@@ -2998,6 +2998,8 @@ source_set("unit_tests") { ...@@ -2998,6 +2998,8 @@ source_set("unit_tests") {
"power/ml/boot_clock_unittest.cc", "power/ml/boot_clock_unittest.cc",
"power/ml/idle_event_notifier_unittest.cc", "power/ml/idle_event_notifier_unittest.cc",
"power/ml/recent_events_counter_unittest.cc", "power/ml/recent_events_counter_unittest.cc",
"power/ml/smart_dim/ml_agent_unittest.cc",
"power/ml/smart_dim/ml_agent_util_unittest.cc",
"power/ml/smart_dim/model_unittest.cc", "power/ml/smart_dim/model_unittest.cc",
"power/ml/user_activity_manager_unittest.cc", "power/ml/user_activity_manager_unittest.cc",
"power/ml/user_activity_ukm_logger_helpers_unittest.cc", "power/ml/user_activity_ukm_logger_helpers_unittest.cc",
......
...@@ -336,6 +336,11 @@ void SmartDimMlAgent::CancelPreviousRequest() { ...@@ -336,6 +336,11 @@ void SmartDimMlAgent::CancelPreviousRequest() {
dim_decision_callback_.Cancel(); dim_decision_callback_.Cancel();
} }
void SmartDimMlAgent::ResetForTesting() {
builtin_worker_.Reset();
download_worker_.Reset();
}
SmartDimWorker* SmartDimMlAgent::GetWorker() { SmartDimWorker* SmartDimMlAgent::GetWorker() {
if (download_worker_.IsReady()) { if (download_worker_.IsReady()) {
// When download_worker_ is ready, builtin_worker_ is not useful any more, // When download_worker_ is ready, builtin_worker_ is not useful any more,
......
...@@ -49,6 +49,9 @@ class SmartDimMlAgent { ...@@ -49,6 +49,9 @@ class SmartDimMlAgent {
const std::string& preprocessor_proto, const std::string& preprocessor_proto,
const std::string& model_flatbuffer); const std::string& model_flatbuffer);
// Called by ml_agent_unittest.cc to reset the builtin and download worker.
void ResetForTesting();
protected: protected:
SmartDimMlAgent(); SmartDimMlAgent();
virtual ~SmartDimMlAgent(); virtual ~SmartDimMlAgent();
......
// Copyright 2020 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 "chrome/browser/chromeos/power/ml/smart_dim/ml_agent_util.h"
#include "base/containers/flat_map.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/path_service.h"
#include "chromeos/test/chromeos_test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace chromeos {
namespace power {
namespace ml {
namespace {
std::string ReadMetadataFromTestData(const std::string& filename) {
base::FilePath json_path;
std::string json_string;
CHECK(
chromeos::test_utils::GetTestDataPath("smart_dim", filename, &json_path));
CHECK(base::ReadFileToString(json_path, &json_string));
return json_string;
}
} // namespace
TEST(SmartDimMlAgentUtilTest, ParseInvalidMetadata) {
const std::string json_string =
ReadMetadataFromTestData("invalid_model_metadata.json");
ASSERT_TRUE(json_string.size());
std::string metrics_model_name;
double threshold;
size_t expected_feature_size;
base::flat_map<std::string, int> inputs;
base::flat_map<std::string, int> outputs;
EXPECT_FALSE(ParseMetaInfoFromString(json_string, &metrics_model_name,
&threshold, &expected_feature_size,
&inputs, &outputs));
EXPECT_EQ(expected_feature_size, 0LU);
EXPECT_EQ(metrics_model_name, "");
EXPECT_DOUBLE_EQ(threshold, 0.0);
EXPECT_EQ(inputs.size(), 0LU);
EXPECT_EQ(outputs.size(), 0LU);
}
TEST(SmartDimMlAgentUtilTest, ParseValidMetadata) {
const std::string json_string =
ReadMetadataFromTestData("valid_model_metadata.json");
ASSERT_TRUE(json_string.size());
std::string metrics_model_name;
double threshold;
size_t expected_feature_size;
base::flat_map<std::string, int> inputs;
base::flat_map<std::string, int> outputs;
EXPECT_TRUE(ParseMetaInfoFromString(json_string, &metrics_model_name,
&threshold, &expected_feature_size,
&inputs, &outputs));
EXPECT_EQ(expected_feature_size, 343LU);
EXPECT_EQ(metrics_model_name, "smart_dim_model");
EXPECT_DOUBLE_EQ(threshold, 0.7);
EXPECT_EQ(inputs, (base::flat_map<std::string, int>{{"x", 3}, {"y", 4}}));
EXPECT_EQ(outputs, (base::flat_map<std::string, int>{{"z", 5}}));
}
} // namespace ml
} // namespace power
} // namespace chromeos
{
"input_names": ["x", "y", "p"],
"input_nodes": [3, 4],
"output_names": ["z"],
"output_nodes": [5],
"threshold": 0.7,
"expected_feature_size": 343,
"metrics_model_name": "smart_dim_model"
}
{
"input_names": ["x", "y"],
"input_nodes": [3, 4],
"output_names": ["z"],
"output_nodes": [5],
"threshold": 0.7,
"expected_feature_size": 343,
"metrics_model_name": "smart_dim_model"
}
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