Skip to content

Commit 73a80a7

Browse files
committed
v3
Tool::flat_json_encode Tool::json_encode errors to strings Debug not based on JSON Bench separated from Debug Adding README doc
1 parent 3e6c384 commit 73a80a7

File tree

4 files changed

+347
-182
lines changed

4 files changed

+347
-182
lines changed

README.md

Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,123 @@ Provides php tools used by other grithin/php* projects.
55

66
# Tools
77

8+
## Bench
9+
Simple benching utility
10+
11+
```php
12+
$bench = new \Grithin\Bench();
13+
for($i=0; $i<10000; $i++){
14+
mt_rand(0,100000) + mt_rand(0,100000);
15+
}
16+
$bench->mark();
17+
for($i=0; $i<10000; $i++){
18+
mt_rand(0,100000) + mt_rand(0,100000);
19+
}
20+
$bench->end_out();
21+
```
22+
23+
```
24+
...
25+
"value": {
26+
"intervals": [
27+
{
28+
"time": 0.0035231113433838,
29+
"mem.change": 808
30+
},
31+
{
32+
"time": 0.0028860569000244,
33+
"mem.change": 776
34+
}
35+
],
36+
"summary": {
37+
"mem": {
38+
"start": 403168,
39+
"end": 404752,
40+
"diff": 1584
41+
},
42+
"time": 0.0064091682434082
43+
}
44+
}
45+
...
46+
```
47+
48+
## Debug
49+
Handling errors and print output
50+
51+
52+
### Set up for error handling
53+
```php
54+
# optionally configure
55+
Debug::configure([
56+
'log_file'=>__DIR__.'/log/'.date('Ymd').'.log',
57+
'err_file'=>__DIR__.'/log/'.date('Ymd').'.err', ]);
58+
59+
set_error_handler(['\Grithin\Debug','handleError'], E_ALL & ~E_DEPRECATED & ~E_STRICT & ~E_NOTICE);
60+
set_exception_handler(['\Grithin\Debug','handleException']);
61+
```
62+
63+
### Output
64+
65+
Printing out any variable
66+
```php
67+
$bob = ['bob'];
68+
$sue = ['sue'];
69+
$bob[] = &$sue;
70+
$sue[] = &$bob;
71+
$sue[] = 'moe';
72+
73+
Debug::out($sue);
74+
75+
echo 'bob';
76+
```
77+
Outputs:
78+
```
79+
{
80+
"file": "\/test.php",
81+
"line": 37,
82+
"i": 1,
83+
"value": []
84+
}bob
85+
```
86+
If run within a web server, it will enclose with `<pre>`
87+
88+
`Debug::quit();` will do `::out()` then `exit`.
89+
90+
91+
92+
93+
### Logging
94+
95+
You can configure the log and err file paths, or allow them to be automatically determined. The names will be `log` and `err`, and the folder will be determined upon:
96+
- if `$_ENV['root_path']`
97+
- if `$_ENV['root_path'].'.log/'`, use
98+
- else use `$_ENV['root_path']`
99+
- else
100+
- if `dirname($_SERVER['SCRIPT_NAME'])`, use
101+
- else use `dirname($_SERVER['SCRIPT_NAME'])`
102+
103+
Errors are automatically logged. You can separately log information using:
104+
```php
105+
Debug::log('BOB');
106+
```
107+
108+
By default, this will be JSON pretty printed. You can turn that off with
109+
```php
110+
Debug::configure(['pretty'=>false]);
111+
```
112+
113+
You can also determine what gets logged. The second parameter is used in a regex max against the configured mode
114+
```php
115+
Debug::configure(['mode'=>'error debug']);
116+
117+
Debug::log('BOB','error');#< will log
118+
Debug::log('BOB1','error|debug');#< will log
119+
Debug::log('BOB2','info'); #< will not log
120+
```
121+
122+
123+
124+
8125
## Files
9126
The primary functions are file inclusion functions `inc`, `req`, `incOnce`, `reqOnce`. They allow inclusion tracking, context isolation, variable injection, variable extraction, and global injection.
10127

@@ -24,4 +141,4 @@ Files::inc('bob.php')
24141
# Using variable injection and variable extraction
25142
Files::inc('bob.php',['bob'=>'sue'], ['extract'=>['bill']])
26143
#< ['sue', 'monkey']
27-
```
144+
```

src/Bench.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?
2+
namespace Grithin;
3+
4+
class Bench{
5+
public $measures = [];
6+
function __construct(){
7+
$this->mark();
8+
}
9+
function mark(){
10+
$next = count($this->measures);
11+
$this->measures[$next]['time'] = microtime(true);
12+
$this->measures[$next]['mem'] = memory_get_usage();
13+
}
14+
function end(){
15+
$this->mark();
16+
$out[$name] = ['diff'=>[]];
17+
18+
$current = current($this->measures);
19+
$mem = ['start'=>$this->measures[0]['mem'], 'end'=>$this->measures[count($this->measures)-1]['mem']];
20+
$mem['diff'] = $mem['end'] - $mem['start'];
21+
$time = 0;
22+
$intervals = [];
23+
while($next = next($this->measures)){
24+
$outItem = &$intervals[];
25+
26+
$time += $outItem['time'] = $next['time'] - $current['time'];
27+
$outItem['mem.change'] = $next['mem'] - $current['mem'];
28+
$current = $next;
29+
}
30+
$summary = ['mem'=>$mem, 'time'=>$time];
31+
return ['intervals'=>$intervals, 'summary'=>$summary];
32+
}
33+
function end_out(){
34+
$end = $this->end();
35+
Debug::out($end);
36+
}
37+
}

0 commit comments

Comments
 (0)