Jerome Wu
5 years ago
18 changed files with 341 additions and 274 deletions
@ -1,4 +1,9 @@ |
|||||||
|
const constants = require('../tests/constants'); |
||||||
global.expect = require('expect.js'); |
global.expect = require('expect.js'); |
||||||
global.fs = require('fs'); |
global.fs = require('fs'); |
||||||
global.path = require('path'); |
global.path = require('path'); |
||||||
global.Tesseract = require('../src'); |
global.Tesseract = require('../src'); |
||||||
|
|
||||||
|
Object.keys(constants).forEach((key) => { |
||||||
|
global[key] = constants[key]; |
||||||
|
}); |
||||||
|
@ -0,0 +1,3 @@ |
|||||||
|
module.exports = (prefix, cnt) => ( |
||||||
|
`${prefix}-${cnt}-${Math.random().toString(16).slice(3, 8)}` |
||||||
|
); |
@ -0,0 +1,18 @@ |
|||||||
|
const resolveURL = require('resolve-url'); |
||||||
|
const { version, dependencies } = require('../../../package.json'); |
||||||
|
const defaultOptions = require('../../constants/defaultOptions'); |
||||||
|
|
||||||
|
/* |
||||||
|
* Default options for browser worker |
||||||
|
*/ |
||||||
|
module.exports = { |
||||||
|
...defaultOptions, |
||||||
|
workerPath: (typeof process !== 'undefined' && process.env.TESS_ENV === 'development') |
||||||
|
? resolveURL(`/dist/worker.dev.js?nocache=${Math.random().toString(36).slice(3)}`) |
||||||
|
: `https://unpkg.com/tesseract.js@v${version}/dist/worker.min.js`, |
||||||
|
/* |
||||||
|
* If browser doesn't support WebAssembly, |
||||||
|
* load ASM version instead |
||||||
|
*/ |
||||||
|
corePath: `https://unpkg.com/tesseract.js-core@v${dependencies['tesseract.js-core'].substring(1)}/tesseract-core.${typeof WebAssembly === 'object' ? 'wasm' : 'asm'}.js`, |
||||||
|
}; |
File diff suppressed because one or more lines are too long
@ -1,33 +1,21 @@ |
|||||||
const { TesseractWorker } = Tesseract; |
const { createScheduler, createWorker } = Tesseract; |
||||||
const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined'; |
const scheduler = createScheduler(); |
||||||
const IMAGE_PATH = 'http://localhost:3000/tests/assets/images'; |
const worker = createWorker(OPTIONS); |
||||||
const loadLangOptions = { |
scheduler.addWorker(worker); |
||||||
langPath: 'http://localhost:3000/tests/assets/traineddata', |
before(function cb() { |
||||||
cachePath: './tests/assets/traineddata', |
this.timeout(0); |
||||||
}; |
return worker.load(); |
||||||
|
}); |
||||||
const getWorker = options => ( |
|
||||||
new TesseractWorker({ |
|
||||||
cacheMethod: 'readOnly', |
|
||||||
...(isBrowser ? { workerPath: 'http://localhost:3000/dist/worker.dev.js' } : {}), |
|
||||||
...loadLangOptions, |
|
||||||
...options, |
|
||||||
}) |
|
||||||
); |
|
||||||
|
|
||||||
describe('detect()', () => { |
describe('detect()', async () => { |
||||||
it('should detect OSD', (done) => { |
it('should detect OSD', () => { |
||||||
[ |
[ |
||||||
{ name: 'cosmic.png', ans: { id: 12, degree: 0 } }, |
{ name: 'cosmic.png', ans: { script: 'Latin' } }, |
||||||
].forEach(({ name, ans: { id, degree } }) => { |
].forEach(async ({ name, ans: { script } }) => { |
||||||
const worker = getWorker(); |
await worker.loadLanguage('osd'); |
||||||
worker |
await worker.initialize('osd'); |
||||||
.detect(`${IMAGE_PATH}/${name}`) |
const { data: { script: s } } = await scheduler.addJob('detect', `${IMAGE_PATH}/${name}`); |
||||||
.then(({ tesseract_script_id, orientation_degrees }) => { |
expect(s).to.be(script); |
||||||
expect(tesseract_script_id).to.be(id); |
|
||||||
expect(orientation_degrees).to.be(degree); |
|
||||||
done(); |
|
||||||
}); |
|
||||||
}); |
}); |
||||||
}).timeout(10000); |
}).timeout(TIMEOUT); |
||||||
}); |
}); |
||||||
|
File diff suppressed because one or more lines are too long
@ -0,0 +1,18 @@ |
|||||||
|
<html> |
||||||
|
<head> |
||||||
|
<meta charset="utf-8"> |
||||||
|
<link rel="stylesheet" href="../node_modules/mocha/mocha.css"> |
||||||
|
</head> |
||||||
|
<body> |
||||||
|
<div id="mocha"></div> |
||||||
|
<script src="../node_modules/mocha/mocha.js"></script> |
||||||
|
<script src="../node_modules/expect.js/index.js"></script> |
||||||
|
<script src="../dist/tesseract.dev.js"></script> |
||||||
|
<script src="./constants.js"></script> |
||||||
|
<script>mocha.setup('bdd');</script> |
||||||
|
<script src="./scheduler.test.js"></script> |
||||||
|
<script> |
||||||
|
mocha.run(); |
||||||
|
</script> |
||||||
|
</body> |
||||||
|
</html> |
@ -0,0 +1,35 @@ |
|||||||
|
const { createScheduler, createWorker } = Tesseract; |
||||||
|
|
||||||
|
let workers = []; |
||||||
|
|
||||||
|
before(async function cb() { |
||||||
|
this.timeout(0); |
||||||
|
const NUM_WORKERS = 10; |
||||||
|
console.log(`Initializing ${NUM_WORKERS} workers`); |
||||||
|
workers = await Promise.all(Array(NUM_WORKERS).fill(0).map(async () => { |
||||||
|
const w = createWorker(OPTIONS); |
||||||
|
await w.load(); |
||||||
|
await w.loadLanguage('eng'); |
||||||
|
await w.initialize('eng'); |
||||||
|
return w; |
||||||
|
})); |
||||||
|
console.log(`Initialized ${NUM_WORKERS} workers`); |
||||||
|
}); |
||||||
|
|
||||||
|
describe('scheduler', () => { |
||||||
|
describe('should speed up with more workers (running 20 jobs)', () => { |
||||||
|
Array(10).fill(0).forEach((_, num) => ( |
||||||
|
it(`support using ${num + 1} workers`, async () => { |
||||||
|
const NUM_JOBS = 30; |
||||||
|
const scheduler = createScheduler(); |
||||||
|
workers.slice(0, num + 1).forEach((w) => { |
||||||
|
scheduler.addWorker(w); |
||||||
|
}); |
||||||
|
const rets = await Promise.all(Array(NUM_JOBS).fill(0).map((_, idx) => ( |
||||||
|
scheduler.addJob('recognize', `${IMAGE_PATH}/${idx % 2 === 0 ? 'simple' : 'cosmic'}.png`) |
||||||
|
))); |
||||||
|
expect(rets.length).to.be(NUM_JOBS); |
||||||
|
}).timeout(60000) |
||||||
|
)); |
||||||
|
}); |
||||||
|
}); |
Loading…
Reference in new issue