Skip to main content

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