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