Skip to content

Commit

Permalink
Setup/Metrics: Use thunks for performance
Browse files Browse the repository at this point in the history
  • Loading branch information
klees committed Jul 31, 2024
1 parent bf69c25 commit c4d7e2e
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 91 deletions.
19 changes: 19 additions & 0 deletions components/ILIAS/Setup/src/CLI/StatusCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,25 @@ public function getMetrics(Agent $agent): Metrics\Metric
$this->achieveObjective($objective, $environment);

$metric = $storage->asMetric();

$filter = explode(".", "key.subkey.subsubkey");
$candidates = [$metric];

while(count($filter) > 0) {
if($metric->getType() !== Metric::TYPE_COLLECTION) {
throw new \RuntimeException("Cannot find key...");
}

$current = array_shift($filter);
$metrics = $metric->getValue();
if (!array_key_exists($current, $metrics)) {
throw new \RuntimeException("Cannot find key...");
}
$metric = $metrics[$current];
}



list($config, $other) = $metric->extractByStability(Metrics\Metric::STABILITY_CONFIG);
if ($other) {
$values = $other->getValue();
Expand Down
42 changes: 18 additions & 24 deletions components/ILIAS/Setup/src/Metrics/Metric.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,38 +63,24 @@ final class Metric
// A collection of metrics that contains multiple named metrics.
public const TYPE_COLLECTION = "collection";

/**
* @var string one of STABILITY_*
*/
protected string $stability;

/**
* @var string one of TYPE_*
*/
protected string $type;

/**
* @var mixed
*/
protected $value;


protected ?string $description;
protected $value = null;

public function __construct(
string $stability,
string $type,
$value,
string $description = null
protected string $stability,
protected string $type,
protected $value_producer,
protected ?string $description = null
) {
$this->checkStability($stability, $type);
$this->checkType($type);
$this->checkValue($type, $value);

$this->stability = $stability;
$this->type = $type;
$this->value = $value;
$this->description = $description;
if (!is_callable($value_producer)) {
throw new \InvalidArgumentException(
"Expected \$value_producer to be callable."
);
}
}

protected function checkStability(string $stability, string $type): void
Expand Down Expand Up @@ -168,6 +154,14 @@ public function getType(): string
*/
public function getValue()
{
if (!is_null($this->value)) {
return $this->value;
}

$value = $this->value_producer();
$this->checkValue($type, $value);
$this->value = $value;

return $this->value;
}

Expand Down
20 changes: 10 additions & 10 deletions components/ILIAS/Setup/tests/Metrics/ArrayStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public function setUp(): void

public function testBasicStorage(): void
{
$m1 = new M(M::STABILITY_CONFIG, M::TYPE_BOOL, true, "desc1");
$m2 = new M(M::STABILITY_CONFIG, M::TYPE_BOOL, true, "desc2");
$m1 = new M(M::STABILITY_CONFIG, M::TYPE_BOOL, fn() => true, "desc1");
$m2 = new M(M::STABILITY_CONFIG, M::TYPE_BOOL, fn() => true, "desc2");

$this->storage->store("m1", $m1);
$this->storage->store("m2", $m2);
Expand All @@ -51,8 +51,8 @@ public function testBasicStorage(): void

public function testOverwrites(): void
{
$m1 = new M(M::STABILITY_CONFIG, M::TYPE_BOOL, true, "desc1");
$m2 = new M(M::STABILITY_CONFIG, M::TYPE_BOOL, true, "desc2");
$m1 = new M(M::STABILITY_CONFIG, M::TYPE_BOOL, fn() => true, "desc1");
$m2 = new M(M::STABILITY_CONFIG, M::TYPE_BOOL, fn() => true, "desc2");

$this->storage->store("m1", $m1);
$this->storage->store("m1", $m2);
Expand All @@ -66,7 +66,7 @@ public function testOverwrites(): void

public function testNesting(): void
{
$m1 = new M(M::STABILITY_CONFIG, M::TYPE_BOOL, true, "desc1");
$m1 = new M(M::STABILITY_CONFIG, M::TYPE_BOOL, fn() => true, "desc1");

$this->storage->store("a.b.c", $m1);

Expand All @@ -83,16 +83,16 @@ public function testNesting(): void

public function testAsMetric(): void
{
$this->storage->store("a", new M(M::STABILITY_STABLE, M::TYPE_COUNTER, 0));
$this->storage->store("b.c", new M(M::STABILITY_VOLATILE, M::TYPE_BOOL, true));
$this->storage->store("a", new M(M::STABILITY_STABLE, M::TYPE_COUNTER, fn() => 0));
$this->storage->store("b.c", new M(M::STABILITY_VOLATILE, M::TYPE_BOOL, fn() => true));

$expected = new M(
M::STABILITY_MIXED,
M::TYPE_COLLECTION,
[
"a" => new M(M::STABILITY_STABLE, M::TYPE_COUNTER, 0),
"b" => new M(M::STABILITY_MIXED, M::TYPE_COLLECTION, [
"c" => new M(M::STABILITY_VOLATILE, M::TYPE_BOOL, true)
"a" => new M(M::STABILITY_STABLE, M::TYPE_COUNTER, fn() => 0),
"b" => new M(M::STABILITY_MIXED, M::TYPE_COLLECTION, fn() => [
"c" => new M(M::STABILITY_VOLATILE, M::TYPE_BOOL, fn() => true)
])
]
);
Expand Down
114 changes: 57 additions & 57 deletions components/ILIAS/Setup/tests/Metrics/MetricTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function testConstructMetric(string $stability, string $type, $value, str
if (!$success) {
$this->expectException(\InvalidArgumentException::class);
}
$metric = new Metrics\Metric($stability, $type, $value, $description);
$metric = new Metrics\Metric($stability, $type, fn() => $value, $description);
$this->assertEquals($stability, $metric->getStability());
$this->assertEquals($type, $metric->getType());
$this->assertEquals($value, $metric->getValue());
Expand All @@ -60,7 +60,7 @@ public function metricProvider(): array
$text = Metrics\Metric::TYPE_TEXT;
$collection = Metrics\Metric::TYPE_COLLECTION;

$other_metric = new Metrics\Metric($volatile, $bool, true);
$other_metric = new Metrics\Metric($volatile, $bool, fn() => true);

return [
"invalid_stability" => ["no_stability", $bool, true, "", false],
Expand Down Expand Up @@ -126,37 +126,37 @@ public function testToYAML(M $metric, string $expected): void
public function typedMetricsProvider(): array
{
return [
"bool_true" => [new M(M::STABILITY_STABLE, M::TYPE_BOOL, true), "true"],
"bool_false" => [new M(M::STABILITY_STABLE, M::TYPE_BOOL, false), "false"],
"counter_0" => [new M(M::STABILITY_STABLE, M::TYPE_COUNTER, 0), "0"],
"counter_1337" => [new M(M::STABILITY_STABLE, M::TYPE_COUNTER, 1337), "1337"],
"gauge_23" => [new M(M::STABILITY_STABLE, M::TYPE_GAUGE, 23), "23"],
"gauge_42_0" => [new M(M::STABILITY_STABLE, M::TYPE_GAUGE, 42.0), "42.000"],
"gauge_42_001" => [new M(M::STABILITY_STABLE, M::TYPE_GAUGE, 42.001), "42.001"],
"timestamp" => [new M(M::STABILITY_STABLE, M::TYPE_TIMESTAMP, new \DateTimeImmutable("1985-05-04T13:37:00+01:00")), "1985-05-04T13:37:00+0100"],
"text" => [new M(M::STABILITY_STABLE, M::TYPE_TEXT, "some text"), "some text"],
"text_with_nl" => [new M(M::STABILITY_STABLE, M::TYPE_TEXT, "some\ntext"), ">\nsome\ntext"],
"bool_true" => [new M(M::STABILITY_STABLE, M::TYPE_BOOL, fn() => true), "true"],
"bool_false" => [new M(M::STABILITY_STABLE, M::TYPE_BOOL, fn() => false), "false"],
"counter_0" => [new M(M::STABILITY_STABLE, M::TYPE_COUNTER, fn() => 0), "0"],
"counter_1337" => [new M(M::STABILITY_STABLE, M::TYPE_COUNTER, fn() => 1337), "1337"],
"gauge_23" => [new M(M::STABILITY_STABLE, M::TYPE_GAUGE, fn() => 23), "23"],
"gauge_42_0" => [new M(M::STABILITY_STABLE, M::TYPE_GAUGE, fn() => 42.0), "42.000"],
"gauge_42_001" => [new M(M::STABILITY_STABLE, M::TYPE_GAUGE, fn() => 42.001), "42.001"],
"timestamp" => [new M(M::STABILITY_STABLE, M::TYPE_TIMESTAMP, fn() => new \DateTimeImmutable("1985-05-04T13:37:00+01:00")), "1985-05-04T13:37:00+0100"],
"text" => [new M(M::STABILITY_STABLE, M::TYPE_TEXT, fn() => "some text"), "some text"],
"text_with_nl" => [new M(M::STABILITY_STABLE, M::TYPE_TEXT, fn() => "some\ntext"), ">\nsome\ntext"],
];
}

public function testIndentation(): void
{
$metrics = new M(M::STABILITY_STABLE, M::TYPE_COLLECTION, [
"a" => new M(M::STABILITY_STABLE, M::TYPE_COLLECTION, [
"h" => new M(M::STABILITY_STABLE, M::TYPE_TEXT, "a_h"),
"c" => new M(M::STABILITY_STABLE, M::TYPE_COLLECTION, [
"d" => new M(M::STABILITY_STABLE, M::TYPE_COLLECTION, [
"e" => new M(M::STABILITY_STABLE, M::TYPE_TEXT, "a_c_d_e"),
"f" => new M(M::STABILITY_STABLE, M::TYPE_TEXT, "a_c_d_f")
$metrics = new M(M::STABILITY_STABLE, M::TYPE_COLLECTION, fn() => [
"a" => new M(M::STABILITY_STABLE, M::TYPE_COLLECTION, fn() => [
"h" => new M(M::STABILITY_STABLE, M::TYPE_TEXT, fn() => "a_h"),
"c" => new M(M::STABILITY_STABLE, M::TYPE_COLLECTION, fn() => [
"d" => new M(M::STABILITY_STABLE, M::TYPE_COLLECTION, fn() => [
"e" => new M(M::STABILITY_STABLE, M::TYPE_TEXT, fn() => "a_c_d_e"),
"f" => new M(M::STABILITY_STABLE, M::TYPE_TEXT, fn() => "a_c_d_f")
]),
"g" => new M(M::STABILITY_STABLE, M::TYPE_TEXT, "a_c_g")
"g" => new M(M::STABILITY_STABLE, M::TYPE_TEXT, fn() => "a_c_g")
]),
"i" => new M(M::STABILITY_STABLE, M::TYPE_TEXT, "a_i\na_i")
"i" => new M(M::STABILITY_STABLE, M::TYPE_TEXT, fn() => "a_i\na_i")
]),
"b" => new M(M::STABILITY_STABLE, M::TYPE_COLLECTION, [
"j" => new M(M::STABILITY_STABLE, M::TYPE_TEXT, "b_j")
"b" => new M(M::STABILITY_STABLE, M::TYPE_COLLECTION, fn() => [
"j" => new M(M::STABILITY_STABLE, M::TYPE_TEXT, fn() => "b_j")
]),
"k" => new M(M::STABILITY_STABLE, M::TYPE_TEXT, "k")
"k" => new M(M::STABILITY_STABLE, M::TYPE_TEXT, fn() => "k")
]);

$expected = <<<METRIC
Expand All @@ -180,45 +180,45 @@ public function testIndentation(): void

public function testExtractBySeverity(): void
{
$metrics = new M(M::STABILITY_MIXED, M::TYPE_COLLECTION, [
"a" => new M(M::STABILITY_MIXED, M::TYPE_COLLECTION, [
"h" => new M(M::STABILITY_CONFIG, M::TYPE_TEXT, "a_h"),
"c" => new M(M::STABILITY_MIXED, M::TYPE_COLLECTION, [
"d" => new M(M::STABILITY_MIXED, M::TYPE_COLLECTION, [
"e" => new M(M::STABILITY_STABLE, M::TYPE_TEXT, "a_c_d_e"),
"f" => new M(M::STABILITY_VOLATILE, M::TYPE_TEXT, "a_c_d_f")
$metrics = new M(M::STABILITY_MIXED, M::TYPE_COLLECTION, fn() => [
"a" => new M(M::STABILITY_MIXED, M::TYPE_COLLECTION, fn() => [
"h" => new M(M::STABILITY_CONFIG, M::TYPE_TEXT, fn() => "a_h"),
"c" => new M(M::STABILITY_MIXED, M::TYPE_COLLECTION, fn() => [
"d" => new M(M::STABILITY_MIXED, M::TYPE_COLLECTION, fn() => [
"e" => new M(M::STABILITY_STABLE, M::TYPE_TEXT, fn() => "a_c_d_e"),
"f" => new M(M::STABILITY_VOLATILE, M::TYPE_TEXT, fn() => "a_c_d_f")
]),
"g" => new M(M::STABILITY_CONFIG, M::TYPE_TEXT, "a_c_g")
"g" => new M(M::STABILITY_CONFIG, M::TYPE_TEXT, fn() => "a_c_g")
]),
"i" => new M(M::STABILITY_STABLE, M::TYPE_TEXT, "a_i\na_i")
"i" => new M(M::STABILITY_STABLE, M::TYPE_TEXT, fn() => "a_i\na_i")
]),
"b" => new M(M::STABILITY_MIXED, M::TYPE_COLLECTION, [
"j" => new M(M::STABILITY_VOLATILE, M::TYPE_TEXT, "b_j")
"b" => new M(M::STABILITY_MIXED, M::TYPE_COLLECTION, fn() => [
"j" => new M(M::STABILITY_VOLATILE, M::TYPE_TEXT, fn() => "b_j")
]),
"k" => new M(M::STABILITY_CONFIG, M::TYPE_TEXT, "k")
"k" => new M(M::STABILITY_CONFIG, M::TYPE_TEXT, fn() => "k")
]);

$expected_extracted = new M(M::STABILITY_CONFIG, M::TYPE_COLLECTION, [
"a" => new M(M::STABILITY_CONFIG, M::TYPE_COLLECTION, [
"h" => new M(M::STABILITY_CONFIG, M::TYPE_TEXT, "a_h"),
"c" => new M(M::STABILITY_CONFIG, M::TYPE_COLLECTION, [
"g" => new M(M::STABILITY_CONFIG, M::TYPE_TEXT, "a_c_g")
$expected_extracted = new M(M::STABILITY_CONFIG, M::TYPE_COLLECTION, fn() => [
"a" => new M(M::STABILITY_CONFIG, M::TYPE_COLLECTION, fn() => [
"h" => new M(M::STABILITY_CONFIG, M::TYPE_TEXT, fn() => "a_h"),
"c" => new M(M::STABILITY_CONFIG, M::TYPE_COLLECTION, fn() => [
"g" => new M(M::STABILITY_CONFIG, M::TYPE_TEXT, fn() => "a_c_g")
]),
]),
"k" => new M(M::STABILITY_CONFIG, M::TYPE_TEXT, "k")
"k" => new M(M::STABILITY_CONFIG, M::TYPE_TEXT, fn() => "k")
]);
$expected_rest = new M(M::STABILITY_MIXED, M::TYPE_COLLECTION, [
"a" => new M(M::STABILITY_MIXED, M::TYPE_COLLECTION, [
"c" => new M(M::STABILITY_MIXED, M::TYPE_COLLECTION, [
"d" => new M(M::STABILITY_MIXED, M::TYPE_COLLECTION, [
"e" => new M(M::STABILITY_STABLE, M::TYPE_TEXT, "a_c_d_e"),
"f" => new M(M::STABILITY_VOLATILE, M::TYPE_TEXT, "a_c_d_f")
$expected_rest = new M(M::STABILITY_MIXED, M::TYPE_COLLECTION, fn() => [
"a" => new M(M::STABILITY_MIXED, M::TYPE_COLLECTION, fn() => [
"c" => new M(M::STABILITY_MIXED, M::TYPE_COLLECTION, fn() => [
"d" => new M(M::STABILITY_MIXED, M::TYPE_COLLECTION, fn() => [
"e" => new M(M::STABILITY_STABLE, M::TYPE_TEXT, fn() => "a_c_d_e"),
"f" => new M(M::STABILITY_VOLATILE, M::TYPE_TEXT, fn() => "a_c_d_f")
])
]),
"i" => new M(M::STABILITY_STABLE, M::TYPE_TEXT, "a_i\na_i")
"i" => new M(M::STABILITY_STABLE, M::TYPE_TEXT, fn() => "a_i\na_i")
]),
"b" => new M(M::STABILITY_MIXED, M::TYPE_COLLECTION, [
"j" => new M(M::STABILITY_VOLATILE, M::TYPE_TEXT, "b_j")
"b" => new M(M::STABILITY_MIXED, M::TYPE_COLLECTION, fn() => [
"j" => new M(M::STABILITY_VOLATILE, M::TYPE_TEXT, fn() => "b_j")
])
]);

Expand All @@ -238,18 +238,18 @@ public function testToArrayWithFlatValues(M $metric, string $expected): void

public function testToArrayWithDeepOne(): void
{
$metric = new M(M::STABILITY_STABLE, M::TYPE_COLLECTION, [
"bool_true" => new M(M::STABILITY_STABLE, M::TYPE_BOOL, true)
$metric = new M(M::STABILITY_STABLE, M::TYPE_COLLECTION, fn() => [
"bool_true" => new M(M::STABILITY_STABLE, M::TYPE_BOOL, fn() => true)
]);

$this->assertEquals(["bool_true" => "true"], $metric->toArray());
}

public function testToArrayWithDeepTwo(): void
{
$metric = new M(M::STABILITY_STABLE, M::TYPE_COLLECTION, [
"db" => new M(M::STABILITY_STABLE, M::TYPE_COLLECTION, [
"bool_true" => new M(M::STABILITY_STABLE, M::TYPE_BOOL, true)
$metric = new M(M::STABILITY_STABLE, M::TYPE_COLLECTION, fn() => [
"db" => new M(M::STABILITY_STABLE, M::TYPE_COLLECTION, fn() => [
"bool_true" => new M(M::STABILITY_STABLE, M::TYPE_BOOL, fn() => true)
])
]);

Expand Down Expand Up @@ -278,7 +278,7 @@ public function testToUIReport(): void
->willReturn($panel_f)
;

$metric = new M(M::STABILITY_STABLE, M::TYPE_TEXT, "string", "");
$metric = new M(M::STABILITY_STABLE, M::TYPE_TEXT, fn() => "string", "");

$result = $metric->toUIReport($factory, "Status");

Expand Down

0 comments on commit c4d7e2e

Please sign in to comment.