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