123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322 |
- <?php
- namespace ZN\Benchmarking;
- class __USE_STATIC_ACCESS__Benchmark implements BenchmarkInterface
- {
- //----------------------------------------------------------------------------------------------------
- //
- // Yazar : Ozan UYKUN <ozanbote@windowslive.com> | <ozanbote@gmail.com>
- // Site : www.zntr.net
- // Lisans : The MIT License
- // Telif Hakkı: Copyright (c) 2012-2016, zntr.net
- //
- //----------------------------------------------------------------------------------------------------
-
- //----------------------------------------------------------------------------------------------------
- // Protected Test
- //----------------------------------------------------------------------------------------------------
- //
- // Test isimleri bilgisi
- //
- // @var array
- //
- //----------------------------------------------------------------------------------------------------
- protected $tests = [];
-
- //----------------------------------------------------------------------------------------------------
- // Protected Memtests
- //----------------------------------------------------------------------------------------------------
- //
- // Bellek test isimleri bilgisi
- //
- // @var array
- //
- //----------------------------------------------------------------------------------------------------
- protected $memtests = [];
-
- //----------------------------------------------------------------------------------------------------
- // Protected Memtests
- //----------------------------------------------------------------------------------------------------
- //
- // Bellek test isimleri bilgisi
- //
- // @var array
- //
- //----------------------------------------------------------------------------------------------------
- protected $usedtests = [];
-
- //----------------------------------------------------------------------------------------------------
- // Protected Test Count
- //----------------------------------------------------------------------------------------------------
- //
- // Test sayısı bilgisi
- //
- // @var numeric
- //
- //----------------------------------------------------------------------------------------------------
- protected $testCount = 0;
-
- //----------------------------------------------------------------------------------------------------
- // Call Method
- //----------------------------------------------------------------------------------------------------
- //
- // __call()
- //
- //----------------------------------------------------------------------------------------------------
- use \CallUndefinedMethodTrait;
-
- //----------------------------------------------------------------------------------------------------
- // Error Control
- //----------------------------------------------------------------------------------------------------
- //
- // $error
- // $success
- //
- // error()
- // success()
- //
- //----------------------------------------------------------------------------------------------------
- use \ErrorControlTrait;
-
- //----------------------------------------------------------------------------------------------------
- // Test Methods Başlangıç
- //----------------------------------------------------------------------------------------------------
- //----------------------------------------------------------------------------------------------------
- // Test Start
- //----------------------------------------------------------------------------------------------------
- //
- // @param string $test
- // @return void
- //
- //----------------------------------------------------------------------------------------------------
- public function start($test = '')
- {
- if( ! is_string($test))
- {
- return \Errors::set('Error', 'stringParameter', 'test');
- }
-
- // Kaç test kullanıldığını hesaplamak için
- // test count değişkeni birer birer artırılıyor.
- $this->testCount++;
-
- // Yöntem içinden tanımlanan kodlardan kaynaklı
- // fazlalık hesaplanıyor.
- $legancy = ( $this->testCount === 1 )
- ? $legancy = 136
- : 48;
-
- $test = $test."_start";
-
- // Mikrotime yöntemi başlatılıyor.
- $this->tests[$test] = microtime();
-
- // Mikrotime yöntemi başlatılıyor.
- $this->usedtests[$test] = get_required_files();
-
- // Bu satıra kadar olan bellek miktarı hesaplanıyor.
- $this->memtests[$test] = memory_get_usage() + $legancy;
- }
-
- //----------------------------------------------------------------------------------------------------
- // Test End
- //----------------------------------------------------------------------------------------------------
- //
- // @param string $test
- // @return void
- //
- //----------------------------------------------------------------------------------------------------
- public function end($test = '')
- {
- if( ! is_string($test) )
- {
- return \Errors::set('Error', 'stringParameter', 'test');
- }
-
- $test = $test."_end";
-
- $this->memtests[$test] = memory_get_usage();
-
- $this->usedtests[$test] = get_required_files();
-
- $this->tests[$test] = microtime();
- }
-
- //----------------------------------------------------------------------------------------------------
- // Test Methods Bitiş
- //----------------------------------------------------------------------------------------------------
-
- //----------------------------------------------------------------------------------------------------
- // Result Methods Başlangıç
- //----------------------------------------------------------------------------------------------------
- //----------------------------------------------------------------------------------------------------
- // Elapsed Time
- //----------------------------------------------------------------------------------------------------
- //
- // @param string $result
- // @param numeric $decimal
- // @return string
- //
- //----------------------------------------------------------------------------------------------------
- public function elapsedTime($result = '', $decimal = 4)
- {
- if( ! is_string($result) )
- {
- return \Errors::set('Error', 'stringParameter', 'result');
- }
- if( ! is_numeric($decimal) )
- {
- $decimal = 4;
- }
-
- $resend = $result."_end";
- $restart = $result."_start";
-
- if( isset($this->tests[$resend]) && isset($this->tests[$restart]) )
- {
- return round(($this->tests[$resend] - $this->tests[$restart]), $decimal);
- }
- else
- {
- return false;
- }
- }
-
- //----------------------------------------------------------------------------------------------------
- // Used Files
- //----------------------------------------------------------------------------------------------------
- //
- // @param string $result
- // @return numeric
- //
- //----------------------------------------------------------------------------------------------------
- public function usedFiles($result = '')
- {
- if( ! is_string($result) )
- {
- return \Errors::set('Error', 'stringParameter', 'result');
- }
-
- if( empty($result) )
- {
- return get_required_files();
- }
-
- $resend = $result."_end";
- $restart = $result."_start";
-
- if( isset($this->usedtests[$resend]) && isset($this->usedtests[$restart]) )
- {
- return array_diff($this->usedtests[$resend], $this->usedtests[$restart]);
- }
- }
-
- //----------------------------------------------------------------------------------------------------
- // Used File Count
- //----------------------------------------------------------------------------------------------------
- //
- // @param string $result
- // @return numeric
- //
- //----------------------------------------------------------------------------------------------------
- public function usedFileCount($result = '')
- {
- if( ! is_string($result) )
- {
- return \Errors::set('Error', 'stringParameter', 'result');
- }
-
- if( empty($result) )
- {
- return get_required_files();
- }
-
- $resend = $result."_end";
- $restart = $result."_start";
-
- if( isset($this->usedtests[$resend]) && isset($this->usedtests[$restart]) )
- {
- return count($this->usedtests[$resend]) - count($this->usedtests[$restart]);
- }
- }
-
- //----------------------------------------------------------------------------------------------------
- // Calculated Memory
- //----------------------------------------------------------------------------------------------------
- //
- // @param string $result
- // @return string
- //
- //----------------------------------------------------------------------------------------------------
- public function calculatedMemory($result = '')
- {
- if( ! is_string($result) )
- {
- return \Errors::set('Error', 'stringParameter', 'result');
- }
-
- $resend = $result."_end";
- $restart = $result."_start";
- if( isset($this->memtests[$resend]) && isset($this->memtests[$restart]) )
- {
- $calc = $this->memtests[$resend] - $this->memtests[$restart];
-
- return $calc;
- }
- else
- {
- return false;
- }
- }
-
- //----------------------------------------------------------------------------------------------------
- // Result Methods Bitiş
- //----------------------------------------------------------------------------------------------------
-
- //----------------------------------------------------------------------------------------------------
- // Memory Methods Başlangıç
- //----------------------------------------------------------------------------------------------------
- //----------------------------------------------------------------------------------------------------
- // Memory Usage
- //----------------------------------------------------------------------------------------------------
- //
- // @param bool $realMemory
- // @return string
- //
- //----------------------------------------------------------------------------------------------------
- public function memoryUsage($realMemory = false)
- {
- if( ! is_bool($realMemory) )
- {
- $realMemory = false;
- }
-
- return memory_get_usage($realMemory);
- }
-
- //----------------------------------------------------------------------------------------------------
- // Max Memory Usage
- //----------------------------------------------------------------------------------------------------
- //
- // @param bool $realMemory
- // @return string
- //
- //----------------------------------------------------------------------------------------------------
- public function maxMemoryUsage($realMemory = false)
- {
- if( ! is_bool($realMemory) )
- {
- $realMemory = false;
- }
-
- return memory_get_peak_usage($realMemory);
- }
-
- //----------------------------------------------------------------------------------------------------
- // Memory Methods Bitiş
- //----------------------------------------------------------------------------------------------------
- }
|