Improve performance.

This commit is contained in:
Miao Jiang 2013-02-19 20:54:47 +08:00
parent 02ba0f25d3
commit 1061e177b2
6 changed files with 29 additions and 9 deletions

1
.gitignore vendored
View file

@ -1,3 +1,4 @@
raw raw
build build
.lock-wscript .lock-wscript
node_modules

View file

@ -1,4 +1,4 @@
var Curl = require('../lib/Curl') var Curl = require('..').Curl
var p = console.log; var p = console.log;
var url = process.argv[2]; var url = process.argv[2];
@ -13,15 +13,18 @@ curl.setopt('CONNECTTIMEOUT', 2);
// on 'data' must be returns chunk.length, or means interrupt the transfer // on 'data' must be returns chunk.length, or means interrupt the transfer
curl.on('data', function(chunk) { curl.on('data', function(chunk) {
p("receive " + chunk.length)
return chunk.length; return chunk.length;
}); });
curl.on('error', function(e) { curl.on('error', function(e) {
p("error: " + e.message)
curl.close(); curl.close();
}); });
curl.on('end', function() { curl.on('end', function() {
p('done.')
curl.close(); curl.close();
}); });

View file

@ -62,14 +62,24 @@ Curl::perform = ->
Curl.process() Curl.process()
@ @
m = 0
p = console.log
Curl.process = -> Curl.process = ->
if Curl.in_process if Curl.in_process
return return
do once = -> do once = ->
num = Curl.process_() n = Curl.process_()
if num > 0 if n > 0
Curl.in_process = true Curl.in_process = true
setTimeout once, 80 if n > 8192 && m < 10
++m
process.nextTick once
else
m = 0
w = (8192 - n) * 80 / 8192
if w < 0
w = 0
setTimeout once, w
else else
Curl.in_process = false Curl.in_process = false

View file

@ -35,6 +35,7 @@
curl = function() { curl = function() {
return curl.perform.apply(curl, arguments); return curl.perform.apply(curl, arguments);
}; };
curl.Curl = Curl;
curl.perform = function() { curl.perform = function() {
var args, c, cb, k, length, v, _ref, _ref1, _ref2, _ref3, _ref4; var args, c, cb, k, length, v, _ref, _ref1, _ref2, _ref3, _ref4;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];

View file

@ -16,6 +16,7 @@ class CurlBuilder
curl = -> curl = ->
curl.perform.apply curl, arguments curl.perform.apply curl, arguments
curl.Curl = Curl
curl.perform = (args...) -> curl.perform = (args...) ->
if @running if @running
throw new Error 'the cURL session is busy, use curl.create to create another cURL Session' throw new Error 'the cURL session is busy, use curl.create to create another cURL Session'

View file

@ -24,6 +24,7 @@ class NodeCurl
static bool is_ref; static bool is_ref;
static std::map< CURL*, NodeCurl* > curls; static std::map< CURL*, NodeCurl* > curls;
static int count; static int count;
static int transfered;
CURL * curl; CURL * curl;
v8::Persistent<v8::Object> handle; v8::Persistent<v8::Object> handle;
@ -107,6 +108,7 @@ class NodeCurl
// curl write function mapping // curl write function mapping
static size_t write_function(char *ptr, size_t size, size_t nmemb, void *userdata) static size_t write_function(char *ptr, size_t size, size_t nmemb, void *userdata)
{ {
transfered += size * nmemb;
NodeCurl *nodecurl = (NodeCurl*)userdata; NodeCurl *nodecurl = (NodeCurl*)userdata;
return nodecurl->on_write(ptr, size * nmemb); return nodecurl->on_write(ptr, size * nmemb);
} }
@ -294,6 +296,7 @@ class NodeCurl
// int process() // int process()
static v8::Handle<v8::Value> process(const v8::Arguments & args) static v8::Handle<v8::Value> process(const v8::Arguments & args)
{ {
transfered = 0;
if (running_handles > 0) if (running_handles > 0)
{ {
CURLMcode code; CURLMcode code;
@ -333,7 +336,7 @@ class NodeCurl
} }
} }
} }
return v8::Integer::New(running_handles); return v8::Integer::New(transfered + (int)(running_handles > 0));
} }
// perform() // perform()
@ -452,5 +455,6 @@ int NodeCurl::running_handles = 0;
bool NodeCurl::is_ref = false; bool NodeCurl::is_ref = false;
std::map< CURL*, NodeCurl* > NodeCurl::curls; std::map< CURL*, NodeCurl* > NodeCurl::curls;
int NodeCurl::count = 0; int NodeCurl::count = 0;
int NodeCurl::transfered = 0;
#endif #endif