-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
330 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
use Leventcz\Top\Data\EventCounter; | ||
|
||
it('counts events', function () { | ||
$counter = new EventCounter(); | ||
$counter->add('foo'); | ||
$counter->add('foo', 4); | ||
$counter->add('bar', 2); | ||
$counter->add('bar', 4); | ||
|
||
expect($counter->get())->toBe(['foo' => 5, 'bar' => 6]); | ||
}); | ||
|
||
it('flushes events', function () { | ||
$counter = new EventCounter(); | ||
$counter->add('foo', 123); | ||
$counter->flush(); | ||
|
||
expect($counter->get())->toBe([]); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
<?php | ||
|
||
use Illuminate\Config\Repository; | ||
use Illuminate\Contracts\Redis\Factory as RedisFactory; | ||
use Illuminate\Redis\Connections\Connection; | ||
use Leventcz\Top\Data\CacheSummary; | ||
use Leventcz\Top\Data\DatabaseSummary; | ||
use Leventcz\Top\Data\EventCounter; | ||
use Leventcz\Top\Data\HandledRequest; | ||
use Leventcz\Top\Data\RequestSummary; | ||
use Leventcz\Top\Data\RouteCollection; | ||
use Leventcz\Top\Repositories\RedisRepository; | ||
|
||
beforeEach(function () { | ||
$this->redisFactory = Mockery::mock(RedisFactory::class); | ||
$this->connection = Mockery::mock(Connection::class); | ||
$this->config = Mockery::mock(Repository::class); | ||
|
||
$this->redisFactory | ||
->shouldReceive('connection') | ||
->andReturn($this->connection); | ||
$this->config | ||
->shouldReceive('get') | ||
->andReturn('top.connection'); | ||
|
||
$this->repository = new RedisRepository($this->redisFactory, $this->config); | ||
}); | ||
|
||
afterEach(function () { | ||
Mockery::close(); | ||
}); | ||
|
||
it('saves handled request and events to redis', function () { | ||
$request = new HandledRequest('GET', '/test', 100, 200, 1622505600); | ||
$eventCounter = new EventCounter(['some-event' => 5, 'another-event' => 10]); | ||
$pipe = Mockery::spy(); | ||
|
||
$this | ||
->connection | ||
->shouldReceive('pipeline') | ||
->once() | ||
->with(Mockery::on(function ($callback) use ($pipe) { | ||
$callback($pipe); | ||
|
||
return true; | ||
})) | ||
->andReturnNull(); | ||
|
||
$this->repository->save($request, $eventCounter); | ||
|
||
$key = "top-requests:$request->timestamp"; | ||
$routeKey = "$request->method:$request->uri:data"; | ||
|
||
foreach ($eventCounter->get() as $event => $times) { | ||
$pipe | ||
->shouldHaveReceived('hIncrBy') | ||
->with($key, "$routeKey:$event", $times) | ||
->once(); | ||
} | ||
|
||
$pipe | ||
->shouldHaveReceived('hIncrBy') | ||
->with($key, "$routeKey:hits", 1) | ||
->once(); | ||
|
||
$pipe | ||
->shouldHaveReceived('hIncrBy') | ||
->with($key, "$routeKey:memory", $request->memory) | ||
->once(); | ||
|
||
$pipe | ||
->shouldHaveReceived('hIncrBy') | ||
->with($key, "$routeKey:duration", $request->duration) | ||
->once(); | ||
|
||
$pipe | ||
->shouldHaveReceived('expire') | ||
->with($key, 10) | ||
->once(); | ||
}); | ||
|
||
it('fetches request summary from redis', function () { | ||
$this | ||
->connection | ||
->shouldReceive('eval') | ||
->once() | ||
->andReturn(json_encode(['averageRequestPerSecond' => 1, 'averageMemoryUsage' => 2, 'averageDuration' => 3])); | ||
|
||
expect($this->repository->getRequestSummary()) | ||
->toBeInstanceOf(RequestSummary::class); | ||
}); | ||
|
||
it('fetches database summary from redis', function () { | ||
$this | ||
->connection | ||
->shouldReceive('eval') | ||
->once() | ||
->andReturn(json_encode(['averageQueryPerSecond' => 1, 'averageQueryDuration' => 2])); | ||
|
||
expect($this->repository->getDatabaseSummary()) | ||
->toBeInstanceOf(DatabaseSummary::class); | ||
}); | ||
|
||
it('fetches cache summary from redis', function () { | ||
$this | ||
->connection | ||
->shouldReceive('eval') | ||
->once() | ||
->andReturn(json_encode([ | ||
'averageHitPerSecond' => 1, 'averageMissPerSecond' => 2, 'averageWritePerSecond' => 3, | ||
])); | ||
|
||
expect($this->repository->getCacheSummary()) | ||
->toBeInstanceOf(CacheSummary::class); | ||
}); | ||
|
||
it('fetches top routes from redis', function () { | ||
$this | ||
->connection | ||
->shouldReceive('eval') | ||
->once() | ||
->andReturn(json_encode([ | ||
[ | ||
'uri' => '', 'method' => '', 'averageRequestPerSecond' => 1, 'averageMemoryUsage' => 1, | ||
'averageDuration' => 1, | ||
], | ||
])); | ||
|
||
expect($this->repository->getTopRoutes()) | ||
->toBeInstanceOf(RouteCollection::class); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
use Illuminate\Config\Repository; | ||
use Illuminate\Contracts\Redis\Factory; | ||
use Illuminate\Foundation\Application; | ||
use Illuminate\Redis\RedisManager; | ||
use Leventcz\Top\Facades\State; | ||
use Leventcz\Top\Facades\Top; | ||
use Leventcz\Top\ServiceProvider; | ||
use Leventcz\Top\StateManager; | ||
use Leventcz\Top\TopManager; | ||
|
||
beforeEach(function () { | ||
$this->app = new Application(); | ||
$this->app->bind('config', fn () => new Repository()); | ||
$this->app->bind(Factory::class, fn () => Mockery::mock(RedisManager::class)); | ||
$this->app->register(ServiceProvider::class); | ||
}); | ||
|
||
afterAll(function () { | ||
Mockery::close(); | ||
}); | ||
|
||
it('ensures top is bound to the container', function () { | ||
expect($this->app->get('top')) | ||
->toBeInstanceOf(TopManager::class); | ||
}); | ||
|
||
it('ensures top.state bound to the container', function () { | ||
expect($this->app->get('top.state')) | ||
->toBeInstanceOf(StateManager::class); | ||
}); | ||
|
||
it('ensures top is always singleton', function () { | ||
$top = $this->app->get('top'); | ||
|
||
expect($this->app->get('top')) | ||
->toBe($top); | ||
}); | ||
|
||
it('ensures top facade resolves correctly', function () { | ||
Top::setFacadeApplication($this->app); | ||
|
||
expect(Top::getFacadeRoot()) | ||
->toBeInstanceOf(TopManager::class); | ||
}); | ||
|
||
it('ensures state facade resolves correctly', function () { | ||
State::setFacadeApplication($this->app); | ||
|
||
expect(State::getFacadeRoot()) | ||
->toBeInstanceOf(StateManager::class); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
use Leventcz\Top\Contracts\Repository; | ||
use Leventcz\Top\Data\EventCounter; | ||
use Leventcz\Top\Data\HandledRequest; | ||
use Leventcz\Top\StateManager; | ||
|
||
beforeEach(function () { | ||
$this->eventCounter = Mockery::mock(EventCounter::class); | ||
$this->repository = Mockery::mock(Repository::class); | ||
$this->stateManager = new StateManager($this->eventCounter, $this->repository); | ||
}); | ||
|
||
afterAll(function () { | ||
Mockery::close(); | ||
}); | ||
|
||
it('adds event to state', function () { | ||
$this | ||
->eventCounter | ||
->shouldReceive('add') | ||
->with('test', 4) | ||
->once(); | ||
|
||
$this->stateManager->add('test', 4); | ||
}); | ||
|
||
it('flushes state', function () { | ||
$this | ||
->eventCounter | ||
->shouldReceive('flush') | ||
->once(); | ||
|
||
$this->stateManager->flush(); | ||
}); | ||
|
||
it('saves state', function () { | ||
$request = new HandledRequest('', '', 1, 1, 1); | ||
|
||
$this | ||
->repository | ||
->shouldReceive('save') | ||
->with($request, $this->eventCounter) | ||
->once(); | ||
|
||
$this->stateManager->save($request); | ||
}); |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.