mirror of
https://github.com/php-curl-class/php-curl-class.git
synced 2026-09-12 03:16:34 +00:00
Merge pull request #389 from zachborboa/master
Add MultiCurl::setConcurrency() to modify the number of concurrent requests
This commit is contained in:
@@ -248,6 +248,7 @@ MultiCurl::complete($callback)
|
||||
MultiCurl::error($callback)
|
||||
MultiCurl::getOpt($option)
|
||||
MultiCurl::setBasicAuthentication($username, $password = '')
|
||||
MultiCurl::setConcurrency($concurrency)
|
||||
MultiCurl::setConnectTimeout($seconds)
|
||||
MultiCurl::setCookie($key, $value)
|
||||
MultiCurl::setCookieFile($cookie_file)
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
require __DIR__ . '/vendor/autoload.php';
|
||||
|
||||
use \Curl\MultiCurl;
|
||||
|
||||
$server_count = 10;
|
||||
$urls = array();
|
||||
$port = 8000;
|
||||
for ($i = 0; $i < $server_count; $i++) {
|
||||
$port += 1;
|
||||
$urls[] = 'http://localhost:' . $port . '/';
|
||||
}
|
||||
|
||||
$multi_curl = new MultiCurl();
|
||||
|
||||
$success = 0;
|
||||
$error = 0;
|
||||
$complete = 0;
|
||||
|
||||
$multi_curl->success(function ($instance) use (&$success) {
|
||||
$success += 1;
|
||||
});
|
||||
$multi_curl->error(function ($instance) use (&$error) {
|
||||
$error += 1;
|
||||
});
|
||||
$multi_curl->complete(function ($instance) use (&$complete) {
|
||||
$complete += 1;
|
||||
});
|
||||
|
||||
$limit = 1000;
|
||||
for ($i = 0; $i < $limit; $i++) {
|
||||
$url = $urls[mt_rand(0, count($urls) - 1)];
|
||||
$instance = $multi_curl->addGet($url);
|
||||
}
|
||||
|
||||
$multi_curl->start();
|
||||
|
||||
echo 'complete: ' . $complete . "\n";
|
||||
echo 'success: ' . $success . "\n";
|
||||
echo 'error: ' . $error . "\n";
|
||||
echo 'done' . "\n";
|
||||
+16
-5
@@ -6,11 +6,11 @@ class MultiCurl
|
||||
{
|
||||
public $baseUrl = null;
|
||||
public $multiCurl;
|
||||
public $windowSize = 25;
|
||||
|
||||
private $curls = array();
|
||||
private $activeCurls = array();
|
||||
private $isStarted = false;
|
||||
private $concurrency = 25;
|
||||
|
||||
private $beforeSendFunction = null;
|
||||
private $successFunction = null;
|
||||
@@ -360,6 +360,17 @@ class MultiCurl
|
||||
$this->setOpt(CURLOPT_USERPWD, $username . ':' . $password);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Concurrency
|
||||
*
|
||||
* @access public
|
||||
* @param $concurrency
|
||||
*/
|
||||
public function setConcurrency($concurrency)
|
||||
{
|
||||
$this->concurrency = $concurrency;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Digest Authentication
|
||||
*
|
||||
@@ -573,12 +584,12 @@ class MultiCurl
|
||||
|
||||
$this->isStarted = true;
|
||||
|
||||
$window_size = $this->windowSize;
|
||||
if ($window_size > count($this->curls)) {
|
||||
$window_size = count($this->curls);
|
||||
$concurrency = $this->concurrency;
|
||||
if ($concurrency > count($this->curls)) {
|
||||
$concurrency = count($this->curls);
|
||||
}
|
||||
|
||||
for ($i = 0; $i < $window_size; $i++) {
|
||||
for ($i = 0; $i < $concurrency; $i++) {
|
||||
$this->initHandle(array_pop($this->curls));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user