|  | 
|  | 1 | +/* | 
|  | 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. | 
|  | 3 | + * All rights reserved. | 
|  | 4 | + * | 
|  | 5 | + * This source code is licensed under the BSD-style license found in the | 
|  | 6 | + * LICENSE file in the root directory of this source tree. | 
|  | 7 | + */ | 
|  | 8 | + | 
|  | 9 | +/// This program is meant as a test bed for exercising the various | 
|  | 10 | +/// (v1) mesh APIs. | 
|  | 11 | +/// | 
|  | 12 | +/// It can also be used as the basis for benchmarks, functionality testing, | 
|  | 13 | +/// etc. | 
|  | 14 | +use std::collections::HashSet; | 
|  | 15 | +use std::time::Duration; | 
|  | 16 | + | 
|  | 17 | +use async_trait::async_trait; | 
|  | 18 | +use hyperactor::Actor; | 
|  | 19 | +use hyperactor::Bind; | 
|  | 20 | +use hyperactor::Context; | 
|  | 21 | +use hyperactor::Handler; | 
|  | 22 | +use hyperactor::Named; | 
|  | 23 | +use hyperactor::PortRef; | 
|  | 24 | +use hyperactor::Unbind; | 
|  | 25 | +use hyperactor_mesh::bootstrap::BootstrapCommand; | 
|  | 26 | +use hyperactor_mesh::comm::multicast::CastInfo; | 
|  | 27 | +use hyperactor_mesh::proc_mesh::global_root_client; | 
|  | 28 | +use hyperactor_mesh::v1::host_mesh::HostMesh; | 
|  | 29 | +use ndslice::Point; | 
|  | 30 | +use ndslice::ViewExt; | 
|  | 31 | +use ndslice::extent; | 
|  | 32 | +use serde::Deserialize; | 
|  | 33 | +use serde::Serialize; | 
|  | 34 | +use tokio::time::Instant; | 
|  | 35 | + | 
|  | 36 | +#[derive(Actor, Default, Debug)] | 
|  | 37 | +#[hyperactor::export( | 
|  | 38 | +    spawn = true, | 
|  | 39 | +    handlers = [ | 
|  | 40 | +        TestMessage { cast = true }, | 
|  | 41 | +    ], | 
|  | 42 | +)] | 
|  | 43 | +struct TestActor {} | 
|  | 44 | + | 
|  | 45 | +#[derive(Debug, Serialize, Deserialize, Named, Clone, Bind, Unbind)] | 
|  | 46 | +enum TestMessage { | 
|  | 47 | +    Ping(#[binding(include)] PortRef<Point>), | 
|  | 48 | +} | 
|  | 49 | + | 
|  | 50 | +#[async_trait] | 
|  | 51 | +impl Handler<TestMessage> for TestActor { | 
|  | 52 | +    async fn handle( | 
|  | 53 | +        &mut self, | 
|  | 54 | +        cx: &Context<Self>, | 
|  | 55 | +        message: TestMessage, | 
|  | 56 | +    ) -> Result<(), anyhow::Error> { | 
|  | 57 | +        match message { | 
|  | 58 | +            TestMessage::Ping(reply) => reply.send(cx, cx.cast_point())?, | 
|  | 59 | +        } | 
|  | 60 | +        Ok(()) | 
|  | 61 | +    } | 
|  | 62 | +} | 
|  | 63 | + | 
|  | 64 | +#[tokio::main] | 
|  | 65 | +async fn main() { | 
|  | 66 | +    hyperactor_telemetry::initialize_logging_for_test(); | 
|  | 67 | + | 
|  | 68 | +    let host_mesh = HostMesh::process(extent!(hosts = 8), BootstrapCommand::current().unwrap()) | 
|  | 69 | +        .await | 
|  | 70 | +        .unwrap(); | 
|  | 71 | + | 
|  | 72 | +    let instance = global_root_client(); | 
|  | 73 | + | 
|  | 74 | +    let proc_mesh = host_mesh | 
|  | 75 | +        .spawn(instance, "test", extent!(procs = 2)) | 
|  | 76 | +        .await | 
|  | 77 | +        .unwrap(); | 
|  | 78 | + | 
|  | 79 | +    let actor_mesh = proc_mesh | 
|  | 80 | +        .spawn::<TestActor>(instance, "test", &()) | 
|  | 81 | +        .await | 
|  | 82 | +        .unwrap(); | 
|  | 83 | + | 
|  | 84 | +    loop { | 
|  | 85 | +        let mut received = HashSet::new(); | 
|  | 86 | +        let (port, mut rx) = instance.open_port(); | 
|  | 87 | +        let begin = Instant::now(); | 
|  | 88 | +        actor_mesh | 
|  | 89 | +            .cast(instance, TestMessage::Ping(port.bind())) | 
|  | 90 | +            .unwrap(); | 
|  | 91 | +        while received.len() < actor_mesh.extent().num_ranks() { | 
|  | 92 | +            received.insert(rx.recv().await.unwrap()); | 
|  | 93 | +        } | 
|  | 94 | + | 
|  | 95 | +        eprintln!("ping {}ms", begin.elapsed().as_millis()); | 
|  | 96 | +        tokio::time::sleep(Duration::from_secs(1)).await; | 
|  | 97 | +    } | 
|  | 98 | +} | 
0 commit comments