-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Add node worker-thread support to jest-worker #7408
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
Merged
rickhanlonii
merged 38 commits into
jestjs:master
from
rickhanlonii:rh-jest-worker-threads
Dec 5, 2018
Merged
Changes from 32 commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
418c6f9
Restructure workers (create a base class and inherit from it)
100c49f
Move child.js to workers folder
f786cb6
Restructure base classes and make a working POC
2a1ac0e
Remove BaseWorker
59ebd73
Remove MessageChannel and cleanup super() calls
9b1f19b
Use worker threads implementation if possible
bea945f
Restructure queues
3e9ce02
Support experimental modules in jest-resolver
02dedef
Rename child.js to processChild.js
5e8347b
Remove private properties from WorkerPoolInterface
e60bc1f
Move common line outside of if-else
b7470d6
Unify interface (use workerId) and remove recursion
092d700
Remove opt-out option for worker_threads in node 10.5+
3947fcb
Alphabetical import sorting
c715f00
Unlock worker after onEnd
4614e54
Cache queue head in the getNextJob loop
e0bfb83
Elegant while loop
9958587
Remove redundand .binds
29e04d2
Clean up interfaces and responsibilites
5b5323d
Merge
rickhanlonii d0041a9
Update jest-worker
rickhanlonii 8d6a0e8
Add changelog and update jest-worker readme
rickhanlonii 6c64753
Fix lint lol
rickhanlonii 6677028
Merge remote-tracking branch 'origin/master' into rh-jest-worker-threads
rickhanlonii 9f3b4aa
Fixes from review
rickhanlonii 0d6ca7a
Merge remote-tracking branch 'upstream/master' into rh-jest-worker-th…
rickhanlonii e46802d
Update Changelog
rickhanlonii 33beafb
rm function
rickhanlonii 3282aef
rm []
rickhanlonii 936a4b3
Make imports alphabetical 🤮
rickhanlonii 0f6d46e
Go back to any
rickhanlonii aa734ec
Fix lint
rickhanlonii 03e21f6
\n
rickhanlonii b55efe3
Fix formatting
rickhanlonii c22536c
Add docs
rickhanlonii 880c16d
Revert canUseWorkerThreads
rickhanlonii b3e2967
Fix lint
rickhanlonii aec3368
Fix pathing on windows
rickhanlonii File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,159 @@ | ||
| /** | ||
|
mjesun marked this conversation as resolved.
|
||
| * 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 { | ||
|
rickhanlonii marked this conversation as resolved.
|
||
| _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> { | ||
|
mjesun marked this conversation as resolved.
|
||
| 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 hidden or 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,52 @@ | ||
| /** | ||
| * 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'; | ||
|
|
||
| let workerThreadsAreSupported = false; | ||
| try { | ||
| // $FlowFixMe: Flow doesn't know about experimental APIs | ||
| require('worker_threads'); | ||
| workerThreadsAreSupported = true; | ||
| } catch (_) {} | ||
|
|
||
| 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 (workerThreadsAreSupported) { | ||
| Worker = require('./workers/NodeThreadsWorker').default; | ||
| } else { | ||
| Worker = require('./workers/ChildProcessWorker').default; | ||
| } | ||
|
|
||
| return new Worker(workerOptions); | ||
| } | ||
| } | ||
|
|
||
| export default WorkerPool; |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.