Skip to content

Commit

Permalink
Fix slow performace (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
juchiast authored Nov 30, 2023
1 parent 782e138 commit 1cfc9a7
Show file tree
Hide file tree
Showing 15 changed files with 190 additions and 44 deletions.
58 changes: 51 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion crates/cmds-std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ name = "cmds-std"
version = "0.0.0"
edition = "2021"

[[bench]]
name = "postgrest"
harness = false

[dependencies]
flow-lib = { workspace = true }

Expand All @@ -17,7 +21,9 @@ rust_decimal = { version = "1.32.0", features = ["serde-with-float"] }
tracing = "0.1.40"
bytes = "1.5.0"
mime_guess = "2.0.4"
postgrest = { git = "https://github.com/space-operator/postgrest-rs", rev = "5f8e53bde0fd30f91f84242d51dbb75f0e7f9fa9" }
postgrest = { git = "https://github.com/space-operator/postgrest-rs", rev = "8e371bb3d42710353d9fc0b5573a9f4cb7d88255" }

[dev-dependencies]
tokio = { version = "1", features = ["rt", "macros"] }
criterion = "0.5"
futures-executor = "0.3"
69 changes: 69 additions & 0 deletions crates/cmds-std/benches/postgrest.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use cmds_std::postgrest::builder_select;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use flow_lib::{
value::{self, array, map},
Context, Value,
};
use reqwest::header::{HeaderMap, HeaderValue};

fn build_header() -> HeaderMap {
let mut map = HeaderMap::new();
map.insert("accept", HeaderValue::from_str("application/json").unwrap());
map
}

pub fn criterion_benchmark(c: &mut Criterion) {
let json = serde_json::from_str::<serde_json::Value>(
r#"
{
"url": "https://base.spaceoperator.com/rest/v1/table",
"body": null,
"is_rpc": false,
"method": "GET",
"schema": null,
"headers": [
[
"accept",
"application/json"
]
],
"queries": []
}"#,
)
.unwrap();
let params = map! {
"query" => json,
"columns" => "*",
};
let cmd = builder_select::build().unwrap();
let ctx = Context::default();
c.bench_function("run_command", |b| {
b.iter(|| {
let fut = cmd.run(black_box(ctx.clone()), black_box(params.clone()));
futures_executor::block_on(fut)
})
});
c.bench_function("deserialize", |b| {
b.iter(|| value::from_map::<builder_select::Input>(black_box(params.clone())).unwrap())
});
let query = value::from_map::<builder_select::Input>(params)
.unwrap()
.query;
c.bench_function("serialize", |b| {
b.iter(|| {
value::to_map(&black_box(builder_select::Output {
query: query.clone(),
}))
.unwrap()
})
});
c.bench_function("build_header", |b| b.iter(|| build_header()));
let value = Value::Array(array![array!["accept", "application/json"]]);
c.bench_function("deser_vec_tuple", |b| {
b.iter(|| value::from_value::<Vec<(String, String)>>(black_box(value.clone())).unwrap())
});
c.bench_function("new_reqwest_client", |b| b.iter(|| reqwest::Client::new()));
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
10 changes: 6 additions & 4 deletions crates/cmds-std/src/postgrest/builder_eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@ const NAME: &str = "postgrest_builder_eq";

#[derive(Deserialize, Debug)]
struct Input {
query: postgrest::Builder,
query: postgrest::Query,
column: String,
filter: String,
}

#[derive(Serialize, Debug)]
struct Output {
query: postgrest::Builder,
query: postgrest::Query,
}

async fn run(_: Context, input: Input) -> Result<Output, CommandError> {
async fn run(ctx: Context, input: Input) -> Result<Output, CommandError> {
Ok(Output {
query: input.query.eq(input.column, input.filter),
query: postgrest::Builder::from_query(input.query, ctx.http)
.eq(input.column, input.filter)
.into(),
})
}

Expand Down
10 changes: 6 additions & 4 deletions crates/cmds-std/src/postgrest/builder_insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@ const NAME: &str = "postgrest_builder_insert";

#[derive(Deserialize, Debug)]
struct Input {
query: postgrest::Builder,
query: postgrest::Query,
body: JsonValue,
}

#[derive(Serialize, Debug)]
struct Output {
query: postgrest::Builder,
query: postgrest::Query,
}

async fn run(_: Context, input: Input) -> Result<Output, CommandError> {
async fn run(ctx: Context, input: Input) -> Result<Output, CommandError> {
Ok(Output {
query: input.query.insert(serde_json::to_string(&input.body)?),
query: postgrest::Builder::from_query(input.query, ctx.http)
.insert(serde_json::to_string(&input.body)?)
.into(),
})
}

Expand Down
10 changes: 6 additions & 4 deletions crates/cmds-std/src/postgrest/builder_is.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@ const NAME: &str = "postgrest_builder_is";

#[derive(Deserialize, Debug)]
struct Input {
query: postgrest::Builder,
query: postgrest::Query,
column: String,
filter: String,
}

#[derive(Serialize, Debug)]
struct Output {
query: postgrest::Builder,
query: postgrest::Query,
}

async fn run(_: Context, input: Input) -> Result<Output, CommandError> {
async fn run(ctx: Context, input: Input) -> Result<Output, CommandError> {
Ok(Output {
query: input.query.is(input.column, input.filter),
query: postgrest::Builder::from_query(input.query, ctx.http)
.is(input.column, input.filter)
.into(),
})
}

Expand Down
10 changes: 6 additions & 4 deletions crates/cmds-std/src/postgrest/builder_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@ const NAME: &str = "postgrest_builder_order";

#[derive(Deserialize, Debug)]
struct Input {
query: postgrest::Builder,
query: postgrest::Query,
columns: String,
}

#[derive(Serialize, Debug)]
struct Output {
query: postgrest::Builder,
query: postgrest::Query,
}

async fn run(_: Context, input: Input) -> Result<Output, CommandError> {
async fn run(ctx: Context, input: Input) -> Result<Output, CommandError> {
Ok(Output {
query: input.query.order(input.columns),
query: postgrest::Builder::from_query(input.query, ctx.http)
.order(input.columns)
.into(),
})
}

Expand Down
18 changes: 10 additions & 8 deletions crates/cmds-std/src/postgrest/builder_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,25 @@ use flow_lib::command::prelude::*;
const NAME: &str = "postgrest_builder_select";

#[derive(Deserialize, Debug)]
struct Input {
query: postgrest::Builder,
columns: String,
pub struct Input {
pub query: postgrest::Query,
pub columns: String,
}

#[derive(Serialize, Debug)]
struct Output {
query: postgrest::Builder,
pub struct Output {
pub query: postgrest::Query,
}

async fn run(_: Context, input: Input) -> Result<Output, CommandError> {
async fn run(ctx: Context, input: Input) -> Result<Output, CommandError> {
Ok(Output {
query: input.query.select(input.columns),
query: postgrest::Builder::from_query(input.query, ctx.http)
.select(input.columns)
.into(),
})
}

fn build() -> BuildResult {
pub fn build() -> BuildResult {
Ok(
CmdBuilder::new(flow_lib::node_definition!("postgrest/builder_select.json"))?
.check_name(NAME)?
Expand Down
10 changes: 6 additions & 4 deletions crates/cmds-std/src/postgrest/builder_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@ const NAME: &str = "postgrest_builder_update";

#[derive(Deserialize, Debug)]
struct Input {
query: postgrest::Builder,
query: postgrest::Query,
body: serde_json::Map<String, JsonValue>,
}

#[derive(Serialize, Debug)]
struct Output {
query: postgrest::Builder,
query: postgrest::Query,
}

async fn run(_: Context, input: Input) -> Result<Output, CommandError> {
async fn run(ctx: Context, input: Input) -> Result<Output, CommandError> {
Ok(Output {
query: input.query.update(serde_json::to_string(&input.body)?),
query: postgrest::Builder::from_query(input.query, ctx.http)
.update(serde_json::to_string(&input.body)?)
.into(),
})
}

Expand Down
4 changes: 2 additions & 2 deletions crates/cmds-std/src/postgrest/execute_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const NAME: &str = "postgrest_execute_query";

#[derive(Deserialize, Debug)]
struct Input {
query: postgrest::Builder,
query: postgrest::Query,
#[serde(default)]
pub headers: Vec<(String, String)>,
}
Expand All @@ -35,7 +35,7 @@ async fn run(mut ctx: Context, input: Input) -> Result<ValueSet, CommandError> {
.url
.starts_with(&format!("{}/rest/v1", ctx.endpoints.supabase));

let mut req = input.query.build();
let mut req = postgrest::Builder::from_query(input.query, ctx.http.clone()).build();
for (k, v) in input.headers {
req = req.header(k, v);
}
Expand Down
Loading

0 comments on commit 1cfc9a7

Please sign in to comment.