Chromium Code Reviews| Index: chrome/browser/task_profiler/task_profiler_data_serializer_unittest.cc |
| diff --git a/chrome/browser/task_profiler/task_profiler_data_serializer_unittest.cc b/chrome/browser/task_profiler/task_profiler_data_serializer_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..56d8aff65bc78dd5998930582654e0449e6493c7 |
| --- /dev/null |
| +++ b/chrome/browser/task_profiler/task_profiler_data_serializer_unittest.cc |
| @@ -0,0 +1,161 @@ |
| +// Copyright (c) 2012 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 <string> |
| + |
| +#include "base/basictypes.h" |
| +#include "base/json/json_writer.h" |
| +#include "base/process_util.h" |
| +#include "base/string_number_conversions.h" |
| +#include "base/tracked_objects.h" |
| +#include "base/values.h" |
| +#include "chrome/browser/task_profiler/task_profiler_data_serializer.h" |
| +#include "content/public/common/process_type.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace { |
| + |
| +std::string GetProcessIdString() { |
| + return base::IntToString(base::GetCurrentProcId()); |
| +} |
| + |
| +void ExpectSerialization( |
| + const tracked_objects::SerializedProcessData& process_data, |
| + content::ProcessType process_type, |
| + const std::string& expected_json) { |
| + base::DictionaryValue serialized_value; |
| + task_profiler::TaskProfilerDataSerializer::SerializeToJson( |
| + process_data, process_type, &serialized_value); |
| + |
| + std::string serialized_json; |
| + base::JSONWriter::Write(&serialized_value, &serialized_json); |
| + |
| + EXPECT_EQ(expected_json, serialized_json); |
| +} |
| + |
| +} // anonymous namespace |
| + |
| +// Tests the JSON serialization format for profiled process data. |
| +TEST(TaskProfilerDataSerializerTest, SerializeProcessDataToJson) { |
| + { |
| + // Empty data. |
| + tracked_objects::SerializedProcessData process_data; |
| + content::ProcessType process_type = content::PROCESS_TYPE_BROWSER; |
| + ExpectSerialization(process_data, process_type, |
| + "{" |
| + "\"descendants\":[" |
| + "]," |
| + "\"list\":[" |
| + "]," |
| + "\"process_id\":" + GetProcessIdString() + "," |
| + "\"process_type\":\"Browser\"" |
| + "}"); |
| + } |
| + |
| + { |
| + // Non-empty data. |
| + tracked_objects::SerializedProcessData process_data; |
| + |
| + tracked_objects::SerializedBirthOnThread parent; |
| + parent.location.file_name = "path/to/foo.cc"; |
| + parent.location.function_name = "WhizBang"; |
| + parent.location.line_number = 101; |
| + parent.thread_name = "CrBrowserMain"; |
| + |
| + tracked_objects::SerializedBirthOnThread child; |
| + child.location.file_name = "path/to/bar.cc"; |
| + child.location.function_name = "FizzBoom"; |
| + child.location.line_number = 433; |
| + child.thread_name = "Chrome_IOThread"; |
| + |
| + |
| + // Add a snapshot. |
| + process_data.snapshots.push_back(tracked_objects::SerializedSnapshot()); |
| + process_data.snapshots.back().birth = parent; |
| + process_data.snapshots.back().death_data.count = 37; |
| + process_data.snapshots.back().death_data.run_duration_sum = 3; |
| + process_data.snapshots.back().death_data.run_duration_max = 17; |
| + process_data.snapshots.back().death_data.run_duration_sample = 5; |
| + process_data.snapshots.back().death_data.queue_duration_sum = 13; |
|
jar (doing other things)
2012/04/04 17:55:35
sums should probably be larger than max or sample
Ilya Sherman
2012/04/05 02:51:04
Oops, maxes should probably be larger than samples
|
| + process_data.snapshots.back().death_data.queue_duration_max = 79; |
| + process_data.snapshots.back().death_data.queue_duration_sample = 53; |
| + process_data.snapshots.back().death_thread_name = "WorkerPool/-1340960768"; |
| + |
| + // Add a second snapshot. |
| + process_data.snapshots.push_back(tracked_objects::SerializedSnapshot()); |
| + process_data.snapshots.back().birth = child; |
| + process_data.snapshots.back().death_data.count = 41; |
| + process_data.snapshots.back().death_data.run_duration_sum = 203; |
| + process_data.snapshots.back().death_data.run_duration_max = 2017; |
| + process_data.snapshots.back().death_data.run_duration_sample = 205; |
| + process_data.snapshots.back().death_data.queue_duration_sum = 2013; |
| + process_data.snapshots.back().death_data.queue_duration_max = 2079; |
| + process_data.snapshots.back().death_data.queue_duration_sample = 2053; |
| + process_data.snapshots.back().death_thread_name = "PAC thread #3"; |
| + |
| + // Add a parent-child pair. |
| + process_data.descendants.push_back( |
| + tracked_objects::SerializedParentChildPair()); |
| + process_data.descendants.back().parent = parent; |
| + process_data.descendants.back().child = child; |
| + |
| + content::ProcessType process_type = content::PROCESS_TYPE_RENDERER; |
| + ExpectSerialization(process_data, process_type, |
| + "{" |
| + "\"descendants\":[" |
| + "{" |
| + "\"child_location\":{" |
| + "\"file_name\":\"path/to/bar.cc\"," |
| + "\"function_name\":\"FizzBoom\"," |
| + "\"line_number\":433" |
| + "}," |
| + "\"child_thread\":\"Chrome_IOThread\"," |
| + "\"parent_location\":{" |
| + "\"file_name\":\"path/to/foo.cc\"," |
| + "\"function_name\":\"WhizBang\"," |
| + "\"line_number\":101" |
| + "}," |
| + "\"parent_thread\":\"CrBrowserMain\"" |
| + "}" |
| + "]," |
| + "\"list\":[{" |
| + "\"birth_location\":{" |
| + "\"file_name\":\"path/to/foo.cc\"," |
| + "\"function_name\":\"WhizBang\"," |
| + "\"line_number\":101" |
| + "}," |
| + "\"birth_thread\":\"CrBrowserMain\"," |
| + "\"death_data\":{" |
| + "\"count\":37," |
| + "\"queue_ms\":13," |
| + "\"queue_ms_max\":79," |
| + "\"queue_ms_sample\":53," |
| + "\"run_ms\":3," |
| + "\"run_ms_max\":17," |
| + "\"run_ms_sample\":5" |
| + "}," |
| + "\"death_thread\":\"WorkerPool/-1340960768\"" |
| + "},{" |
| + "\"birth_location\":{" |
| + "\"file_name\":\"path/to/bar.cc\"," |
| + "\"function_name\":\"FizzBoom\"," |
| + "\"line_number\":433" |
| + "}," |
| + "\"birth_thread\":\"Chrome_IOThread\"," |
| + "\"death_data\":{" |
| + "\"count\":41," |
| + "\"queue_ms\":2013," |
| + "\"queue_ms_max\":2079," |
| + "\"queue_ms_sample\":2053," |
| + "\"run_ms\":203," |
| + "\"run_ms_max\":2017," |
| + "\"run_ms_sample\":205" |
| + "}," |
| + "\"death_thread\":\"PAC thread #3\"" |
| + "}]," |
| + "\"process_id\":" + GetProcessIdString() + "," |
| + "\"process_type\":\"Tab\"" |
| + "}"); |
| + } |
| +} |