Skip to content

Commit

Permalink
chore: benchmark in the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-online committed Apr 11, 2024
1 parent f4df794 commit 55191da
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,5 @@ Cargo.lock
!.yarn/versions

*.node

*.sqlite
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ sea-orm = { version = "0.12.15", features = [
"sqlx-mysql",
"sqlx-postgres",
"sqlx-sqlite",
"macros",
"macros"
] }
brinedb-entity = { path = "crates/entity" }
brinedb-migration = { path = "crates/migration" }

[build-dependencies]
napi-build = "2.0.1"
napi-build = "2.1.2"

[profile.release]
lto = true
Expand Down
4 changes: 3 additions & 1 deletion crates/migration/src/m20220101_000001_create_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ impl MigrationTrait for Migration {
.col(ColumnDef::new(Brine::Value).text())
.to_owned(),
)
.await
.await?;

Ok(())
}

async fn down(&self, _manager: &SchemaManager) -> Result<(), DbErr> {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@napi-rs/cli": "^2.18.0",
"@sapphire/ts-config": "^5.0.1",
"@vitest/coverage-v8": "^1.4.0",
"tinybench": "^2.6.0",
"tslib": "^2.6.2",
"tsup": "^8.0.2",
"typedoc": "^0.25.13",
Expand Down
21 changes: 18 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use migration::MigratorTrait;
use migration::OnConflict;
use napi::bindgen_prelude::*;
use sea_orm::ColumnTrait;
use sea_orm::ConnectOptions;
use sea_orm::ConnectionTrait;
use sea_orm::DatabaseConnection;
use sea_orm::EntityTrait;
use sea_orm::PaginatorTrait;
Expand Down Expand Up @@ -34,10 +36,23 @@ impl BrineDB {

#[napi]
pub async unsafe fn connect(&mut self) -> Result<bool> {
let connection = sea_orm::Database::connect(&self.connection_uri)
let opt = ConnectOptions::new(&self.connection_uri);

let connection = sea_orm::Database::connect(opt)
.await
.map_err(|err| Error::new(Status::GenericFailure, err.to_string()))?;

if self.connection_uri.starts_with("sqlite") {
connection
.execute_unprepared("PRAGMA journal_mode = wal;")
.await
.map_err(|err| Error::new(Status::GenericFailure, err.to_string()))?;
connection
.execute_unprepared("PRAGMA synchronous = 1;")
.await
.map_err(|err| Error::new(Status::GenericFailure, err.to_string()))?;
}

self.connection = Some(connection);

Ok(true)
Expand Down Expand Up @@ -83,12 +98,12 @@ impl BrineDB {
.as_ref()
.ok_or_else(|| Error::new(Status::GenericFailure, "No connection found"))?;

let doc = ActiveDocumentModel {
let model = ActiveDocumentModel {
key: Set(key),
value: Set(value),
};

Document::insert(doc)
Document::insert(model)
.on_conflict(
OnConflict::column(DocumentColumn::Key)
.update_column(DocumentColumn::Value)
Expand Down
63 changes: 63 additions & 0 deletions tests/bench.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { describe, test } from "vitest";
import { Bench } from "tinybench";
import Brine from "../src";
import { BrineDatabases } from "../dist";

const randomData = (length: number) => {
const chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
let result = "";

for (let i = 0; i < length; i++) {
result += chars.charAt(Math.floor(Math.random() * chars.length));
}

return result;
};

describe("Benchmark", () => {
test("Brine", async () => {
const bench = new Bench({ time: 200 });
const brinedb = new Brine(BrineDatabases.sqlite.file("test.sqlite"));

await brinedb.init();

const setInitialManyData: [string, string][] = [];
const size = 1_000;

await new Promise<void>((done) => {
for (let i = 0; i < size; i++) {
setInitialManyData.push([`key-${i}`, randomData(100)]);
}

done();
});

await brinedb.setMany(setInitialManyData);

bench
.add("get", async () => {
const res = await brinedb.get(
`key-${Math.floor(Math.random() * size)}`,
);

if (res == null) {
throw new Error("Value not found");
}
})
.add("set", async () => {
await brinedb.set(
`key-${Math.floor(Math.random() * size)}`,
randomData(100),
);
})
.add("count", async () => {
await brinedb.count();
});

await bench.warmup();
await bench.run();

console.table(bench.table());
});
});
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ __metadata:
"@napi-rs/cli": "npm:^2.18.0"
"@sapphire/ts-config": "npm:^5.0.1"
"@vitest/coverage-v8": "npm:^1.4.0"
tinybench: "npm:^2.6.0"
tslib: "npm:^2.6.2"
tsup: "npm:^8.0.2"
typedoc: "npm:^0.25.13"
Expand Down

0 comments on commit 55191da

Please sign in to comment.