optimizations

This commit is contained in:
2024-12-29 20:08:10 -05:00
parent a20db6c522
commit be85ea3aa6
8 changed files with 165 additions and 109 deletions

View File

@@ -7,6 +7,7 @@ use server::core::{
};
use std::error::Error;
use std::time::Duration;
use tokio::select;
use tokio::sync::mpsc;
use tokio::sync::mpsc::Sender;
use tokio::time::Instant;
@@ -38,30 +39,36 @@ impl Telemetry {
let client_stored = client.clone();
let cancel = CancellationToken::new();
let cancel_stored = cancel.clone();
let (local_tx, mut local_rx) = mpsc::channel(128);
let (local_tx, mut local_rx) = mpsc::channel(16);
tokio::spawn(async move {
while !cancel.is_cancelled() {
let (server_tx, server_rx) = mpsc::channel(1);
let (server_tx, server_rx) = mpsc::channel(16);
let response_stream = client
.insert_telemetry(ReceiverStream::new(server_rx))
.await;
if let Ok(response_stream) = response_stream {
let mut response_stream = response_stream.into_inner();
while let Some(item) = local_rx.recv().await {
match server_tx.send(item).await {
Ok(_) => {}
Err(_) => break,
}
if let Some(response) = response_stream.next().await {
match response {
Ok(_) => {}
Err(_) => {
break;
loop {
select! {
_ = cancel.cancelled() => {
break;
},
Some(item) = local_rx.recv() => {
match server_tx.send(item).await {
Ok(_) => {}
Err(_) => break,
}
}
} else {
break;
},
Some(response) = response_stream.next() => {
match response {
Ok(_) => {}
Err(_) => {
break;
}
}
},
else => break,
}
}
} else {
@@ -136,6 +143,18 @@ impl TelemetryItemHandle {
async fn main() -> Result<(), Box<dyn Error>> {
let mut tlm = Telemetry::new("http://[::1]:50051").await?;
let index_handle = tlm
.register("simple_producer/time_offset".into(), TelemetryDataType::Float64)
.await?;
let publish_offset = tlm
.register("simple_producer/publish_offset".into(), TelemetryDataType::Float64)
.await?;
let await_offset = tlm
.register("simple_producer/await_offset".into(), TelemetryDataType::Float64)
.await?;
let sin_tlm_handle = tlm
.register("simple_producer/sin".into(), TelemetryDataType::Float32)
.await?;
@@ -187,84 +206,98 @@ async fn main() -> Result<(), Box<dyn Error>> {
});
}
let mut next_time = Instant::now();
let start_time = chrono::Utc::now();
let start_instant = Instant::now();
let mut next_time = start_instant;
let mut index = 0;
let mut tasks = vec![];
while !cancellation_token.is_cancelled() {
next_time += Duration::from_millis(10);
index += 1;
tokio::time::sleep_until(next_time).await;
sin_tlm_handle
let publish_time = start_time + chrono::TimeDelta::from_std(next_time - start_instant).unwrap();
let actual_time = Instant::now();
tasks.push(index_handle
.publish(
Value::Float64((actual_time - next_time).as_secs_f64()),
chrono::Utc::now(),
));
tasks.push(sin_tlm_handle
.publish(
Value::Float32((f32::TAU() * (index as f32) / (1000.0_f32)).sin()),
chrono::Utc::now(),
)
.await?;
cos_tlm_handle
publish_time,
));
tasks.push(cos_tlm_handle
.publish(
Value::Float64((f64::TAU() * (index as f64) / (1000.0_f64)).cos()),
chrono::Utc::now(),
)
.await?;
sin2_tlm_handle
publish_time,
));
tasks.push(sin2_tlm_handle
.publish(
Value::Float32((f32::TAU() * (index as f32) / (500.0_f32)).sin()),
chrono::Utc::now(),
)
.await?;
cos2_tlm_handle
publish_time,
));
tasks.push(cos2_tlm_handle
.publish(
Value::Float64((f64::TAU() * (index as f64) / (500.0_f64)).cos()),
chrono::Utc::now(),
)
.await?;
sin3_tlm_handle
publish_time,
));
tasks.push(sin3_tlm_handle
.publish(
Value::Float32((f32::TAU() * (index as f32) / (333.0_f32)).sin()),
chrono::Utc::now(),
)
.await?;
cos3_tlm_handle
publish_time,
));
tasks.push(cos3_tlm_handle
.publish(
Value::Float64((f64::TAU() * (index as f64) / (333.0_f64)).cos()),
chrono::Utc::now(),
)
.await?;
sin4_tlm_handle
publish_time,
));
tasks.push(sin4_tlm_handle
.publish(
Value::Float32((f32::TAU() * (index as f32) / (250.0_f32)).sin()),
chrono::Utc::now(),
)
.await?;
cos4_tlm_handle
publish_time,
));
tasks.push(cos4_tlm_handle
.publish(
Value::Float64((f64::TAU() * (index as f64) / (250.0_f64)).cos()),
chrono::Utc::now(),
)
.await?;
sin5_tlm_handle
publish_time,
));
tasks.push(sin5_tlm_handle
.publish(
Value::Float32((f32::TAU() * (index as f32) / (200.0_f32)).sin()),
chrono::Utc::now(),
)
.await?;
cos5_tlm_handle
publish_time,
));
tasks.push(cos5_tlm_handle
.publish(
Value::Float64((f64::TAU() * (index as f64) / (200.0_f64)).cos()),
chrono::Utc::now(),
)
.await?;
sin6_tlm_handle
publish_time,
));
tasks.push(sin6_tlm_handle
.publish(
Value::Float32((f32::TAU() * (index as f32) / (166.0_f32)).sin()),
chrono::Utc::now(),
)
.await?;
cos6_tlm_handle
publish_time,
));
tasks.push(cos6_tlm_handle
.publish(
Value::Float64((f64::TAU() * (index as f64) / (166.0_f64)).cos()),
publish_time,
));
tasks.push(publish_offset
.publish(
Value::Float64((Instant::now() - actual_time).as_secs_f64()),
chrono::Utc::now(),
)
.await?;
));
for task in tasks.drain(..) {
task.await?;
}
tasks.push(await_offset
.publish(
Value::Float64((Instant::now() - actual_time).as_secs_f64()),
chrono::Utc::now(),
));
}
Ok(())