Advertisement
Pathoschild

Simulated crosswiki tool on Tools Labs

Aug 31st, 2013
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.46 KB | None | 0 0
  1. <?php
  2. ###############
  3. ## This code simulates a tool connecting to every wiki database
  4. ## with cached connections. This is a common scenario for crosswiki
  5. ## tools, so the performance is important. It uses as little framework
  6. ## code as possible to eliminate confounding factors for the test.
  7. ##
  8. ## This code runs here: http://tools.wmflabs.org/pathoschild-contrib/accounteligibility/test.php
  9. ## The profiling times are shown at the bottom of that page (click the
  10. ## little [+] next to "Page generated in...").
  11. ################
  12. require_once("../backend/modules/Backend.php");
  13. $backend = Backend::create("Crosswiki tool performance", "Simulates a tool connecting to every wiki DB with cached connections. This is a common scenario for crosswiki tools, so the performance is important.")->header();
  14. echo "<pre>";
  15.  
  16.  
  17. # get DB details
  18. $config = parse_ini_file("/data/project/pathoschild-contrib/replica.my.cnf");
  19. $username = $config["user"];
  20. $password = $config["password"];
  21.  
  22.  
  23. # define a few helpers to make code more readable
  24. function connect($slice, $db) {
  25.     global $username, $password;
  26.     return new PDO("mysql:host=$slice;dbname=${db}_p", $username, $password);
  27. }
  28.  
  29. function executeQuery($connection, $sql) {
  30.     $query = $connection->prepare($sql);
  31.     $query->execute();
  32.     return $query;
  33. }
  34.  
  35. function fetchQuery($connection, $sql) {
  36.     return executeQuery($connection, $sql)->fetchAll(PDO::FETCH_ASSOC);
  37. }
  38.  
  39. function start($name) {
  40.     global $backend;
  41.     $backend->profiler->start($name);
  42. }
  43.  
  44. function stop($name) {
  45.     global $backend;
  46.     $backend->profiler->stop($name);
  47. }
  48.  
  49.  
  50. # fetch wiki dbname/slice pairs from DB
  51. start("fetch wikis");
  52. $connection = connect("metawiki.labsdb", "metawiki");
  53. $wikis = fetchQuery($connection, "SELECT dbname, slice FROM meta_p.wiki WHERE url IS NOT NULL");
  54. stop("fetch wikis");
  55.  
  56. # connect once to each slice
  57. start("connect to each slice");
  58. $sliceCache = array();
  59. foreach($wikis as $wiki) {
  60.     $slice = $wiki["slice"];
  61.     $dbname = $wiki["dbname"];
  62.     if(!isset($sliceCache[$slice])) {
  63.         echo "Connected slice: $slice\n";
  64.         $sliceCache[$slice] = connect($slice, $dbname);
  65.     }
  66. }
  67. stop("connect to each slice");
  68.  
  69.  
  70. # connect to each wiki
  71. echo "Switching to each of the " . count($wikis) . " wikis...";
  72. start("switch to each wiki DB");
  73. foreach($wikis as $wiki) {
  74.     $slice = $wiki["slice"];
  75.     $dbName = $wiki["dbname"];
  76.     executeQuery($sliceCache[$slice], "use `{$dbName}`");
  77. }
  78. stop("switch to each wiki DB");
  79.  
  80.  
  81. echo "done!";
  82. $backend->footer();
  83. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement