Skip to content

Commit

Permalink
fix(driver-adapters-executor): benchmark GH action (#4667)
Browse files Browse the repository at this point in the history
Co-authored-by: Miguel Fernandez <[email protected]>
  • Loading branch information
jkomyno and miguelff authored Jan 23, 2024
1 parent 6d02932 commit e086e3a
Showing 1 changed file with 26 additions and 19 deletions.
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)!;
},
};
}

0 comments on commit e086e3a

Please sign in to comment.