Posts tagged with ‘Class’

2

Benchmarking and Timing PHP Class

Introducing a simple, yet deceivingly useful, micro benchmarking / timer class called Bench.

I’ve found myself on more than a few occasion using this familiar block of code in order to quickly narrow down a bottleneck or test a specific section of code.

1
2
3
4
$start = microtime(true);
// [Some Code To Test]
$time = (microtime(true) - $start);
echo $time.' Seconds';

This is a crude but effective way of finding out how long it takes to get from line A to line B. On the down-side, this technique is annoying to implement, only measures time between two points, and makes code look very cluttered — so I started developing Bench.

Features

Bench offers more than just clean looking code or timing between two lines. Its goal is to be more powerful and reusable than simple Microtime Arithmetic while trying avoid stepping on anyone’s toes (*cough* *cough* Xdebug, see below).

Start and Stop

Bench can perform the same operation as the example above but in a much cleaner manner.

1
2
3
Bench::start();
// [Some Code To Test]
echo Bench::stop() . ' Seconds.';

Marks

“Marks” are simply points in code that one would like tracked by Bench. They are easy to use, quick to implement, and very useful when tracking multiple parts of a script/request.

1
2
3
4
5
6
7
8
9
Bench::start();
// [Application Bootstrap]
Bench::mark('bootstrap');
// [Database Connection Opened]
Bench::mark('database');
// [Data Processing + Manipulation]
Bench::mark('processing');
// [HTML Creation]
Bench::mark('html');

Each call to mark() creates a Mark Array that contains the id, microtime of when it occurred, seconds since start, and seconds since the previously called mark.

(more…)

0

WinCache PHP Class

I’ve developed a new class for WinCache that wraps around the wincache_ucache* functions. The goal of this class/project is to improve — and add to — the functionality and usability of WinCache’s User Cache feature.

Basic Usage

Below is a quick code example of how this class simplifies the way in which a developer can take advantage of the user cache.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
 * Get Cache Object
 */

$cacheObj = DDWinUCache::getInstance();
/**
 * Cache Data
 */

$cacheObj->set('foo', 'bar');
$cacheObj->set('bar', 'candy');
$cacheObj->set('hello_world', 'we did it!');
$cacheObj->set('apple_red_core', 'simple tagging');
/**
 * Delete Cache Entries
 */

$cacheObj->delete('*or*'); // array('apple_red_core', 'hello_world');
/**
 * Get Cached Data
 */

$cacheObj->get('foo'); // "bar"

New Features

Within this class I’ve greatly improved the functionality of the delete method. I’ve also made getting information about the user cache simpler by creating accessor methods for all user cache related data. And to top it all off I’ve implemented ArrayAccess to push the simplicity over the edge.

Improved Delete

Arguably the biggest feature of this class is the ability to delete cache entries via three methods.

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
 * Traditional
 */

$cacheObj->delete('this_is_a_test_entry');
/**
 * Wild Cards '*' and '?'
 */

$cacheObj->delete('this*');
$cacheObj->delete('this?is?a?test?entry');
/**
 * Regular Expression (PCRE)
 */

$cacheObj->delete('/^this.*?entry$/i');

Accessor Methods

I’ve mapped all the data available via the arrays returned by wincache_ucahce_info() and wincache_ucahce_meminfo() into accessor methods. I’ve also added two more derived methods that can be used to help control the user cache — getMemoryUsed() and getMemoryUsedPercent()

(more…)