Tuesday, November 3, 2009
How to optimize PHP applications
The best reading I've found are written by Ilia Alshanetsky [blog]:
PHP & PERFORMANCE [pdf]
By: Ilia Alshanetsky
Common Optimization Mistakes [pdf]
PHP Quebec 2009
Enjoy !
Thursday, October 15, 2009
a Javascript function to manage function timeout
function callTimeout(funcName, timeout, args) {
if (args==undefined) {args = "";}
if (timeout == undefined) { timeout=500; }
if (this.timeOutsArray == undefined) {
this.timeOutsArray = new Array(); }
if (this.timeOutsArray[funcName] == undefined) {
this.timeOutsArray[funcName] = 0;
}
if (this.timeOutsArray[funcName]) {
clearTimeout(this.timeOutsArray[funcName]);
}
eval("this.timeOutsArray['"+funcName+"'] = setTimeout('" + funcName + "("+args+")', "+timeout+");");
}
Example of use with JQuery:
$("#field1").keyup(function(){callTimeout("validatefield1",500);})
$("#field2").keyup(function(){callTimeout("validatefield2",500);})
$("#fieldN").keyup(function(){callTimeout("validatefieldN",500);})
Monday, October 12, 2009
printing the backtrace in a complex PHP application
the PHP debug_backtrace [man] function is very useful to understand where a function/method is called.
It prints the back trace of the code.
Example: In the framework I'm currently using there are ORM classes to access the DB. So, it takes long to understand where the query is launched when needed. Solution: save in a global variable (or in a field of the class) the list of the queries launched and the code that has launched each query.
Code:
class DB {
# ...
public function query($query) {
if (DEBUG_MODE) { ##
$_dbt = debug_backtrace();
$_fromFile = isset( $_dbt[0]['file']) ? str_replace(_ROOT, "",$_dbt[0]['file']) : "";
$_fromLine = isset( $_dbt[0]['line']) ? $_dbt[0]['line'] : "";
$_launchedFrom = "launched from $_fromFile:$_fromLine";
$this->logQueries[] = "[$query][$_launchedFrom]";
# ..query.
}
}
# ...
}
Example of result:
Array
(
[0] => [set names utf8][launched from C:\wamp\www\dev\index.php:68]
[1] => [SELECT * FROM `Setting` WHERE `id` = 1][launched from \class\Setting.class.php:20]
[2] => [SELECT id FROM `User`WHERE id = 293968 LIMIT 1][launched from \class\User.class.php:598]
[3] => [SELECT * FROM `User` WHERE `id` = 293968][launched from \class\User.class.php:45]
[4] => [SELECT f.store_id FROM `Store_Monthly_Featured` f ORDER BY f.order ASC][launched from \class\Store_Monthly_Featured.class.php:16]
)
Useful function to print only line and number
function debug_backtrace_filelines() {
$ret = array();
$b = debug_backtrace();
foreach ($b as $elements) {
$ret[] = $elements['file'].':'.$elements['line'];
}
}
How to search complex source code using regular expressions
Today I needed to search the source code that ware calling a function with a complex array of parameters
URL::Make( 'site/store.inc.php', array( 'action'=>'view','id'=> [... STH...] ,'idTwo'=> [... STH...] ) );
Considering this requirements:
- there are lots of similar function (to exclude from the search) with an argument less or an argument more
- there may are additional spaces in the line
- some other delopers have probably used double quotes instead of single quotes for array keys
- the value of the keys may be a PHP code
- every function call is in one line
An elegant solution is to search using regular expression. Netbeans (my favorite editor) supports POSIX extended regular expression and is very quickly to search a complex epression in a huge amount of source code.
Solution 1
#using char length and patter ".{0,MAX}" (that is: every sequence, MAX maximum chars). Easy !
URL::Make.{0,10}site/store.{0,40}action.{0,9}view.{0,20}id.{ 0,200}idTwo.{0,30}\).{0,10}\)
Solution 2
#using regexpr for separators. more complex. more accurate in some cases but fails in some cases (eg: comments inside)
URL::Make.*\([ ]*['"]site/store.inc.php['"][ ]*,[ ]*array[ ]*\([ ]*['"]action['"][ ]*=>[ ]*['"]view['"][ ]*,[ ]*['"]id['"][ ]*=>.{0,100}['"]idTwo['"][ ]*=>.{0,100}[ ]*\)[ ]*\)
An idea for DB server load balancing
private function queryServer($query, $server = "mainServer" ) {
$db = DB::GetInstance($server);
return $db->query();
}
public function loadBalancedQuery($query) {
if (preg_match("/[]*(insert|INSERT|update|UPDATE|alter|ALTER).*/",
$query)){
$this->queryServer( $query, "mainServer" );
$this->queryServer( $query, "secondaryServer" );
} else {
$this->queryServer( $query, rand(0,1) ? "mainServer": "secondaryServer" );
}
}
}
Sunday, October 11, 2009
UTF8 in LAMP applications: overview and how to solve the common issues
- set your IDE to save and open source files using utf8 encoding
- set the content-type of your application to utf-8 (better a apache/htaccess rule instead of the meta tag).
- set the database server to use utf8 encoding (also tables must be converted). If the db is utf8 but client encoding is latin1, execute first of all the query "SET NAMES utf8"
- if the application was using latin1 and PHP convert functions, remove all the existing function to encode/decode special characters/entities.
Monday, September 28, 2009
Mass rows copying (duplicating) with filed customization - MySQL
# `table`
id | a | b | c |
---------------------------
1 | "aaa" | "xxx" | "ccc"
2 | "aab" | "yyy" | "ccc"
3 | "aba" | "yyy" | "ccc"
4 | "abc" | "xxx" | "ccc"
5 | "dcz" | "xxx" | "eee"
Now, we want to copy some records (only some columns) to the same table AND change some of them with a fixed value.
Requirements:
- duplicate all the rows with `b` equal to "xxx"
- for the new rows inserted, the value of `c` has to be changed to "ddd" (fixed value!)
- `id` is the primary key, auto increment
Query (pay attention to the quote characters):
INSERT INTO `table`
(SELECT
Result:
id | a | b | c |
---------------------------
1 | "aaa" | "xxx" | "ccc" #row1
2 | "aab" | "yyy" | "ccc"
3 | "aba" | "yyy" | "ccc"
4 | "abc" | "xxx" | "ccc" #row4
5 | "dcz" | "xxx" | "eee" #row5
6 | "aaa" | "xxx" | "ddd" #copied from #row1
7 | "aab" | "xxx" | "ddd" #copied from #row4
8 | "dcz" | "xxx" | "ddd" #copied from #row5
Example: If before there was a record with the same values as in line 9 and there was a unique key on columns (a,b,c), the query would insert only rows on line 6,7 and 8 (9 fails).
Some old version of MySQL stop execution in case of duplicate entries. In this case, add IGNORE to the query to skip duplicates: INSERT IGNORE INTO ...