Skip to content

Commit df7aedd

Browse files
committed
chore: tweaks at ServerTimingDoctrine middleware
1 parent 9a713bd commit df7aedd

2 files changed

Lines changed: 28 additions & 21 deletions

File tree

app/Http/Controllers/Apis/Protected/Summit/Traits/ParametrizedGetAll.php

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
**/
1414

1515
use App\ModelSerializers\SerializerUtils;
16+
use http\Env\Response;
1617
use Illuminate\Support\Facades\Request;
18+
use Illuminate\Support\Facades\Session;
1719
use Illuminate\Support\Facades\Validator;
1820
use libs\utils\PaginationValidationRules;
1921
use models\exceptions\ValidationException;
@@ -119,7 +121,7 @@ public function _getAll
119121
$order = call_user_func($defaultOrderRules);
120122
}
121123
}
122-
124+
$dbStart = microtime(true);
123125
if (!is_null($queryCallable))
124126
$data = call_user_func($queryCallable,
125127
$page,
@@ -136,21 +138,26 @@ public function _getAll
136138
$order,
137139
$applyExtraFilters
138140
);
139-
141+
$dbEnd = (microtime(true)-$dbStart)*1000;
142+
$transformStart = microtime(true);
140143
$serializerParams['filter'] = $filter;
141-
142-
return $this->ok
144+
$res = $data->toArray
143145
(
144-
$data->toArray
145-
(
146-
SerializerUtils::getExpand(),
147-
SerializerUtils::getFields(),
148-
SerializerUtils::getRelations(),
149-
$serializerParams,
150-
$serializerType && is_callable($serializerType) ? call_user_func($serializerType) : SerializerRegistry::SerializerType_Public
151-
)
146+
SerializerUtils::getExpand(),
147+
SerializerUtils::getFields(),
148+
SerializerUtils::getRelations(),
149+
$serializerParams,
150+
$serializerType && is_callable($serializerType) ? call_user_func($serializerType) : SerializerRegistry::SerializerType_Public
152151
);
153-
152+
$transformEnd = (microtime(true)-$transformStart)*1000;
153+
$encodeStart = microtime(true);
154+
$json_response = $this->ok($res);
155+
$encodeEnd = (microtime(true)-$encodeStart)*1000;
156+
Session::put("db_time", $dbEnd );
157+
Session::put("transform_time", $transformEnd );
158+
Session::put("encode_time", $encodeEnd );
159+
Session::save();
160+
return $json_response;
154161
});
155162
}
156163

@@ -272,4 +279,4 @@ public function _getAllCSV
272279

273280
});
274281
}
275-
}
282+
}

app/Http/Middleware/ServerTimingDoctrine.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Closure;
66
use Doctrine\ORM\EntityManagerInterface;
7+
use Illuminate\Support\Facades\Session;
78
use LaravelDoctrine\ORM\Facades\Registry;
89
use models\utils\SilverstripeBaseModel;
910
use Psr\Log\LoggerInterface;
@@ -87,16 +88,15 @@ public function debug($m, array $c = []):void { $this->log('debug', $m, $c); }
8788
$response = $next($request);
8889
}
8990

90-
9191
$totalMs = (microtime(true) - $start) * 1000.0;
9292
$bootMs = defined('LARAVEL_START') ? max(($start - LARAVEL_START) * 1000.0, 0.0) : 0.0;
9393
$appMs = max($totalMs - $dbMs, 0.0);
94-
95-
// Al setear el header:
96-
$response->headers->set(
97-
'Server-Timing',
98-
sprintf('boot;dur=%.1f, db;dur=%.1f, app;dur=%.1f, total;dur=%.1f', $bootMs, $dbMs, $appMs, $totalMs)
99-
);
94+
$dbMs = Session::has("db_time") ? (float) Session::get("db_time") : $dbMs;
95+
$transformMs = Session::has("transform_time") ? (float) Session::get("transform_time") : 0.0;
96+
$encodeMs = Session::has("encode_time") ? (float) Session::get("encode_time") : 0.0;
97+
$response->headers->set('Server-Timing',
98+
sprintf('boot;dur=%.1f,db;dur=%.1f,transform;dur=%.1f,encode;dur=%.1f,app;dur=%.1f,total;dur=%.1f',
99+
$bootMs,$dbMs,$transformMs,$encodeMs,$appMs,$totalMs));
100100
$response->headers->set('Timing-Allow-Origin', '*');
101101

102102
return $response;

0 commit comments

Comments
 (0)