├── Documentation └── fusion-tracing-trace-viewer.png ├── Configuration └── Settings.yaml ├── composer.json ├── README.md ├── LICENSE.md ├── Classes ├── Http │ └── Middleware │ │ └── CheckEnabledMiddleware.php └── Aspect │ └── RuntimeTracing.php └── Tests └── Unit └── Aspect └── RuntimeTracingTest.php /Documentation/fusion-tracing-trace-viewer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Flowpack/Flowpack.Fusion.Tracing/HEAD/Documentation/fusion-tracing-trace-viewer.png -------------------------------------------------------------------------------- /Configuration/Settings.yaml: -------------------------------------------------------------------------------- 1 | Neos: 2 | Flow: 3 | http: 4 | middlewares: 5 | flowpackFusionTracingCheckEnabled: 6 | position: 'start 999' 7 | middleware: 'Flowpack\Fusion\Tracing\Http\Middleware\CheckEnabledMiddleware' 8 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "description": "Profile Fusion rendering by creating traces for Trace-Viewer", 3 | "license": "MIT", 4 | "type": "neos-package", 5 | "name": "flowpack/fusion-tracing", 6 | "require": { 7 | "php": "^7.4 || ^8.0", 8 | "neos/fusion": "^7.3 || ^8.0" 9 | }, 10 | "autoload": { 11 | "psr-4": { 12 | "Flowpack\\Fusion\\Tracing\\": "Classes/" 13 | } 14 | }, 15 | "extra": { 16 | "neos": { 17 | "package-key": "Flowpack.Fusion.Tracing" 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Flowpack.Fusion.Tracing 2 | 3 | **Profile Fusion rendering by creating traces for Trace-Viewer** 4 | 5 | ## Installation 6 | 7 | ``` 8 | composer require flowpack/fusion-tracing 9 | ``` 10 | 11 | ## Usage 12 | 13 | * perform requests to Neos with `X-Fusion-Tracing: my-trace-name` header 14 | * or call `Flowpack\Fusion\Tracing\Aspect\RuntimeTracing::enable` explicitly as needed 15 | 16 | Traces are written to `FLOW_PATH_DATA/Logs/Traces` in a [Tracer-Viewer](https://github.com/catapult-project/catapult/tree/master/tracing) compatible format. 17 | Open `about:tracing` in Chrome/Chromium and load the generated trace. 18 | 19 | ![Screenshot](./Documentation/fusion-tracing-trace-viewer.png) 20 | 21 | ## Interpreting the trace 22 | 23 | All evaluations in Fusion are traced with the time of beginning and end. 24 | The so called flamegraph visualizes the recursive evaluation of a Fusion rendering. 25 | 26 | ## License 27 | 28 | MIT 29 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2019 Christopher Hlubek 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /Classes/Http/Middleware/CheckEnabledMiddleware.php: -------------------------------------------------------------------------------- 1 | getHeaderLine('X-Fusion-Tracing'); 30 | if (!empty($traceNameHeader)) { 31 | $this->runtimeTracing->enable($traceNameHeader); 32 | } 33 | return $handler->handle($request); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Tests/Unit/Aspect/RuntimeTracingTest.php: -------------------------------------------------------------------------------- 1 | getStackFrame('root'); 18 | $sfs[] = $aspect->getStackFrame('root'); 19 | $sfs[] = $aspect->getStackFrame('root'); 20 | 21 | foreach ($sfs as $sf) { 22 | $this->assertEquals(1, $sf); 23 | } 24 | 25 | $this->assertEquals([ 26 | 1 => ['name' => 'root'], 27 | ], $aspect->getStackFrames()); 28 | } 29 | 30 | /** 31 | * @test 32 | */ 33 | public function getStackFrame_with_multiple_paths() 34 | { 35 | $aspect = new RuntimeTracing(); 36 | 37 | $sfs[] = $aspect->getStackFrame('root'); 38 | $sfs[] = $aspect->getStackFrame('root/default'); 39 | $sfs[] = $aspect->getStackFrame('root/default/body'); 40 | $sfs[] = $aspect->getStackFrame('root/test/__meta/cache/entryIdentifier'); 41 | $sfs[] = $aspect->getStackFrame('root/test2'); 42 | 43 | foreach ($sfs as $sf) { 44 | $this->assertIsInt($sf); 45 | } 46 | 47 | $this->assertEquals([ 48 | 1 => [ 49 | 'name' => 'root' 50 | ], 51 | 2 => [ 52 | 'name' => 'default', 53 | 'parent' => '1' 54 | ], 55 | 3 => [ 56 | 'name' => 'body', 57 | 'parent' => '2' 58 | ], 59 | 4 => [ 60 | 'name' => 'test', 61 | 'parent' => '1' 62 | ], 63 | 5 => [ 64 | 'name' => '__meta', 65 | 'parent' => '4' 66 | ], 67 | 6 => [ 68 | 'name' => 'cache', 69 | 'parent' => '5' 70 | ], 71 | 7 => [ 72 | 'name' => 'entryIdentifier', 73 | 'parent' => '6' 74 | ], 75 | 8 => [ 76 | 'name' => 'test2', 77 | 'parent' => '1' 78 | ], 79 | ], $aspect->getStackFrames()); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /Classes/Aspect/RuntimeTracing.php: -------------------------------------------------------------------------------- 1 | enter())") 57 | */ 58 | public function onEnter(JoinPointInterface $joinPoint) 59 | { 60 | if (!$this->enabled) { 61 | return; 62 | } 63 | 64 | $fusionPath = $joinPoint->getMethodArgument('fusionPath'); 65 | $this->writeStart($fusionPath); 66 | } 67 | 68 | /** 69 | * @Flow\After("method(Neos\Fusion\Core\Cache\RuntimeContentCache->leave())") 70 | */ 71 | public function onLeave(JoinPointInterface $joinPoint) 72 | { 73 | if (!$this->enabled) { 74 | return; 75 | } 76 | 77 | $this->writeEnd(); 78 | } 79 | 80 | public function writeStart( 81 | string $fusionPath, 82 | bool $computeStackFrame = true, 83 | string $cat = 'evaluate', 84 | array $args = [] 85 | ): void { 86 | $name = $fusionPath; 87 | 88 | $lastSlash = strrpos($fusionPath, '/'); 89 | if ($lastSlash !== false) { 90 | $name = substr($fusionPath, $lastSlash + 1); 91 | } 92 | 93 | $sf = null; 94 | if ($computeStackFrame) { 95 | $sf = $this->getStackFrame($fusionPath); 96 | } 97 | 98 | $evt = [ 99 | 'name' => $name, 100 | 'cat' => $cat, 101 | 'ph' => 'B', 102 | 'ts' => $this->ts(), 103 | 'pid' => 1, 104 | 'tid' => 1, 105 | 'sf' => $sf, 106 | 'args' => $args 107 | ]; 108 | 109 | $this->appendEvent($evt); 110 | } 111 | 112 | public function writeEnd(array $args = []): void 113 | { 114 | $evt = [ 115 | 'ph' => 'E', 116 | 'ts' => $this->ts(), 117 | 'pid' => 1, 118 | 'tid' => 1, 119 | 'args' => $args 120 | ]; 121 | $this->appendEvent($evt); 122 | } 123 | 124 | private function ts(): int 125 | { 126 | if ($this->baseTs === null) { 127 | $this->baseTs = microtime(true) * 1000 * 1000; 128 | return 0; 129 | } 130 | 131 | return (microtime(true) * 1000 * 1000) - $this->baseTs; 132 | } 133 | 134 | public function appendEvent(array $evt): void 135 | { 136 | if ($this->tracefile === null) { 137 | $filename = FLOW_PATH_DATA . 'Logs/Traces/' . $this->traceName . '-' . time() . '.trace'; 138 | 139 | $this->tracefile = fopen($filename, 'w'); 140 | fwrite($this->tracefile, "{\"traceEvents\":[\n"); 141 | } 142 | 143 | fwrite($this->tracefile, ($this->evtCnt > 0 ? ',' : '') . json_encode($evt) . "\n"); 144 | $this->evtCnt++; 145 | } 146 | 147 | public function shutdownObject() 148 | { 149 | if ($this->tracefile != null) { 150 | fwrite($this->tracefile, 151 | "],\n\"stackFrames\":" . json_encode($this->stackFrames, JSON_PRETTY_PRINT) . "}\n"); 152 | } 153 | } 154 | 155 | public function getStackFrame(string $fusionPath): int 156 | { 157 | $pathParts = explode('/', $fusionPath); 158 | 159 | // Find SF of longest existing prefix 160 | 161 | $baseSf = null; 162 | $n = count($pathParts); 163 | for ($i = $n; $i > 0; $i--) { 164 | $pathPrefix = implode('/', array_slice($pathParts, 0, $i)); 165 | if (isset($this->stackFramesByPath[$pathPrefix])) { 166 | $baseSf = $this->stackFramesByPath[$pathPrefix]; 167 | break; 168 | } 169 | } 170 | 171 | // Build child SFs 172 | 173 | for ($j = $i; $j < $n; $j++) { 174 | $pathPrefix = implode('/', array_slice($pathParts, 0, $j + 1)); 175 | $frame = [ 176 | 'name' => $pathParts[$j] 177 | ]; 178 | if ($baseSf !== null) { 179 | $frame['parent'] = (string)$baseSf; 180 | } 181 | $baseSf = $this->sfCnt++; 182 | $this->stackFrames[(string)$baseSf] = $frame; 183 | $this->stackFramesByPath[$pathPrefix] = $baseSf; 184 | } 185 | 186 | return $baseSf; 187 | } 188 | 189 | public function getStackFrames(): array 190 | { 191 | return $this->stackFrames; 192 | } 193 | 194 | public function enable(string $traceName): void 195 | { 196 | $this->enabled = true; 197 | $this->traceName = $traceName; 198 | 199 | $this->createTracesDirectory(); 200 | } 201 | 202 | private function createTracesDirectory() 203 | { 204 | Files::createDirectoryRecursively(FLOW_PATH_DATA . 'Logs/Traces'); 205 | } 206 | 207 | } 208 | --------------------------------------------------------------------------------