Skip to content

Commit 0394861

Browse files
committed
sqlite: add statement duration payload property
1 parent 9d216f0 commit 0394861

2 files changed

Lines changed: 43 additions & 6 deletions

File tree

src/node_sqlite.cc

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,15 +1027,15 @@ bool DatabaseSync::Open() {
10271027
diagnostics_channel::Channel* ch =
10281028
diagnostics_channel::Channel::Get(env(), "sqlite.db.query");
10291029
if (ch != nullptr && ch->HasSubscribers()) {
1030-
sqlite3_trace_v2(connection_, SQLITE_TRACE_STMT, TraceCallback, this);
1030+
sqlite3_trace_v2(connection_, SQLITE_TRACE_PROFILE, TraceCallback, this);
10311031
}
10321032

10331033
return true;
10341034
}
10351035

10361036
void DatabaseSync::EnableTracing() {
10371037
if (!IsOpen()) return;
1038-
sqlite3_trace_v2(connection_, SQLITE_TRACE_STMT, TraceCallback, this);
1038+
sqlite3_trace_v2(connection_, SQLITE_TRACE_PROFILE, TraceCallback, this);
10391039
}
10401040

10411041
void DatabaseSync::DisableTracing() {
@@ -2591,7 +2591,7 @@ int DatabaseSync::TraceCallback(unsigned int type,
25912591
void* user_data,
25922592
void* p,
25932593
void* x) {
2594-
if (type != SQLITE_TRACE_STMT) {
2594+
if (type != SQLITE_TRACE_PROFILE) {
25952595
return 0;
25962596
}
25972597

@@ -2625,6 +2625,10 @@ int DatabaseSync::TraceCallback(unsigned int type,
26252625
}
26262626
}
26272627

2628+
// x points to the estimated statement run time in nanoseconds. A double is
2629+
// sufficient since 2^53 ns (~104 days) exceeds any realistic query duration.
2630+
sqlite3_int64 duration_ns = *static_cast<sqlite3_int64*>(x);
2631+
26282632
Local<Object> payload = Object::New(isolate);
26292633
if (payload
26302634
->Set(context,
@@ -2635,6 +2639,11 @@ int DatabaseSync::TraceCallback(unsigned int type,
26352639
->Set(context,
26362640
FIXED_ONE_BYTE_STRING(isolate, "database"),
26372641
db->object())
2642+
.IsNothing() ||
2643+
payload
2644+
->Set(context,
2645+
FIXED_ONE_BYTE_STRING(isolate, "duration"),
2646+
Number::New(isolate, static_cast<double>(duration_ns)))
26382647
.IsNothing()) {
26392648
return 0;
26402649
}

test/parallel/test-sqlite-trace.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,7 @@ suite('sqlite.db.query diagnostics channel', () => {
147147
const calls = [];
148148
const db1 = new DatabaseSync(':memory:');
149149
const db2 = new DatabaseSync(':memory:');
150-
t.after(() => {
151-
db1.close(); db2.close();
152-
});
150+
t.after(() => { db1.close(); db2.close(); });
153151

154152
const handler = (msg) => calls.push(msg);
155153
dc.subscribe('sqlite.db.query', handler);
@@ -163,4 +161,34 @@ suite('sqlite.db.query diagnostics channel', () => {
163161
assert.strictEqual(calls[1].database, db2);
164162
assert.notStrictEqual(calls[0].database, calls[1].database);
165163
});
164+
165+
it('duration is a number', (t) => {
166+
const calls = [];
167+
const db = new DatabaseSync(':memory:');
168+
t.after(() => db.close());
169+
170+
const handler = (msg) => calls.push(msg);
171+
dc.subscribe('sqlite.db.query', handler);
172+
t.after(() => dc.unsubscribe('sqlite.db.query', handler));
173+
174+
db.exec('CREATE TABLE t (x INTEGER)');
175+
176+
assert.strictEqual(calls.length, 1);
177+
assert.strictEqual(typeof calls[0].duration, 'number');
178+
});
179+
180+
it('duration is non-negative', (t) => {
181+
const calls = [];
182+
const db = new DatabaseSync(':memory:');
183+
t.after(() => db.close());
184+
185+
const handler = (msg) => calls.push(msg);
186+
dc.subscribe('sqlite.db.query', handler);
187+
t.after(() => dc.unsubscribe('sqlite.db.query', handler));
188+
189+
db.exec('CREATE TABLE t (x INTEGER)');
190+
191+
assert.strictEqual(calls.length, 1);
192+
assert.ok(calls[0].duration >= 0);
193+
});
166194
});

0 commit comments

Comments
 (0)