Exemplo n.º 1
0
 public function testSimple()
 {
     $this->expectOutputString("onFinish\nonFinish");
     $item = new Item();
     $item->attach($this);
     $manager = new Manager(new Item('N'));
     $this->assertSame('N', $manager->getPrototype()->getTitle());
     $this->assertSame($manager, $manager->setPrototype($item));
     $this->assertSame($item, $manager->getPrototype());
     $this->assertInstanceOf(\SplQueue::class, $manager->getIterator());
     $this->assertNull($manager->top());
     foreach ($manager as $item) {
         $this->fail('Should be empty iteration now: ' . gettype($item));
     }
     $newItem = $manager->create('1');
     $this->assertInstanceOf(Item::class, $newItem);
     $this->assertSame('1', $newItem->getTitle());
     $this->assertNotSame($item, $newItem);
     $this->assertSame($newItem, $manager->start());
     $this->assertInstanceOf(StateRunning::class, $newItem->getState());
     $this->assertInstanceOf(StatePaused::class, $manager->pause()->getState());
     $this->assertSame($newItem, $manager->top());
     $this->assertInstanceOf(StateRunning::class, $manager->resume()->getState());
     $newItem->stop();
     $this->assertNull($manager->discard());
     echo "\n";
     $this->assertNotSame($newItem, $newItem2 = $manager->namedStart('2'));
     $this->assertCount(1, $manager);
     $this->assertSame('2', $newItem2->getTitle());
     $this->assertSame($newItem2, $manager->stop());
     $this->assertCount(2, $manager);
     $item->detach($this);
     $this->assertSame('3', $manager->start()->setTitle('3')->setHandled(true)->stop()->getTitle());
     $this->assertCount(2, $manager);
 }
Exemplo n.º 2
0
 public function testBasic()
 {
     $this->expectOutputRegex('/\\[\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}] test\\.DEBUG: 1 \\{"PHProfiling\\\\\\\\Example\\\\\\\\TaggingObserver":\\{"tag":"EX"\\},"PHProfiling\\\\\\\\Example\\\\\\\\MemoryPeekUseObserver":\\{"usage":\\d+\\}\\} \\[\\]/');
     $logger = new Logger('test');
     $logger->pushHandler(new StreamHandler('php://output'));
     $prototype = new Item();
     $prototype->attach(new MonologOutput($logger, Logger::DEBUG));
     $prototype->attach(new TaggingObserver('EX'));
     $prototype->attach(new MemoryPeekUseObserver());
     $manager = new Manager($prototype);
     $manager->namedStart('1');
     $memoryEater = range(0, 500);
     $this->assertTrue($manager->stop()->isHandled());
     unset($memoryEater);
 }
Exemplo n.º 3
0
 /**
  * @param Item $item
  * @return mixed
  */
 public function onFinish(Item $item)
 {
     $this->assertArrayHasKey(BacktraceObserver::class, $item->getStatistics());
 }
Exemplo n.º 4
0
 /**
  * @param Item $item
  * @return mixed
  */
 public function start(Item $item)
 {
     return $item->setRunningState()->notify(StartObserver::class);
 }
Exemplo n.º 5
0
 protected function setUp()
 {
     $prototype = new Item();
     $prototype->attach(new TimingObserver());
     $this->manager = new Manager($prototype);
 }
Exemplo n.º 6
0
 protected function handle(Item $item)
 {
     if (!$item->isHandled() && $item->getParent() === null) {
         $this->handled->enqueue($item);
     }
 }
Exemplo n.º 7
0
 /**
  * @param Item $item
  */
 public function onStart(Item $item)
 {
     $item->setStatistic(self::class, self::FILED_START, $this->getT())->setStatistic(self::class, self::FILED_RUNTIME, 0);
 }
Exemplo n.º 8
0
 /**
  * @param Item $item
  * @return Item
  */
 public function discard(Item $item)
 {
     return $item->setInitState();
 }
Exemplo n.º 9
0
 /**
  * @param Item $item
  */
 public function onStart(Item $item)
 {
     $item->setStatistic(static::class, static::FILED_TAG, $this->tag);
 }
Exemplo n.º 10
0
 public function testStateChanges()
 {
     $item = new Item();
     $this->assertInstanceOf(StateInit::class, $item->getState());
     $this->assertInstanceOf(StateInit::class, $item->pause()->getState());
     $this->assertInstanceOf(StateInit::class, $item->resume()->getState());
     $this->assertInstanceOf(StateInit::class, $item->stop()->getState());
     $this->assertInstanceOf(StateInit::class, $item->discard()->getState());
     $this->assertInstanceOf(StateRunning::class, $item->start()->getState());
     $this->assertInstanceOf(StateRunning::class, $item->start()->getState());
     $this->assertInstanceOf(StateRunning::class, $item->resume()->getState());
     $this->assertInstanceOf(StatePaused::class, $item->pause()->getState());
     $this->assertInstanceOf(StatePaused::class, $item->pause()->getState());
     $this->assertInstanceOf(StatePaused::class, $item->start()->getState());
     $this->assertInstanceOf(StateStopped::class, $item->stop()->getState());
     $this->assertInstanceOf(StateStopped::class, $item->start()->getState());
     $this->assertInstanceOf(StateStopped::class, $item->pause()->getState());
     $this->assertInstanceOf(StateStopped::class, $item->resume()->getState());
     $this->assertInstanceOf(StateStopped::class, $item->stop()->getState());
     $this->assertInstanceOf(StateStopped::class, $item->discard()->getState());
     $this->assertInstanceOf(StateInit::class, $item->setInitState()->getState());
     $this->assertInstanceOf(StateInit::class, $item->start()->discard()->getState());
     $this->assertInstanceOf(StateInit::class, $item->start()->pause()->discard()->getState());
     $this->assertInstanceOf(StateStopped::class, $item->start()->stop()->getState());
     $cloned = clone $item;
     $this->assertInstanceOf(StateInit::class, $cloned->getState());
 }
Exemplo n.º 11
0
 /**
  * @param Item $item
  */
 public function onStop(Item $item)
 {
     $item->setStatistic(static::class, static::FILED_MEMORY_USAGE, memory_get_peak_usage($this->realUsage) - $item->getStatistic(static::class, static::FILED_MEMORY_USAGE));
 }
Exemplo n.º 12
0
 /**
  * @param Item $item
  * @param string $field
  * @param string $invoke
  * @param string $file
  * @param int $line
  * @return $this
  */
 protected function writeStatistics(Item $item, $field, $invoke, $file, $line)
 {
     $item->setStatistic(static::class, $field, [static::SUB_FILED_EXPRESSION => $invoke, static::SUB_FILED_FILE => $file, static::SUB_FILED_LINE => $line]);
     return $this;
 }
Exemplo n.º 13
0
 /**
  * @param Item $item
  */
 public function onFinish(Item $item)
 {
     $this->logger->addRecord($this->level, $item->getTitle(), $item->getStatistics());
     $item->setHandled(true);
 }