From 1061e177b27747b973d3356ee0680330b43fd6c1 Mon Sep 17 00:00:00 2001 From: Miao Jiang Date: Tue, 19 Feb 2013 20:54:47 +0800 Subject: [PATCH] Improve performance. --- .gitignore | 1 + examples/low-level.js | 13 ++++++++----- lib/Curl.toffee | 16 +++++++++++++--- lib/CurlBuilder.js | 1 + lib/CurlBuilder.toffee | 1 + src/node-curl.h | 6 +++++- 6 files changed, 29 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 4375156..5a60fd4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ raw build .lock-wscript +node_modules diff --git a/examples/low-level.js b/examples/low-level.js index 5da8e8e..77b21df 100644 --- a/examples/low-level.js +++ b/examples/low-level.js @@ -1,4 +1,4 @@ -var Curl = require('../lib/Curl') +var Curl = require('..').Curl var p = console.log; var url = process.argv[2]; @@ -6,23 +6,26 @@ var url = process.argv[2]; var curl = new Curl(); if (!url) -url = 'www.yahoo.com'; + url = 'www.yahoo.com'; curl.setopt('URL', url); curl.setopt('CONNECTTIMEOUT', 2); // on 'data' must be returns chunk.length, or means interrupt the transfer curl.on('data', function(chunk) { - return chunk.length; + p("receive " + chunk.length) + return chunk.length; }); curl.on('error', function(e) { - curl.close(); + p("error: " + e.message) + curl.close(); }); curl.on('end', function() { - curl.close(); + p('done.') + curl.close(); }); curl.perform(); diff --git a/lib/Curl.toffee b/lib/Curl.toffee index c80814e..56a4efc 100644 --- a/lib/Curl.toffee +++ b/lib/Curl.toffee @@ -62,14 +62,24 @@ Curl::perform = -> Curl.process() @ +m = 0 +p = console.log Curl.process = -> if Curl.in_process return do once = -> - num = Curl.process_() - if num > 0 + n = Curl.process_() + if n > 0 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 Curl.in_process = false diff --git a/lib/CurlBuilder.js b/lib/CurlBuilder.js index 15b8b16..ec0ffd1 100644 --- a/lib/CurlBuilder.js +++ b/lib/CurlBuilder.js @@ -35,6 +35,7 @@ curl = function() { return curl.perform.apply(curl, arguments); }; + curl.Curl = Curl; curl.perform = function() { var args, c, cb, k, length, v, _ref, _ref1, _ref2, _ref3, _ref4; args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; diff --git a/lib/CurlBuilder.toffee b/lib/CurlBuilder.toffee index 1ffbb7c..8a0cae7 100644 --- a/lib/CurlBuilder.toffee +++ b/lib/CurlBuilder.toffee @@ -16,6 +16,7 @@ class CurlBuilder curl = -> curl.perform.apply curl, arguments + curl.Curl = Curl curl.perform = (args...) -> if @running throw new Error 'the cURL session is busy, use curl.create to create another cURL Session' diff --git a/src/node-curl.h b/src/node-curl.h index f0331e9..ea0e593 100644 --- a/src/node-curl.h +++ b/src/node-curl.h @@ -24,6 +24,7 @@ class NodeCurl static bool is_ref; static std::map< CURL*, NodeCurl* > curls; static int count; + static int transfered; CURL * curl; v8::Persistent handle; @@ -107,6 +108,7 @@ class NodeCurl // curl write function mapping static size_t write_function(char *ptr, size_t size, size_t nmemb, void *userdata) { + transfered += size * nmemb; NodeCurl *nodecurl = (NodeCurl*)userdata; return nodecurl->on_write(ptr, size * nmemb); } @@ -294,6 +296,7 @@ class NodeCurl // int process() static v8::Handle process(const v8::Arguments & args) { + transfered = 0; if (running_handles > 0) { CURLMcode code; @@ -333,7 +336,7 @@ class NodeCurl } } } - return v8::Integer::New(running_handles); + return v8::Integer::New(transfered + (int)(running_handles > 0)); } // perform() @@ -452,5 +455,6 @@ int NodeCurl::running_handles = 0; bool NodeCurl::is_ref = false; std::map< CURL*, NodeCurl* > NodeCurl::curls; int NodeCurl::count = 0; +int NodeCurl::transfered = 0; #endif