Skip to content

Commit

Permalink
perf(client): Use dmmf from a running engine (#16798)
Browse files Browse the repository at this point in the history
* perf(client): Use dmmf from a running engine

To get dmmf, engine needs to perform validation and internal model
construction. If we are re-using processed schema from running engines,
those steps could be skipped and we can get directly to schema <-> dmmf
conversion, saving some time.

Close prisma/client-planning#157

* Correctly sanitize prisma-fmt snapshots
  • Loading branch information
SevInf committed Dec 19, 2022
1 parent 98945e9 commit e6e95ea
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 53 deletions.
2 changes: 1 addition & 1 deletion packages/cli/src/__tests__/commands/Version.test.ts
Expand Up @@ -118,7 +118,7 @@ function cleanSnapshot(str: string, versionOverride?: string): string {

// TODO: replace '[a-z0-9]{40}' with 'ENGINE_VERSION'.
// Currently, the engine version of @prisma/prisma-fmt-wasm isn't necessarily the same as the enginesVersion
str = str.replace(new RegExp('([0-9]+.[0-9]+.[0-9]+-[0-9]+.)([a-z0-9]{40})', 'g'), 'CLI_VERSION.ENGINE_VERSION')
str = str.replace(/([0-9]+\.[0-9]+\.[0-9]+-[0-9]+\.)([a-z0-9-]+)/g, 'CLI_VERSION.ENGINE_VERSION')

// replace engine version hash
const defaultEngineVersion = enginesVersion
Expand Down
Expand Up @@ -9,7 +9,7 @@ exports[`tracing (provider=cockroachdb) tracing connect should trace the implici
attributes: {},
kind: 0,
links: [],
name: prisma:client:serialize,
name: prisma:client:connect,
parentSpanId: <parentSpanId>,
},
},
Expand All @@ -19,7 +19,7 @@ exports[`tracing (provider=cockroachdb) tracing connect should trace the implici
attributes: {},
kind: 0,
links: [],
name: prisma:client:connect,
name: prisma:client:serialize,
parentSpanId: <parentSpanId>,
},
},
Expand Down Expand Up @@ -1352,7 +1352,7 @@ exports[`tracing (provider=mongodb) tracing connect should trace the implicit $c
attributes: {},
kind: 0,
links: [],
name: prisma:client:serialize,
name: prisma:client:connect,
parentSpanId: <parentSpanId>,
},
},
Expand All @@ -1362,7 +1362,7 @@ exports[`tracing (provider=mongodb) tracing connect should trace the implicit $c
attributes: {},
kind: 0,
links: [],
name: prisma:client:connect,
name: prisma:client:serialize,
parentSpanId: <parentSpanId>,
},
},
Expand Down Expand Up @@ -2385,7 +2385,7 @@ exports[`tracing (provider=mysql) tracing connect should trace the implicit $con
attributes: {},
kind: 0,
links: [],
name: prisma:client:serialize,
name: prisma:client:connect,
parentSpanId: <parentSpanId>,
},
},
Expand All @@ -2395,7 +2395,7 @@ exports[`tracing (provider=mysql) tracing connect should trace the implicit $con
attributes: {},
kind: 0,
links: [],
name: prisma:client:connect,
name: prisma:client:serialize,
parentSpanId: <parentSpanId>,
},
},
Expand Down Expand Up @@ -3728,7 +3728,7 @@ exports[`tracing (provider=postgresql) tracing connect should trace the implicit
attributes: {},
kind: 0,
links: [],
name: prisma:client:serialize,
name: prisma:client:connect,
parentSpanId: <parentSpanId>,
},
},
Expand All @@ -3738,7 +3738,7 @@ exports[`tracing (provider=postgresql) tracing connect should trace the implicit
attributes: {},
kind: 0,
links: [],
name: prisma:client:connect,
name: prisma:client:serialize,
parentSpanId: <parentSpanId>,
},
},
Expand Down Expand Up @@ -5071,7 +5071,7 @@ exports[`tracing (provider=sqlite) tracing connect should trace the implicit $co
attributes: {},
kind: 0,
links: [],
name: prisma:client:serialize,
name: prisma:client:connect,
parentSpanId: <parentSpanId>,
},
},
Expand All @@ -5081,7 +5081,7 @@ exports[`tracing (provider=sqlite) tracing connect should trace the implicit $co
attributes: {},
kind: 0,
links: [],
name: prisma:client:connect,
name: prisma:client:serialize,
parentSpanId: <parentSpanId>,
},
},
Expand Down Expand Up @@ -6343,7 +6343,7 @@ exports[`tracing (provider=sqlserver) tracing connect should trace the implicit
attributes: {},
kind: 0,
links: [],
name: prisma:client:serialize,
name: prisma:client:connect,
parentSpanId: <parentSpanId>,
},
},
Expand All @@ -6353,7 +6353,7 @@ exports[`tracing (provider=sqlserver) tracing connect should trace the implicit
attributes: {},
kind: 0,
links: [],
name: prisma:client:connect,
name: prisma:client:serialize,
parentSpanId: <parentSpanId>,
},
},
Expand Down
81 changes: 43 additions & 38 deletions packages/client/tests/functional/tracing/tests.ts
Expand Up @@ -11,6 +11,7 @@ import {
} from '@opentelemetry/sdk-trace-base'
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'
import { PrismaInstrumentation } from '@prisma/instrumentation'
import { ClientEngineType, getClientEngineType } from '@prisma/internals'

import { NewPrismaClient } from '../_utils/types'
import testMatrix from './_matrix'
Expand Down Expand Up @@ -767,61 +768,65 @@ testMatrix.setupTestSuite(
await _prisma.$disconnect()
})

test('should trace the implicit $connect call', async () => {
const email = faker.internet.email()
// Different order of traces between binary and library
testIf(getClientEngineType() === ClientEngineType.Library)(
'should trace the implicit $connect call',
async () => {
const email = faker.internet.email()

await _prisma.user.findMany({
where: {
email: email,
},
})
await _prisma.user.findMany({
where: {
email: email,
},
})

const tree = await waitForSpanTree()
const tree = await waitForSpanTree()

expect(cleanSpanTreeForSnapshot(tree)).toMatchSnapshot()
expect(cleanSpanTreeForSnapshot(tree)).toMatchSnapshot()

expect(tree.span.name).toEqual('prisma:client:operation')
expect(tree.span.attributes['method']).toEqual('findMany')
expect(tree.span.attributes['model']).toEqual('User')
expect(tree.span.name).toEqual('prisma:client:operation')
expect(tree.span.attributes['method']).toEqual('findMany')
expect(tree.span.attributes['model']).toEqual('User')

expect(tree.children).toHaveLength(3)
expect(tree.children).toHaveLength(3)

const serialize = (tree?.children || [])[0] as unknown as Tree
expect(serialize.span.name).toEqual('prisma:client:serialize')
const connect = (tree?.children || [])[0] as unknown as Tree
expect(connect.span.name).toEqual('prisma:client:connect')

const connect = (tree?.children || [])[1] as unknown as Tree
expect(connect.span.name).toEqual('prisma:client:connect')
const serialize = (tree?.children || [])[1] as unknown as Tree
expect(serialize.span.name).toEqual('prisma:client:serialize')

expect(connect.children).toHaveLength(0)
expect(connect.children).toHaveLength(0)

const engine = (tree?.children || [])[2] as unknown as Tree
expect(engine.span.name).toEqual('prisma:engine')
const engine = (tree?.children || [])[2] as unknown as Tree
expect(engine.span.name).toEqual('prisma:engine')

const getConnection = (engine.children || [])[0]
expect(getConnection.span.name).toEqual('prisma:engine:connection')
const getConnection = (engine.children || [])[0]
expect(getConnection.span.name).toEqual('prisma:engine:connection')

if (provider === 'mongodb') {
expect(engine.children).toHaveLength(3)
if (provider === 'mongodb') {
expect(engine.children).toHaveLength(3)

const dbQuery1 = (engine.children || [])[1]
expect(dbQuery1.span.name).toEqual('prisma:engine:db_query')
expect(dbQuery1.span.attributes['db.statement']).toContain('db.User.findMany(*)')
const dbQuery1 = (engine.children || [])[1]
expect(dbQuery1.span.name).toEqual('prisma:engine:db_query')
expect(dbQuery1.span.attributes['db.statement']).toContain('db.User.findMany(*)')

const engineSerialize = (engine.children || [])[2]
expect(engineSerialize.span.name).toEqual('prisma:engine:serialize')
const engineSerialize = (engine.children || [])[2]
expect(engineSerialize.span.name).toEqual('prisma:engine:serialize')

return
}
return
}

expect(engine.children).toHaveLength(3)
expect(engine.children).toHaveLength(3)

const select = (engine.children || [])[1]
expect(select.span.name).toEqual('prisma:engine:db_query')
expect(select.span.attributes['db.statement']).toContain('SELECT')
const select = (engine.children || [])[1]
expect(select.span.name).toEqual('prisma:engine:db_query')
expect(select.span.attributes['db.statement']).toContain('SELECT')

const engineSerialize = (engine.children || [])[2]
expect(engineSerialize.span.name).toEqual('prisma:engine:serialize')
})
const engineSerialize = (engine.children || [])[2]
expect(engineSerialize.span.name).toEqual('prisma:engine:serialize')
},
)
})

describe('tracing disconnect', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/engine-core/src/library/LibraryEngine.ts
Expand Up @@ -430,9 +430,9 @@ You may have to run ${chalk.greenBright('prisma generate')} for your changes to
}

async getDmmf(): Promise<DMMF.Document> {
await this.libraryInstantiationPromise
await this.start()

return JSON.parse(await this.library!.dmmf(this.datamodel))
return JSON.parse(await this.engine!.dmmf())
}

version(): string {
Expand Down
1 change: 1 addition & 0 deletions packages/engine-core/src/library/types/Library.ts
Expand Up @@ -9,6 +9,7 @@ export type QueryEngineInstance = {
*/
query(requestStr: string, headersStr: string, transactionId?: string): Promise<string>
sdlSchema(): Promise<string>
dmmf(): Promise<string>
startTransaction(options: string, traceHeaders: string): Promise<string>
commitTransaction(id: string, traceHeaders: string): Promise<string>
rollbackTransaction(id: string, traceHeaders: string): Promise<string>
Expand Down

0 comments on commit e6e95ea

Please sign in to comment.