1 /*
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 <cstdio>
18 #include <string>
19
20 #include <json/reader.h>
21 #include <json/value.h>
22
23 #include "perfetto/ext/trace_processor/export_json.h"
24 #include "perfetto/trace_processor/basic_types.h"
25 #include "perfetto/trace_processor/trace_processor_storage.h"
26 #include "src/base/test/status_matchers.h"
27 #include "src/base/test/utils.h"
28 #include "test/gtest_and_gmock.h"
29
30 namespace perfetto {
31 namespace trace_processor {
32 namespace {
33
34 class JsonStringOutputWriter : public json::OutputWriter {
35 public:
AppendString(const std::string & string)36 base::Status AppendString(const std::string& string) override {
37 buffer += string;
38 return base::OkStatus();
39 }
40 std::string buffer;
41 };
42
43 class StorageMinimalSmokeTest : public ::testing::Test {
44 public:
StorageMinimalSmokeTest()45 StorageMinimalSmokeTest()
46 : storage_(TraceProcessorStorage::CreateInstance(Config())) {}
47
48 protected:
49 std::unique_ptr<TraceProcessorStorage> storage_;
50 };
51
TEST_F(StorageMinimalSmokeTest,GraphicEventsIgnored)52 TEST_F(StorageMinimalSmokeTest, GraphicEventsIgnored) {
53 const size_t MAX_SIZE = 1 << 20;
54 auto f = fopen(base::GetTestDataPath("test/data/gpu_trace.pb").c_str(), "rb");
55 std::unique_ptr<uint8_t[]> buf(new uint8_t[MAX_SIZE]);
56 auto rsize = fread(reinterpret_cast<char*>(buf.get()), 1, MAX_SIZE, f);
57 base::Status status = storage_->Parse(std::move(buf), rsize);
58 ASSERT_TRUE(status.ok());
59 ASSERT_OK(storage_->NotifyEndOfFile());
60
61 JsonStringOutputWriter output_writer;
62 json::ExportJson(storage_.get(), &output_writer);
63 Json::CharReaderBuilder b;
64 auto reader = std::unique_ptr<Json::CharReader>(b.newCharReader());
65
66 Json::Value result;
67 std::string& o = output_writer.buffer;
68 ASSERT_TRUE(reader->parse(o.data(), o.data() + o.length(), &result, nullptr));
69
70 // We should only see a single event (the mapping of the idle thread to have
71 // name "swapper").
72 ASSERT_EQ(result["traceEvents"].size(), 1u);
73 }
74
TEST_F(StorageMinimalSmokeTest,SystraceReturnsError)75 TEST_F(StorageMinimalSmokeTest, SystraceReturnsError) {
76 const size_t MAX_SIZE = 1 << 20;
77 auto f =
78 fopen(base::GetTestDataPath("test/data/systrace.html").c_str(), "rb");
79 std::unique_ptr<uint8_t[]> buf(new uint8_t[MAX_SIZE]);
80 auto rsize = fread(reinterpret_cast<char*>(buf.get()), 1, MAX_SIZE, f);
81 base::Status status = storage_->Parse(std::move(buf), rsize);
82
83 ASSERT_FALSE(status.ok());
84 }
85
TEST_F(StorageMinimalSmokeTest,TrackEventsImported)86 TEST_F(StorageMinimalSmokeTest, TrackEventsImported) {
87 const size_t MAX_SIZE = 1 << 20;
88 auto f = fopen("test/data/track_event_typed_args.pb", "rb");
89 std::unique_ptr<uint8_t[]> buf(new uint8_t[MAX_SIZE]);
90 auto rsize = fread(reinterpret_cast<char*>(buf.get()), 1, MAX_SIZE, f);
91 base::Status status = storage_->Parse(std::move(buf), rsize);
92 ASSERT_TRUE(status.ok());
93 ASSERT_OK(storage_->NotifyEndOfFile());
94
95 JsonStringOutputWriter output_writer;
96 json::ExportJson(storage_.get(), &output_writer);
97 Json::CharReaderBuilder b;
98 auto reader = std::unique_ptr<Json::CharReader>(b.newCharReader());
99
100 Json::Value result;
101 std::string& o = output_writer.buffer;
102 ASSERT_TRUE(reader->parse(o.data(), o.data() + o.length(), &result, nullptr));
103
104 // We have an "extra" event from the mapping of the idle thread to have name
105 // "swapper".
106 ASSERT_EQ(result["traceEvents"].size(), 5u);
107 }
108
109 } // namespace
110 } // namespace trace_processor
111 } // namespace perfetto
112