1 /*
2 * Copyright (C) 2023 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 <string_view>
18
19 #include "perfetto/ext/base/subprocess.h"
20 #include "perfetto/ext/base/utils.h"
21 #include "protos/perfetto/trace_processor/trace_processor.gen.h"
22 #include "test/gtest_and_gmock.h"
23 #include "test/test_helper.h"
24
25 namespace perfetto::trace_processor {
26 namespace {
27
28 using TraceProcessorRpc = protos::gen::TraceProcessorRpc;
29 using TraceProcessorRpcStream = protos::gen::TraceProcessorRpcStream;
30 using CellsBatch = protos::gen::QueryResult::CellsBatch;
31
32 using testing::AllOf;
33 using testing::ElementsAre;
34 using testing::IsEmpty;
35 using testing::Property;
36 using testing::SizeIs;
37
38 const std::string_view kSimpleSystrace = R"(# tracer
39 surfaceflinger-598 ( 598) [004] .... 10852.771242: tracing_mark_write: B|598|some event
40 surfaceflinger-598 ( 598) [004] .... 10852.771245: tracing_mark_write: E|598
41 )";
42
TEST(TraceProcessorShellIntegrationTest,StdioSimpleRequestResponse)43 TEST(TraceProcessorShellIntegrationTest, StdioSimpleRequestResponse) {
44 TraceProcessorRpcStream req;
45
46 auto* rpc = req.add_msg();
47 rpc->set_append_trace_data(kSimpleSystrace.data(), kSimpleSystrace.size());
48 rpc->set_request(TraceProcessorRpc::TPM_APPEND_TRACE_DATA);
49
50 rpc = req.add_msg();
51 rpc->set_request(TraceProcessorRpc::TPM_FINALIZE_TRACE_DATA);
52
53 rpc = req.add_msg();
54 rpc->set_request(TraceProcessorRpc::TPM_QUERY_STREAMING);
55 rpc->mutable_query_args()->set_sql_query("SELECT ts, dur FROM slice");
56
57 base::Subprocess process(
58 {base::GetCurExecutableDir() + "/trace_processor_shell", "--stdiod"});
59 process.args.stdin_mode = base::Subprocess::InputMode::kBuffer;
60 process.args.stdout_mode = base::Subprocess::OutputMode::kBuffer;
61 process.args.stderr_mode = base::Subprocess::OutputMode::kInherit;
62 process.args.input = req.SerializeAsString();
63 process.Start();
64
65 ASSERT_TRUE(process.Wait(kDefaultTestTimeoutMs));
66
67 TraceProcessorRpcStream stream;
68 stream.ParseFromString(process.output());
69
70 ASSERT_THAT(stream.msg(),
71 ElementsAre(Property(&TraceProcessorRpc::response,
72 TraceProcessorRpc::TPM_APPEND_TRACE_DATA),
73 Property(&TraceProcessorRpc::response,
74 TraceProcessorRpc::TPM_FINALIZE_TRACE_DATA),
75 Property(&TraceProcessorRpc::response,
76 TraceProcessorRpc::TPM_QUERY_STREAMING)));
77 ASSERT_THAT(stream.msg()[0].append_result().error(), IsEmpty());
78 ASSERT_THAT(stream.msg()[2].query_result().batch(), SizeIs(1));
79 ASSERT_THAT(stream.msg()[2].query_result().batch()[0].varint_cells(),
80 ElementsAre(10852771242000, 3000));
81 }
82
83 } // namespace
84 } // namespace perfetto::trace_processor
85