From fe8f450bbfbd33736975c400c0c6ad8cc12f3fff Mon Sep 17 00:00:00 2001 From: joners Date: Sat, 30 Apr 2022 18:01:49 +0200 Subject: [PATCH] Add test for usage with worker threads --- Makefile | 1 + package-lock.json | 5 +++++ package.json | 3 ++- tests/testworker.js | 18 ++++++++++++++++++ tests/worker-thread-test.js | 19 +++++++++++++++++++ 5 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 tests/testworker.js create mode 100644 tests/worker-thread-test.js diff --git a/Makefile b/Makefile index a6eaa06..591b5f0 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,7 @@ build-wrapper: test: tests/data/text8-vector.json node tests/smalltest.js + node tests/worker-thread-test.js node tests/smalltest-manhattan.js node tests/basictests.js basic-config.js diff --git a/package-lock.json b/package-lock.json index 3d38b7d..8a08b1f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -651,6 +651,11 @@ "resolved": "https://registry.npmjs.org/node-gyp-build/-/node-gyp-build-4.1.1.tgz", "integrity": "sha512-dSq1xmcPDKPZ2EED2S6zw/b9NKsqzXRE6dVr8TVQnI3FJOTteUMuqF3Qqs6LZg+mLGYJWqQzMbIjMtJqTv87nQ==" }, + "node-worker-threads-pool": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/node-worker-threads-pool/-/node-worker-threads-pool-1.5.1.tgz", + "integrity": "sha512-7TXAhpMm+jO4MfESxYLtMGSnJWv+itdNHMdaFmeZuPXxwFGU90mtEB42BciUULXOUAxYBfXILAuvrSG3rQZ7mw==" + }, "object-inspect": { "version": "1.7.0", "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.7.0.tgz", diff --git a/package.json b/package.json index 26a8815..8d8a9b3 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,7 @@ "dependencies": { "bindings": "^1.2.1", "level": "^6.0.0", - "nan": "^2.14.0" + "nan": "^2.14.0", + "node-worker-threads-pool": "^1.5.1" } } diff --git a/tests/testworker.js b/tests/testworker.js new file mode 100644 index 0000000..ac8a32d --- /dev/null +++ b/tests/testworker.js @@ -0,0 +1,18 @@ +const { parentPort } = require('worker_threads'); +var Annoy = require('../index'); + +var annoyPath = __dirname + '/data/test.annoy'; + +const annoyIndex = new Annoy(10, 'Angular') + +if(!annoyIndex.load(annoyPath)) { + throw new Error('Loading annoy index failed!') +} else { + console.log('Successfully loaded annoy index.') +} + +parentPort.on('message', async (id) => { + // return the result to the main thread + const result = annoyIndex.getNNsByItem(id, 10, -1, false) + parentPort.postMessage(result); +}); \ No newline at end of file diff --git a/tests/worker-thread-test.js b/tests/worker-thread-test.js new file mode 100644 index 0000000..60eeaf8 --- /dev/null +++ b/tests/worker-thread-test.js @@ -0,0 +1,19 @@ +var test = require('tape'); +const { StaticPool } = require('node-worker-threads-pool') + +var workerPath = __dirname + '/testworker.js'; + +test('Worker thread test', workerThreadTest); + +async function workerThreadTest(t) { + const workerPool = new StaticPool({ + size: 2, + task: workerPath + }); + + const idsToLookUp = [0, 1] + const indexLookups = idsToLookUp.map(id => workerPool.exec(id)) + console.log("Lookup results: ", (await Promise.all(indexLookups))) + workerPool.destroy() + t.end() +}