Expose libvips thread pool size

This commit is contained in:
Lovell Fuller 2014-09-03 19:52:03 +01:00
parent 87f6e83988
commit eb3e739f7b
5 changed files with 83 additions and 37 deletions

View File

@ -365,6 +365,18 @@ sharp.cache(200); // { current: 75, high: 99, memory: 200, items: 500 }
sharp.cache(50, 200); // { current: 49, high: 99, memory: 50, items: 200} sharp.cache(50, 200); // { current: 49, high: 99, memory: 50, items: 200}
``` ```
#### sharp.concurrency([threads])
`threads`, if provided, is the Number of threads _libvips'_ should create for image processing. The default value is the number of CPU cores. A value of `0` will reset to this default.
This method always returns the current concurrency.
```javascript
var threads = sharp.concurrency(); // 4
sharp.concurrency(2); // 2
sharp.concurrency(0); // 4
```
#### sharp.counters() #### sharp.counters()
Provides access to internal task counters. Provides access to internal task counters.

View File

@ -430,6 +430,16 @@ module.exports.cache = function(memory, items) {
return sharp.cache(memory, items); return sharp.cache(memory, items);
}; };
/*
Get and set size of thread pool
*/
module.exports.concurrency = function(concurrency) {
if (Number.isNaN(concurrency)) {
concurrency = null;
}
return sharp.concurrency(concurrency);
};
/* /*
Get internal counters Get internal counters
*/ */

View File

@ -775,6 +775,20 @@ NAN_METHOD(cache) {
NanReturnValue(cache); NanReturnValue(cache);
} }
/*
Get and set size of thread pool
*/
NAN_METHOD(concurrency) {
NanScope();
// Set concurrency
if (args[0]->IsInt32()) {
vips_concurrency_set(args[0]->Int32Value());
}
// Get concurrency
NanReturnValue(NanNew<Number>(vips_concurrency_get()));
}
/* /*
Get internal counters (queued tasks, processing tasks) Get internal counters (queued tasks, processing tasks)
*/ */
@ -807,6 +821,7 @@ extern "C" void init(Handle<Object> target) {
NODE_SET_METHOD(target, "metadata", metadata); NODE_SET_METHOD(target, "metadata", metadata);
NODE_SET_METHOD(target, "resize", resize); NODE_SET_METHOD(target, "resize", resize);
NODE_SET_METHOD(target, "cache", cache); NODE_SET_METHOD(target, "cache", cache);
NODE_SET_METHOD(target, "concurrency", concurrency);
NODE_SET_METHOD(target, "counters", counters); NODE_SET_METHOD(target, "counters", counters);
} }

View File

@ -8,8 +8,10 @@ var inputJpg = path.join(__dirname, "fixtures/2569067123_aca715a2ee_o.jpg"); //
var width = 720; var width = 720;
var height = 480; var height = 480;
sharp.concurrency(1);
var timer = setInterval(function() { var timer = setInterval(function() {
console.dir(sharp.cache()); console.dir(sharp.counters());
}, 100); }, 100);
async.mapSeries([1, 1, 2, 4, 8, 16, 32, 64, 128], function(parallelism, next) { async.mapSeries([1, 1, 2, 4, 8, 16, 32, 64, 128], function(parallelism, next) {
@ -33,5 +35,5 @@ async.mapSeries([1, 1, 2, 4, 8, 16, 32, 64, 128], function(parallelism, next) {
); );
}, function() { }, function() {
clearInterval(timer); clearInterval(timer);
console.dir(sharp.cache()); console.dir(sharp.counters());
}); });

View File

@ -28,6 +28,13 @@ var inputGif = path.join(fixturesPath, "Crash_test.gif"); // http://upload.wikim
sharp.cache(0); // Disable sharp.cache(0); // Disable
sharp.cache(50, 500); // 50MB, 500 items sharp.cache(50, 500); // 50MB, 500 items
// Ensure concurrency can be set
var defaultConcurrency = sharp.concurrency();
sharp.concurrency(16);
assert.strictEqual(16, sharp.concurrency());
sharp.concurrency(0);
assert.strictEqual(defaultConcurrency, sharp.concurrency());
async.series([ async.series([
// Resize with exact crop // Resize with exact crop
function(done) { function(done) {