Permalink
Browse files

Add MongoProfiler

  • Loading branch information...
1 parent 45d8886 commit 73eee78ea7633d29ba1c767d39074c1526978ef4 @spib spib committed Nov 22, 2012
Showing with 90 additions and 1 deletion.
  1. +11 −0 mongodb.php
  2. +72 −0 mongoprofiler.php
  3. +7 −1 start.php
View
11 mongodb.php 100644 → 100755
@@ -323,6 +323,8 @@ public function remove_file( array $criteria = array(), array $options = array()
/* Run Command */
protected function _call($command, array $arguments = array(), array $values = NULL)
{
+ $start = microtime(true);
+
$this->_connected OR $this->connect();
extract($arguments);
@@ -394,7 +396,16 @@ protected function _call($command, array $arguments = array(), array $values = N
break;
}
+ $this->log($command, $start, $arguments);
+
return $r;
}
+
+ protected function log($command, $start, $arguments) {
+
+ $time = number_format((microtime(true) - $start) * 1000, 2);
+
+ \Laravel\Event::fire('laravel.mongoquery', array($this->_db, $command, $arguments, $time));
+ }
}
?>
View
@@ -0,0 +1,72 @@
+<?php namespace Mongor;
+
+use Laravel\View;
+use Laravel\File;
+use Laravel\Event;
+use Laravel\Config;
+use Laravel\Request;
+use Laravel\Database;
+
+class MongoProfiler extends \Laravel\Profiling\Profiler
+{
+
+ /**
+ * Add a performed MongoDB query to the Profiler.
+ *
+ * @param string $sql
+ * @param array $bindings
+ * @param float $time
+ *
+ * @return void
+ */
+ public static function mongo_query($db, $sql, $bindings, $time)
+ {
+ $sql = 'Command:' . $sql .', params [';
+
+ $sql = $sql . implode(', ', MongoProfiler::format_parameters($bindings));
+
+ $sql .= '] (DB ' . $db . ')';
+
+ static::$data['queries'][] = array($sql, $time);
+ }
+
+ protected static function format_parameters($bindings)
+ {
+ $statements = array();
+
+ foreach ($bindings as $key => $binding) {
+
+ if(isset($binding) && !empty($binding)) {
+
+ $statement = $key . ':';
+
+ if (is_array($binding)) {
+ $statements = array_merge($statements, MongoProfiler::format_parameters($binding));
+ }
+ else {
+ $statement .= htmlspecialchars($binding);
+ }
+
+ $statements[] = $statement;
+ }
+ }
+
+ return $statements;
+ }
+
+ /**
+ * Attach the Profiler's event listeners.
+ *
+ * @return void
+ */
+ public static function attach()
+ {
+ \Laravel\Profiling\Profiler::attach();
+
+ Event::listen('laravel.mongoquery', function ($db, $sql, $bindings, $time) {
+ MongoProfiler::mongo_query($db, $sql, $bindings, $time);
+ });
+ }
+
+
+}
View
8 start.php 100644 → 100755
@@ -5,8 +5,14 @@
'Mongor\\MongoDB' => path('bundle').'mongor/mongodb.php',
'Mongor\\Hydrator' => path('bundle').'mongor/hydrator.php',
'Mongor\\MongoAuth'=> path('bundle').'mongor/mongoauth.php',
+ 'Mongor\\MongoProfiler'=> path('bundle').'mongor/mongoprofiler.php',
));
Auth::extend('mongo', function() {
return new Mongor\MongoAuth(Config::get('auth.model'));
-});
+});
+
+if (Config::get('application.profiler'))
+{
+ Mongor\MongoProfiler::attach();
+}

0 comments on commit 73eee78

Please sign in to comment.