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