devtools/actors/
performance.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use malloc_size_of_derive::MallocSizeOf;
6use serde::Serialize;
7use serde_json::{Map, Value};
8
9use crate::StreamId;
10use crate::actor::{Actor, ActorError, ActorRegistry};
11use crate::protocol::{ActorDescription, ClientRequest, Method};
12
13#[derive(MallocSizeOf)]
14pub(crate) struct PerformanceActor {
15    name: String,
16}
17
18#[derive(Serialize)]
19#[serde(rename_all = "camelCase")]
20struct PerformanceFeatures {
21    with_markers: bool,
22    with_memory: bool,
23    with_ticks: bool,
24    with_allocations: bool,
25    #[serde(rename = "withJITOptimizations")]
26    with_jitoptimizations: bool,
27}
28
29#[derive(Serialize)]
30struct PerformanceTraits {
31    features: PerformanceFeatures,
32}
33
34#[derive(Serialize)]
35struct ConnectReply {
36    from: String,
37    traits: PerformanceTraits,
38}
39
40#[derive(Serialize)]
41struct CanCurrentlyRecordReply {
42    from: String,
43    value: SuccessMsg,
44}
45
46#[derive(Serialize)]
47struct SuccessMsg {
48    success: bool,
49    errors: Vec<Error>,
50}
51
52#[derive(Serialize)]
53enum Error {}
54
55impl Actor for PerformanceActor {
56    fn name(&self) -> String {
57        self.name.clone()
58    }
59
60    fn handle_message(
61        &self,
62        request: ClientRequest,
63        _registry: &ActorRegistry,
64        msg_type: &str,
65        _msg: &Map<String, Value>,
66        _id: StreamId,
67    ) -> Result<(), ActorError> {
68        match msg_type {
69            "connect" => {
70                let msg = ConnectReply {
71                    from: self.name(),
72                    traits: PerformanceTraits {
73                        features: PerformanceFeatures {
74                            with_markers: true,
75                            with_memory: true,
76                            with_ticks: true,
77                            with_allocations: true,
78                            with_jitoptimizations: true,
79                        },
80                    },
81                };
82                request.reply_final(&msg)?
83            },
84            "canCurrentlyRecord" => {
85                let msg = CanCurrentlyRecordReply {
86                    from: self.name(),
87                    value: SuccessMsg {
88                        success: true,
89                        errors: vec![],
90                    },
91                };
92                request.reply_final(&msg)?
93            },
94            _ => return Err(ActorError::UnrecognizedPacketType),
95        };
96        Ok(())
97    }
98}
99
100impl PerformanceActor {
101    pub fn register(registry: &ActorRegistry) -> String {
102        let name = registry.new_name::<Self>();
103        let actor = PerformanceActor { name: name.clone() };
104        registry.register::<Self>(actor);
105        name
106    }
107
108    pub fn description() -> ActorDescription {
109        ActorDescription {
110            category: "actor",
111            type_name: "performance",
112            methods: vec![Method {
113                name: "canCurrentlyRecord",
114                request: Value::Object(
115                    vec![(
116                        "type".to_owned(),
117                        Value::String("canCurrentlyRecord".to_owned()),
118                    )]
119                    .into_iter()
120                    .collect(),
121                ),
122                response: Value::Object(
123                    vec![(
124                        "value".to_owned(),
125                        Value::Object(
126                            vec![("_retval".to_owned(), Value::String("json".to_owned()))]
127                                .into_iter()
128                                .collect(),
129                        ),
130                    )]
131                    .into_iter()
132                    .collect(),
133                ),
134            }],
135        }
136    }
137}