Skip to content

Commit 1e8fa34

Browse files
committed
add initial benchmarks, renamed apis
1 parent fd7db86 commit 1e8fa34

6 files changed

Lines changed: 273 additions & 296 deletions

File tree

README.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ An experimental SQLite library for Node using Neon
2020
* Target latest node and electron versions
2121
* Promise based API
2222
* Blob support
23+
* ESNext `BigInt` support
2324
* SQLite extensions support
2425
* Typescript support
2526
* Query caching support
@@ -35,7 +36,7 @@ import Sqlite from '@sqlite/sqlite';
3536
const connector = new Sqlite();
3637

3738
// Creating a regular async connection
38-
const conn = await connection.create({
39+
const conn = await connection.open({
3940
database: '/path/to/database', // ':memory:'
4041
verbose: true // process.env.NODE_ENV !== 'production'
4142
});
@@ -48,7 +49,26 @@ await Promise.all([
4849

4950
// Statement currying
5051
const updateTables = await db.statement('UPDATE tbl SET name = ? WHERE email = ?');
51-
await updateTables('john', 'john@gmail.com');
52+
const result = await updateTables('john', 'john@gmail.com');
53+
console.log(result);
54+
55+
// Batching queries
56+
const updateTables = await db.batch(
57+
`
58+
ATTACH DATABASE ':memory:' AS my_attached;
59+
BEGIN;
60+
CREATE TABLE my_attached.foo(x INTEGER);
61+
INSERT INTO my_attached.foo VALUES(42);
62+
END
63+
`;
64+
);
65+
const updateTables = await db.batch([
66+
'ATTACH DATABASE ':memory:' AS my_attached;',
67+
'BEGIN;',
68+
'CREATE TABLE my_attached.foo(x INTEGER);',
69+
'INSERT INTO my_attached.foo VALUES(42);',
70+
'END'
71+
]);
5272
```
5373

5474
## Local Development

bench/basic.bench.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const sqlite3 = require('sqlite3');
2+
const Sqlite = require('../');
3+
4+
const t = process.hrtime();
5+
const db = new sqlite3.Database(':memory:');
6+
db.serialize(() => {
7+
db.run('CREATE TABLE lorem (info TEXT)');
8+
9+
const stmt = db.prepare('INSERT INTO lorem VALUES (?)');
10+
for (let i = 0; i < 10; i++) {
11+
stmt.run(`Ipsum ${i}`);
12+
}
13+
stmt.finalize();
14+
15+
db.each('SELECT rowid AS id, info FROM lorem', (err, row) => {
16+
// console.log(`${row.id}: ${row.info}`);
17+
});
18+
db.close();
19+
console.log('sqlite3', process.hrtime(t)[1]);
20+
});
21+
22+
const connector = new Sqlite.Sqlite();
23+
24+
(async () => {
25+
const s = process.hrtime();
26+
// Creating a regular async connection
27+
const conn = await connector.open({
28+
verbose: true // process.env.NODE_ENV !== 'production'
29+
});
30+
// conn.prepare('CREATE TABLE lorem (info TEXT)');
31+
// conn.close();
32+
console.log('neon sqlite', process.hrtime(s)[1]);
33+
})();
34+
35+
(async () => {
36+
const s = process.hrtime();
37+
// Creating a regular async connection
38+
// Sqlite.example();
39+
// conn.close();
40+
console.log('example', process.hrtime(s)[1]);
41+
})();

native/src/lib.rs

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![feature(test)]
2+
extern crate test;
13
#[macro_use]
24
extern crate neon;
35
extern crate rusqlite;
@@ -15,16 +17,6 @@ pub struct Sqlite {
1517
pub verbose: Option<bool>,
1618
}
1719

18-
fn map_err_to_js_err<T>(res: Result<T, rusqlite::Error>) -> T {
19-
match res {
20-
Ok(r) => r,
21-
Err(e) => {
22-
println!("asdfasfd{}", e);
23-
panic!(e);
24-
}
25-
}
26-
}
27-
2820
declare_types! {
2921
pub class JsSqlite for Sqlite {
3022
init(_cx) {
@@ -35,7 +27,7 @@ declare_types! {
3527
})
3628
}
3729

38-
method create(mut cx) {
30+
method open(mut cx) {
3931
let database_value: String;
4032
let verbose_value: bool;
4133

@@ -107,7 +99,15 @@ declare_types! {
10799
Ok(js_array.upcast())
108100
}
109101

110-
method statement(mut cx) {
102+
method prepare(mut cx) {
103+
let sql = cx.argument::<JsString>(0)?.value();
104+
let this = cx.this();
105+
106+
let _ = cx.borrow(&this, |sqlite| {
107+
let conn = (&sqlite.conn).as_ref().unwrap();
108+
let _ = conn.prepare(&sql).unwrap();
109+
});
110+
111111
Ok(cx.undefined().upcast())
112112
}
113113
}
@@ -118,3 +118,18 @@ register_module!(mut m, {
118118
m.export_function("version", version)?;
119119
Ok(())
120120
});
121+
122+
#[cfg(test)]
123+
mod tests {
124+
use super::*;
125+
use test::Bencher;
126+
127+
#[bench]
128+
fn bench_create_table_rusqlite(b: &mut Bencher) {
129+
b.iter(|| {
130+
let conn = Connection::open_in_memory().unwrap();
131+
let stmt = conn.prepare("CREATE TABLE lorem (info TEXT)").unwrap();
132+
// conn.close().unwrap();
133+
});
134+
}
135+
}

package.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@
1515
"test": "jest ."
1616
},
1717
"devDependencies": {
18+
"better-sqlite3": "^5.0.1",
1819
"eslint": "^5.9.0",
1920
"eslint-config-bliss": "^3.0.0",
20-
"jest": "^23.6.0"
21+
"jest": "^23.6.0",
22+
"sqlite3": "^4.0.4"
2123
},
2224
"eslintConfig": {
23-
"extends": "bliss",
24-
"rules": {
25-
"prettier/prettier": ["error", {"singleQuote": true, "parser": "flow"}]
26-
}
25+
"extends": "bliss"
2726
}
2827
}

test/basic.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('basic', () => {
1111
it('should execute query statements', async () => {
1212
const conn = new Sqlite();
1313

14-
await conn.create({
14+
await conn.open({
1515
verbose: true,
1616
database: path.join(__dirname, 'test.db')
1717
});
@@ -38,7 +38,7 @@ describe('basic', () => {
3838
it('should format errors', async () => {
3939
const conn = new Sqlite();
4040

41-
await conn.create({
41+
await conn.open({
4242
verbose: true,
4343
database: path.join(__dirname, 'test.db')
4444
});

0 commit comments

Comments
 (0)