Skip to content

Commit

Permalink
~ ttimed decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
vantoozz committed May 1, 2020
1 parent 3e380f7 commit 5953bae
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<?php declare(strict_types=1);


namespace Vantoozz\ProxyScraper\Scrapers;
namespace Vantoozz\ProxyScraper\Scrapers\Decorators;

use Generator;
use Vantoozz\ProxyScraper\Exceptions\ScraperException;
use Vantoozz\ProxyScraper\Scrapers\ScraperInterface;

/**
* Class Distinct
* @package Vantoozz\ProxyScraper\Scrapers
* @package Vantoozz\ProxyScraper\Scrapers\Decorators
*/
final class Distinct implements ScraperInterface
{
Expand Down
55 changes: 55 additions & 0 deletions src/Scrapers/Decorators/Timed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php declare(strict_types=1);

namespace Vantoozz\ProxyScraper\Scrapers\Decorators;

use Generator;
use Vantoozz\ProxyScraper\Exceptions\ScraperException;
use Vantoozz\ProxyScraper\Scrapers\ScraperInterface;

/**
* Class Timed
* @package Vantoozz\ProxyScraper\Scrapers\Decorators
*/
final class Timed implements ScraperInterface
{
public const EVENT_DONE = 'done';
public const EVENT_PROXY_FOUND = 'proxy_found';

/**
* @var ScraperInterface
*/
private $scraper;

/**
* @var Generator
*/
private $output;

/**
* Timed constructor.
* @param ScraperInterface $scraper
* @param Generator $output
*/
public function __construct(ScraperInterface $scraper, Generator $output)
{
$this->scraper = $scraper;
$this->output = $output;
}

/**
* @return Generator
* @throws ScraperException
*/
public function get(): Generator
{
$startTime = microtime(true);

foreach ($this->scraper->get() as $proxy) {
$iterationStartTime = microtime(true);
yield $proxy;
$this->output->send([static::EVENT_PROXY_FOUND, microtime(true) - $iterationStartTime]);
}

$this->output->send([static::EVENT_DONE, microtime(true) - $startTime]);
}
}
21 changes: 19 additions & 2 deletions tests/system/ProxiesMiner/ScrapersProxiesMiner.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace Vantoozz\ProxyScraper\SystemTests\ProxiesMiner;

use Generator;
use Vantoozz\ProxyScraper\Exceptions\ScraperException;
use Vantoozz\ProxyScraper\Scrapers\Decorators\Timed;
use Vantoozz\ProxyScraper\Scrapers\ScraperInterface;
use Vantoozz\ProxyScraper\SystemTests\Timed;

/**
* Class ScrapersProxiesMiner
Expand Down Expand Up @@ -55,7 +56,7 @@ public function getProxies(): array
private function fetchProxies(ScraperInterface $scraper): array
{
$proxies = [];
foreach ((new Timed($scraper))->get() as $proxy) {
foreach ((new Timed($scraper, $this->output($scraper)))->get() as $proxy) {
$parts = explode(':', (string)$proxy);
if (!isset($proxies[ip2long($parts[0])])) {
$proxies[ip2long($parts[0])] = [];
Expand All @@ -65,4 +66,20 @@ private function fetchProxies(ScraperInterface $scraper): array
ksort($proxies);
return $proxies;
}

/**
* @param ScraperInterface $scraper
* @return Generator
*/
private function output(ScraperInterface $scraper): Generator
{
$fullClass = explode('\\', get_class($scraper));
$class = end($fullClass);
while ([$event, $time] = yield) {
if (Timed::EVENT_DONE !== $event) {
continue;
}
echo $class . ' => ' . round($time, 2) . "s\n";
}
}
}
43 changes: 0 additions & 43 deletions tests/system/Timed.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,28 +1,50 @@
<?php declare(strict_types=1);

namespace Vantoozz\ProxyScraper\UnitTests\Scrapers;
namespace Vantoozz\ProxyScraper\UnitTests\Scrapers\Decorators;

use Generator;
use PHPUnit\Framework\TestCase;
use Vantoozz\ProxyScraper\Ipv4;
use Vantoozz\ProxyScraper\Port;
use Vantoozz\ProxyScraper\Proxy;
use Vantoozz\ProxyScraper\Scrapers\Distinct;
use Vantoozz\ProxyScraper\Scrapers\Decorators\Distinct;
use Vantoozz\ProxyScraper\Scrapers\ScraperInterface;

/**
* Class DistinctTest
* @package Vantoozz\ProxyScraper\UnitTests\Scrapers
* @package Vantoozz\ProxyScraper\UnitTests\Scrapers\Decorators
*/
final class DistinctTest extends TestCase
{

/**
* @test
*/
public function it_returns_proxy(): void
{
/** @var Proxy[] $proxies */
$proxies = iterator_to_array((new Distinct($this->scraper()))->get(), true);

static::assertInstanceOf(Proxy::class, $proxies[0]);
}

/**
* @test
*/
public function it_filters_out_repeating_proxies(): void
{
$scraper = new class implements ScraperInterface {
/** @var Proxy[] $proxies */
$proxies = iterator_to_array((new Distinct($this->scraper()))->get(), true);
static::assertCount(2, $proxies);
static::assertNotSame((string)$proxies[0], (string)$proxies[1]);
}

/**
* @return ScraperInterface
*/
private function scraper(): ScraperInterface
{
return new class implements ScraperInterface {
/**
* @return Generator
*/
Expand All @@ -33,10 +55,5 @@ public function get(): Generator
yield new Proxy(new Ipv4('123.123.123.123'), new Port(1234));
}
};

/** @var Proxy[] $proxies */
$proxies = iterator_to_array((new Distinct($scraper))->get(), true);
static::assertCount(2, $proxies);
static::assertNotSame((string)$proxies[0], (string)$proxies[1]);
}
}
105 changes: 105 additions & 0 deletions tests/unit/Scrapers/Decorators/TimedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php declare(strict_types=1);


namespace Vantoozz\ProxyScraper\UnitTests\Scrapers\Decorators;


use Generator;
use PHPUnit\Framework\TestCase;
use Vantoozz\ProxyScraper\Ipv4;
use Vantoozz\ProxyScraper\Port;
use Vantoozz\ProxyScraper\Proxy;
use Vantoozz\ProxyScraper\Scrapers\Decorators\Timed;
use Vantoozz\ProxyScraper\Scrapers\ScraperInterface;

/**
* Class TimedTest
* @package Vantoozz\ProxyScraper\UnitTests\Scrapers\Decorators
*/
final class TimedTest extends TestCase
{

/**
* @test
*/
public function it_returns_proxy(): void
{
$events = [];
/** @var Proxy[] $proxies */
$proxies = iterator_to_array((new Timed($this->scraper(), $this->output($events)))->get());

static::assertCount(2, $proxies);
static::assertInstanceOf(Proxy::class, $proxies[0]);
}

/**
* @test
*/
public function it_emits_events(): void
{
$events = [];

iterator_to_array((new Timed($this->scraper(), $this->output($events)))->get());

static::assertCount(3, $events);
}

/**
* @test
*/
public function it_emits_done_events(): void
{
$events = [];

iterator_to_array((new Timed($this->scraper(), $this->output($events)))->get());

static::assertSame('done', $events[2][0]);
static::assertIsFloat($events[2][1]);
}

/**
* @test
*/
public function it_emits_proxy_found_events(): void
{
$events = [];

iterator_to_array((new Timed($this->scraper(), $this->output($events)))->get());

static::assertSame('proxy_found', $events[0][0]);
static::assertSame('proxy_found', $events[1][0]);
static::assertIsFloat($events[0][1]);
static::assertIsFloat($events[1][1]);
static::assertNotSame($events[0][1], $events[1][1]);
}

/**
* @return ScraperInterface
*/
private function scraper(): ScraperInterface
{
return new class implements ScraperInterface {
/**
* @return Generator
*/
public function get(): Generator
{
usleep(100000);
yield new Proxy(new Ipv4('123.123.123.123'), new Port(1234));
usleep(150000);
yield new Proxy(new Ipv4('234.234.234.234'), new Port(2345));
}
};
}

/**
* @param array $events
* @return Generator
*/
private function output(array &$events): Generator
{
while ($event = yield) {
$events[] = $event;
}
}
}

0 comments on commit 5953bae

Please sign in to comment.