Archive for June 17th, 2009
PHP Benchmark – MySQL vs File vs Semaphore
by z3n on Jun.17, 2009, under Coding, Tips & Hints
Problem:
Which method is faster? MySQL? MySQL HEAP? Direct File access? Semaphore?
Solution:
First a little explanation:
This test will incrase a variable/value from 0 to 10000, I did this test to figure out what’s the best method to do a heavy usage counter.
MySQL: will do a simple update query on a table, update tbl set counter=counter+1 limit 1
MySQL HEAP: will do the same of MySQL, however using a heap table which is stored directly on computer memory, many people claims that this is the fastest method.
Direct File: will store the variable on a file that is readden then written everytime a change is needed.
Semaphore: will store a variable straight into kernel shared memory (those variables will also be avaliable to programs external to apache/php)
Regular Variable: just for comparsion propouse, this test will show how much time it would take to incrase the counter into a regular php variable, not shared or anything.
Test Setup:
Athlon +2200 256kb cache
1GB RAM
Fedora 10
MySQL 5.0.77
PHP 5.2.9
Apache 2.2.9
Results: (test type (numbers added): how much time it took – how many times can be done in a day)
Regular Variable (10000): 0.003432035446167s – 251745651684.61/day
Semaphore (10000): 0.8582170009613s – 1006738387.8812/day
File (10000): 124.96164608002s – 6914121.4692927/day
MySQL (10000): 46.476830005646s – 18589908.130461/day
MySQL Heap (10000): 24.242248058319s – 35640259.018945/day
Conclusion:
As stated on the tests, semaphore is way faster than any other method, it might be a bit tricky to work with but it really did the job well, MySQL HEAP, was the second, althought it’s about 24 times slower, might be a good solution for you if you need to work with databases, however, MySQL HEAP has some limitations that might be a issue. Regular MySQL also shown itself fast, but about 2 times slower than heap. The file method could be faster on a SCSI or SSD, since i don’t have any of those i will stick with my slow SATA disk, however, i don’t think that even on a fast SSD this would be faster than semaphore, one simple reason is that the I/Os would be so many that they would limit the speed and viability of this idea, and also consume massive system resources.
* A note about semaphore: This method uses a shared part of memory, you can raise the limit of shared memory depending on your needs (check shared memory configuration link), note that if you’re going to use this method by many different processes you need to build up a queue system in order to make it work right, php has all the functions able to do it.
* Note 2: Semaphore stored values will not go away if php or apache crashes, they are stored straight into kernel shared memory, unless the whole machine crashes/reboot it will still there.
Links:
Faking Google Analytics Statistics
by z3n on Jun.17, 2009, under Coding, Tips & Hints
Problem:
Let’s assume you want to build up fake statistics on google analytics using a php script.
Solution:
You just need to input the google analytics UA code and you’re ready to go:
-
-
// (c) z3n – R1V1@090617 – z3n666@gmail.com – www.overflow.biz
-
-
// Fake Resolutions
-
$resolutions=array("1024×768","1280×800","1280×1024","1440×900","1680×1050");
-
// Fake Flash Versions
-
$flash=array("10.0%20r2","10.0%20r1","9.0%20r12");
-
// Fake Languages
-
$languages=array("en-us","de","ja","ko","pt-br");
-
-
// functions
-
-
function baseurl($x) { //v1.03
-
$y=str_replace("http://","",$x);
-
$s=strpos($y,"/");
-
if ($s === false) {
-
$s=strpos($y,"?");
-
}
-
if ($s !== false) {
-
$y=substr($y,0,$s);
-
}
-
return "http://".$y;
-
}
-
function getmicrotime() { list($usec, $sec) = explode(" ",microtime());return ((float)$usec + (float)$sec); }
-
function ga_fake($url,$ua) {
-
global $resolutions,$flash,$languages;
-
$gmt=round(getmicrotime(),0); // timestamp
-
$uid=mt_rand(70710490,92710490); // unique id number
-
$bid=mt_rand(21234567,91234567).mt_rand(1018864,9999999).mt_rand(1021,9999); // big random number
-
$java=(rand(0,100) > 85) ? 0 : 1; // java enabled?
-
$x="http://www.google-analytics.com/__utm.gif?utmwv=4.3&utmn=".mt_rand(64045995,94045995)."&utmhn=".str_replace("http://","",baseurl($url))."&utmcs=ISO-8859-1&utmsr=".$resolutions[array_rand($resolutions,1)]."&utmsc=32-bit&utmul=".$languages[array_rand($languages,1)]."&utmje=".$java."&utmfl=".$flash[array_rand($flash,1)]."&utmhid=".mt_rand(1650046796,1890046796)."&utmr=-&utmp=".str_replace(baseurl($url),"",$url)."&utmac=".$ua."&utmcc=__utma%3D".$uid.".".$bid.".".$gmt.".".$gmt.".".$gmt.".1%3B%2B__utmz%3D".$uid.".".$gmt.".1.1.utmcsr%3D(direct)%7Cutmccn%3D(direct)%7Cutmcmd%3D(none)%3B";
-
@file_get_contents($x);
-
}
-
-
// now you just need to call it
-
-
ga_fake("http://someurl/where/the/hit/happened/","UA-123456-78");