-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Review wapod-pherry, wapod-types and the benchmark app #1
Open
kvinwang
wants to merge
9
commits into
review-a
Choose a base branch
from
review-b
base: review-a
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c9073e2
Update curve25519-dalek
kvinwang 6035ab9
Review wapod-pherry, wapod-types and benchmark
kvinwang c793a75
Remove log
kvinwang 79f71d6
Remove log
kvinwang 85626ff
bench: Use warn instead of info for net time failures
kvinwang 561e59b
bench: Use warn instead of info for net time failures
kvinwang 131de7d
code style
kvinwang 7c3848f
code style
kvinwang f1084b9
Merge branch 'main' into review-b
kvinwang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
[package] | ||
name = "benchmark" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
log = "0.4.16" | ||
once_cell = "1.10.0" | ||
sha2 = "0.10.8" | ||
tokio = { version = "1.38.0", features = ["macros"] } | ||
anyhow = "1.0.86" | ||
httptime = { version = "0.1.0", path = "../../httptime" } | ||
futures = "0.3.30" | ||
serde = { version = "1.0.203", features = ["derive"] } | ||
wapod-types = { path = "../../wapod-types" } | ||
serde_json = "1.0.117" | ||
|
||
[dependencies.wapo] | ||
version = "0.1.0" | ||
path = "../../wapo" | ||
features = ["hyper-v0"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"label": "worker-benchmark", | ||
"output_dir": "dist", | ||
"wasm_code": "../../target/wasm32-wasi/release/benchmark.wasm", | ||
"args": [], | ||
"env_vars": { | ||
"RUST_LOG": "info" | ||
}, | ||
"on_demand": false, | ||
"resizable": true, | ||
"max_query_size": 102400, | ||
"deps": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
fn sha256(data: &[u8]) -> [u8; 32] { | ||
use sha2::{Digest, Sha256}; | ||
let mut hasher = Sha256::new(); | ||
hasher.update(data); | ||
hasher.finalize().into() | ||
} | ||
|
||
pub async fn benchmark() { | ||
let mut buffer = sha256(b"init"); | ||
loop { | ||
for _ in 0..10000 { | ||
buffer = sha256(&buffer); | ||
} | ||
std::hint::black_box(&buffer); | ||
wapo::time::breath().await; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#![cfg_attr(not(test), no_main)] | ||
|
||
mod bench; | ||
mod query; | ||
|
||
#[wapo::main] | ||
async fn main() { | ||
wapo::logger::init(); | ||
tokio::select! { | ||
_ = bench::benchmark() => {} | ||
_ = query::query_serve() => {} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
use anyhow::{bail, Result}; | ||
use futures::stream::FuturesUnordered; | ||
use futures::StreamExt as _; | ||
use log::{debug, info, warn}; | ||
use std::time::{Duration, Instant, SystemTime}; | ||
use tokio::sync::mpsc::Sender; | ||
|
||
use wapod_types::bench_app::{BenchScore, SignedMessage, SigningMessage}; | ||
use wapod_types::scale::Encode; | ||
|
||
struct State { | ||
score: BenchScore, | ||
} | ||
|
||
pub async fn query_serve() { | ||
let query_rx = wapo::channel::incoming_queries(); | ||
|
||
let (score_tx, mut score_rx) = tokio::sync::mpsc::channel(1); | ||
|
||
debug!("spawning score update task"); | ||
wapo::spawn_named("score update", score_update(score_tx)); | ||
|
||
let mut state = State { | ||
score: BenchScore::default(), | ||
}; | ||
|
||
loop { | ||
tokio::select! { | ||
query = query_rx.next() => { | ||
let Some(query) = query else { | ||
break; | ||
}; | ||
match handle_query(&mut state, query.path, query.payload).await { | ||
Ok(reply) => { | ||
_ = query.reply_tx.send(&reply); | ||
} | ||
Err(e) => { | ||
_ = query.reply_tx.send_error(&e.to_string()); | ||
} | ||
} | ||
} | ||
score = score_rx.recv() => { | ||
let Some(score) = score else { | ||
info!("score channel closed"); | ||
break; | ||
}; | ||
state.score = score; | ||
} | ||
} | ||
} | ||
} | ||
|
||
async fn score_update(tx: Sender<BenchScore>) { | ||
debug!("score update task started"); | ||
loop { | ||
let net_start_time = net_now().await; | ||
let local_start_time = Instant::now(); | ||
let (gas_at_start, _) = | ||
wapo::ocall::app_gas_consumed().expect("failed to get gas consumed"); | ||
debug!("net_start_time: {:?}", net_start_time); | ||
debug!("gas_at_start: {:?}", gas_at_start); | ||
|
||
wapo::time::sleep(Duration::from_secs(60)).await; | ||
|
||
let (gas_at_end, token) = | ||
wapo::ocall::app_gas_consumed().expect("failed to get gas consumed"); | ||
let local_elapsed = local_start_time.elapsed(); | ||
let net_end_time = net_now().await; | ||
debug!("net_end_time: {:?}", net_end_time); | ||
debug!("gas_at_end: {:?}", gas_at_end); | ||
|
||
let (Ok(net_start_time), Ok(net_end_time)) = (&net_start_time, &net_end_time) else { | ||
warn!("failed to get net time, skipping score update"); | ||
continue; | ||
}; | ||
let Ok(net_elapsed) = net_end_time.duration_since(*net_start_time) else { | ||
warn!("invalid net time, skipping score update"); | ||
continue; | ||
}; | ||
let diff = local_elapsed.as_secs_f64() - net_elapsed.as_secs_f64(); | ||
if diff.abs() > 5_f64 { | ||
warn!("time diff between local and net is too large: {diff}"); | ||
continue; | ||
} | ||
let gas_diff = gas_at_end.saturating_sub(gas_at_start); | ||
let score = gas_diff.saturating_div(local_elapsed.as_secs()); | ||
debug!("score: {:?}", score); | ||
let score = BenchScore { | ||
gas_per_second: score, | ||
gas_consumed: gas_at_end, | ||
timestamp_secs: net_end_time | ||
.duration_since(SystemTime::UNIX_EPOCH) | ||
.expect("failed to get timestamp") | ||
.as_secs(), | ||
metrics_token: token, | ||
}; | ||
tx.send(score).await.expect("failed to send score"); | ||
} | ||
} | ||
|
||
async fn net_now() -> Result<SystemTime> { | ||
let servers = [ | ||
"https://www.cloudflare.com", | ||
"https://www.apple.com", | ||
"https://www.baidu.com", | ||
"https://kernel.org/", | ||
]; | ||
|
||
let mut futures = FuturesUnordered::new(); | ||
for server in servers { | ||
futures.push(httptime::get_time(server, Duration::from_secs(2))); | ||
} | ||
loop { | ||
let Some(result) = futures.next().await else { | ||
bail!("all servers failed"); | ||
}; | ||
if let Ok(time) = result { | ||
return Ok(time); | ||
} | ||
} | ||
} | ||
|
||
async fn handle_query(state: &mut State, path: String, _payload: Vec<u8>) -> Result<Vec<u8>> { | ||
match path.as_str() { | ||
"/version" => Ok(env!("CARGO_PKG_VERSION").as_bytes().to_vec()), | ||
"/score" => serde_json::to_vec(&state.score) | ||
.map_err(|e| anyhow::anyhow!("failed to serialize score: {}", e)), | ||
"/signedScore" => { | ||
let score = &state.score; | ||
let message = SigningMessage::BenchScore(score.clone()); | ||
let encoded_message = message.encode(); | ||
let signature = wapo::ocall::sign(&encoded_message) | ||
.expect("ocall::sign never fails") | ||
.into(); | ||
let address = wapo::ocall::app_address().expect("failed to get app address"); | ||
let worker_pubkey = wapo::ocall::worker_pubkey().expect("failed to get worker pubkey"); | ||
let signed_message = SignedMessage { | ||
message, | ||
signature, | ||
worker_pubkey, | ||
app_address: address, | ||
}; | ||
Ok(signed_message.encode()) | ||
} | ||
_ => { | ||
bail!("unknown path: {}", path); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/target | ||
/data |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fetch timestamp from the internet via the HTTP response header
Date: xxxx-xx-xx...
.