~ overflow ~

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

Source script

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:

Shared Memory Configuration

PHP Semaphore Manual

MySQL Table Storage Engine Manual

Leave a Comment :, , , , , more...

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:

  1.  
  2. // (c) z3n – R1V1@090617 – z3n666@gmail.com – www.overflow.biz
  3.  
  4. // Fake Resolutions
  5. $resolutions=array("1024×768","1280×800","1280×1024","1440×900","1680×1050");
  6. // Fake Flash Versions
  7. $flash=array("10.0%20r2","10.0%20r1","9.0%20r12");
  8. // Fake Languages
  9. $languages=array("en-us","de","ja","ko","pt-br");
  10.  
  11. // functions
  12.  
  13. function baseurl($x) { //v1.03
  14.  $y=str_replace("http://","",$x);
  15.  $s=strpos($y,"/");
  16.  if ($s === false) {
  17.   $s=strpos($y,"?");
  18.  }
  19.  if ($s !== false) {
  20.   $y=substr($y,0,$s);
  21.  }
  22.  return "http://".$y;
  23. }
  24. function getmicrotime() { list($usec, $sec) = explode(" ",microtime());return ((float)$usec + (float)$sec); }
  25. function ga_fake($url,$ua) {
  26.  global $resolutions,$flash,$languages;
  27.  $gmt=round(getmicrotime(),0); // timestamp
  28.  $uid=mt_rand(70710490,92710490); // unique id number
  29.  $bid=mt_rand(21234567,91234567).mt_rand(1018864,9999999).mt_rand(1021,9999); // big random number
  30.  $java=(rand(0,100) > 85) ? 0 : 1; // java enabled?
  31.  $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";
  32.  @file_get_contents($x);
  33. }
  34.  
  35. // now you just need to call it
  36.  
  37. ga_fake("http://someurl/where/the/hit/happened/","UA-123456-78");
1 Comment :, , , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!