blob: a4bbbf524d45630c7bb5e0d170b850f7936df7e3 [file] [log] [blame]
Mikhail Khokhlova8d310d2019-05-07 17:34:21 +01001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "src/trace_processor/export_json.h"
18
19#include "perfetto/base/temp_file.h"
20
21#include "gmock/gmock.h"
22#include "gtest/gtest.h"
23
24#include <json/reader.h>
25#include <json/value.h>
26
27namespace perfetto {
28namespace trace_processor {
29namespace json {
30namespace {
31
32std::string ReadFile(FILE* input) {
33 fseek(input, 0, SEEK_SET);
34 const int kBufSize = 1000;
35 char buffer[kBufSize];
36 fread(buffer, sizeof(char), kBufSize, input);
37 return std::string(buffer);
38}
39
40TEST(ExportJsonTest, EmptyStorage) {
41 TraceStorage storage;
42
43 base::TempFile temp_file = base::TempFile::Create();
44 FILE* output = fopen(temp_file.path().c_str(), "w+");
45 int code = ExportJson(&storage, output);
46
47 EXPECT_EQ(code, kResultOk);
48
49 Json::Reader reader;
50 Json::Value result;
51
52 EXPECT_TRUE(reader.parse(ReadFile(output), result));
53 EXPECT_EQ(result["traceEvents"].size(), 0);
54}
55
56TEST(ExportJsonTest, StorageWithOneSlice) {
57 const int64_t kTimestamp = 10000000;
58 const int64_t kDuration = 10000;
59 const int64_t kThreadID = 100;
60 const char* kCategory = "cat";
61 const char* kName = "name";
62
63 TraceStorage storage;
64 UniqueTid utid = storage.AddEmptyThread(kThreadID);
65 StringId cat_id = storage.InternString(base::StringView(kCategory));
66 StringId name_id = storage.InternString(base::StringView(kName));
67 storage.mutable_nestable_slices()->AddSlice(
68 kTimestamp, kDuration, utid, RefType::kRefUtid, cat_id, name_id, 0, 0, 0);
69
70 base::TempFile temp_file = base::TempFile::Create();
71 FILE* output = fopen(temp_file.path().c_str(), "w+");
72 int code = ExportJson(&storage, output);
73
74 EXPECT_EQ(code, kResultOk);
75
76 Json::Reader reader;
77 Json::Value result;
78 EXPECT_TRUE(reader.parse(ReadFile(output), result));
79 EXPECT_EQ(result["traceEvents"].size(), 1);
80
81 Json::Value event = result["traceEvents"][0];
82 EXPECT_EQ(event["ph"].asString(), "X");
83 EXPECT_EQ(event["ts"].asInt64(), kTimestamp / 1000);
84 EXPECT_EQ(event["dur"].asInt64(), kDuration / 1000);
85 EXPECT_EQ(event["tid"].asUInt(), kThreadID);
86 EXPECT_EQ(event["cat"].asString(), kCategory);
87 EXPECT_EQ(event["name"].asString(), kName);
88}
89
90TEST(ExportJsonTest, StorageWithThreadName) {
91 const int64_t kThreadID = 100;
92 const char* kName = "thread";
93
94 TraceStorage storage;
95 UniqueTid utid = storage.AddEmptyThread(kThreadID);
96 StringId name_id = storage.InternString(base::StringView(kName));
97 storage.GetMutableThread(utid)->name_id = name_id;
98
99 base::TempFile temp_file = base::TempFile::Create();
100 FILE* output = fopen(temp_file.path().c_str(), "w+");
101 int code = ExportJson(&storage, output);
102
103 EXPECT_EQ(code, kResultOk);
104
105 Json::Reader reader;
106 Json::Value result;
107 EXPECT_TRUE(reader.parse(ReadFile(output), result));
108 EXPECT_EQ(result["traceEvents"].size(), 1);
109
110 Json::Value event = result["traceEvents"][0];
111 EXPECT_EQ(event["ph"].asString(), "M");
112 EXPECT_EQ(event["tid"].asUInt(), kThreadID);
113 EXPECT_EQ(event["name"].asString(), "thread_name");
114 EXPECT_EQ(event["args"]["name"].asString(), kName);
115}
116
117TEST(ExportJsonTest, WrongRefType) {
118 TraceStorage storage;
119 storage.mutable_nestable_slices()->AddSlice(0, 0, 0, RefType::kRefCpuId, 0, 0,
120 0, 0, 0);
121
122 base::TempFile temp_file = base::TempFile::Create();
123 FILE* output = fopen(temp_file.path().c_str(), "w+");
124 int code = ExportJson(&storage, output);
125
126 EXPECT_EQ(code, kResultWrongRefType);
127}
128
129} // namespace
130} // namespace json
131} // namespace trace_processor
132} // namespace perfetto