Skip to content

Commit 1f77699

Browse files
authored
Merge pull request #74 from glensc/importer-cli
2 parents 51e65ae + 6844863 commit 1f77699

4 files changed

Lines changed: 106 additions & 1 deletion

File tree

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ In order to profile your application, you need to:
2626
- [Install profiler extension](#installing-profilers)
2727
- [Instantiate the profiler](#create-profiler)
2828
- [Configure saver to send data to XHGui](#savers)
29+
- [Import jsonl files](#import-jsonl-files) (optional)
2930

3031
## Installation
3132

@@ -202,7 +203,7 @@ Example config:
202203
),
203204
```
204205

205-
To import a saved files, use XHGui's provided `external/import.php` script.
206+
To import a saved files, see [Import jsonl files](#import-jsonl-files) section.
206207

207208
### MongoDB Saver
208209

@@ -292,6 +293,23 @@ class StdOutSaver implements SaverInterface
292293
$profiler->setSaver(new StdOutSaver());
293294
```
294295

296+
### Import jsonl files
297+
298+
You can use `./bin/import.php` script to submit files saved by [File Saver](#file-saver) to XHGui server.
299+
300+
1. [Setup config file](#using-config-file)
301+
1. Configure to use [Upload Saver](#upload-saver)
302+
1. Execute the `./bin/import.php` script
303+
304+
The script can take multiple [jsonl] formatted files, or if none given read stdin stream.
305+
306+
```sh
307+
$ ./bin/import.php tests/tmp/php-profiler-xhgui-test-1596093567.787220-c857.json
308+
Imported 1 lines
309+
```
310+
311+
[jsonl]: https://jsonlines.org/
312+
295313
## Configure Profiling Rate
296314

297315
You may want to change how frequently you profile the application. The

bin/import.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
use Xhgui\Profiler\ImporterFactory;
5+
6+
require __DIR__ . '/../vendor/autoload.php';
7+
8+
$importer = ImporterFactory::create();
9+
10+
if ($argc > 1) {
11+
foreach (array_splice($argv, 1) as $file) {
12+
if (!is_readable($file)) {
13+
throw new RuntimeException("{$file} isn't readable");
14+
}
15+
16+
$fp = fopen($file, 'r');
17+
if (!$fp) {
18+
throw new RuntimeException("Can't open {$file}");
19+
}
20+
$importer->import($fp);
21+
fclose($fp);
22+
}
23+
} else {
24+
$importer->import(STDIN);
25+
}
26+

src/Importer.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Xhgui\Profiler;
4+
5+
use Exception;
6+
use Xhgui\Profiler\Saver\SaverInterface;
7+
8+
final class Importer
9+
{
10+
/** @var SaverInterface */
11+
private $saver;
12+
13+
public function __construct(SaverInterface $saver)
14+
{
15+
$this->saver = $saver;
16+
}
17+
18+
public function import($stream)
19+
{
20+
$saver = $this->saver;
21+
$lines = 0;
22+
while (!feof($stream)) {
23+
$line = trim(fgets($stream));
24+
if (!$line) {
25+
continue;
26+
}
27+
$data = json_decode($line, true);
28+
if (!$data) {
29+
error_log("Ignoring malformed JSON line: $line");
30+
continue;
31+
}
32+
33+
try {
34+
$saver->save($data);
35+
$lines++;
36+
} catch (Exception $e) {
37+
error_log($e);
38+
}
39+
}
40+
error_log("Imported {$lines} lines");
41+
}
42+
}

src/ImporterFactory.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Xhgui\Profiler;
4+
5+
use RuntimeException;
6+
7+
final class ImporterFactory
8+
{
9+
public static function create()
10+
{
11+
$config = Config::create();
12+
$saver = SaverFactory::create($config['save.handler'], $config);
13+
if (!$saver) {
14+
throw new RuntimeException("Unable to obtain saver");
15+
}
16+
17+
return new Importer($saver);
18+
}
19+
}

0 commit comments

Comments
 (0)