Skip to content

Expand --nstimes option #421

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 88 additions & 29 deletions lib/Zonemaster/CLI.pm
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
# Brief help module to define the exception we use for early exits.
package Zonemaster::Engine::Exception::NormalExit;
use 5.014002;
use v5.26;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does that mean that we increase the lowest Perl version of Zonemaster from v5.16 to v5.26?

use warnings;
use parent 'Zonemaster::Engine::Exception';

# The actual interesting module.
package Zonemaster::CLI;

use 5.014002;
use v5.26;

use strict;
use warnings;

use version; our $VERSION = version->declare( "v7.2.0" );
Expand Down Expand Up @@ -643,38 +642,98 @@ sub run {

if ( $opt_nstimes ) {
my $zone = Zonemaster::Engine->zone( $domain );
my $max = max map { length( "$_" ) } ( @{ $zone->ns }, q{Server} );
my %all_nss = %{ Zonemaster::Engine::Nameserver::object_cache };
my @child_nss = @{ $zone->ns };
my @parent_nss = @{ $zone->parent->ns };
my @all_responded_nss;

foreach my $ns_name ( keys %all_nss ) {
foreach my $ns ( values %{ $all_nss{$ns_name} } ) {
push @all_responded_nss, $ns if scalar @{ $ns->times } > 0;
}
}

my %nss_filter = map { $_ => undef } ( @child_nss, @parent_nss );
my @other_nss = grep { ! exists $nss_filter{$_} } @all_responded_nss;

if ( $opt_json ) {
my @times = ();
foreach my $ns ( @{ $zone->ns } ) {
push @times,
{
'ns' => $ns->string,
'max' => 1000 * $ns->max_time,
'min' => 1000 * $ns->min_time,
'avg' => 1000 * $ns->average_time,
'stddev' => 1000 * $ns->stddev_time,
'median' => 1000 * $ns->median_time,
'total' => 1000 * $ns->sum_time
};
my @times;

my sub json_nstimes {
my ( $ns ) = @_;
return {
'ns' => $ns->string,
'max' => 1000 * $ns->max_time,
'min' => 1000 * $ns->min_time,
'avg' => 1000 * $ns->average_time,
'stddev' => 1000 * $ns->stddev_time,
'median' => 1000 * $ns->median_time,
'total' => 1000 * $ns->sum_time,
'count' => scalar @{ $ns->times }
};
}

my %section_mapping = (
'child' => \@child_nss,
'parent' => \@parent_nss,
'other' => \@other_nss
);

foreach my $section_name ( sort keys %section_mapping ) {
my @entries = map { json_nstimes( $_ ) } sort @{ $section_mapping{$section_name} };
push @times, { $section_name => \@entries };
}

$json_output->{nstimes} = \@times;
}
else {
print "\n";
printf "%${max}s %s\n", 'Server', ' Max Min Avg Stddev Median Total';
printf "%${max}s %s\n", '=' x $max, ' ======== ======== ======== ======== ======== =========';

foreach my $ns ( @{ $zone->ns } ) {
printf "%${max}s ", $ns->string;
printf "%9.2f ", 1000 * $ns->max_time;
printf "%8.2f ", 1000 * $ns->min_time;
printf "%8.2f ", 1000 * $ns->average_time;
printf "%8.2f ", 1000 * $ns->stddev_time;
printf "%8.2f ", 1000 * $ns->median_time;
printf "%9.2f\n", 1000 * $ns->sum_time;
my $header = __( 'Name servers' );
my $max = max map { length( "$_" ) } ( ( @child_nss, @parent_nss, @all_responded_nss ), $header );
printf "\n%${max}s %s\n", $header, ' Max Min Avg Stddev Median Total Count';
printf "%${max}s %s\n", '=' x $max, ' ========== ========== ========== ========== ========== =========== ===========';
Comment on lines +690 to +693
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This table’s columns aren’t fully localized and that’s a problem.

Also, it's better to write printf "%*s", $width, $string instead of printf "%${width}s", $string.

Suggested change
my $header = __( 'Name servers' );
my $max = max map { length( "$_" ) } ( ( @child_nss, @parent_nss, @all_responded_nss ), $header );
printf "\n%${max}s %s\n", $header, ' Max Min Avg Stddev Median Total Count';
printf "%${max}s %s\n", '=' x $max, ' ========== ========== ========== ========== ========== =========== ===========';
my $max = max map { length( "$_" ) } ( ( @child_nss, @parent_nss, @all_responded_nss ), __( 'Name servers' ) );
my @columns = (
{ label => __( 'Name servers' ), width => $max },
{ label => __( 'Max' ), width => 11 },
{ label => __( 'Min' ), width => 10 },
{ label => __( 'Avg' ), width => 10 },
{ label => __( 'Stddev' ), width => 10 },
{ label => __( 'Median' ), width => 10 },
{ label => __( 'Total' ), width => 11 },
{ label => __( 'Count' ), width => 11 },
);
# Table header
print "\n";
say join " ", map { sprintf "%*s", $_->{width}, $_->{label} } @columns;
say join " ", map { "=" x $_->{width} } @columns;

Copy link
Contributor

@matsduf matsduf Jun 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The width of each the column is wide enough for the English header name, but in translation the header name might get wider. I guess the solution is to have adaptive column width for all comlumns.

Or does the second suggestion take care of that?


my $total_queries_count = 0;
my $total_queries_times = 0;
my %nss_already_processed;

my sub print_nstimes {
my ( $ns, $max, $total_queries_count, $total_queries_times, $nss_already_processed_ref ) = @_;
my %nss_already_processed = %{ $nss_already_processed_ref };

printf "%${max}s ", $ns->string;
printf "%11.2f ", 1000 * $ns->max_time;
printf "%10.2f ", 1000 * $ns->min_time;
printf "%10.2f ", 1000 * $ns->average_time;
printf "%10.2f ", 1000 * $ns->stddev_time;
printf "%10.2f ", 1000 * $ns->median_time;
printf "%11.2f ", 1000 * $ns->sum_time;
printf "%11d\n", scalar @{ $ns->times };
Comment on lines +703 to +710
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks to introducing the @columns variable above in my previous suggestion, we can have a single source of truth for the column widths.

And there’s another trick: by using %*2$s instead of %*s as a format string, the width field can be put second instead of first in the call to printf.

Suggested change
printf "%${max}s ", $ns->string;
printf "%11.2f ", 1000 * $ns->max_time;
printf "%10.2f ", 1000 * $ns->min_time;
printf "%10.2f ", 1000 * $ns->average_time;
printf "%10.2f ", 1000 * $ns->stddev_time;
printf "%10.2f ", 1000 * $ns->median_time;
printf "%11.2f ", 1000 * $ns->sum_time;
printf "%11d\n", scalar @{ $ns->times };
printf '%*2$s ', $ns->string, $columns[0]->{width};
printf '%*2$.2f ', 1000 * $ns->max_time, $columns[1]->{width};
printf '%*2$.2f ', 1000 * $ns->min_time, $columns[2]->{width};
printf '%*2$.2f ', 1000 * $ns->average_time, $columns[3]->{width};
printf '%*2$.2f ', 1000 * $ns->stddev_time, $columns[4]->{width};
printf '%*2$.2f ', 1000 * $ns->median_time, $columns[5]->{width};
printf '%*2$.2f ', 1000 * $ns->sum_time, $columns[6]->{width};
printf "%*2\$d\n", scalar @{ $ns->times }, $columns[7]->{width};

$total_queries_count += scalar @{ $ns->times } unless $nss_already_processed{$ns};
$total_queries_times += ( 1000 * $ns->sum_time ) unless $nss_already_processed{$ns};

return $total_queries_count, $total_queries_times;
}

my %section_mapping = (
1 => { __( 'Child zone' ) => \@child_nss },
2 => { __( 'Parent zone' ) => \@parent_nss },
3 => { __( 'Other' ) => \@other_nss }
);

foreach my $section_order ( sort keys %section_mapping ) {
foreach my $section_header ( keys % { $section_mapping{$section_order} } ) {
printf "%s %s\n", $section_header, '-' x ( ( $max - length $section_header ) - 1 );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using printf to do such a thing is discouraged:

Suggested change
printf "%s %s\n", $section_header, '-' x ( ( $max - length $section_header ) - 1 );
say $section_header, ' ', '-' x ( ( $max - length $section_header ) - 1 );


foreach my $section_nss ( sort @{ $section_mapping{$section_order}{$section_header} } ) {
( $total_queries_count, $total_queries_times ) =
print_nstimes( $section_nss, $max, $total_queries_count, $total_queries_times, \%nss_already_processed );
$nss_already_processed{$section_nss} = 1;
}
}
}

printf "%${max}s %s\n", '=' x $max, ' ========== ========== ========== ========== ========== =========== ===========';
printf "%${max}s %67.2f %11s\n", __( 'Grand total' ), $total_queries_times, $total_queries_count;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the grand total be included in the JSON output also?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wondered about that too, but decided against it for now. It could be done more easily once I do the refactoring you suggested.

Comment on lines +735 to +736
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, using a single source of truth for the column widths:

Suggested change
printf "%${max}s %s\n", '=' x $max, ' ========== ========== ========== ========== ========== =========== ===========';
printf "%${max}s %67.2f %11s\n", __( 'Grand total' ), $total_queries_times, $total_queries_count;
# Totals line
say join " ", map { "=" x $_->{width} } @columns;
printf "%*s %67.2f %11s\n", $max, __( 'Grand total' ), $total_queries_times, $total_queries_count;

}
} ## end if ( $opt_nstimes )

Expand All @@ -685,7 +744,7 @@ sub run {
$json_output->{elapsed} = $last->timestamp;
}
else {
printf "Total test run time: %0.1f seconds.\n", $last->timestamp;
printf "\nTotal test run time: %0.1f seconds.\n", $last->timestamp;
}
}

Expand Down
5 changes: 3 additions & 2 deletions script/zonemaster-cli
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,9 @@ level that were logged during the run, as well as a count of each message tag.

=item B<--[no-]nstimes>

Print a summary, at the end of a run, of the times (in milliseconds) the zone's
name servers took to answer.
Print a summary, at the end of a run, of various metrics related to the times
(in milliseconds) each name server took to answer, as well as a count of the
number of sent queries per name server.
(default: disabled)

=item B<--[no-]elapsed>
Expand Down
36 changes: 32 additions & 4 deletions t/usage.t
Original file line number Diff line number Diff line change
Expand Up @@ -390,9 +390,15 @@ do {
text => qr{
\QLooks OK.\E
.*
Server \s+ Max \s+ Min \s+ Avg \s+ Stddev \s+ Median \s+ Total
\QName servers\E \s+ Max \s+ Min \s+ Avg \s+ Stddev \s+ Median \s+ Total \s+ Count
.*
\Qa.root-servers.net/\E
\QChild zone\E
.*
\QParent zone\E
.*
Other
.*
\QGrand total\E \s+ \d+
}msx,
json => {
type => "object",
Expand All @@ -401,8 +407,30 @@ do {
nstimes => {
type => "array",
items => {
type => "object",
required => [qw( avg max median min ns stddev total)],
type => "object",
properties => {
child => {
type => "array",
items => {
type => "object",
required => [qw( avg max median min ns stddev total count)],
},
},
parent => {
type => "array",
items => {
type => "object",
required => [qw( avg max median min ns stddev total count)],
},
},
other => {
type => "array",
items => {
type => "object",
required => [qw( avg max median min ns stddev total count)],
},
},
},
},
},
},
Expand Down