| 1 | # perl | ||
| 2 | # | ||
| 3 | # Demo to start examples for PORF Perl Open Report Framework | ||
| 4 | # | ||
| 5 | # Ralf Peine, Wed May 14 10:39:11 2014 | ||
| 6 | # | ||
| 7 | #------------------------------------------------------------------------------ | ||
| 8 | |||
| 9 | use warnings; | ||
| 10 | use strict; | ||
| 11 | |||
| 12 | $|=1; | ||
| 13 | |||
| 14 | use lib '../../..'; | ||
| 15 | |||
| 16 | use Time::HiRes; | ||
| 17 | use Report::Porf::examples::Person; | ||
| 18 | |||
| 19 | #------------------------------------------------------------------------------ | ||
| 20 | # | ||
| 21 | # Defaults | ||
| 22 | # | ||
| 23 | #------------------------------------------------------------------------------ | ||
| 24 | |||
| 25 | our $CountMax = 1000; | ||
| 26 | our $CountDefault = 13; | ||
| 27 | |||
| 28 | #------------------------------------------------------------------------------ | ||
| 29 | # | ||
| 30 | # Methods | ||
| 31 | # | ||
| 32 | #------------------------------------------------------------------------------ | ||
| 33 | |||
| 34 | # --- check count for lists --------------------------------------------------- | ||
| 35 | sub inspect_count { | ||
| 36 | my $count = shift; | ||
| 37 | |||
| 38 | $count = $CountDefault unless $count; | ||
| 39 | $count = $CountMax if $count > $CountMax; | ||
| 40 | |||
| 41 | return $count; | ||
| 42 | } | ||
| 43 | |||
| 44 | # --- create Test Data As List Of $data_type -------------------------------------- | ||
| 45 | sub create_data_list { | ||
| 46 | my $data_type = shift; | ||
| 47 | my $count = inspect_count(shift); | ||
| 48 | |||
| 49 | if ( lc($data_type) eq 'array' ) { | ||
| 50 | return create_persons_as_array($count); | ||
| 51 | } | ||
| 52 | elsif ( lc($data_type) eq 'hash' ) { | ||
| 53 | return create_persons_as_hash($count); | ||
| 54 | } | ||
| 55 | elsif ( lc($data_type) eq 'object' ) { | ||
| 56 | return create_persons_as_object($count); | ||
| 57 | } | ||
| 58 | |||
| 59 | die "Cannot create test data in format '$data_type'"; | ||
| 60 | } | ||
| 61 | |||
| 62 | #------------------------------------------------------------------------------ | ||
| 63 | # | ||
| 64 | # create Test Data as Array | ||
| 65 | # | ||
| 66 | #------------------------------------------------------------------------------ | ||
| 67 | |||
| 68 | sub create_persons_as_array { | ||
| 69 | my $max_entries = shift; | ||
| 70 | |||
| 71 | my $start_time = hires_actual_time(); | ||
| 72 | my @rows; | ||
| 73 | |||
| 74 | foreach my $l (1..$max_entries) { | ||
| 75 | my $time = hires_diff_time($start_time, hires_actual_time()); | ||
| 76 | my @data = ( | ||
| 77 | $l, | ||
| 78 | "Vorname $l", | ||
| 79 | "Name $l", | ||
| 80 | ($l/$max_entries)*100, | ||
| 81 | $time, | ||
| 82 | ); | ||
| 83 | |||
| 84 | push (@rows, \@data); | ||
| 85 | } | ||
| 86 | |||
| 87 | return \@rows; | ||
| 88 | } | ||
| 89 | |||
| 90 | #-------------------------------------------------------------------------------- | ||
| 91 | # | ||
| 92 | # create Test Data as Hash | ||
| 93 | # | ||
| 94 | #-------------------------------------------------------------------------------- | ||
| 95 | |||
| 96 | sub create_persons_as_hash { | ||
| 97 | my $max_entries = shift; | ||
| 98 | |||
| 99 | my $start_time = hires_actual_time(); | ||
| 100 | my @rows; | ||
| 101 | |||
| 102 | foreach my $l (1..$max_entries) { | ||
| 103 | my $time = hires_diff_time($start_time, hires_actual_time()); | ||
| 104 | my %data = ( | ||
| 105 | Count => $l, | ||
| 106 | Prename => "Vorname $l", | ||
| 107 | Surname => "Name $l", | ||
| 108 | Age => ($l/$max_entries)*100, | ||
| 109 | Time => $time, | ||
| 110 | ); | ||
| 111 | |||
| 112 | push (@rows, \%data); | ||
| 113 | } | ||
| 114 | |||
| 115 | return \@rows; | ||
| 116 | } | ||
| 117 | |||
| 118 | #-------------------------------------------------------------------------------- | ||
| 119 | # | ||
| 120 | # create Test Data as Object | ||
| 121 | # | ||
| 122 | #-------------------------------------------------------------------------------- | ||
| 123 | |||
| 124 | sub create_persons_as_object { | ||
| 125 | my $max_entries = shift; | ||
| 126 | |||
| 127 | my $start_time = hires_actual_time(); | ||
| 128 | my @rows; | ||
| 129 | |||
| 130 | foreach my $l (1..$max_entries) { | ||
| 131 | my $time = hires_diff_time($start_time, hires_actual_time()); | ||
| 132 | my $person = Report::Porf::examples::Person->new(); | ||
| 133 | $person->set_count ($l); | ||
| 134 | $person->set_prename("Vorname $l"); | ||
| 135 | $person->set_surname("Name $l"); | ||
| 136 | $person->set_age (($l/$max_entries)*100); | ||
| 137 | $person->set_time ($time); | ||
| 138 | |||
| 139 | push (@rows, $person); | ||
| 140 | } | ||
| 141 | |||
| 142 | return \@rows; | ||
| 143 | } | ||
| 144 | |||
| 145 | #-------------------------------------------------------------------------------- | ||
| 146 | # | ||
| 147 | # Helper subs | ||
| 148 | # | ||
| 149 | #-------------------------------------------------------------------------------- | ||
| 150 | |||
| 151 | # --- get hiresolution time, uses Time::HiRes --- | ||
| 152 | sub hires_actual_time { | ||
| 153 | return [Time::HiRes::gettimeofday]; | ||
| 154 | } | ||
| 155 | |||
| 156 | # --- get difference from now to $start_time in hiresolution time, uses Time::HiRes --- | ||
| 157 | sub hires_diff_time { | ||
| 158 | my $start_time = shift; | ||
| 159 | my $end_time = shift; # actual time, if omitted | ||
| 160 | return Time::HiRes::tv_interval ($start_time, $end_time); | ||
| 161 | } | ||
| 162 | |||
| 163 | #-------------------------------------------------------------------------------- | ||
| 164 | # | ||
| 165 | # Main Example | ||
| 166 | # | ||
| 167 | #-------------------------------------------------------------------------------- | ||
| 168 | |||
| 169 | my $max_lines = shift; | ||
| 170 | my $format = shift; | ||
| 171 | my $data_type = shift; | ||
| 172 | my $example = shift; | ||
| 173 | |||
| 174 | $format = 'text' unless $format; | ||
| 175 | $data_type = 'array' unless $data_type; | ||
| 176 | $data_type = lc($data_type); | ||
| 177 | $example = "minimal_$data_type.pl" unless $example; | ||
| 178 | |||
| 179 | require $example; | ||
| 180 | |||
| 181 | $max_lines = $CountDefault unless $max_lines; | ||
| 182 | |||
| 183 | # --- measure time -------------------------------------- | ||
| 184 | my $start_time = hires_actual_time(); | ||
| 185 | |||
| 186 | #-------------------------------------------------------------------------------- | ||
| 187 | # | ||
| 188 | # Run Example | ||
| 189 | # | ||
| 190 | #-------------------------------------------------------------------------------- | ||
| 191 | |||
| 192 | my $row_count = run_example(create_data_list($data_type, $max_lines), $format); | ||
| 193 | |||
| 194 | # --- measure time -------------------------------------- | ||
| 195 | |||
| 196 | my $end_time = hires_actual_time(); | ||
| 197 | |||
| 198 | my $time_needed = "Time needed for export of $row_count lines ($data_type) by example script '$example': ". | ||
| 199 | hires_diff_time($start_time, $end_time)." sec\n"; | ||
| 200 | print "# $time_needed"; | ||
| 201 | warn "$time_needed"; |