From 149832cf4691503daddae4f4ccb58238d5af422d Mon Sep 17 00:00:00 2001 From: "C. Scott Ananian" Date: Fri, 2 Aug 2024 15:27:57 -0400 Subject: [PATCH] Parsoid SiteConfig: allow stat counters incremented by arbitrary values Bug: T354908 Bug: T369719 Change-Id: I9ad5cbb9e26138fda1965f3fa4a677c15cb41d07 --- includes/parser/Parsoid/Config/SiteConfig.php | 4 ++-- .../unit/includes/parser/Parsoid/Config/SiteConfigTest.php | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/includes/parser/Parsoid/Config/SiteConfig.php b/includes/parser/Parsoid/Config/SiteConfig.php index f9161077c24..1794d26050b 100644 --- a/includes/parser/Parsoid/Config/SiteConfig.php +++ b/includes/parser/Parsoid/Config/SiteConfig.php @@ -276,13 +276,13 @@ class SiteConfig extends ISiteConfig { * @param array $labels * @return void */ - public function incrementCounter( string $name, array $labels ) { + public function incrementCounter( string $name, array $labels, float $amount = 1 ) { $component = $this->getStatsPrefix( true ); $metric = $this->statsFactory->withComponent( $component )->getCounter( $name ); foreach ( $labels as $labelKey => $labelValue ) { $metric->setLabel( $labelKey, $labelValue ); } - $metric->increment(); + $metric->incrementBy( $amount ); } public function galleryOptions(): array { diff --git a/tests/phpunit/unit/includes/parser/Parsoid/Config/SiteConfigTest.php b/tests/phpunit/unit/includes/parser/Parsoid/Config/SiteConfigTest.php index a5cff38ae70..6b75dc57249 100644 --- a/tests/phpunit/unit/includes/parser/Parsoid/Config/SiteConfigTest.php +++ b/tests/phpunit/unit/includes/parser/Parsoid/Config/SiteConfigTest.php @@ -935,6 +935,14 @@ class SiteConfigTest extends MediaWikiUnitTestCase { $this->assertSame( 1.0, $counter->getSamples()[0]->getValue() ); $this->assertSame( $expectedValues[1], $counter->getSamples()[1]->getLabelValues() ); $this->assertSame( 1.0, $counter->getSamples()[1]->getValue() ); + // Check zero $amount + $config->incrementCounter( $name, $labels[0], 0 ); + $this->assertSame( 3, $counter->getSampleCount() ); + $this->assertSame( 0.0, $counter->getSamples()[2]->getValue() ); + // Check non-unit $amount + $config->incrementCounter( $name, $labels[1], 12 ); + $this->assertSame( 4, $counter->getSampleCount() ); + $this->assertSame( 12.0, $counter->getSamples()[3]->getValue() ); } /** -- 2.11.4.GIT