Skip to content

Commit d24f822

Browse files
authored
Merge pull request php-curl-class#389 from zachborboa/master
Add MultiCurl::setConcurrency() to modify the number of concurrent requests
2 parents c41fbba + 443f5dd commit d24f822

File tree

3 files changed

+58
-5
lines changed

3 files changed

+58
-5
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ MultiCurl::complete($callback)
248248
MultiCurl::error($callback)
249249
MultiCurl::getOpt($option)
250250
MultiCurl::setBasicAuthentication($username, $password = '')
251+
MultiCurl::setConcurrency($concurrency)
251252
MultiCurl::setConnectTimeout($seconds)
252253
MultiCurl::setCookie($key, $value)
253254
MultiCurl::setCookieFile($cookie_file)

examples/multi_curl_get_load_test.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
require __DIR__ . '/vendor/autoload.php';
3+
4+
use \Curl\MultiCurl;
5+
6+
$server_count = 10;
7+
$urls = array();
8+
$port = 8000;
9+
for ($i = 0; $i < $server_count; $i++) {
10+
$port += 1;
11+
$urls[] = 'http://localhost:' . $port . '/';
12+
}
13+
14+
$multi_curl = new MultiCurl();
15+
16+
$success = 0;
17+
$error = 0;
18+
$complete = 0;
19+
20+
$multi_curl->success(function ($instance) use (&$success) {
21+
$success += 1;
22+
});
23+
$multi_curl->error(function ($instance) use (&$error) {
24+
$error += 1;
25+
});
26+
$multi_curl->complete(function ($instance) use (&$complete) {
27+
$complete += 1;
28+
});
29+
30+
$limit = 1000;
31+
for ($i = 0; $i < $limit; $i++) {
32+
$url = $urls[mt_rand(0, count($urls) - 1)];
33+
$instance = $multi_curl->addGet($url);
34+
}
35+
36+
$multi_curl->start();
37+
38+
echo 'complete: ' . $complete . "\n";
39+
echo 'success: ' . $success . "\n";
40+
echo 'error: ' . $error . "\n";
41+
echo 'done' . "\n";

src/Curl/MultiCurl.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ class MultiCurl
66
{
77
public $baseUrl = null;
88
public $multiCurl;
9-
public $windowSize = 25;
109

1110
private $curls = array();
1211
private $activeCurls = array();
1312
private $isStarted = false;
13+
private $concurrency = 25;
1414

1515
private $beforeSendFunction = null;
1616
private $successFunction = null;
@@ -360,6 +360,17 @@ public function setBasicAuthentication($username, $password = '')
360360
$this->setOpt(CURLOPT_USERPWD, $username . ':' . $password);
361361
}
362362

363+
/**
364+
* Set Concurrency
365+
*
366+
* @access public
367+
* @param $concurrency
368+
*/
369+
public function setConcurrency($concurrency)
370+
{
371+
$this->concurrency = $concurrency;
372+
}
373+
363374
/**
364375
* Set Digest Authentication
365376
*
@@ -573,12 +584,12 @@ public function start()
573584

574585
$this->isStarted = true;
575586

576-
$window_size = $this->windowSize;
577-
if ($window_size > count($this->curls)) {
578-
$window_size = count($this->curls);
587+
$concurrency = $this->concurrency;
588+
if ($concurrency > count($this->curls)) {
589+
$concurrency = count($this->curls);
579590
}
580591

581-
for ($i = 0; $i < $window_size; $i++) {
592+
for ($i = 0; $i < $concurrency; $i++) {
582593
$this->initHandle(array_pop($this->curls));
583594
}
584595

0 commit comments

Comments
 (0)