Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(143)

Side by Side Diff: base/trace_event/heap_profiler_type_name_deduplicator_unittest.cc

Issue 1851233002: Reland of [tracing] Adding task information to heap profiler (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove event from sequenced_worker_pool.cc Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <string> 5 #include <string>
6 6
7 #include "base/json/json_reader.h" 7 #include "base/json/json_reader.h"
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "base/trace_event/heap_profiler_type_name_deduplicator.h" 9 #include "base/trace_event/heap_profiler_type_name_deduplicator.h"
10 #include "base/values.h" 10 #include "base/values.h"
11 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
12 12
13 namespace base { 13 namespace base {
14 namespace trace_event { 14 namespace trace_event {
15 15
16 namespace {
17
16 // Define all strings once, because the deduplicator requires pointer equality, 18 // Define all strings once, because the deduplicator requires pointer equality,
17 // and string interning is unreliable. 19 // and string interning is unreliable.
18 const char kInt[] = "int"; 20 const char kInt[] = "int";
19 const char kBool[] = "bool"; 21 const char kBool[] = "bool";
20 const char kString[] = "string"; 22 const char kString[] = "string";
21 const char kNeedsEscape[] = "\"quotes\""; 23 const char kNeedsEscape[] = "\"quotes\"";
22 24
25 #if defined(OS_POSIX)
26 const char kTaskFileName[] = "../../base/trace_event/trace_log.cc";
27 const char kTaskPath[] = "base/trace_event";
28 #else
29 const char kTaskFileName[] = "..\\..\\base\\memory\\memory_win.cc";
30 const char kTaskPath[] = "base\\memory";
31 #endif
32
23 scoped_ptr<Value> DumpAndReadBack(const TypeNameDeduplicator& deduplicator) { 33 scoped_ptr<Value> DumpAndReadBack(const TypeNameDeduplicator& deduplicator) {
24 std::string json; 34 std::string json;
25 deduplicator.AppendAsTraceFormat(&json); 35 deduplicator.AppendAsTraceFormat(&json);
26 return JSONReader::Read(json); 36 return JSONReader::Read(json);
27 } 37 }
28 38
39 // Inserts a single type name into a new TypeNameDeduplicator instance and
40 // checks if the value gets inserted and the exported value for |type_name| is
41 // the same as |expected_value|.
42 void TestInsertTypeAndReadback(const char* type_name,
43 const char* expected_value) {
44 scoped_ptr<TypeNameDeduplicator> dedup(new TypeNameDeduplicator);
45 ASSERT_EQ(1, dedup->Insert(type_name));
46
47 scoped_ptr<Value> type_names = DumpAndReadBack(*dedup);
48 ASSERT_NE(nullptr, type_names);
49
50 const DictionaryValue* dictionary;
51 ASSERT_TRUE(type_names->GetAsDictionary(&dictionary));
52
53 // When the type name was inserted, it got ID 1. The exported key "1"
54 // should be equal to |expected_value|.
55 std::string value;
56 ASSERT_TRUE(dictionary->GetString("1", &value));
57 ASSERT_EQ(expected_value, value);
58 }
59
60 } // namespace
61
29 TEST(TypeNameDeduplicatorTest, Deduplication) { 62 TEST(TypeNameDeduplicatorTest, Deduplication) {
30 // The type IDs should be like this: 63 // The type IDs should be like this:
31 // 0: [unknown] 64 // 0: [unknown]
32 // 1: int 65 // 1: int
33 // 2: bool 66 // 2: bool
34 // 3: string 67 // 3: string
35 68
36 scoped_ptr<TypeNameDeduplicator> dedup(new TypeNameDeduplicator); 69 scoped_ptr<TypeNameDeduplicator> dedup(new TypeNameDeduplicator);
37 ASSERT_EQ(1, dedup->Insert(kInt)); 70 ASSERT_EQ(1, dedup->Insert(kInt));
38 ASSERT_EQ(2, dedup->Insert(kBool)); 71 ASSERT_EQ(2, dedup->Insert(kBool));
39 ASSERT_EQ(3, dedup->Insert(kString)); 72 ASSERT_EQ(3, dedup->Insert(kString));
40 73
41 // Inserting again should return the same IDs. 74 // Inserting again should return the same IDs.
42 ASSERT_EQ(2, dedup->Insert(kBool)); 75 ASSERT_EQ(2, dedup->Insert(kBool));
43 ASSERT_EQ(1, dedup->Insert(kInt)); 76 ASSERT_EQ(1, dedup->Insert(kInt));
44 ASSERT_EQ(3, dedup->Insert(kString)); 77 ASSERT_EQ(3, dedup->Insert(kString));
45 78
46 // A null pointer should yield type ID 0. 79 // A null pointer should yield type ID 0.
47 ASSERT_EQ(0, dedup->Insert(nullptr)); 80 ASSERT_EQ(0, dedup->Insert(nullptr));
48 } 81 }
49 82
50 TEST(TypeNameDeduplicatorTest, EscapeTypeName) { 83 TEST(TypeNameDeduplicatorTest, EscapeTypeName) {
51 scoped_ptr<TypeNameDeduplicator> dedup(new TypeNameDeduplicator); 84 // Reading json should not fail, because the type name should have been
52 ASSERT_EQ(1, dedup->Insert(kNeedsEscape)); 85 // escaped properly and exported value should contain quotes.
86 TestInsertTypeAndReadback(kNeedsEscape, kNeedsEscape);
87 }
53 88
54 // Reading json should not fail, because the type name should have been 89 TEST(TypeNameDeduplicatorTest, TestExtractFileName) {
55 // escaped properly. 90 // The exported value for passed file name should be the folders in the path.
56 scoped_ptr<Value> type_names = DumpAndReadBack(*dedup); 91 TestInsertTypeAndReadback(kTaskFileName, kTaskPath);
57 ASSERT_NE(nullptr, type_names);
58
59 const DictionaryValue* dictionary;
60 ASSERT_TRUE(type_names->GetAsDictionary(&dictionary));
61
62 // When the type name was inserted, it got ID 1. The exported key "1"
63 // should contain the name, with quotes.
64 std::string type_name;
65 ASSERT_TRUE(dictionary->GetString("1", &type_name));
66 ASSERT_EQ("\"quotes\"", type_name);
67 } 92 }
68 93
69 } // namespace trace_event 94 } // namespace trace_event
70 } // namespace base 95 } // namespace base
OLDNEW
« no previous file with comments | « base/trace_event/heap_profiler_type_name_deduplicator.cc ('k') | base/trace_event/trace_event.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698