xref: /aosp_15_r20/external/perfetto/python/perfetto/trace_processor/http.py (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1#!/usr/bin/env python3
2# Copyright (C) 2020 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
16import http.client
17from typing import List
18
19from perfetto.trace_processor.protos import ProtoFactory
20
21
22class TraceProcessorHttp:
23
24  def __init__(self, url: str, protos: ProtoFactory):
25    self.protos = protos
26    self.conn = http.client.HTTPConnection(url)
27
28  def execute_query(self, query: str):
29    args = self.protos.QueryArgs()
30    args.sql_query = query
31    byte_data = args.SerializeToString()
32    self.conn.request('POST', '/query', body=byte_data)
33    with self.conn.getresponse() as f:
34      result = self.protos.QueryResult()
35      result.ParseFromString(f.read())
36      return result
37
38  def compute_metric(self, metrics: List[str]):
39    args = self.protos.ComputeMetricArgs()
40    args.metric_names.extend(metrics)
41    byte_data = args.SerializeToString()
42    self.conn.request('POST', '/compute_metric', body=byte_data)
43    with self.conn.getresponse() as f:
44      result = self.protos.ComputeMetricResult()
45      result.ParseFromString(f.read())
46      return result
47
48  def parse(self, chunk: bytes):
49    self.conn.request('POST', '/parse', body=chunk)
50    with self.conn.getresponse() as f:
51      result = self.protos.AppendTraceDataResult()
52      result.ParseFromString(f.read())
53      return result
54
55  def notify_eof(self):
56    self.conn.request('GET', '/notify_eof')
57    with self.conn.getresponse() as f:
58      return f.read()
59
60  def status(self):
61    self.conn.request('GET', '/status')
62    with self.conn.getresponse() as f:
63      result = self.protos.StatusResult()
64      result.ParseFromString(f.read())
65      return result
66
67  def enable_metatrace(self):
68    self.conn.request('GET', '/enable_metatrace')
69    with self.conn.getresponse() as f:
70      return f.read()
71
72  def disable_and_read_metatrace(self):
73    self.conn.request('GET', '/disable_and_read_metatrace')
74    with self.conn.getresponse() as f:
75      result = self.protos.DisableAndReadMetatraceResult()
76      result.ParseFromString(f.read())
77      return result
78