forked from kaylangan/jest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add node worker-thread support to jest-worker (jestjs#7408)
* Restructure workers (create a base class and inherit from it) Clean up types, create missing interfaes and make Flow and Jest happy * Move child.js to workers folder * Restructure base classes and make a working POC * Remove BaseWorker * Remove MessageChannel and cleanup super() calls * Use worker threads implementation if possible * Restructure queues * Support experimental modules in jest-resolver * Rename child.js to processChild.js * Remove private properties from WorkerPoolInterface * Move common line outside of if-else * Unify interface (use workerId) and remove recursion * Remove opt-out option for worker_threads in node 10.5+ * Alphabetical import sorting * Unlock worker after onEnd * Cache queue head in the getNextJob loop * Elegant while loop * Remove redundand .binds * Clean up interfaces and responsibilites * Update jest-worker * Add changelog and update jest-worker readme * Fix lint lol * Fixes from review * Update Changelog * rm function * rm [] * Make imports alphabetical 🤮 * Go back to any * Fix lint * \n * Fix formatting * Add docs * Revert canUseWorkerThreads * Fix lint * Fix pathing on windows
- Loading branch information
1 parent
2652406
commit ed23ed6
Showing
29 changed files
with
2,533 additions
and
824 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.idea | ||
.DS_STORE | ||
.eslintcache | ||
*.swp | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
/** | ||
* Copyright (c) 2017-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
'use strict'; | ||
|
||
import type { | ||
ChildMessage, | ||
QueueChildMessage, | ||
WorkerInterface, | ||
OnStart, | ||
OnEnd, | ||
} from './types'; | ||
import {CHILD_MESSAGE_CALL} from './types'; | ||
|
||
export default class Farm { | ||
_computeWorkerKey: (string, ...Array<any>) => ?string; | ||
_cacheKeys: {[string]: WorkerInterface, __proto__: null}; | ||
_callback: Function; | ||
_last: Array<QueueChildMessage>; | ||
_locks: Array<boolean>; | ||
_numOfWorkers: number; | ||
_offset: number; | ||
_queue: Array<?QueueChildMessage>; | ||
|
||
constructor( | ||
numOfWorkers: number, | ||
callback: Function, | ||
computeWorkerKey?: (string, ...Array<any>) => ?string, | ||
) { | ||
this._callback = callback; | ||
this._numOfWorkers = numOfWorkers; | ||
this._cacheKeys = Object.create(null); | ||
this._queue = []; | ||
this._last = []; | ||
this._locks = []; | ||
this._offset = 0; | ||
if (computeWorkerKey) { | ||
this._computeWorkerKey = computeWorkerKey; | ||
} | ||
} | ||
|
||
doWork(method: string, ...args: Array<any>): Promise<mixed> { | ||
return new Promise((resolve, reject) => { | ||
const computeWorkerKey = this._computeWorkerKey; | ||
const request: ChildMessage = [CHILD_MESSAGE_CALL, false, method, args]; | ||
|
||
let worker: ?WorkerInterface = null; | ||
let hash: ?string = null; | ||
|
||
if (computeWorkerKey) { | ||
hash = computeWorkerKey.apply(this, [method].concat(args)); | ||
worker = hash == null ? null : this._cacheKeys[hash]; | ||
} | ||
|
||
const onStart: OnStart = (worker: WorkerInterface) => { | ||
if (hash != null) { | ||
this._cacheKeys[hash] = worker; | ||
} | ||
}; | ||
|
||
const onEnd: OnEnd = (error: ?Error, result: ?mixed) => { | ||
if (error) { | ||
reject(error); | ||
} else { | ||
resolve(result); | ||
} | ||
}; | ||
|
||
const task = {onEnd, onStart, request}; | ||
if (worker) { | ||
this._enqueue(task, worker.getWorkerId()); | ||
} else { | ||
this._push(task); | ||
} | ||
}); | ||
} | ||
|
||
_getNextJob(workerId: number): ?QueueChildMessage { | ||
let queueHead = this._queue[workerId]; | ||
|
||
while (queueHead && queueHead.request[1]) { | ||
queueHead = queueHead.next; | ||
} | ||
|
||
this._queue[workerId] = queueHead; | ||
|
||
return queueHead; | ||
} | ||
|
||
_process(workerId: number): Farm { | ||
if (this.isLocked(workerId)) { | ||
return this; | ||
} | ||
|
||
const job = this._getNextJob(workerId); | ||
|
||
if (!job) { | ||
return this; | ||
} | ||
|
||
const onEnd = (error: ?Error, result: mixed) => { | ||
job.onEnd(error, result); | ||
this.unlock(workerId); | ||
this._process(workerId); | ||
}; | ||
|
||
this.lock(workerId); | ||
|
||
this._callback(workerId, job.request, job.onStart, onEnd); | ||
|
||
job.request[1] = true; | ||
|
||
return this; | ||
} | ||
|
||
_enqueue(task: QueueChildMessage, workerId: number): Farm { | ||
if (task.request[1]) { | ||
return this; | ||
} | ||
|
||
if (this._queue[workerId]) { | ||
this._last[workerId].next = task; | ||
} else { | ||
this._queue[workerId] = task; | ||
} | ||
|
||
this._last[workerId] = task; | ||
this._process(workerId); | ||
|
||
return this; | ||
} | ||
|
||
_push(task: QueueChildMessage): Farm { | ||
for (let i = 0; i < this._numOfWorkers; i++) { | ||
const workerIdx = (this._offset + i) % this._numOfWorkers; | ||
this._enqueue(task, workerIdx); | ||
} | ||
this._offset++; | ||
|
||
return this; | ||
} | ||
|
||
lock(workerId: number): void { | ||
this._locks[workerId] = true; | ||
} | ||
|
||
unlock(workerId: number): void { | ||
this._locks[workerId] = false; | ||
} | ||
|
||
isLocked(workerId: number): boolean { | ||
return this._locks[workerId]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* Copyright (c) 2017-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
'use strict'; | ||
|
||
import BaseWorkerPool from './base/BaseWorkerPool'; | ||
|
||
import type { | ||
ChildMessage, | ||
WorkerOptions, | ||
OnStart, | ||
OnEnd, | ||
WorkerPoolInterface, | ||
WorkerInterface, | ||
} from './types'; | ||
|
||
const canUseWorkerThreads = () => { | ||
try { | ||
// $FlowFixMe: Flow doesn't know about experimental APIs | ||
require('worker_threads'); | ||
return true; | ||
} catch (_) { | ||
return false; | ||
} | ||
}; | ||
|
||
class WorkerPool extends BaseWorkerPool implements WorkerPoolInterface { | ||
send( | ||
workerId: number, | ||
request: ChildMessage, | ||
onStart: OnStart, | ||
onEnd: OnEnd, | ||
): void { | ||
this.getWorkerById(workerId).send(request, onStart, onEnd); | ||
} | ||
|
||
createWorker(workerOptions: WorkerOptions): WorkerInterface { | ||
let Worker; | ||
if (canUseWorkerThreads()) { | ||
Worker = require('./workers/NodeThreadsWorker').default; | ||
} else { | ||
Worker = require('./workers/ChildProcessWorker').default; | ||
} | ||
|
||
return new Worker(workerOptions); | ||
} | ||
} | ||
|
||
export default WorkerPool; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 6 additions & 1 deletion
7
packages/jest-worker/src/__performance_tests__/workers/jest_worker.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 6 additions & 1 deletion
7
packages/jest-worker/src/__performance_tests__/workers/worker_farm.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.