Skip to content

Commit 1db2467

Browse files
fix performance
1 parent 04dc1e3 commit 1db2467

File tree

2 files changed

+25
-20
lines changed
  • packages
    • datadog-instrumentations/src
    • dd-trace/src/ci-visibility/exporters/test-worker

2 files changed

+25
-20
lines changed

packages/datadog-instrumentations/src/vitest.js

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,6 @@ function getCreateCliWrapper (vitestPackage, frameworkVersion) {
369369
return vitestPackage
370370
}
371371

372-
373372
// UNUSED RIGHT NOW, but we can use it to bind the async resource to the test fn
374373
// getFn is what's used to get the test fn to run it with vitest:
375374
// https://github.com/vitest-dev/vitest/blob/0cbad1b0d0d56f1ec60f8496678d1435f8bb8977/packages/runner/src/run.ts#L315-L321
@@ -386,29 +385,31 @@ addHook({
386385
return suitePackage
387386
})
388387

389-
const processToHandler = new WeakMap()
388+
const processToHandler = new WeakSet()
389+
390+
function threadHandler (thread) {
391+
if (processToHandler.has(thread.process)) {
392+
return
393+
}
394+
processToHandler.add(thread.process)
395+
thread.process.on('message', (message) => {
396+
if (message.__tinypool_worker_message__ && message.data) {
397+
workerReporterCh.publish(message.data)
398+
}
399+
})
400+
}
390401

391402
addHook({
392403
name: 'tinypool',
393404
versions: ['>=1.0.0'],
394405
file: 'dist/index.js'
395406
}, (TinyPool) => {
396-
// we can pass handle here to the worker, and then use it to send messages to the main process
397-
shimmer.wrap(TinyPool.prototype, 'run', run => function (_, { channel }) {
398-
const res = run.apply(this, arguments)
399-
400-
this.threads.forEach(thread => {
401-
if (processToHandler.has(thread.process)) {
402-
return
403-
}
404-
processToHandler.set(thread.process, true)
405-
thread.process.on('message', (message) => {
406-
if (message.__tinypool_worker_message__ && message.data) {
407-
workerReporterCh.publish(message.data)
408-
}
409-
})
410-
})
411-
407+
shimmer.wrap(TinyPool.prototype, 'run', run => async function () {
408+
// we need to do this before and after because the threads list gets recycled
409+
// (the processes are re-created)
410+
this.threads.forEach(threadHandler)
411+
const res = await run.apply(this, arguments)
412+
this.threads.forEach(threadHandler)
412413
return res
413414
})
414415

packages/dd-trace/src/ci-visibility/exporters/test-worker/writer.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
'use strict'
22
const { JSONEncoder } = require('../../encode/json-encoder')
3+
const { getEnvironmentVariable } = require('../../../config-helper')
4+
5+
const isVitestWorker = !!getEnvironmentVariable('TINYPOOL_WORKER_ID')
36

47
class Writer {
58
constructor (interprocessCode) {
@@ -34,7 +37,7 @@ class Writer {
3437
// See cucumber code:
3538
// https://github.com/cucumber/cucumber-js/blob/5ce371870b677fe3d1a14915dc535688946f734c/src/runtime/parallel/run_worker.ts#L13
3639
if (process.send) { // it only works if process.send is available
37-
if (process.env.TINYPOOL_WORKER_ID) {
40+
if (isVitestWorker) {
3841
// in vitest we have to trick the main process into thinking these are messages from
3942
// tinypool so they are not rejected
4043
process.send({ __tinypool_worker_message__: true, data }, () => {
@@ -45,8 +48,9 @@ class Writer {
4548
onDone()
4649
})
4750
}
51+
} else {
52+
onDone()
4853
}
49-
// onDone()
5054
}
5155
}
5256

0 commit comments

Comments
 (0)