Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(driver-adapters-executor): benchmark GH action #4667

Merged
merged 3 commits into from
Jan 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 26 additions & 19 deletions query-engine/driver-adapters/executor/src/recording.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
type Result,
type ResultSet,
} from "@prisma/driver-adapter-utils";
import { RetryHandler } from "undici";

type Recordings = ReturnType<typeof createInMemoryRecordings>;

export function recording(adapter: DriverAdapter) {
const recordings = createInMemoryRecordings();
Expand All @@ -15,7 +16,7 @@ export function recording(adapter: DriverAdapter) {
};
}

function recorder(adapter: DriverAdapter, recordings) {
function recorder(adapter: DriverAdapter, recordings: Recordings) {
return {
provider: adapter.provider,
startTransaction: () => {
Expand All @@ -25,20 +26,19 @@ function recorder(adapter: DriverAdapter, recordings) {
return adapter.getConnectionInfo!();
},
queryRaw: async (params) => {
const result = adapter.queryRaw(params);
const result = await adapter.queryRaw(params);
recordings.addQueryResults(params, result);
return result;
},

executeRaw: async (params) => {
const result = adapter.executeRaw(params);
const result = await adapter.executeRaw(params);
recordings.addCommandResults(params, result);
return result;
},
};
}

function replayer(adapter: DriverAdapter, recordings) {
function replayer(adapter: DriverAdapter, recordings: Recordings) {
return {
provider: adapter.provider,
recordings: recordings,
Expand All @@ -58,42 +58,49 @@ function replayer(adapter: DriverAdapter, recordings) {
}

function createInMemoryRecordings() {
const queryResults = {};
const commandResults = {};
const queryResults: Map<string, Result<ResultSet>> = new Map();
const commandResults: Map<string, Result<number>> = new Map();

// Recording is currently only used in benchmarks. Before we used to serialize the whole query
// (sql + args) but since bigints are not serialized by JSON.stringify, and we didn’t really need
// (sql + args) but since bigints are not serialized by JSON.stringify, and we didn't really need
// args for benchmarks, we just serialize the sql part.
//
// If this ever changes (we reuse query recording in tests) we need to make sure to serialize the
// args as well.
const queryToKey = (query) => JSON.stringify(query.sql);
const queryToKey = (params: Query) => {
return JSON.stringify(params.sql);
};

return {
addQueryResults: (params, result) => {
addQueryResults: (params: Query, result: Result<ResultSet>) => {
const key = queryToKey(params);
queryResults[key] = result;
queryResults.set(key, result);
},

getQueryResults: (params) => {
getQueryResults: (params: Query) => {
const key = queryToKey(params);
if (!(key in queryResults)) {

if (!queryResults.has(key)) {
throw new Error(`Query not recorded: ${key}`);
}
return queryResults[key];

return queryResults.get(key)!;
},

addCommandResults: (params, result) => {
addCommandResults: (params: Query, result: Result<number>) => {
const key = queryToKey(params);
commandResults[key] = result;
commandResults.set(key, result);
},

getCommandResults: (params) => {
getCommandResults: (params: Query) => {
const key = queryToKey(params);
if (!(key in commandResults)) {

if (!commandResults.has(key)) {
throw new Error(`Command not recorded: ${key}`);
}
return commandResults[key];

return commandResults.get(key)!;
},
};
}
Loading