s/Context/EventTracer/g

This commit is contained in:
Shish 2019-07-05 20:49:47 +01:00
parent f0326dc3ab
commit ccb9c493d5
10 changed files with 63 additions and 58 deletions

View file

@ -31,7 +31,7 @@
"ifixit/php-akismet" : "1.*", "ifixit/php-akismet" : "1.*",
"google/recaptcha" : "~1.1", "google/recaptcha" : "~1.1",
"dapphp/securimage" : "3.6.*", "dapphp/securimage" : "3.6.*",
"shish/libcontext-php" : "dev-master", "shish/eventtracer-php" : "dev-master",
"enshrined/svg-sanitize" : "0.8.2", "enshrined/svg-sanitize" : "0.8.2",
"bower-asset/jquery" : "1.12.3", "bower-asset/jquery" : "1.12.3",

35
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "b679340e0fc96ae78f8919db742d17da", "content-hash": "b1d9675735299d17a988bc9dfc8e5973",
"packages": [ "packages": [
{ {
"name": "bower-asset/jquery", "name": "bower-asset/jquery",
@ -297,26 +297,33 @@
"type": "library" "type": "library"
}, },
{ {
"name": "shish/libcontext-php", "name": "shish/eventtracer-php",
"version": "dev-master", "version": "dev-master",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/shish/libcontext-php.git", "url": "https://github.com/shish/eventtracer-php.git",
"reference": "f57c377e0a5e700fb4d9406e47051a3b7478170e" "reference": "8fc0001dffa6cc68ee56e8681a2e27579eb70dca"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/shish/libcontext-php/zipball/f57c377e0a5e700fb4d9406e47051a3b7478170e", "url": "https://api.github.com/repos/shish/eventtracer-php/zipball/8fc0001dffa6cc68ee56e8681a2e27579eb70dca",
"reference": "f57c377e0a5e700fb4d9406e47051a3b7478170e", "reference": "8fc0001dffa6cc68ee56e8681a2e27579eb70dca",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
"php": ">=7.0" "ext-json": "*",
"ext-posix": "*",
"php": "^7.1"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "6.*" "phpunit/phpunit": "^7"
}, },
"type": "library", "type": "library",
"autoload": {
"classmap": [
"src/"
]
},
"notification-url": "https://packagist.org/downloads/", "notification-url": "https://packagist.org/downloads/",
"license": [ "license": [
"MIT" "MIT"
@ -329,13 +336,9 @@
"role": "Developer" "role": "Developer"
} }
], ],
"description": "A performance monitoring thing", "description": "An API to write JSON traces as used by the Chrome Trace Viewer",
"homepage": "https://github.com/shish/libcontext-php", "homepage": "https://github.com/shish/eventtracer-php",
"keywords": [ "time": "2019-07-05T14:44:25+00:00"
"performance",
"profiler"
],
"time": "2017-09-21T13:25:55+00:00"
} }
], ],
"packages-dev": [ "packages-dev": [
@ -1815,7 +1818,7 @@
"aliases": [], "aliases": [],
"minimum-stability": "dev", "minimum-stability": "dev",
"stability-flags": { "stability-flags": {
"shish/libcontext-php": 20, "shish/eventtracer-php": 20,
"bower-asset/tablesorter": 20 "bower-asset/tablesorter": 20
}, },
"prefer-stable": false, "prefer-stable": false,

View file

@ -4,12 +4,11 @@
* actually do anything as far as the app is concerned * actually do anything as far as the app is concerned
*/ */
global $config, $database, $user, $page, $_shm_ctx; global $config, $database, $user, $page, $_tracer;
require_once "core/sys_config.php"; require_once "core/sys_config.php";
require_once "core/polyfills.php"; require_once "core/polyfills.php";
require_once "core/util.php"; require_once "core/util.php";
require_once "vendor/shish/libcontext-php/context.php";
require_once "vendor/autoload.php"; require_once "vendor/autoload.php";
// set up and purify the environment // set up and purify the environment
@ -17,8 +16,8 @@ _version_check();
_sanitise_environment(); _sanitise_environment();
// load base files // load base files
$_shm_ctx->log_start("Bootstrap"); $_tracer->begin("Bootstrap");
$_shm_ctx->log_start("Opening files"); $_tracer->begin("Opening files");
$_shm_files = array_merge( $_shm_files = array_merge(
zglob("core/*.php"), zglob("core/*.php"),
zglob("core/{".ENABLED_MODS."}/*.php"), zglob("core/{".ENABLED_MODS."}/*.php"),
@ -31,27 +30,27 @@ foreach ($_shm_files as $_shm_filename) {
} }
unset($_shm_files); unset($_shm_files);
unset($_shm_filename); unset($_shm_filename);
$_shm_ctx->log_endok(); $_tracer->end();
// connect to the database // connect to the database
$_shm_ctx->log_start("Connecting to DB"); $_tracer->begin("Connecting to DB");
$database = new Database(); $database = new Database();
$config = new DatabaseConfig($database); $config = new DatabaseConfig($database);
$_shm_ctx->log_endok(); $_tracer->end();
// load the theme parts // load the theme parts
$_shm_ctx->log_start("Loading themelets"); $_tracer->begin("Loading themelets");
foreach (_get_themelet_files(get_theme()) as $themelet) { foreach (_get_themelet_files(get_theme()) as $themelet) {
require_once $themelet; require_once $themelet;
} }
unset($themelet); unset($themelet);
$page = class_exists("CustomPage") ? new CustomPage() : new Page(); $page = class_exists("CustomPage") ? new CustomPage() : new Page();
$_shm_ctx->log_endok(); $_tracer->end();
// hook up event handlers // hook up event handlers
$_shm_ctx->log_start("Loading extensions"); $_tracer->begin("Loading extensions");
_load_event_listeners(); _load_event_listeners();
$_shm_ctx->log_endok(); $_tracer->end();
send_event(new InitExtEvent()); send_event(new InitExtEvent());
$_shm_ctx->log_endok(); $_tracer->end();

View file

@ -188,11 +188,14 @@ class Cache
public function get(string $key) public function get(string $key)
{ {
global $_tracer;
$_tracer->begin("Cache Query", null, ["key"=>$key]);
$val = $this->engine->get($key); $val = $this->engine->get($key);
if ((DEBUG_CACHE === true) || (is_null(DEBUG_CACHE) && @$_GET['DEBUG_CACHE'])) { if ((DEBUG_CACHE === true) || (is_null(DEBUG_CACHE) && @$_GET['DEBUG_CACHE'])) {
$hit = $val === false ? "hit" : "miss"; $hit = $val === false ? "hit" : "miss";
file_put_contents("data/cache.log", "Cache $hit: $key\n", FILE_APPEND); file_put_contents("data/cache.log", "Cache $hit: $key\n", FILE_APPEND);
} }
$_tracer->end();
if ($val !== false) { if ($val !== false) {
$this->hits++; $this->hits++;
return $val; return $val;

View file

@ -221,6 +221,8 @@ class Database
public function execute(string $query, array $args=[]): PDOStatement public function execute(string $query, array $args=[]): PDOStatement
{ {
global $_tracer;
$_tracer->begin("DB Query", null, ["query"=>$query]);
try { try {
if (is_null($this->db)) { if (is_null($this->db)) {
$this->connect_db(); $this->connect_db();
@ -246,7 +248,9 @@ class Database
return $stmt; return $stmt;
} catch (PDOException $pdoe) { } catch (PDOException $pdoe) {
throw new SCoreException($pdoe->getMessage()."<p><b>Query:</b> ".$query); throw new SCoreException($pdoe->getMessage()."<p><b>Query:</b> ".$query);
} } finally {
$_tracer->end();
}
} }
/** /**

View file

@ -262,17 +262,21 @@ class Image
public static function query_accelerator($req) public static function query_accelerator($req)
{ {
global $_tracer;
$fp = @fsockopen("127.0.0.1", 21212); $fp = @fsockopen("127.0.0.1", 21212);
if (!$fp) { if (!$fp) {
return null; return null;
} }
fwrite($fp, json_encode($req)); $_tracer->begin("Query Accelerator", null, ["req"=>$req_str]);
$req_str = json_encode($req);
fwrite($fp, $req_str);
$data = ""; $data = "";
while (($buffer = fgets($fp, 4096)) !== false) { while (($buffer = fgets($fp, 4096)) !== false) {
$data .= $buffer; $data .= $buffer;
} }
$_tracer->end();
if (!feof($fp)) { if (!feof($fp)) {
die("Error: unexpected fgets() fail in query_accelerator($req)\n"); die("Error: unexpected fgets() fail in query_accelerator($req_str)\n");
} }
fclose($fp); fclose($fp);
return json_decode($data); return json_decode($data);

View file

@ -101,7 +101,7 @@ $_shm_event_count = 0;
*/ */
function send_event(Event $event): void function send_event(Event $event): void
{ {
global $_shm_event_listeners, $_shm_event_count, $_shm_ctx; global $_shm_event_listeners, $_shm_event_count, $_tracer;
if (!isset($_shm_event_listeners[get_class($event)])) { if (!isset($_shm_event_listeners[get_class($event)])) {
return; return;
} }
@ -109,31 +109,23 @@ function send_event(Event $event): void
// send_event() is performance sensitive, and with the number // send_event() is performance sensitive, and with the number
// of times context gets called the time starts to add up // of times context gets called the time starts to add up
$ctx_enabled = constant('CONTEXT'); $tracer_enabled = constant('EVENT_TRACE');
if ($ctx_enabled) { if ($tracer_enabled) $_tracer->begin(get_class($event));
$_shm_ctx->log_start(get_class($event));
}
// SHIT: http://bugs.php.net/bug.php?id=35106 // SHIT: http://bugs.php.net/bug.php?id=35106
$my_event_listeners = $_shm_event_listeners[get_class($event)]; $my_event_listeners = $_shm_event_listeners[get_class($event)];
ksort($my_event_listeners); ksort($my_event_listeners);
foreach ($my_event_listeners as $listener) { foreach ($my_event_listeners as $listener) {
if ($ctx_enabled) { if ($tracer_enabled) $_tracer->begin(get_class($listener));
$_shm_ctx->log_start(get_class($listener));
}
if (method_exists($listener, $method_name)) { if (method_exists($listener, $method_name)) {
$listener->$method_name($event); $listener->$method_name($event);
} }
if ($ctx_enabled) { if ($tracer_enabled) $_tracer->end();
$_shm_ctx->log_endok();
}
if($event->stop_processing===true) { if($event->stop_processing===true) {
break; break;
} }
} }
$_shm_event_count++; $_shm_event_count++;
if ($ctx_enabled) { if ($tracer_enabled) $_tracer->end();
$_shm_ctx->log_endok();
}
} }

View file

@ -33,7 +33,7 @@ _d("DEBUG", false); // boolean print various debugging details
_d("DEBUG_SQL", false); // boolean dump SQL queries to data/sql.log _d("DEBUG_SQL", false); // boolean dump SQL queries to data/sql.log
_d("DEBUG_CACHE", false); // boolean dump cache queries to data/cache.log _d("DEBUG_CACHE", false); // boolean dump cache queries to data/cache.log
_d("COVERAGE", false); // boolean activate xdebug coverage monitor _d("COVERAGE", false); // boolean activate xdebug coverage monitor
_d("CONTEXT", null); // string file to log performance data into _d("EVENT_TRACE", null); // string file to log performance data into
_d("CACHE_HTTP", false); // boolean output explicit HTTP caching headers _d("CACHE_HTTP", false); // boolean output explicit HTTP caching headers
_d("COOKIE_PREFIX", 'shm'); // string if you run multiple galleries with non-shared logins, give them different prefixes _d("COOKIE_PREFIX", 'shm'); // string if you run multiple galleries with non-shared logins, give them different prefixes
_d("SPEED_HAX", false); // boolean do some questionable things in the name of performance _d("SPEED_HAX", false); // boolean do some questionable things in the name of performance

View file

@ -1,6 +1,4 @@
<?php <?php
require_once "vendor/shish/libcontext-php/context.php";
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
* Misc * * Misc *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
@ -460,7 +458,7 @@ date and you should plan on moving elsewhere.
function _sanitise_environment(): void function _sanitise_environment(): void
{ {
global $_shm_ctx; global $_tracer;
if (TIMEZONE) { if (TIMEZONE) {
date_default_timezone_set(TIMEZONE); date_default_timezone_set(TIMEZONE);
@ -472,10 +470,7 @@ function _sanitise_environment(): void
error_reporting(E_ALL); error_reporting(E_ALL);
} }
$_shm_ctx = new Context(); $_tracer = new EventTracer();
if (CONTEXT) {
$_shm_ctx->set_log(CONTEXT);
}
if (COVERAGE) { if (COVERAGE) {
_start_coverage(); _start_coverage();

View file

@ -83,9 +83,11 @@ EOD;
exit; exit;
} }
require_once "core/_bootstrap.php";
//$_tracer->mark(@$_SERVER["REQUEST_URI"]);
$_tracer->begin($_SERVER["REQUEST_URI"] ?? "No Request");
try { try {
require_once "core/_bootstrap.php";
$_shm_ctx->log_start(@$_SERVER["REQUEST_URI"], true, true);
// start the page generation waterfall // start the page generation waterfall
$user = _get_user(); $user = _get_user();
@ -104,12 +106,15 @@ try {
if (function_exists("fastcgi_finish_request")) { if (function_exists("fastcgi_finish_request")) {
fastcgi_finish_request(); fastcgi_finish_request();
} }
$_shm_ctx->log_endok();
} catch (Exception $e) { } catch (Exception $e) {
if ($database && $database->transaction===true) { if ($database && $database->transaction===true) {
$database->rollback(); $database->rollback();
} }
_fatal_error($e); _fatal_error($e);
$_shm_ctx->log_ender();
} }
log_slow(); log_slow();
$_tracer->end();
if (EVENT_TRACE) {
$_tracer->flush(EVENT_TRACE);
}