#!/usr/bin/perl use strict; #################### main pod documentation begin ################### =head1 NAME Zymonic - Test script to generate filter output, for performance testing =head1 SYNOPSIS =head1 DESCRIPTION This script will take in afile and generate output for it. =head1 SUPPORT As in the license, Zymonic is provided without warranty or support unless purchased separately, however... If you email zymonic-support@zednax.com your issue will be noted and may receive a response. For security issues, please contact zymonic-security@zednax.com and someone will respond within 8 working hours. =head1 AUTHOR Alex Masidlover et al. CPAN ID: MODAUTHOR Zednax Limited alex.masidlover@zednax.com http://www.zednax.com =head1 COPYRIGHT This program is free software licensed under the... Zymonic Public License 1.0 The full text of the license can be found in the LICENSE file included with this module. Other licenses may be acceptable if including parts of Zymonic in larger projects, please contact Zednax for details. =head1 SEE ALSO perl(1). =cut #################### main pod documentation end ################### # modules use Data::Dumper; use List::Util qw(shuffle sum); use Term::ReadKey; use Time::HiRes qw(time); use Zymonic; use Zymonic::Auth; use Zymonic::Config; use Zymonic::Session; use Zymonic::Utils qw(death_handler random_string); # use all field subclasses to avoid module loading times use Zymonic::Field::Choice; use Zymonic::Field::DateTime; use Zymonic::Field::DecryptorCrypt; use Zymonic::Field::DecryptorEncrypted; use Zymonic::Field::Email; use Zymonic::Field::File; use Zymonic::Field::FileChooser; use Zymonic::Field::Glob; use Zymonic::Field::LinkedField; use Zymonic::Field::MultipleChoiceLinkedField; use Zymonic::Field::PAN; use Zymonic::Field::PasswordCapture; use Zymonic::Field::Stylesheet; use Zymonic::Field::SubForm; use Zymonic::Field::SubFilter; # death handler $main::SIG{__DIE__} = \&death_handler; if ( $ENV{ZZDEBUG} ) { open( my $fh, ">", $ENV{ZZDEBUG} ) || die "Couldn't open debug file: $ENV{ZZDEBUG}"; $Zymonic::Utils::debugfile = $fh; } # setup the field factory $Zymonic::field_factory = Zymonic::FieldFactory->new(); # enable cache for table includes and relationship permissions $Zymonic::object_cache = { 'Zymonic::TableInclude' => {}, 'Zymonic::RelationshipPermissions' => {}, }; # Create a session (CGI only), give ident so can be parent for fields $Zymonic::session = Zymonic::Session->new( ident => 'field_output_script', ); print "Loaded Session\n"; unless ( $ENV{ZZDEBUG} ) { # force no debugs $Zymonic::Utils::debugs_written = 1; } # set system $Zymonic::system = $ARGV[0]; usage() unless $Zymonic::system; # create config $Zymonic::ZCONFIG{$Zymonic::system} = Zymonic::Config->new( system_name => $Zymonic::system, config_dir => "/etc/zymonic", ); print "Loaded Config\n"; # set session db $Zymonic::session->{DB} = $Zymonic::ZCONFIG{$Zymonic::system}->{DB}; # set session config so can use as field parent $Zymonic::session->{config} = $Zymonic::ZCONFIG{$Zymonic::system}; # create auth, make it use manual auth so don't need permissions $Zymonic::ZCONFIG{$Zymonic::system}->{authtype} = 'Manual'; $Zymonic::ZCONFIG{$Zymonic::system}->{building_documentation} = 'Y'; my $auth = Zymonic::Auth->new( config => $Zymonic::ZCONFIG{$Zymonic::system}, session => $Zymonic::session, DB => $Zymonic::ZCONFIG{$Zymonic::system}->{DB}, user => '', credentials => '', logged_in => '', ); main(); exit(0); #################### subroutine header begin #################### =head2 main Usage : main(); Purpose : main script functionality Returns : nothing Argument : nothing Throws : nothing Comment : See Also : =cut #################### subroutine header end ################### sub main { my $filter_zname = $ARGV[1]; my $count = $ARGV[2] || 1; usage() unless $filter_zname; # load the filter my $start = time() * 1000; my $filter = Zymonic::Filter->new( parent => $Zymonic::session, zname => $filter_zname, ident => 'add_random_record_script_' . $filter_zname . '_', config => $Zymonic::ZCONFIG{$Zymonic::system}, auth => $auth, DB => $Zymonic::ZCONFIG{$Zymonic::system}->{DB}, get_user_values => 'true', expand_all => 'true', no_history => 'true', ); my $end = time() * 1000; my $time_taken = ( $end - $start ); print "Loaded Filter $filter_zname: " . sprintf( '%5.6f', $time_taken ) . " ms\n"; print "Report Fields: " . @{ $filter->get_fields( '', '', 'refs_only' ) } . "\n"; print "Search Fields: " . $filter->get_search_fields('refs_only') . "\n"; $filter->{autorun} = 'Y'; # generate the output print "# Generating output $count times for Filter $filter_zname\n\n"; my $total = 0; foreach my $i ( 1 .. $count ) { # load the filter output my $start = time() * 1000; my $output = $filter->output('no_display_attributes'); my $end = time() * 1000; my $time_taken = ( $end - $start ); $total += $time_taken; print "Output $i:\t" . sprintf( '%-5.6f', $time_taken ) . " ms\n"; } print "Total: \t" . sprintf( '%5.6f', $total ) . " ms\nAverage:\t" . sprintf( '%5.6f', $total / $count ) . " ms\n"; } #################### subroutine header begin #################### =head2 random_fields Usage : random_fields($count); Purpose : Picks random fields from system def. Returns : array of field znames Argument : nothing Throws : nothing Comment : See Also : =cut #################### subroutine header end ################### sub random_fields { my $count = shift; # grab and shuffle fields from def my $zname_list = $Zymonic::ZCONFIG{$Zymonic::system}->{SysDef}->{zname_list}; my @fields_from_def = shuffle grep { ( $zname_list->{$_}->{type} || '' ) eq 'Field' } keys %{$zname_list}; if ( $count > @fields_from_def ) { $count = @fields_from_def; } # return the number we want from top of the list return @fields_from_def[ 0 .. $count - 1 ]; } #################### subroutine header begin #################### =head2 load_fields Usage : load_fields(\@fields); Purpose : Creates the field refs for each field and loads it. Returns : nothing Argument : array of field znames Throws : nothing Comment : See Also : =cut #################### subroutine header end ################### sub load_fields { my $fields = shift || []; # usually fields are stored from table or filter # for this script just store them all on session foreach my $field_zname ( @{$fields} ) { $Zymonic::session->store_field_ref($field_zname); } } #################### subroutine header begin #################### =head2 usage Usage : usage(); Purpose : Prints script usage. Returns : nothing Argument : nothing Throws : nothing Comment : See Also : =cut #################### subroutine header end ################### sub usage { print "Usage: filter_output.pl [system] [filter_zname] [number of iterations (defaults to 1)]\n"; exit(1); }