From ccb9c493d51148b9c3627a62616e99e5530ca2e9 Mon Sep 17 00:00:00 2001 From: Shish Date: Fri, 5 Jul 2019 20:49:47 +0100 Subject: [PATCH] s/Context/EventTracer/g --- composer.json | 2 +- composer.lock | 35 +++++++++++++++++++---------------- core/_bootstrap.php | 23 +++++++++++------------ core/cacheengine.php | 3 +++ core/database.php | 6 +++++- core/imageboard/image.php | 8 ++++++-- core/send_event.php | 20 ++++++-------------- core/sys_config.php | 2 +- core/util.php | 9 ++------- index.php | 13 +++++++++---- 10 files changed, 63 insertions(+), 58 deletions(-) diff --git a/composer.json b/composer.json index 219f4b3b..746582e0 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,7 @@ "ifixit/php-akismet" : "1.*", "google/recaptcha" : "~1.1", "dapphp/securimage" : "3.6.*", - "shish/libcontext-php" : "dev-master", + "shish/eventtracer-php" : "dev-master", "enshrined/svg-sanitize" : "0.8.2", "bower-asset/jquery" : "1.12.3", diff --git a/composer.lock b/composer.lock index 5fead0de..faa3dd04 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "b679340e0fc96ae78f8919db742d17da", + "content-hash": "b1d9675735299d17a988bc9dfc8e5973", "packages": [ { "name": "bower-asset/jquery", @@ -297,26 +297,33 @@ "type": "library" }, { - "name": "shish/libcontext-php", + "name": "shish/eventtracer-php", "version": "dev-master", "source": { "type": "git", - "url": "https://github.com/shish/libcontext-php.git", - "reference": "f57c377e0a5e700fb4d9406e47051a3b7478170e" + "url": "https://github.com/shish/eventtracer-php.git", + "reference": "8fc0001dffa6cc68ee56e8681a2e27579eb70dca" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/shish/libcontext-php/zipball/f57c377e0a5e700fb4d9406e47051a3b7478170e", - "reference": "f57c377e0a5e700fb4d9406e47051a3b7478170e", + "url": "https://api.github.com/repos/shish/eventtracer-php/zipball/8fc0001dffa6cc68ee56e8681a2e27579eb70dca", + "reference": "8fc0001dffa6cc68ee56e8681a2e27579eb70dca", "shasum": "" }, "require": { - "php": ">=7.0" + "ext-json": "*", + "ext-posix": "*", + "php": "^7.1" }, "require-dev": { - "phpunit/phpunit": "6.*" + "phpunit/phpunit": "^7" }, "type": "library", + "autoload": { + "classmap": [ + "src/" + ] + }, "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" @@ -329,13 +336,9 @@ "role": "Developer" } ], - "description": "A performance monitoring thing", - "homepage": "https://github.com/shish/libcontext-php", - "keywords": [ - "performance", - "profiler" - ], - "time": "2017-09-21T13:25:55+00:00" + "description": "An API to write JSON traces as used by the Chrome Trace Viewer", + "homepage": "https://github.com/shish/eventtracer-php", + "time": "2019-07-05T14:44:25+00:00" } ], "packages-dev": [ @@ -1815,7 +1818,7 @@ "aliases": [], "minimum-stability": "dev", "stability-flags": { - "shish/libcontext-php": 20, + "shish/eventtracer-php": 20, "bower-asset/tablesorter": 20 }, "prefer-stable": false, diff --git a/core/_bootstrap.php b/core/_bootstrap.php index 1d146f1f..6dffbf14 100644 --- a/core/_bootstrap.php +++ b/core/_bootstrap.php @@ -4,12 +4,11 @@ * 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/polyfills.php"; require_once "core/util.php"; -require_once "vendor/shish/libcontext-php/context.php"; require_once "vendor/autoload.php"; // set up and purify the environment @@ -17,8 +16,8 @@ _version_check(); _sanitise_environment(); // load base files -$_shm_ctx->log_start("Bootstrap"); -$_shm_ctx->log_start("Opening files"); +$_tracer->begin("Bootstrap"); +$_tracer->begin("Opening files"); $_shm_files = array_merge( zglob("core/*.php"), zglob("core/{".ENABLED_MODS."}/*.php"), @@ -31,27 +30,27 @@ foreach ($_shm_files as $_shm_filename) { } unset($_shm_files); unset($_shm_filename); -$_shm_ctx->log_endok(); +$_tracer->end(); // connect to the database -$_shm_ctx->log_start("Connecting to DB"); +$_tracer->begin("Connecting to DB"); $database = new Database(); $config = new DatabaseConfig($database); -$_shm_ctx->log_endok(); +$_tracer->end(); // load the theme parts -$_shm_ctx->log_start("Loading themelets"); +$_tracer->begin("Loading themelets"); foreach (_get_themelet_files(get_theme()) as $themelet) { require_once $themelet; } unset($themelet); $page = class_exists("CustomPage") ? new CustomPage() : new Page(); -$_shm_ctx->log_endok(); +$_tracer->end(); // hook up event handlers -$_shm_ctx->log_start("Loading extensions"); +$_tracer->begin("Loading extensions"); _load_event_listeners(); -$_shm_ctx->log_endok(); +$_tracer->end(); send_event(new InitExtEvent()); -$_shm_ctx->log_endok(); +$_tracer->end(); diff --git a/core/cacheengine.php b/core/cacheengine.php index 61fd84ab..aa32a4dc 100644 --- a/core/cacheengine.php +++ b/core/cacheengine.php @@ -188,11 +188,14 @@ class Cache public function get(string $key) { + global $_tracer; + $_tracer->begin("Cache Query", null, ["key"=>$key]); $val = $this->engine->get($key); if ((DEBUG_CACHE === true) || (is_null(DEBUG_CACHE) && @$_GET['DEBUG_CACHE'])) { $hit = $val === false ? "hit" : "miss"; file_put_contents("data/cache.log", "Cache $hit: $key\n", FILE_APPEND); } + $_tracer->end(); if ($val !== false) { $this->hits++; return $val; diff --git a/core/database.php b/core/database.php index 21fc034e..2b17b751 100644 --- a/core/database.php +++ b/core/database.php @@ -221,6 +221,8 @@ class Database public function execute(string $query, array $args=[]): PDOStatement { + global $_tracer; + $_tracer->begin("DB Query", null, ["query"=>$query]); try { if (is_null($this->db)) { $this->connect_db(); @@ -246,7 +248,9 @@ class Database return $stmt; } catch (PDOException $pdoe) { throw new SCoreException($pdoe->getMessage()."

Query: ".$query); - } + } finally { + $_tracer->end(); + } } /** diff --git a/core/imageboard/image.php b/core/imageboard/image.php index 2f224c8b..67edec76 100644 --- a/core/imageboard/image.php +++ b/core/imageboard/image.php @@ -262,17 +262,21 @@ class Image public static function query_accelerator($req) { + global $_tracer; $fp = @fsockopen("127.0.0.1", 21212); if (!$fp) { 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 = ""; while (($buffer = fgets($fp, 4096)) !== false) { $data .= $buffer; } + $_tracer->end(); 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); return json_decode($data); diff --git a/core/send_event.php b/core/send_event.php index 9cdf3b8c..434068aa 100644 --- a/core/send_event.php +++ b/core/send_event.php @@ -101,7 +101,7 @@ $_shm_event_count = 0; */ 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)])) { return; } @@ -109,31 +109,23 @@ function send_event(Event $event): void // send_event() is performance sensitive, and with the number // of times context gets called the time starts to add up - $ctx_enabled = constant('CONTEXT'); + $tracer_enabled = constant('EVENT_TRACE'); - if ($ctx_enabled) { - $_shm_ctx->log_start(get_class($event)); - } + if ($tracer_enabled) $_tracer->begin(get_class($event)); // SHIT: http://bugs.php.net/bug.php?id=35106 $my_event_listeners = $_shm_event_listeners[get_class($event)]; ksort($my_event_listeners); foreach ($my_event_listeners as $listener) { - if ($ctx_enabled) { - $_shm_ctx->log_start(get_class($listener)); - } + if ($tracer_enabled) $_tracer->begin(get_class($listener)); if (method_exists($listener, $method_name)) { $listener->$method_name($event); } - if ($ctx_enabled) { - $_shm_ctx->log_endok(); - } + if ($tracer_enabled) $_tracer->end(); if($event->stop_processing===true) { break; } } $_shm_event_count++; - if ($ctx_enabled) { - $_shm_ctx->log_endok(); - } + if ($tracer_enabled) $_tracer->end(); } diff --git a/core/sys_config.php b/core/sys_config.php index a4d54fb6..e2ce789b 100644 --- a/core/sys_config.php +++ b/core/sys_config.php @@ -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_CACHE", false); // boolean dump cache queries to data/cache.log _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("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 diff --git a/core/util.php b/core/util.php index ea74a7db..ee7b39fc 100644 --- a/core/util.php +++ b/core/util.php @@ -1,6 +1,4 @@ set_log(CONTEXT); - } + $_tracer = new EventTracer(); if (COVERAGE) { _start_coverage(); diff --git a/index.php b/index.php index 00a51305..efad8c65 100644 --- a/index.php +++ b/index.php @@ -83,9 +83,11 @@ EOD; exit; } +require_once "core/_bootstrap.php"; +//$_tracer->mark(@$_SERVER["REQUEST_URI"]); +$_tracer->begin($_SERVER["REQUEST_URI"] ?? "No Request"); + try { - require_once "core/_bootstrap.php"; - $_shm_ctx->log_start(@$_SERVER["REQUEST_URI"], true, true); // start the page generation waterfall $user = _get_user(); @@ -104,12 +106,15 @@ try { if (function_exists("fastcgi_finish_request")) { fastcgi_finish_request(); } - $_shm_ctx->log_endok(); } catch (Exception $e) { if ($database && $database->transaction===true) { $database->rollback(); } _fatal_error($e); - $_shm_ctx->log_ender(); } + log_slow(); +$_tracer->end(); +if (EVENT_TRACE) { + $_tracer->flush(EVENT_TRACE); +}