Skip to main content

inspect_testing/
lib.rs

1// Copyright 2023 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::table::*;
6use clap::Parser;
7use fidl_fuchsia_inspect as finspect;
8use fuchsia_async as fasync;
9use fuchsia_inspect::{
10    self as inspect, ArithmeticArrayProperty, ArrayProperty, DoubleArrayProperty,
11    DoubleExponentialHistogramProperty, DoubleLinearHistogramProperty, Inspector, IntArrayProperty,
12    IntExponentialHistogramProperty, IntLinearHistogramProperty, Node, UintArrayProperty,
13    UintExponentialHistogramProperty, UintLinearHistogramProperty,
14};
15use futures::FutureExt;
16use std::ops::AddAssign;
17
18pub mod table;
19
20pub struct PopulateParams<T> {
21    pub floor: T,
22    pub step: T,
23    pub count: usize,
24}
25
26pub fn populated<H: inspect::HistogramProperty>(histogram: H, params: PopulateParams<H::Type>) -> H
27where
28    H::Type: AddAssign + Copy,
29{
30    let mut value = params.floor;
31    for _ in 0..params.count {
32        histogram.insert(value);
33        value += params.step;
34    }
35    histogram
36}
37
38#[derive(Debug, Parser, Default)]
39#[command(
40    name = "example",
41    about = "Example component to showcase Inspect API objects, including an NxM nested table"
42)]
43pub struct Options {
44    #[arg(long)]
45    pub rows: usize,
46
47    #[arg(long)]
48    pub columns: usize,
49
50    /// If set, publish a top-level number called "extra_number".
51    #[arg(long = "extra-number")]
52    pub extra_number: Option<i64>,
53}
54
55#[derive(Default)]
56pub struct ExampleInspectData {
57    int_array: IntArrayProperty,
58    uint_array: UintArrayProperty,
59    double_array: DoubleArrayProperty,
60    int_linear_hist: IntLinearHistogramProperty,
61    uint_linear_hist: UintLinearHistogramProperty,
62    double_linear_hist: DoubleLinearHistogramProperty,
63    int_exp_hist: IntExponentialHistogramProperty,
64    uint_exp_hist: UintExponentialHistogramProperty,
65    double_exp_hist: DoubleExponentialHistogramProperty,
66    table: Option<Table>,
67}
68
69impl ExampleInspectData {
70    /// Allocates a collection of example inspect values for testing.
71    ///
72    /// The allocated data remains visible to other components until this object
73    /// is dropped.
74    pub fn write_to(&mut self, node: &Node) {
75        // Changing the order of the function calls below also changes the generated
76        // identifiers that are used for each piece of example inspect data.
77        reset_unique_names();
78        self.table.replace(new_example_table(node));
79        self.int_array = new_example_int_array(node);
80        self.uint_array = new_example_uint_array(node);
81        self.double_array = new_example_double_array(node);
82        self.int_linear_hist = new_example_int_linear_hist(node);
83        self.uint_linear_hist = new_example_uint_linear_hist(node);
84        self.double_linear_hist = new_example_double_linear_hist(node);
85        self.int_exp_hist = new_example_int_exp_hist(node);
86        self.uint_exp_hist = new_example_uint_exp_hist(node);
87        self.double_exp_hist = new_example_double_exp_hist(node);
88
89        // Lazy values are closures registered with the node, and don't need
90        // to be held by this struct to avoid being dropped.
91        new_example_lazy_double(node);
92        new_example_lazy_uint(node);
93    }
94}
95
96pub async fn serve_inspect_tree(stream: finspect::TreeRequestStream, inspector: &Inspector) {
97    let scope = fasync::Scope::new_with_name("serve_inspect_tree");
98    let settings = inspect_runtime::TreeServerSendPreference::default();
99    inspect_runtime::service::spawn_tree_server_with_stream(
100        inspector.clone(),
101        settings,
102        stream,
103        &scope,
104    );
105    scope.join().await;
106}
107
108pub fn new_example_table(node: &Node) -> Table {
109    let table_node_name = unique_name("table");
110    Table::new(3, 3, node.create_child(&table_node_name))
111}
112
113pub fn new_example_int_array(node: &Node) -> IntArrayProperty {
114    let int_array = node.create_int_array(unique_name("array"), 3);
115    int_array.set(0, 1);
116    int_array.add(1, 10);
117    int_array.subtract(2, 3);
118    int_array
119}
120
121pub fn new_example_uint_array(node: &Node) -> UintArrayProperty {
122    let uint_array = node.create_uint_array(unique_name("array"), 3);
123    uint_array.set(0, 1u64);
124    uint_array.add(1, 10);
125    uint_array.set(2, 3u64);
126    uint_array.subtract(2, 1);
127    uint_array
128}
129
130pub fn new_example_double_array(node: &Node) -> DoubleArrayProperty {
131    let double_array = node.create_double_array(unique_name("array"), 3);
132    double_array.set(0, 0.25);
133    double_array.add(1, 1.25);
134    double_array.subtract(2, 0.75);
135    double_array
136}
137
138pub fn new_example_int_linear_hist(node: &Node) -> IntLinearHistogramProperty {
139    populated(
140        node.create_int_linear_histogram(
141            unique_name("histogram"),
142            inspect::LinearHistogramParams { floor: -10, step_size: 5, buckets: 3 },
143        ),
144        PopulateParams { floor: -20, step: 1, count: 40 },
145    )
146}
147
148pub fn new_example_uint_linear_hist(node: &Node) -> UintLinearHistogramProperty {
149    populated(
150        node.create_uint_linear_histogram(
151            unique_name("histogram"),
152            inspect::LinearHistogramParams { floor: 5, step_size: 5, buckets: 3 },
153        ),
154        PopulateParams { floor: 0, step: 1, count: 40 },
155    )
156}
157
158pub fn new_example_double_linear_hist(node: &Node) -> DoubleLinearHistogramProperty {
159    populated(
160        node.create_double_linear_histogram(
161            unique_name("histogram"),
162            inspect::LinearHistogramParams { floor: 0.0, step_size: 0.5, buckets: 3 },
163        ),
164        PopulateParams { floor: -1.0, step: 0.1, count: 40 },
165    )
166}
167
168pub fn new_example_int_exp_hist(node: &Node) -> IntExponentialHistogramProperty {
169    populated(
170        node.create_int_exponential_histogram(
171            unique_name("histogram"),
172            inspect::ExponentialHistogramParams {
173                floor: -10,
174                initial_step: 5,
175                step_multiplier: 2,
176                buckets: 3,
177            },
178        ),
179        PopulateParams { floor: -20, step: 1, count: 40 },
180    )
181}
182
183pub fn new_example_uint_exp_hist(node: &Node) -> UintExponentialHistogramProperty {
184    populated(
185        node.create_uint_exponential_histogram(
186            unique_name("histogram"),
187            inspect::ExponentialHistogramParams {
188                floor: 0,
189                initial_step: 1,
190                step_multiplier: 2,
191                buckets: 3,
192            },
193        ),
194        PopulateParams { floor: 0, step: 1, count: 40 },
195    )
196}
197
198pub fn new_example_double_exp_hist(node: &Node) -> DoubleExponentialHistogramProperty {
199    populated(
200        node.create_double_exponential_histogram(
201            unique_name("histogram"),
202            inspect::ExponentialHistogramParams {
203                floor: 0.0,
204                initial_step: 1.25,
205                step_multiplier: 3.0,
206                buckets: 5,
207            },
208        ),
209        PopulateParams { floor: -1.0, step: 0.1, count: 40 },
210    )
211}
212
213pub fn new_example_lazy_uint(node: &Node) {
214    node.record_lazy_child("lazy-node", move || {
215        async move {
216            let inspector = inspect::Inspector::default();
217            inspector.root().record_uint("uint", 3);
218            Ok(inspector)
219        }
220        .boxed()
221    });
222}
223
224pub fn new_example_lazy_double(node: &Node) {
225    node.record_lazy_values("lazy-values", move || {
226        async move {
227            let inspector = inspect::Inspector::default();
228            inspector.root().record_double("lazy-double", 3.25);
229            Ok(inspector)
230        }
231        .boxed()
232    });
233}