Archive | PHP RSS feed for this section

29 June 2010 View Comments

mysqlind_qc: Client Side Caching for MySQL extensions for PHP

mysqlind_qc: Client Side Caching for MySQL extensions for PHP

Here’s something in the making, a PHP extension which caches MySQL queries irrespective of the extension being used. It’s a plugin for mysqlnd, the native MySQL driver for PHP called  mysqlnd_qc. This currently requires PHP 5.3.3-dev which is still in development.

The extension allows caching buffered queries through mysql_query and mysqli_query. It allows cache-invalidation though TTL or a user defined callback. This extension is still in the protoype stage right now and will evolve as the project progresses.

The Query Cache is implemented as a PHP extension. It is written in C and operates “under the hood” of PHP. During the start up of the PHP interpreter it gets registered as a mysqlnd plugin to replaces selected mysqlnd methods.

At PHP run time it proxies queries send from mysqlnd/PHP to the MySQL server. If a query string starts with the SQL hint (/*qc=on*/) to enable caching of it and the query is not cached (Cache miss), the query cache plugin will record the raw wire protocol data send from MySQL to PHP to answer the query. The query cache records the wire protocol data in its cache medium and replays it, if still valid, on a cache hit.

Note that the query cache does not hold decoded result sets consisting of zvals (C struct representing a PHP variable). It stores the raw wire data of the MySQL client server protocol. In case of a cache hits, mysqlnd still needs to decode the cached raw wire data into PHP variables before passing the result to the user space. This approach has one major advantage: simplicity.

You can get more information and installation instructions over at the MySQLND Query Cache Project page at Mysql Forge.

Check out this slide which gives you more information about this extension:

Enhanced by Zemanta

21 June 2010 View Comments

Free Ebook on the Zend Framework

Free Ebook on the Zend Framework

Here’s a very good e-book on the Zend Framework called Zend Framework: Surviving The Deep End by Pádraic Brady. You can access this book from it’s site at www.survivethedeepend.com. It’s not yet available in print or download, but the author does mention that there’s a print book coming out in the near future.
Here’s a top level Table of contents of the subjects covered in this book:

1. Introduction
2. The Architecture of Zend Framework Applications
3. The Model
4. Installing The Zend Framework
5. A Not So Simple Hello World Tutorial
6. Standardise The Bootstrap Class With Zend_Application
7. Handling Application Errors Gracefully
8. Developing A Blogging Application
9. Implementing The Domain Model: Entries and Authors
10. Setting The Design With Zend_View, Zend_Layout, HTML 5 and Yahoo! User Interface Library
A. Creating A Local Domain Using Apache Virtual Hosts
B. Performance Optimisation For Zend Framework Applications
C. Copyright Information
If you’re already working on the Zend Framework, take a look at the section on Performance optimizations for apps built on the Zend Framework, which has some useful tips.

27 May 2010 View Comments

Create RESTful Webservices in minutes with FRAPI

Create RESTful Webservices in minutes with FRAPI

Planning on starting a project with REST webservices? You’ve gotta check out this new framework called FRAPI. This API framework built in PHP eases development of REST webservices, by allowing you to add actions, responses and even create users and assign API keys to them for accessing the services you create. Here’s a video showing an overview of FRAPI and how simple it is to deploy and manage webservices.

This project is still quite new and their documentation is down to the bare minimum. They do have the required documentation for you you’ll need to get your hands dirty with FRAPI. First you’ll have to download the sources and set it up on your own. One you’ve downloaded the files and setup the directory permissions, you’ll also have to make the Apache Virtual host settings to get FRAPI to start working.

FRAPI has the following requirements for it to run:

  • PHP 5.2.2 or later
  • APC: Required for caching of actions, errors, etc.
  • HTTP: (http://pecl.php.net/pecl_http
  • PEAR: (http://pear.php.net)
  • PEAR::HTTP_Request2: This could be replaced by pecl_http however it is used in the bundled ArmChair package which is used to access CouchDB (http://pear.php.net/HTTP_Request2)

Some of the advantages I see after playing around with FRAPI is that it auto-generates the code skeleton for you to work on so you just have to go fill in the blanks to create your service. You also don’t have to worry about transfoming the your data into different formats, FRAPI supports JSON, XML, PHP, and plain text natively. This enables you to make your webservices cross-platform and even power mobile apps. You also don’t have to tweak around with code which handles http requests and responses while creating your webservices.

For more information on this project, head over to their site  getfrapi.com or their project page on github.

Reblog this post [with Zemanta]

18 May 2010 View Comments

Check your server load before you process

Check your server load before you process

Most of us just write applications to be deployed on a webserver without thinking about what to do when the server becomes overloaded. What happens in most cases is that the application would go trying to run itself on every request, and on a heavily loaded system, it just goes on the aggravate the problem, making increasing the load on the server, till finally the server becomes unreachable.

What if you could actually check the server load in your PHP application? Would you think about checking the server load before doing some heavy computational task or database accesses? There’s function in PHP which will allow you to check the load averages on a server.

The sys_getloadavg() in PHP gives you the load averages for your server. You can use this to check the load on your server before processing a request.

$serverload = sys_getloadavg();
print_r($serverload);

The code above gives the output:

Array
(
    [0] => 1.07
    [1] => 0.89
    [2] => 1
)

The output from the code shows the system load averages where [0] is the load averages for the past 1 minute, [1] is for the past 5 minutes and [2] is for the past 15 minutes.

On an ideal server, the load averages on the server shouldn’t go above 3. A load average of more than 15 would mean that the server is already running much lower than normal, and you may not want to add more load to this.

You can use this output to decide whether to serve a process intensive page depending on the server load. Here’s a pseudo-code on how you’d do something like this:

$serverload = sys_getloadavg();
if ($serverload[0]<10)
  {
    // process loads of data now
    some_big_process();
  } else
     {   // Send a 503 header stating that the server is overloaded.
         header('HTTP/1.1 503 Too busy, try again later');
         die('The server is busy at present and cannot process your request.');
      }

You can also run the application in such a way that you can wait till the load on the server decreases to run your process. This is useful if you are running a cron for processing data:

set_time_limit(0);
//set time limit to 0, so PHP's max execution time doesn't interfere with the processing script
$serverload = sys_getloadavg();
//Check load and see if it's low enough to start processing
while ($serverload[0]>5)
{  //Wait for 1 minute to check load again
    sleep(60);
}
// out of the loop - so let's do some processing now!
some_big_process();

To ensure that the script doesn’t run endlessly on a permanently overloaded server, time check in the while loop to auto-end the script it the server load remains high for a certain amount of time.

More reading:
Load & Load Averages on Wikipedia
PHP function – sys_getloadavg

16 April 2010 View Comments

Bangalore PHP Meetup April ’10

Bangalore PHP Meetup April ’10

Just a quick heads up in case you missed out on the announcement. The Bangalore PHP Meetup’s happening this month on the 24th. For a change, I’m not involved much in the organization of this event. Indus Khaithan took care of the venue arragements and Harsha MV’s taken up co-ordinating with the potential speakers and finalizing the talk list. Thanks a ton guys for taking taking the initiative.

Harsha’s put up a tenative list of talks for this month’s event:

  • Developing Facebook Application using PHP by Sriram Kumar
  • Job Queues by Abhinav Lal
  • Zend ACL Component - Bare_Acl by Sudheer Satyanarayana

Here’s the link to this month’s meetup event – www.meetup.com/Bangalore-PHP-Users/calendar/13135127. We’ll keep that page updated with the latest details about the event. If you are planning on coming for this, join our Meetup group and RSVP ‘YES’ to the event.

As always – Please forward that link to your friends who you think would be interested in our group as well.

13 January 2010 View Comments

Interesting Zend Webinars for January

Interesting Zend Webinars for January

Here are some webinars from Zend which you may find interesting:

Webinar – PHP Development Best Practices: The Untold Story of Geekville
January 19, 2010 – 9:00 am PST – your computer via webex

Have you ever wondered how you could advance your PHP development? Have you considered using an Integrated Development Environment (IDE), an Issue Tracker or a Version Control tool, but were concerned about how complex it might be? Taking PHP development to the next level may be easier than you think.  Attend this webinar, hosted by Atlassian and Zend More Information/Registration

Webinar – Troubleshooting PHP Issues: Best (and Worst) Techniques
January 28, 2010 – 8:00 am PST – your computer via webex

Understanding what’s causing your PHP application to be slow or just break is often time-consuming, and almost always frustrating. Join this information-packed webinar, delivered by a senior Zend PHP consultant, to learn what techniques PHP professionals use for pinpointing PHP issues in development, testing and production. More Information/Registration

Reblog this post [with Zemanta]

Tags:
12 January 2010 View Comments

PHP 5 Power Programming – Free Ebook

PHP 5 Power Programming – Free Ebook

Here’s a free PHP Ebook from Bruce Perens’ Open Source Series. More about this book:

In this book, PHP 5′s co-creator and two leading PHP developers show you how to make the most of PHP 5′s industrial-strength enhancements in any project—no matter how large or complex. Their unique insights and realistic examples illuminate PHP 5′s new object model, powerful design patterns, improved XML Web services support, and much more. Whether you’re creating web applications, extensions, packages, or shell scripts—or migrating PHP 4 code—here are high-powered solutions you won’t find anywhere else.

Review PHP’s syntax and master its object-oriented capabilities—from properties and methods to polymorphism, interfaces, and reflection

  • Master the four most important design patterns for PHP development
  • Write powerful web applications: handle input, cookies, session extension, and more
  • Integrate with MySQL, SQLite, and other database engines
  • Provide efficient error handling that’s transparent to your users
  • Leverage PHP 5′s improved XML support—including parsing, XSLT conversions, and more
  • Build XML-based web services with XML-RPC and SOAP
  • Make the most of PEAR: work with the repository, use key packages, and create your own
  • Upgrade PHP 4 code to PHP 5—compatibility issues, techniques, and practical workarounds
  • Improve script performance: tips and tools for PHP optimization
  • Use PHP extensions to handle files/streams, regular expressions, dates/times, and graphics
  • Create original extensions and shell scripts

If you’re a software developer new to PHP, you’ll leap quickly into PHP and its new object-oriented capabilities. If you’re an experienced PHP programmer, you already recognize PHP’s convenience and simplicity. Now, discover all of its extraordinary power!

Free Ebook download: Download PHP 5 Power Programming (PDF)
If you like this book and would like to order the Print book, check out Amazon’s deals on PHP 5 Power Programming.

4 January 2010 View Comments

Practical PHP Testing – Free Ebook

Practical PHP Testing – Free Ebook

If you’re new to testing in PHP, and were wondering how to create and run automated tests, here’s an ebook which will help you get the basics right.

Practical PHP Testing is an ebook which is a compilation of  articles from Giorgio Sironi’s blog on Practical PHP testing.

This book takes you though the basics of PHPUnit – how to install it and start writing simple tests using PHP Unit. Here are some of what this ebook covers:

  • bonus chapter on TDD theory;
  • a case study on testing a php function;
  • working code samples, some of whom were originally kept on pastebin.com;
  • sets of TDD exercises at the end of each chapter;
  • glossary that substitutes external links to wiki and other posts, to not interrupt your reading with terms lookup.

More information and download link is available here.

Reblog this post [with Zemanta]

30 December 2009 View Comments

PHP Profiling – XHProf

PHP Profiling – XHProf

binary code and magnifying glass
XHProf is a heriarcical profiler for PHP originally developed by Facebook and then opensourced. The raw data collection component is implemented in C (as a PHP extension). The reporting/UI layer is all in PHP. It is capable of reporting function-level inclusive and exclusive wall times, memory usage, CPU times and number of calls for each function. Additionally, it supports ability to compare two runs (hierarchical DIFF reports), or aggregate results from multiple runs.

Here are some of the reports that XHProf provides:

If you are looking for an article which gives you details on how to install this extension in Ubuntu and a quick runthrough of how to use this, Lorenzo Alberton has an excellent article on this at Profiling with XHProf. There’s also a good background article on XHProf and why Facebook developed this extension over at Facebook.

Links:
XHProf Documentation
XHProf on PECL
Profiling with XHProf

Image Credit: from Crestock Photos

Tags: ,
24 November 2009 View Comments

Compress your serialize output using igbinary

igbinary in phpinfo

Igbinary is a  replacement for the standard PHP serializer. While the PHP serializer uses a texual format to represent the data in the serialized version, igbinary uses a binary format which is compact. This helps in brining down the storage size of the serialized data. This helps while trying to store the data in shared memory or memcache, which uses (limited) RAM to provide faster access to data.

Since I couldn’t find any pre-built binaries, I compiled the code which I got from their site. I compiled the 1.02 build.

Here are quick steps to compile this as a PHP extension for your system ( I tried this on Ubuntu, but should work on other distros as well). Uncompress the contents of the file to a directory and head over to the directory and run the following comands one after the other. Make sure there are no errors in each stage.

>phpize
>./configure CFLAGS="-O2 -g" --enable-igbinary
>make
>make install

If all goes well, the file igbinary.so should be present in your default php extension directory. Once you see this file there, head over to your php.ini file and add the following line at the end of it so that the extension is loaded with PHP.

# Load igbinary extension
extension=igbinary.so

Once you do this, restart your apache server to reload the PHP configurations. When you run phpinfo(), you should see the following lines in the output:
igbinary in phpinfo

Once you’ve got that in your output, all you need to do is to substitute serialize with igbinary_serialize and unserialize with igbinary_unserialize in your code.

To see the differences in the two formats and see if the serialized and the subsequent unserialize I use the following script with some dummy data to print out the size of the output string of serialize and igbinary_serialize.

$arrc[3]=1;
$arrc[2]=2;
$arrc[0]=3;
$arrc[1][]="Testing";
$arrc[1][]="another data";
$arrc[1][]="structure";
$arrc[]=1;
$arrc[]=2;
$arrc[]=3;
$a = serialize($arrc);
echo "<br />size of Serialize :". strlen($a);
$b = igbinary_serialize($arrc);
echo "<br />size of igbinary :". strlen($b);

The output of this script came up showing that output from igbinary_unserialize does indeed use less space than the the output of serialize. Here’s the output of this script when I ran it:

size of Serialize :126
size of igbinary :74

If you want the igbinary functions to auto replace the default serialize in the PHP session handler, all you need to do is to add the following lines in your php.ini

# Use igbinary as session serializer
session.serialize_handler=igbinary

If you have any experience in using this extension on your projects, let us know your thoughts and observations. You can get more information on igbinary from the author’s site at: http://opensource.dynamoid.com/