Skip to content

Commit

Permalink
Merge pull request #196 from Automattic/add-log-runtime-measurements
Browse files Browse the repository at this point in the history
Log shutdown message to IRC
  • Loading branch information
gudmdharalds authored Aug 12, 2021
2 parents cf582a0 + 78f1ba2 commit dc5ed3a
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 4 deletions.
18 changes: 14 additions & 4 deletions main.php
Original file line number Diff line number Diff line change
Expand Up @@ -2305,19 +2305,29 @@ function vipgoci_run() {
vipgoci_log(
'Shutting down',
array(
'vipgoci_version' => VIPGOCI_VERSION,
'repo_owner' => $options['repo-owner'],
'repo_name' => $options['repo-name'],
'pr_numbers' => array_column( $prs_implicated, 'number' ),
'run_time_seconds' => time() - $startup_time,
'run_time_measurements' =>
vipgoci_runtime_measure(
VIPGOCI_RUNTIME_DUMP,
null
vipgoci_round_array_items(
vipgoci_runtime_measure(
VIPGOCI_RUNTIME_DUMP,
null
),
4,
PHP_ROUND_HALF_UP
),
'counters_report' => $counter_report,

'github_api_rate_limit' =>
$github_api_rate_limit_usage->resources->core,

'results' => $results,
)
),
0,
true // Log to IRC
);

/*
Expand Down
18 changes: 18 additions & 0 deletions misc.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,24 @@ function vipgoci_convert_string_to_type( $str ) {
return $ret;
}

/*
* Round items in an array to a certain precision, return
* new array with results. Essentially a wrapper around the
* PHP round() function.
*/
function vipgoci_round_array_items(
array $arr,
int $precision = 0,
int $mode = PHP_ROUND_HALF_UP
): array {
return array_map(
function( $item ) use ( $precision, $mode ) {
return round( $item, $precision, $mode );
},
$arr
);
}

/*
* Given a patch-file, the function will return an
* associative array, mapping the patch-file
Expand Down
5 changes: 5 additions & 0 deletions statistics.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ function vipgoci_runtime_measure( $action = null, $type = null ) {

// Dump all runtimes we have
if ( VIPGOCI_RUNTIME_DUMP === $action ) {
/*
* Sort by value and maintain index association
*/
arsort( $runtime, SORT_NUMERIC );

return $runtime;
}

Expand Down
45 changes: 45 additions & 0 deletions tests/MiscRoundArrayItemsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Vipgoci\tests;

require_once( __DIR__ . '/IncludesForTests.php' );

use PHPUnit\Framework\TestCase;

// phpcs:disable PSR1.Files.SideEffects

final class MiscRoundArrayItemsTest extends TestCase {
/**
* @covers ::vipgoci_round_array_items
*/
public function testRoundArrayItems() {
$org_array = array(
'test1' => 10.333330,
'test2' => 0.034444444,
'test3' => 3.359999999,
'test4' => 5.0000003,
'test5' => 7.377777777,
'test6' => 5.00000001,
);

$res_array = vipgoci_round_array_items(
$org_array,
2,
PHP_ROUND_HALF_UP
);

$expected_array = array(
'test1' => 10.33,
'test2' => 0.03,
'test3' => 3.36,
'test4' => 5.00,
'test5' => 7.38,
'test6' => 5.0,
);

$this->assertSame(
$expected_array,
$res_array
);
}
}

0 comments on commit dc5ed3a

Please sign in to comment.