public function testMemcachedArray() { if (!class_exists('\\Memcached')) { $this->markTestSkipped("extension not loaded"); } $this->_connection = new \Memcached(); $this->_connection->addServer("localhost", 11211); $factory = new Factory(); $cb = $factory->getMemcachedInstance($this->_connection, 3, 1); $this->assertTrue($cb instanceof CircuitBreakerInterface); // service with multiple failures $this->assertTrue($cb->isAvailable("serviceOne")); $cb->reportFailure("serviceOne"); $cb->reportFailure("serviceOne"); $cb->reportFailure("serviceOne"); $cb->reportFailure("serviceOne"); $this->assertFalse($cb->isAvailable("serviceOne")); // ervice seen first time = ok $this->assertTrue($cb->isAvailable("serviceUnknown")); // service with one failure is ok $cb->reportSuccess("serviceTwo"); $this->assertTrue($cb->isAvailable("serviceTwo")); // service with successes is ok $cb->reportFailure("serviceFour"); $cb->reportFailure("serviceFour"); $cb->reportFailure("serviceFour"); $cb->reportFailure("serviceFour"); $cb->reportSuccess("serviceFour"); $cb->reportSuccess("serviceFour"); $this->assertTrue($cb->isAvailable("serviceFour")); }
<?php /** * Simple script calling circuit breaker thousands of times reporting success or error and checking statuses * * This can be used to see how circuit breaker can be instantinated and used easily, it also gives gauge * of the approximate performance. * * Memcached to local memcached: ~0.0005s per check+report */ namespace Tests\Manual\Performance; use Ejsmont\CircuitBreaker\Factory; $callCount = 10000; $connection = new \Memcached(); $connection->addServer("localhost", 11211); $connection->delete("EjsmontCircuitBreakerCircuitBreakerStatsAggregatedStats"); $factory = new Factory(); $cb = $factory->getMemcachedInstance($connection, 30, 3600); $start = microtime(true); for ($i = 0; $i < $callCount; $i++) { $serviceName = "someServiceName" . $i % 5; $cb->isAvailable($serviceName); if (mt_rand(1, 1000) > 700) { $cb->reportSuccess($serviceName); } else { $cb->reportFailure($serviceName); } } $stop = microtime(true); print_r(array(sprintf("Total time for %d calls: %.4f", $callCount, $stop - $start), unserialize($connection->get("EjsmontCircuitBreakerCircuitBreakerStatsAggregatedStats"))));
/** * Get breaker instance. */ private function getBreaker() { $breakerFactory = new Factory(); return $breakerFactory->getRedisInstance($this->getRedisInstance(), 1, 10); }
<?php /** * Simple script calling circuit breaker thousands of times reporting success or error and checking statuses * * This can be used to see how circuit breaker can be instantinated and used easily, it also gives gauge * of the approximate performance. * * APC backend: ~0.0001s per check+report */ namespace Tests\Manual\Performance; use Ejsmont\CircuitBreaker\Factory; require dirname(__FILE__) . '/../../SplClassLoader.php'; $callCount = 10000; $autoLoader = new \SplClassLoader('Ejsmont', dirname(__FILE__) . '/../../../src'); $autoLoader->register(); $factory = new Factory(); $cb = $factory->getSingleApcInstance(30, 3600); $start = microtime(true); for ($i = 0; $i < $callCount; $i++) { $serviceName = "someServiceName" . $i % 5; $cb->isAvailable($serviceName); if (mt_rand(1, 1000) > 700) { $cb->reportSuccess($serviceName); } else { $cb->reportFailure($serviceName); } } $stop = microtime(true); print_r(array(sprintf("Total time for %d calls: %.4f", $callCount, $stop - $start), unserialize(apc_fetch("EjsmontCircuitBreakerCircuitBreakerStatsAggregatedStats"))));