• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 <map>
18 #include <random>
19 #include <string>
20 
21 #include "gtest/gtest.h"
22 #include "perfetto/base/logging.h"
23 #include "perfetto/base/scoped_file.h"
24 #include "perfetto/trace_processor/trace_processor.h"
25 #include "src/base/test/utils.h"
26 #include "src/trace_processor/json_trace_parser.h"
27 
28 namespace perfetto {
29 namespace trace_processor {
30 namespace {
31 
32 class TraceProcessorIntegrationTest : public ::testing::Test {
33  public:
TraceProcessorIntegrationTest()34   TraceProcessorIntegrationTest()
35       : processor_(TraceProcessor::CreateInstance(Config())) {}
36 
37  protected:
LoadTrace(const char * name,int min_chunk_size=1)38   bool LoadTrace(const char* name, int min_chunk_size = 1) {
39     base::ScopedFstream f(fopen(base::GetTestDataPath(name).c_str(), "rb"));
40     std::minstd_rand0 rnd_engine(0);
41     std::uniform_int_distribution<> dist(min_chunk_size, 1024);
42     while (!feof(*f)) {
43       size_t chunk_size = static_cast<size_t>(dist(rnd_engine));
44       std::unique_ptr<uint8_t[]> buf(new uint8_t[chunk_size]);
45       auto rsize = fread(reinterpret_cast<char*>(buf.get()), 1, chunk_size, *f);
46       if (!processor_->Parse(std::move(buf), rsize))
47         return false;
48     }
49     processor_->NotifyEndOfFile();
50     return true;
51   }
52 
Query(const std::string & query)53   TraceProcessor::Iterator Query(const std::string& query) {
54     return processor_->ExecuteQuery(query.c_str());
55   }
56 
57  private:
58   std::unique_ptr<TraceProcessor> processor_;
59 };
60 
TEST_F(TraceProcessorIntegrationTest,AndroidSchedAndPs)61 TEST_F(TraceProcessorIntegrationTest, AndroidSchedAndPs) {
62   ASSERT_TRUE(LoadTrace("android_sched_and_ps.pb"));
63   auto it = Query(
64       "select count(*), max(ts) - min(ts) from sched "
65       "where dur != 0 and utid != 0");
66   ASSERT_TRUE(it.Next());
67   ASSERT_EQ(it.Get(0).type, SqlValue::kLong);
68   ASSERT_EQ(it.Get(0).long_value, 139787);
69   ASSERT_EQ(it.Get(1).type, SqlValue::kLong);
70   ASSERT_EQ(it.Get(1).long_value, 19684308497);
71   ASSERT_FALSE(it.Next());
72 }
73 
TEST_F(TraceProcessorIntegrationTest,Sfgate)74 TEST_F(TraceProcessorIntegrationTest, Sfgate) {
75   ASSERT_TRUE(LoadTrace("sfgate.json", strlen("{\"traceEvents\":[")));
76   auto it =
77       Query("select count(*), max(ts) - min(ts) from slices where utid != 0");
78   ASSERT_TRUE(it.Next());
79   ASSERT_EQ(it.Get(0).type, SqlValue::kLong);
80   ASSERT_EQ(it.Get(0).long_value, 39828);
81   ASSERT_EQ(it.Get(1).type, SqlValue::kLong);
82   ASSERT_EQ(it.Get(1).long_value, 40532506000);
83   ASSERT_FALSE(it.Next());
84 }
85 
TEST_F(TraceProcessorIntegrationTest,UnsortedTrace)86 TEST_F(TraceProcessorIntegrationTest, UnsortedTrace) {
87   ASSERT_TRUE(LoadTrace("unsorted_trace.json", strlen("{\"traceEvents\":[")));
88   auto it = Query("select ts, depth from slices order by ts");
89   ASSERT_TRUE(it.Next());
90   ASSERT_EQ(it.Get(0).type, SqlValue::kLong);
91   ASSERT_EQ(it.Get(0).long_value, 50000);
92   ASSERT_EQ(it.Get(1).type, SqlValue::kLong);
93   ASSERT_EQ(it.Get(1).long_value, 0);
94   ASSERT_TRUE(it.Next());
95   ASSERT_EQ(it.Get(0).type, SqlValue::kLong);
96   ASSERT_EQ(it.Get(0).long_value, 100000);
97   ASSERT_EQ(it.Get(1).type, SqlValue::kLong);
98   ASSERT_EQ(it.Get(1).long_value, 1);
99   ASSERT_FALSE(it.Next());
100 }
101 
TEST_F(TraceProcessorIntegrationTest,TraceBounds)102 TEST_F(TraceProcessorIntegrationTest, TraceBounds) {
103   ASSERT_TRUE(LoadTrace("android_sched_and_ps.pb"));
104   auto it = Query("select start_ts, end_ts from trace_bounds");
105   ASSERT_TRUE(it.Next());
106   ASSERT_EQ(it.Get(0).type, SqlValue::kLong);
107   ASSERT_EQ(it.Get(0).long_value, 81473009948313);
108   ASSERT_EQ(it.Get(1).type, SqlValue::kLong);
109   ASSERT_EQ(it.Get(1).long_value, 81492700784311);
110   ASSERT_FALSE(it.Next());
111 }
112 
113 // TODO(hjd): Add trace to test_data.
TEST_F(TraceProcessorIntegrationTest,DISABLED_AndroidBuildTrace)114 TEST_F(TraceProcessorIntegrationTest, DISABLED_AndroidBuildTrace) {
115   ASSERT_TRUE(LoadTrace("android_build_trace.json", strlen("[\n{")));
116 }
117 
118 }  // namespace
119 }  // namespace trace_processor
120 }  // namespace perfetto
121