Skip to content

Commit

Permalink
perf(qe): Examples useful for performance analysis of connecting to D…
Browse files Browse the repository at this point in the history
…B servers (prisma#3892)

* Update benchmarking/profiling scripts

* Add a few useful examples for benchmarking / profiling

* Update Dockerfile to profile code snippets in typescript too
  • Loading branch information
Miguel Fernández authored Apr 26, 2023
1 parent 03571f8 commit 993890f
Show file tree
Hide file tree
Showing 15 changed files with 234 additions and 115 deletions.
82 changes: 76 additions & 6 deletions Cargo.lock

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

9 changes: 6 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ LIBRARY_EXT := $(shell \

default: build

#####################
# Boostrap commands #
#####################
###################
# script wrappers #
###################

bootstrap-darwin:
script/bootstrap-darwin

profile-shell:
script/profile-shell

##################
# Build commands #
##################
Expand Down
5 changes: 5 additions & 0 deletions query-engine/connectors/sql-query-connector/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,8 @@ version = "1.2"
[dependencies.user-facing-errors]
features = ["sql"]
path = "../../../libs/user-facing-errors"

[dev-dependencies]
postgres = "0.19"
tokio-postgres = "0.7"
mysql_async = { git="https://github.com/prisma/mysql_async", branch="vendored-openssl" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use mysql_async::prelude::*;
use std::time::Instant;

#[tokio::main]
async fn main() -> () {
let url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be set to a mysql URL");
let start = Instant::now();

let now = Instant::now();
let pool = mysql_async::Pool::from_url(&url).unwrap();
println!("Building pool: {:?}", now.elapsed());

let mut conn = pool.get_conn().await.unwrap();
println!("Checking out connection: {:?}", now.elapsed());

let now = Instant::now();
let _: Vec<u8> = conn.query("SELECT 1").await.unwrap();
println!("Query: {:?}", now.elapsed());

println!("Total: {:?}", start.elapsed());
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use std::time::Instant;

use quaint::{pooled::Quaint, prelude::Queryable};

#[tokio::main]
async fn main() -> () {
let url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be set to a quaint-compatible database URL");
let start = Instant::now();

let now = Instant::now();
let mut builder = Quaint::builder(&url).expect("should connect");
builder.health_check_interval(std::time::Duration::from_secs(15));
builder.test_on_check_out(true);
let pool = builder.build();
println!("Building pool: {:?}", now.elapsed());

let now = Instant::now();
let conn = pool.check_out().await.unwrap();
println!("Checking out connection: {:?}", now.elapsed());

let now = Instant::now();
conn.execute_raw("SELECT 1", &[]).await.unwrap();
println!("Query: {:?}", now.elapsed());

println!("Total: {:?}", start.elapsed());
}
25 changes: 25 additions & 0 deletions query-engine/connectors/sql-query-connector/examples/tokio_pg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use std::time::Instant;

#[tokio::main]
async fn main() -> () {
let url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be set to a postgres URL");
let start = Instant::now();

let now = Instant::now();
let (client, connection) = tokio_postgres::connect(&url, postgres::NoTls).await.unwrap();
println!("Connect: {:?}", now.elapsed());

tokio::spawn(async move {
let now = Instant::now();
if let Err(e) = connection.await {
eprintln!("connection error: {}", e);
}
eprintln!("connection time: {:?}", now.elapsed());
});

let now = Instant::now();
client.query("SELECT 1;", &[]).await.unwrap();
println!("Query: {:?}", now.elapsed());

println!("Total: {:?}", start.elapsed());
}
33 changes: 33 additions & 0 deletions script/.profile-shell/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
ARG RUST_VERSION
FROM rust:$RUST_VERSION-buster
RUN printf "deb http://ftp.de.debian.org/debian buster main" >> /etc/apt/sources.list
RUN apt update
RUN apt-get install -y git valgrind linux-perf strace

RUN curl -fsSL https://fnm.vercel.app/install | bash
RUN source ~/.bashrc
RUN fnm install v16

# Symlink caches
RUN mkdir -p /.cargo/registry
RUN mkdir .cargo/git
RUN rm -rf /usr/local/cargo/git \
/usr/local/cargo/registry \
/usr/local/cargo/.package-cache \
/usr/local/cargo/.crates.toml \
/usr/local/cargo/.crates2.json
RUN ln -s /.cargo/git /usr/local/cargo
RUN ln -s /.cargo/registry /usr/local/cargo
RUN ln -s /.cargo/.package-cache /usr/local/cargo
RUN ln -s /.cargo/.crates.toml /usr/local/cargo
RUN ln -s /.cargo/.crates2.json /usr/local/cargo

# Set env
ENV PATH="${PATH}:/prisma-engines/script/.profile-shell/bin"
ENV VALGRIND_LIB=/usr/lib/aarch64-linux-gnu/valgrind/
ENV CARGO_TARGET_DIR=target-alternatives

# Install cargo-flamegraph
RUN cargo install flamegraph

CMD ["/bin/bash"]
4 changes: 4 additions & 0 deletions script/.profile-shell/bin/build
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#/bin/bash
CMD="cargo build --profile=profiling --examples --benches"
echo "==> Running command: $CMD"
cd /prisma-engines && eval "$CMD"
26 changes: 0 additions & 26 deletions script/.profile/Dockerfile

This file was deleted.

3 changes: 0 additions & 3 deletions script/.profile/bin/build

This file was deleted.

3 changes: 0 additions & 3 deletions script/.profile/bin/build-and-run

This file was deleted.

23 changes: 0 additions & 23 deletions script/.profile/bin/run-valgrind

This file was deleted.

Loading

0 comments on commit 993890f

Please sign in to comment.