devtools/actors/
memory.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::ClientRequest;
11
12#[derive(Serialize)]
13#[serde(rename_all = "camelCase")]
14pub struct TimelineMemoryReply {
15    js_object_size: u64,
16    js_string_size: u64,
17    js_other_size: u64,
18    dom_size: u64,
19    style_size: u64,
20    other_size: u64,
21    total_size: u64,
22    js_milliseconds: f64,
23    #[serde(rename = "nonJSMilliseconds")]
24    non_js_milliseconds: f64,
25}
26
27pub struct MemoryActor {
28    pub name: String,
29}
30
31impl Actor for MemoryActor {
32    fn name(&self) -> String {
33        self.name.clone()
34    }
35
36    fn handle_message(
37        &self,
38        _request: ClientRequest,
39        _registry: &ActorRegistry,
40        _msg_type: &str,
41        _msg: &Map<String, Value>,
42        _id: StreamId,
43    ) -> Result<(), ActorError> {
44        Err(ActorError::UnrecognizedPacketType)
45    }
46}
47
48impl MemoryActor {
49    /// return name of actor
50    pub fn create(registry: &ActorRegistry) -> String {
51        let actor_name = registry.new_name("memory");
52        let actor = MemoryActor {
53            name: actor_name.clone(),
54        };
55
56        registry.register_later(Box::new(actor));
57        actor_name
58    }
59
60    pub fn measure(&self) -> TimelineMemoryReply {
61        TimelineMemoryReply {
62            js_object_size: 1,
63            js_string_size: 1,
64            js_other_size: 1,
65            dom_size: 1,
66            style_size: 1,
67            other_size: 1,
68            total_size: 1,
69            js_milliseconds: 1.1,
70            non_js_milliseconds: 1.1,
71        }
72    }
73}