| 1 | package BenchmarkAnything::Storage::Backend::SQL; | ||
| 2 | # git description: v0.027-2-gae57af0 | ||
| 3 | |||
| 4 | our $AUTHORITY = 'cpan:TAPPER'; | ||
| 5 | # ABSTRACT: Autonomous SQL backend to store benchmarks | ||
| 6 | $BenchmarkAnything::Storage::Backend::SQL::VERSION = '0.028'; | ||
| 7 | use 5.008; | ||
| 8 | use utf8; | ||
| 9 | use strict; | ||
| 10 | use warnings; | ||
| 11 | use Try::Tiny; | ||
| 12 | |||
| 13 | my $hr_default_config = { | ||
| 14 | select_cache => 0, | ||
| 15 | default_aggregation => 'min', | ||
| 16 | tables => { | ||
| 17 | unit_table => 'bench_units', | ||
| 18 | benchmark_table => 'benchs', | ||
| 19 | benchmark_value_table => 'bench_values', | ||
| 20 | subsume_type_table => 'bench_subsume_types', | ||
| 21 | benchmark_backup_value_table => 'bench_backup_values', | ||
| 22 | additional_type_table => 'bench_additional_types', | ||
| 23 | additional_value_table => 'bench_additional_values', | ||
| 24 | additional_relation_table => 'bench_additional_relations', | ||
| 25 | additional_type_relation_table => 'bench_additional_type_relations', | ||
| 26 | backup_additional_relation_table => 'bench_backup_additional_relations', | ||
| 27 | }, | ||
| 28 | }; | ||
| 29 | |||
| 30 | my $hr_column_ba_mapping = { | ||
| 31 | bench_value_id => 'VALUE_ID', | ||
| 32 | bench => 'NAME', | ||
| 33 | bench_value => 'VALUE', | ||
| 34 | bench_unit => 'UNIT', | ||
| 35 | created_at => 'CREATED', | ||
| 36 | }; | ||
| 37 | |||
| 38 | my $fn_add_subsumed_point = sub { | ||
| 39 | |||
| 40 | my ( $or_self, $hr_atts ) = @_; | ||
| 41 | |||
| 42 | $or_self->{query}->start_transaction(); | ||
| 43 | |||
| 44 | eval { | ||
| 45 | |||
| 46 | # insert subsumed benchmark value | ||
| 47 | $or_self->{query}->insert_benchmark_value( | ||
| 48 | $hr_atts->{rows}[0]{bench_id}, | ||
| 49 | $hr_atts->{type_id}, | ||
| 50 | $hr_atts->{VALUE}, | ||
| 51 | ); | ||
| 52 | my $i_bench_value_id = $or_self->{query}->last_insert_id( | ||
| 53 | $or_self->{config}{tables}{benchmark_value_table}, | ||
| 54 | 'bench_value_id', | ||
| 55 | ); | ||
| 56 | |||
| 57 | # insert subsumed benchmark additional values | ||
| 58 | $or_self->{query}->copy_additional_values({ | ||
| 59 | new_bench_value_id => $i_bench_value_id, | ||
| 60 | old_bench_value_id => $hr_atts->{rows}[0]{bench_value_id}, | ||
| 61 | }); | ||
| 62 | |||
| 63 | for my $hr_backup_row ( @{$hr_atts->{rows}} ) { | ||
| 64 | |||
| 65 | if ( $hr_backup_row->{bench_subsume_type_rank} == 1 ) { | ||
| 66 | if ( $hr_atts->{backup} ) { | ||
| 67 | # copy data rows to backup table | ||
| 68 | $or_self->{query}->copy_benchmark_backup_value({ | ||
| 69 | new_bench_value_id => $i_bench_value_id, | ||
| 70 | old_bench_value_id => $hr_backup_row->{bench_value_id}, | ||
| 71 | }); | ||
| 72 | my $i_bench_backup_value_id = $or_self->{query}->last_insert_id( | ||
| 73 | $or_self->{config}{tables}{benchmark_backup_value_table}, | ||
| 74 | 'bench_backup_value_id', | ||
| 75 | ); | ||
| 76 | $or_self->{query}->copy_benchmark_backup_additional_relations({ | ||
| 77 | new_bench_value_id => $i_bench_backup_value_id, | ||
| 78 | old_bench_value_id => $hr_backup_row->{bench_value_id}, | ||
| 79 | }); | ||
| 80 | } | ||
| 81 | } | ||
| 82 | else { | ||
| 83 | # update bench_value_id in backup table | ||
| 84 | $or_self->{query}->update_benchmark_backup_value({ | ||
| 85 | new_bench_value_id => $i_bench_value_id, | ||
| 86 | old_bench_value_id => $hr_backup_row->{bench_value_id}, | ||
| 87 | }); | ||
| 88 | } | ||
| 89 | |||
| 90 | # now lets remove the old rows | ||
| 91 | $or_self->{query}->delete_benchmark_additional_relations( | ||
| 92 | $hr_backup_row->{bench_value_id}, | ||
| 93 | ); | ||
| 94 | $or_self->{query}->delete_benchmark_value( | ||
| 95 | $hr_backup_row->{bench_value_id}, | ||
| 96 | ); | ||
| 97 | |||
| 98 | } | ||
| 99 | |||
| 100 | }; | ||
| 101 | |||
| 102 | $or_self->{query}->finish_transaction( $@ ); | ||
| 103 | |||
| 104 | return 1; | ||
| 105 | |||
| 106 | }; | ||
| 107 | |||
| 108 | sub new { | ||
| 109 | |||
| 110 | my ( $s_self, $hr_atts ) = @_; | ||
| 111 | |||
| 112 | my $or_self = bless {}, $s_self; | ||
| 113 | |||
| 114 | for my $s_key (qw/ dbh /) { | ||
| 115 | if (! $hr_atts->{$s_key} ) { | ||
| 116 | require Carp; | ||
| 117 | Carp::confess("missing '$s_key' parameter"); | ||
| 118 | return; | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | # get tapper benchmark configuration | ||
| 123 | $or_self->{config} = { %{$hr_default_config} }; | ||
| 124 | |||
| 125 | if ( $hr_atts->{config} ) { | ||
| 126 | require Hash::Merge; | ||
| 127 | $or_self->{config} = { | ||
| 128 | Hash::Merge | ||
| 129 | ->new('LEFT_PRECEDENT') | ||
| 130 | ->merge( | ||
| 131 | %{$hr_atts->{config}}, | ||
| 132 | %{$or_self->{config}}, | ||
| 133 | ) | ||
| 134 | }; | ||
| 135 | } | ||
| 136 | |||
| 137 | require CHI; | ||
| 138 | if ( $or_self->{config}{select_cache} ) { | ||
| 139 | $or_self->{cache} = CHI->new( driver => 'RawMemory', global => 1 ); | ||
| 140 | } | ||
| 141 | |||
| 142 | my $s_module = "BenchmarkAnything::Storage::Backend::SQL::Query::$hr_atts->{dbh}{Driver}{Name}"; | ||
| 143 | |||
| 144 | my $fn_new_sub; | ||
| 145 | eval { | ||
| 146 | require Module::Load; | ||
| 147 | Module::Load::load( $s_module ); | ||
| 148 | $fn_new_sub = $s_module->can('new'); | ||
| 149 | }; | ||
| 150 | |||
| 151 | if ( $@ || !$fn_new_sub ) { | ||
| 152 | require Carp; | ||
| 153 | Carp::confess("database engine '$hr_atts->{dbh}{Driver}{Name}' not supported"); | ||
| 154 | return; | ||
| 155 | } | ||
| 156 | else { | ||
| 157 | $or_self->{query} = $s_module->new({ | ||
| 158 | dbh => $hr_atts->{dbh}, | ||
| 159 | driver => $hr_atts->{dbh}{Driver}{Name}, | ||
| 160 | debug => $hr_atts->{debug} || 0, | ||
| 161 | verbose=> $hr_atts->{verbose} || 0, | ||
| 162 | config => $or_self->{config}, | ||
| 163 | }); | ||
| 164 | } | ||
| 165 | |||
| 166 | $or_self->{searchengine} = $hr_atts->{searchengine} if $hr_atts->{searchengine}; | ||
| 167 | $or_self->{debug} = $hr_atts->{debug} || 0; | ||
| 168 | $or_self->{verbose} = $hr_atts->{verbose} || 0; | ||
| 169 | $or_self->{dbh_config} = $hr_atts->{dbh_config}; | ||
| 170 | |||
| 171 | return $or_self; | ||
| 172 | |||
| 173 | } | ||
| 174 | |||
| 175 | |||
| 176 | sub add_single_benchmark { | ||
| 177 | |||
| 178 | my ( $or_self, $hr_benchmark, $hr_options ) = @_; | ||
| 179 | |||
| 180 | my $hr_config = $or_self->{config}; | ||
| 181 | |||
| 182 | my $VALUE_ID; # same spelling as reserved key in BenchmarkAnything schema | ||
| 183 | |||
| 184 | # benchmark | ||
| 185 | my $i_benchmark_id; | ||
| 186 | if ( $hr_benchmark->{NAME} ) { | ||
| 187 | if ( | ||
| 188 | my $hr_bench_select = $or_self->{query} | ||
| 189 | ->select_benchmark( $hr_benchmark->{NAME} ) | ||
| 190 | ->fetchrow_hashref() | ||
| 191 | ) { | ||
| 192 | $i_benchmark_id = $hr_bench_select->{bench_id}; | ||
| 193 | } | ||
| 194 | else { | ||
| 195 | my $i_unit_id; | ||
| 196 | if ( $hr_benchmark->{UNIT} ) { | ||
| 197 | if ( | ||
| 198 | my $hr_unit_select = $or_self->{query} | ||
| 199 | ->select_unit( $hr_benchmark->{UNIT} ) | ||
| 200 | ->fetchrow_hashref() | ||
| 201 | ) { | ||
| 202 | $i_unit_id = $hr_unit_select->{bench_unit_id}; | ||
| 203 | } | ||
| 204 | else { | ||
| 205 | $or_self->{query}->insert_unit( | ||
| 206 | $hr_benchmark->{UNIT}, | ||
| 207 | ); | ||
| 208 | $i_unit_id = $or_self->{query}->last_insert_id( | ||
| 209 | $hr_config->{tables}{unit_table}, | ||
| 210 | 'bench_unit_id', | ||
| 211 | ); | ||
| 212 | } | ||
| 213 | } | ||
| 214 | $or_self->{query}->insert_benchmark( | ||
| 215 | $hr_benchmark->{NAME}, $i_unit_id, | ||
| 216 | ); | ||
| 217 | $i_benchmark_id = $or_self->{query}->last_insert_id( | ||
| 218 | $hr_config->{tables}{benchmark_table}, | ||
| 219 | 'bench_id', | ||
| 220 | ); | ||
| 221 | } | ||
| 222 | } | ||
| 223 | else { | ||
| 224 | require Carp; | ||
| 225 | Carp::confess('missing element "NAME"'); | ||
| 226 | return 0; | ||
| 227 | } | ||
| 228 | |||
| 229 | if ( | ||
| 230 | $hr_benchmark->{data} | ||
| 231 | && ref( $hr_benchmark->{data} ) eq 'ARRAY' | ||
| 232 | && @{$hr_benchmark->{data}} | ||
| 233 | ) { | ||
| 234 | |||
| 235 | my $i_benchmark_subsume_type_id = $or_self->{query} | ||
| 236 | ->select_min_subsume_type() | ||
| 237 | ->fetchrow_hashref() | ||
| 238 | ->{bench_subsume_type_id} | ||
| 239 | ; | ||
| 240 | |||
| 241 | my $i_counter = 1; | ||
| 242 | for my $hr_point ( @{$hr_benchmark->{data}} ) { | ||
| 243 | |||
| 244 | if ( not exists $hr_point->{VALUE} ) { | ||
| 245 | require Carp; | ||
| 246 | if ( $hr_options->{force} ) { | ||
| 247 | Carp::cluck("missing parameter 'VALUE' in element $i_counter"); | ||
| 248 | } | ||
| 249 | else { | ||
| 250 | Carp::confess("missing parameter 'VALUE' in element $i_counter"); | ||
| 251 | } | ||
| 252 | } | ||
| 253 | |||
| 254 | # benchmark value | ||
| 255 | $or_self->{query}->insert_benchmark_value( | ||
| 256 | $i_benchmark_id, $i_benchmark_subsume_type_id, $hr_point->{VALUE}, | ||
| 257 | ); | ||
| 258 | my $i_benchmark_value_id = $or_self->{query}->last_insert_id( | ||
| 259 | $hr_config->{tables}{benchmark_value_table}, | ||
| 260 | 'bench_value_id', | ||
| 261 | ); | ||
| 262 | $VALUE_ID = $i_benchmark_value_id; | ||
| 263 | |||
| 264 | ADDITIONAL: for my $s_key ( keys %{$hr_point} ) { | ||
| 265 | |||
| 266 | next ADDITIONAL if $s_key eq 'VALUE'; | ||
| 267 | next ADDITIONAL if not defined $hr_point->{$s_key}; | ||
| 268 | |||
| 269 | # additional type | ||
| 270 | my $i_addtype_id; | ||
| 271 | if ( $or_self->{cache} ) { | ||
| 272 | $i_addtype_id = $or_self->{cache}->get("addtype||$s_key"); | ||
| 273 | } | ||
| 274 | if ( !$i_addtype_id ) { | ||
| 275 | if ( | ||
| 276 | my $hr_addtype_select = $or_self->{query} | ||
| 277 | ->select_addtype( $s_key ) | ||
| 278 | ->fetchrow_hashref() | ||
| 279 | ) { | ||
| 280 | $i_addtype_id = $hr_addtype_select->{bench_additional_type_id}; | ||
| 281 | } | ||
| 282 | else { | ||
| 283 | $or_self->{query}->insert_addtype( | ||
| 284 | $s_key, | ||
| 285 | ); | ||
| 286 | $i_addtype_id = $or_self->{query}->last_insert_id( | ||
| 287 | $hr_config->{tables}{addition_type_table}, | ||
| 288 | 'bench_additional_type_id', | ||
| 289 | ); | ||
| 290 | } | ||
| 291 | if ( $or_self->{cache} ) { | ||
| 292 | $or_self->{cache}->set( "addtype||$s_key" => $i_addtype_id ); | ||
| 293 | } | ||
| 294 | } | ||
| 295 | |||
| 296 | # benchmark - additional type - relation | ||
| 297 | my $b_inserted = 0; | ||
| 298 | my $s_addtyperel = "$i_benchmark_id|$i_addtype_id"; | ||
| 299 | if ( $or_self->{cache} ) { | ||
| 300 | if ( $or_self->{cache}->get("addtyperel||$s_addtyperel") ) { | ||
| 301 | $b_inserted = 1; | ||
| 302 | } | ||
| 303 | } | ||
| 304 | if (! $b_inserted ) { | ||
| 305 | if(! | ||
| 306 | $or_self->{query} | ||
| 307 | ->select_addtyperelation( $i_benchmark_id, $i_addtype_id ) | ||
| 308 | ->fetchrow_hashref() | ||
| 309 | ) { | ||
| 310 | $or_self->{query} | ||
| 311 | ->insert_addtyperelation( $i_benchmark_id, $i_addtype_id ) | ||
| 312 | ; | ||
| 313 | } | ||
| 314 | if ( $or_self->{cache} ) { | ||
| 315 | $or_self->{cache}->set("addtyperel||$s_addtyperel" => 1 ); | ||
| 316 | } | ||
| 317 | } | ||
| 318 | |||
| 319 | # additional value | ||
| 320 | my $i_addvalue_id; | ||
| 321 | my $s_addvalue_key = "$i_addtype_id|$hr_point->{$s_key}"; | ||
| 322 | if ( $or_self->{cache} ) { | ||
| 323 | $i_addvalue_id = $or_self->{cache}->get("addvalue||$s_addvalue_key"); | ||
| 324 | } | ||
| 325 | if (! $i_addvalue_id ) { | ||
| 326 | if ( | ||
| 327 | my $hr_addvalue_select = $or_self->{query} | ||
| 328 | ->select_addvalue( $i_addtype_id, $hr_point->{$s_key} ) | ||
| 329 | ->fetchrow_hashref() | ||
| 330 | ) { | ||
| 331 | $i_addvalue_id = $hr_addvalue_select->{bench_additional_value_id}; | ||
| 332 | } | ||
| 333 | else { | ||
| 334 | $or_self->{query}->insert_addvalue( | ||
| 335 | $i_addtype_id, $hr_point->{$s_key}, | ||
| 336 | ); | ||
| 337 | $i_addvalue_id = $or_self->{query}->last_insert_id( | ||
| 338 | $hr_config->{tables}{addition_type_table}, | ||
| 339 | 'bench_additional_value_id', | ||
| 340 | ); | ||
| 341 | } | ||
| 342 | if ( $or_self->{cache} ) { | ||
| 343 | $or_self->{cache}->set( "addvalue||$s_addvalue_key" => $i_addvalue_id ); | ||
| 344 | } | ||
| 345 | } | ||
| 346 | |||
| 347 | # additional value relation | ||
| 348 | $or_self->{query}->insert_addvaluerelation( | ||
| 349 | $i_benchmark_value_id, $i_addvalue_id, | ||
| 350 | ); | ||
| 351 | |||
| 352 | } # ADDITIONAL | ||
| 353 | |||
| 354 | $i_counter++; | ||
| 355 | |||
| 356 | } | ||
| 357 | } | ||
| 358 | else { | ||
| 359 | require Carp; | ||
| 360 | Carp::cluck('no benchmark data found'); | ||
| 361 | return 0; | ||
| 362 | } | ||
| 363 | |||
| 364 | if ( $or_self->{searchengine}{elasticsearch}{index_single_added_values_immediately} ) | ||
| 365 | { | ||
| 366 | require BenchmarkAnything::Storage::Search::Elasticsearch; | ||
| 367 | my ($or_es, $s_index, $s_type) = BenchmarkAnything::Storage::Search::Elasticsearch::get_elasticsearch_client | ||
| 368 | ( | ||
| 369 | {searchengine => $or_self->{searchengine}, ownjson => 1} | ||
| 370 | ); | ||
| 371 | |||
| 372 | # Sic, we re-read from DB to get the very same data we | ||
| 373 | # *really got* stored, not just what we wish it should | ||
| 374 | # have stored. That gives us translations like | ||
| 375 | # num->string, CREATED date, etc., etc. | ||
| 376 | |||
| 377 | my $hr_bmk = $or_self->get_single_benchmark_point($VALUE_ID); | ||
| 378 | my $ret = $or_es->index(index => $s_index, | ||
| 379 | type => $s_type, | ||
| 380 | id => $VALUE_ID, | ||
| 381 | body => $hr_bmk); | ||
| 382 | } | ||
| 383 | |||
| 384 | return 1; | ||
| 385 | |||
| 386 | } | ||
| 387 | |||
| 388 | sub enqueue_multi_benchmark { | ||
| 389 | |||
| 390 | my ( $or_self, $ar_data_points, $hr_options ) = @_; | ||
| 391 | |||
| 392 | require Sereal::Encoder; | ||
| 393 | |||
| 394 | my $s_serialized = Sereal::Encoder->new->encode($ar_data_points); | ||
| 395 | $or_self->{query}->insert_raw_bench_bundle($s_serialized); | ||
| 396 | |||
| 397 | return 1; | ||
| 398 | |||
| 399 | } | ||
| 400 | |||
| 401 | # dequeues a single bundle (can contain multiple data points) | ||
| 402 | sub process_queued_multi_benchmark { | ||
| 403 | |||
| 404 | my ( $or_self, $bulkcount, $hr_options ) = @_; | ||
| 405 | |||
| 406 | my $i_id; | ||
| 407 | my $ar_results_lock; | ||
| 408 | my @a_bench_bundle_ids; | ||
| 409 | |||
| 410 | my $driver = $or_self->{query}{dbh}{Driver}{Name}; | ||
| 411 | |||
| 412 | $or_self->{query}{dbh}->do("set transaction isolation level read committed") if $driver eq "mysql"; # avoid deadlocks due to gap locking | ||
| 413 | $or_self->{query}->start_transaction; | ||
| 414 | |||
| 415 | # ===== exclusively pick single raw entry ===== | ||
| 416 | # Lock single row via processing=1 so that only one worker handles it! | ||
| 417 | eval { | ||
| 418 | try { | ||
| 419 | $ar_results_lock = $or_self->{query}->select_raw_bench_bundle_for_lock2($bulkcount); | ||
| 420 | @a_bench_bundle_ids = map { $_->[0] } @{$ar_results_lock->fetchall_arrayref()}; | ||
| 421 | } | ||
| 422 | catch { | ||
| 423 | if (/Deadlock found when trying to get lock/) { | ||
| 424 | # very normal, handled by eval{} and finish_transaction() in this sub. | ||
| 425 | # warn("IGNORED - DEADLOCK\n"); | ||
| 426 | die $_; | ||
| 427 | } elsif (/DBD::mysql::st fetchrow_hashref failed: fetch.. without execute/) { | ||
| 428 | # very normal with multiple workers, usually related | ||
| 429 | # to above deadlock. It is handled by eval{} and | ||
| 430 | # finish_transaction() in this sub. | ||
| 431 | warn("IGNORED - FETCH-WITHOUT-EXECUTE\n"); | ||
| 432 | die $_; | ||
| 433 | } else { | ||
| 434 | # An unexpected exception can happen anytime. It is | ||
| 435 | # handled by eval{} and finish_transaction() in this | ||
| 436 | # sub. Still, we print it to know what's happening. | ||
| 437 | require Carp; | ||
| 438 | Carp::cluck("SQL DATABASE EXCEPTION: {{{\n$_\n}}}\n"); | ||
| 439 | die $_; | ||
| 440 | } | ||
| 441 | }; | ||
| 442 | |||
| 443 | if (@a_bench_bundle_ids) { | ||
| 444 | |||
| 445 | my @a_serialized; | ||
| 446 | my $ar_results_process; | ||
| 447 | |||
| 448 | $ar_results_process = $or_self->{query}->select_raw_bench_bundle_for_processing2(@a_bench_bundle_ids); | ||
| 449 | @a_serialized = map { $_->[0] } @{$ar_results_process->fetchall_arrayref()}; | ||
| 450 | eval { | ||
| 451 | require Sereal::Decoder; | ||
| 452 | |||
| 453 | my @a_data_points = map { | ||
| 454 | @{Sereal::Decoder::decode_sereal($_) || []}; | ||
| 455 | } @a_serialized; | ||
| 456 | |||
| 457 | # please note, this reorders data points for efficiency, | ||
| 458 | # read the comments there. | ||
| 459 | # (maybe this bulk insert is broken - use with care, double check) | ||
| 460 | # $or_self->add_multi_benchmark(\@a_data_points, $hr_options); | ||
| 461 | |||
| 462 | # preserve order by adding each data_point separately, | ||
| 463 | # otherwise add_multi_benchmark() would reorder to optimize insert | ||
| 464 | foreach my $hr_data_point (@a_data_points) { | ||
| 465 | $or_self->add_multi_benchmark([$hr_data_point], $hr_options); | ||
| 466 | } | ||
| 467 | }; | ||
| 468 | if (!$@) { | ||
| 469 | $or_self->{query}->update_raw_bench_bundle_set_processed3(@a_bench_bundle_ids); | ||
| 470 | } | ||
| 471 | } | ||
| 472 | }; | ||
| 473 | |||
| 474 | $or_self->{query}->finish_transaction($@, { silent => 1 }); | ||
| 475 | |||
| 476 | $or_self->{query}{dbh}->do("set transaction isolation level repeatable read") if $driver eq "mysql"; # reset to normal gap locking | ||
| 477 | my $err = $@; | ||
| 478 | print STDERR "err: ".$err."\n" if $err && $or_self->{debug}; | ||
| 479 | return ($err ? () : @a_bench_bundle_ids); | ||
| 480 | |||
| 481 | } | ||
| 482 | |||
| 483 | # garbage collect - initially raw_bench_bundles, but also other stuff. | ||
| 484 | sub gc { | ||
| 485 | |||
| 486 | my ( $or_self, $hr_options ) = @_; | ||
| 487 | |||
| 488 | $or_self->{query}->delete_processed_raw_bench_bundles; | ||
| 489 | if ($or_self->{searchengine}{elasticsearch}{enable_query}) { | ||
| 490 | require BenchmarkAnything::Storage::Search::Elasticsearch; | ||
| 491 | my ($or_es, $s_index, $s_type) = BenchmarkAnything::Storage::Search::Elasticsearch::get_elasticsearch_client | ||
| 492 | ( | ||
| 493 | {searchengine => $or_self->{searchengine}} | ||
| 494 | ); | ||
| 495 | $or_es->indices->clear_cache(index => $s_index); | ||
| 496 | } | ||
| 497 | } | ||
| 498 | |||
| 499 | sub add_multi_benchmark { | ||
| 500 | |||
| 501 | # How ordering happens: | ||
| 502 | # | ||
| 503 | # We keep the order inside points of the same NAME and process | ||
| 504 | # NAMEs in order they appeared inside a group of processed | ||
| 505 | # bundles. | ||
| 506 | # | ||
| 507 | # However, if multiple reports were processed, where each report | ||
| 508 | # contains a metric A, B, and C, and we process 10 such reports at | ||
| 509 | # once, then the we get first 10 different As in order, then 10 | ||
| 510 | # different B in order, then 10 different C in order, BUT NOT: the | ||
| 511 | # 1st A, 1st B, 1st C, then 2nd A, 2nd B, 2nd C, etc. | ||
| 512 | |||
| 513 | my ( $or_self, $ar_data_points, $hr_options ) = @_; | ||
| 514 | |||
| 515 | my $i_counter = 1; | ||
| 516 | my %h_benchmarks = (); | ||
| 517 | my @a_name_order = (); | ||
| 518 | |||
| 519 | for my $hr_data_point ( @{$ar_data_points} ) { | ||
| 520 | |||
| 521 | for my $s_param (qw/ NAME VALUE /) { | ||
| 522 | if ( not exists $hr_data_point->{$s_param} ) { | ||
| 523 | require Carp; | ||
| 524 | if ( $hr_options->{force} ) { | ||
| 525 | Carp::cluck("missing parameter '$s_param' in element $i_counter"); | ||
| 526 | } | ||
| 527 | else { | ||
| 528 | Carp::confess("missing parameter '$s_param' in element $i_counter"); | ||
| 529 | } | ||
| 530 | } | ||
| 531 | } | ||
| 532 | |||
| 533 | my ( $s_name, $s_unit ) = delete @{$hr_data_point}{qw/ NAME UNIT /}; | ||
| 534 | |||
| 535 | if (! $h_benchmarks{$s_name} ) { | ||
| 536 | $h_benchmarks{$s_name} = { | ||
| 537 | NAME => $s_name, | ||
| 538 | UNIT => $s_unit, | ||
| 539 | data => [], | ||
| 540 | }; | ||
| 541 | push @a_name_order, $s_name; | ||
| 542 | } | ||
| 543 | else { | ||
| 544 | $h_benchmarks{$s_name}{UNIT} ||= $s_unit; | ||
| 545 | } | ||
| 546 | |||
| 547 | push @{$h_benchmarks{$s_name}{data}}, $hr_data_point; | ||
| 548 | |||
| 549 | $i_counter++; | ||
| 550 | |||
| 551 | } | ||
| 552 | |||
| 553 | foreach (@a_name_order) { | ||
| 554 | my $hr_benchmark = $h_benchmarks{$_}; | ||
| 555 | $or_self->add_single_benchmark( $hr_benchmark, $hr_options ); | ||
| 556 | } | ||
| 557 | |||
| 558 | return 1; | ||
| 559 | |||
| 560 | } | ||
| 561 | |||
| 562 | sub search { | ||
| 563 | |||
| 564 | my ( $or_self, $hr_search ) = @_; | ||
| 565 | |||
| 566 | return $or_self->{query}->select_benchmark_values( | ||
| 567 | $hr_search | ||
| 568 | ); | ||
| 569 | |||
| 570 | } | ||
| 571 | |||
| 572 | sub list_benchmark_names { | ||
| 573 | |||
| 574 | my ( $or_self, $s_pattern ) = @_; | ||
| 575 | |||
| 576 | my $ar_pattern = defined($s_pattern) ? [$s_pattern] : []; | ||
| 577 | |||
| 578 | my $s_key; | ||
| 579 | if ( $or_self->{cache} ) { | ||
| 580 | require JSON::XS; | ||
| 581 | $s_key = JSON::XS::encode_json($ar_pattern); | ||
| 582 | if ( my $ar_search_data = $or_self->{cache}->get("list_benchmark_names||$s_key") ) { | ||
| 583 | return $ar_search_data; | ||
| 584 | } | ||
| 585 | } | ||
| 586 | |||
| 587 | my $ar_result = $or_self->{query} | ||
| 588 | ->select_benchmark_names( @$ar_pattern ) | ||
| 589 | ->fetchall_arrayref([0]); | ||
| 590 | my $ar_benchmark_names = [ map { $_->[0] } @$ar_result ]; | ||
| 591 | |||
| 592 | if ( $or_self->{cache} ) { | ||
| 593 | $or_self->{cache}->set( "list_benchmark_names||$s_key" => $ar_benchmark_names ); | ||
| 594 | } | ||
| 595 | |||
| 596 | return $ar_benchmark_names; | ||
| 597 | |||
| 598 | } | ||
| 599 | |||
| 600 | sub list_additional_keys { | ||
| 601 | |||
| 602 | my ( $or_self, $s_pattern ) = @_; | ||
| 603 | |||
| 604 | my $ar_pattern = defined($s_pattern) ? [$s_pattern] : []; | ||
| 605 | |||
| 606 | my $s_key; | ||
| 607 | if ( $or_self->{cache} ) { | ||
| 608 | require JSON::XS; | ||
| 609 | $s_key = JSON::XS::encode_json($ar_pattern); | ||
| 610 | if ( my $ar_search_data = $or_self->{cache}->get("list_additional_keys||$s_key") ) { | ||
| 611 | return $ar_search_data; | ||
| 612 | } | ||
| 613 | } | ||
| 614 | |||
| 615 | my $ar_result = $or_self->{query} | ||
| 616 | ->select_additional_keys( @$ar_pattern ) | ||
| 617 | ->fetchall_arrayref([0]); | ||
| 618 | my $ar_key_names = [ map { $_->[0] } @$ar_result ]; | ||
| 619 | |||
| 620 | if ( $or_self->{cache} ) { | ||
| 621 | $or_self->{cache}->set( "list_additional_keys||$s_key" => $ar_key_names ); | ||
| 622 | } | ||
| 623 | |||
| 624 | return $ar_key_names; | ||
| 625 | |||
| 626 | } | ||
| 627 | |||
| 628 | sub get_stats { | ||
| 629 | |||
| 630 | my ( $or_self ) = @_; | ||
| 631 | |||
| 632 | my %h_searchengine_stats = (); | ||
| 633 | my %h_flat_searchengine_stats = (); | ||
| 634 | my %stats = (); | ||
| 635 | |||
| 636 | # Not strictly *stats* but useful information. | ||
| 637 | if ( $or_self->{searchengine}{elasticsearch}{index} ) | ||
| 638 | { | ||
| 639 | require BenchmarkAnything::Storage::Search::Elasticsearch; | ||
| 640 | my ($or_es, $s_index, $s_type) = BenchmarkAnything::Storage::Search::Elasticsearch::get_elasticsearch_client | ||
| 641 | ( | ||
| 642 | {searchengine => $or_self->{searchengine}} | ||
| 643 | ); | ||
| 644 | |||
| 645 | $stats{count_datapoints} = (map {chomp; $_} split(qr/ +/, $or_es->cat->count))[2]; | ||
| 646 | %h_searchengine_stats = | ||
| 647 | ( | ||
| 648 | index => $or_self->{searchengine}{elasticsearch}{index} || 'UNKNOWN', | ||
| 649 | type => $or_self->{searchengine}{elasticsearch}{type} || 'UNKNOWN', | ||
| 650 | enable_query => $or_self->{searchengine}{elasticsearch}{enable_query} || 0, | ||
| 651 | cluster_health => $or_es->cluster->health, | ||
| 652 | index_single_added_values_immediately => $or_self->{searchengine}{elasticsearch}{index_single_added_values_immediately} || 0, | ||
| 653 | ); | ||
| 654 | # boolean -> 0/1 | ||
| 655 | for (values %{$h_searchengine_stats{elasticsearch}{cluster_health}}) { | ||
| 656 | $_ = $_ ? 1 : 0 if ref eq 'JSON::XS::Boolean'; | ||
| 657 | } | ||
| 658 | $h_flat_searchengine_stats{"elasticsearch_$_"} = $h_searchengine_stats{$_} | ||
| 659 | for qw(index type enable_query index_single_added_values_immediately); | ||
| 660 | $h_flat_searchengine_stats{"elasticsearch_cluster_health_$_"} = $h_searchengine_stats{cluster_health}{$_} | ||
| 661 | for qw(cluster_name active_shards_percent_as_number active_primary_shards number_of_nodes status); | ||
| 662 | } | ||
| 663 | |||
| 664 | $stats{count_datapoints} ||= 0+$or_self->{query}->select_count_datapoints->fetch->[0]; | ||
| 665 | $stats{count_datapointkeys} = 0+$or_self->{query}->select_count_datapointkeys->fetch->[0] if $or_self->{verbose}; | ||
| 666 | $stats{count_metrics} = 0+$or_self->{query}->select_count_metrics->fetch->[0] if $or_self->{verbose}; | ||
| 667 | $stats{count_keys} = 0+$or_self->{query}->select_count_keys->fetch->[0] if $or_self->{verbose}; | ||
| 668 | |||
| 669 | %stats = (%stats, %h_flat_searchengine_stats); | ||
| 670 | |||
| 671 | return \%stats; | ||
| 672 | } | ||
| 673 | |||
| 674 | sub get_single_benchmark_point { | ||
| 675 | |||
| 676 | my ( $or_self, $i_bench_value_id ) = @_; | ||
| 677 | |||
| 678 | return {} unless $i_bench_value_id; | ||
| 679 | |||
| 680 | # cache? | ||
| 681 | my $s_key; | ||
| 682 | if ( $or_self->{cache} ) { | ||
| 683 | require JSON::XS; | ||
| 684 | $s_key = JSON::XS::encode_json({bench_value_id => $i_bench_value_id}); | ||
| 685 | if ( my $hr_search_data = $or_self->{cache}->get("get_single_benchmark_point||$s_key") ) { | ||
| 686 | return $hr_search_data; | ||
| 687 | } | ||
| 688 | } | ||
| 689 | |||
| 690 | # fetch all additional key/value fields | ||
| 691 | my $ar_query_result = $or_self->{query} | ||
| 692 | ->select_complete_benchmark_point( $i_bench_value_id ) | ||
| 693 | ->fetchall_arrayref({}); | ||
| 694 | |||
| 695 | # fetch essentials, like NAME, VALUE, UNIT | ||
| 696 | my $hr_essentials = $or_self->{query} | ||
| 697 | ->select_benchmark_point_essentials( $i_bench_value_id ) | ||
| 698 | ->fetchrow_hashref(); | ||
| 699 | |||
| 700 | # create complete BenchmarkAnything-like key/value entry | ||
| 701 | my $hr_result; | ||
| 702 | $hr_result = { map { ($_->{bench_additional_type} => $_->{bench_additional_value} ) } @$ar_query_result }; | ||
| 703 | $hr_result->{NAME} = $hr_essentials->{bench}; | ||
| 704 | $hr_result->{VALUE} = $hr_essentials->{bench_value}; | ||
| 705 | $hr_result->{VALUE_ID} = $hr_essentials->{bench_value_id}; | ||
| 706 | $hr_result->{CREATED} = $hr_essentials->{created_at}; | ||
| 707 | $hr_result->{UNIT} = $hr_essentials->{bench_unit} if $hr_essentials->{bench_unit}; | ||
| 708 | |||
| 709 | # cache! | ||
| 710 | if ( $or_self->{cache} ) { | ||
| 711 | $or_self->{cache}->set( "get_single_benchmark_point||$s_key" => $hr_result ); | ||
| 712 | } | ||
| 713 | |||
| 714 | return $hr_result; | ||
| 715 | } | ||
| 716 | |||
| 717 | sub get_full_benchmark_points { | ||
| 718 | |||
| 719 | my ( $or_self, $i_bench_value_id, $i_count ) = @_; | ||
| 720 | |||
| 721 | return [] unless $i_bench_value_id; | ||
| 722 | |||
| 723 | $i_count ||= 1; | ||
| 724 | |||
| 725 | # cache? | ||
| 726 | my $s_key; | ||
| 727 | if ( $or_self->{cache} ) { | ||
| 728 | require JSON::XS; | ||
| 729 | $s_key = JSON::XS::encode_json({bench_value_id => $i_bench_value_id}); | ||
| 730 | if ( my $hr_search_data = $or_self->{cache}->get("get_full_benchmark_points||$s_key") ) { | ||
| 731 | return $hr_search_data; | ||
| 732 | } | ||
| 733 | } | ||
| 734 | |||
| 735 | # fetch essentials, like NAME, VALUE, UNIT | ||
| 736 | my $ar_essentials = $or_self->{query} | ||
| 737 | ->select_multiple_benchmark_points_essentials($i_bench_value_id, $i_count) | ||
| 738 | ->fetchall_arrayref({}); | ||
| 739 | # additional key/value pairs | ||
| 740 | my $ar_additional_values = $or_self->{query} | ||
| 741 | ->select_multiple_benchmark_points_additionals($i_bench_value_id, $i_count) | ||
| 742 | ->fetchall_arrayref({}); | ||
| 743 | |||
| 744 | # map columns into BenchmarkAnything schema | ||
| 745 | my $hr_bmk; | ||
| 746 | foreach my $k (keys %$hr_column_ba_mapping) | ||
| 747 | { | ||
| 748 | my $K = $hr_column_ba_mapping->{$k}; | ||
| 749 | foreach my $e (@$ar_essentials) { | ||
| 750 | $hr_bmk->{$e->{bench_value_id}}{$K} = $e->{$k} if $k ne 'bench_unit' or defined $e->{$k}; | ||
| 751 | } | ||
| 752 | } | ||
| 753 | foreach (@$ar_additional_values) { | ||
| 754 | $hr_bmk->{$_->{bench_value_id}}{$_->{bench_additional_type}} = $_->{bench_additional_value}; | ||
| 755 | } | ||
| 756 | # sorted (by VALUE_ID) array of BenchmarkAnything entries | ||
| 757 | my @a_bmk = map { $hr_bmk->{$_} } sort keys %$hr_bmk; | ||
| 758 | |||
| 759 | # cache! | ||
| 760 | if ( $or_self->{cache} ) { | ||
| 761 | $or_self->{cache}->set( "get_full_benchmark_points||$s_key" => \@a_bmk ); | ||
| 762 | } | ||
| 763 | |||
| 764 | return \@a_bmk; | ||
| 765 | } | ||
| 766 | |||
| 767 | sub search_array { | ||
| 768 | |||
| 769 | my ( $or_self, $hr_search ) = @_; | ||
| 770 | |||
| 771 | my $debug = $or_self->{debug} || $or_self->{searchengine}{elasticsearch}{debug}; | ||
| 772 | |||
| 773 | my $s_key; | ||
| 774 | if ( $or_self->{cache} ) { | ||
| 775 | require JSON::XS; | ||
| 776 | $s_key = JSON::XS::encode_json($hr_search); | ||
| 777 | if ( my $ar_search_data = $or_self->{cache}->get("search_array||$s_key") ) { | ||
| 778 | return $ar_search_data; | ||
| 779 | } | ||
| 780 | } | ||
| 781 | |||
| 782 | if ( $debug ) | ||
| 783 | { | ||
| 784 | require JSON::XS; | ||
| 785 | require Data::Dumper; | ||
| 786 | print STDERR ',-------------------'."\n"; | ||
| 787 | print STDERR "benchmarkanything query:\n"; | ||
| 788 | print STDERR "benchmarkanything-storage search -d '\n"; | ||
| 789 | print STDERR JSON::XS->new->pretty->encode($hr_search); | ||
| 790 | print STDERR "'\n"; | ||
| 791 | print STDERR '`-------------------'."\n"; | ||
| 792 | } | ||
| 793 | |||
| 794 | if ( $or_self->{searchengine}{elasticsearch}{enable_query} ) | ||
| 795 | { | ||
| 796 | # If anything goes wrong with Elasticsearch we just continue | ||
| 797 | # below with relational backend query. | ||
| 798 | |||
| 799 | require BenchmarkAnything::Storage::Search::Elasticsearch; | ||
| 800 | my $hr_es_query = BenchmarkAnything::Storage::Search::Elasticsearch::get_elasticsearch_query($hr_search); | ||
| 801 | |||
| 802 | if ($debug) | ||
| 803 | { | ||
| 804 | require JSON::XS; | ||
| 805 | require Data::Dumper; | ||
| 806 | print STDERR ',-------------------'."\n"; | ||
| 807 | print STDERR "elasticsearch query:\n"; | ||
| 808 | print STDERR "curl -s -XGET 'http://localhost:9200/tapper/benchmarkanything/_search?pretty' -d '\n"; | ||
| 809 | print STDERR JSON::XS->new->pretty->encode($hr_es_query); | ||
| 810 | print STDERR "'\n"; | ||
| 811 | print STDERR '`-------------------'."\n"; | ||
| 812 | } | ||
| 813 | |||
| 814 | # If we could transform the query then we run it against Elasticsearch and return its result. | ||
| 815 | if (defined $hr_es_query) | ||
| 816 | { | ||
| 817 | # ===== client ===== | ||
| 818 | |||
| 819 | require BenchmarkAnything::Storage::Search::Elasticsearch; | ||
| 820 | my ($or_es, $s_index, $s_type) = BenchmarkAnything::Storage::Search::Elasticsearch::get_elasticsearch_client | ||
| 821 | ( | ||
| 822 | {searchengine => $or_self->{searchengine}, ownjson => 1} | ||
| 823 | ); | ||
| 824 | |||
| 825 | # ===== prepare ===== | ||
| 826 | |||
| 827 | # If sort fields are of type 'text' then those fields needs to | ||
| 828 | # get their properties being declared as "fielddata":true. | ||
| 829 | my $field_mapping = {}; | ||
| 830 | my @sort_fields = map {keys %$_} @{$hr_es_query->{sort}||[]}; | ||
| 831 | if (@sort_fields) { | ||
| 832 | $field_mapping = $or_es->indices->get_mapping->{$s_index}{mappings}{$s_type}{properties}; | ||
| 833 | } | ||
| 834 | foreach my $sort_field (@sort_fields) | ||
| 835 | { | ||
| 836 | if ($field_mapping->{$sort_field}{type} and $field_mapping->{$sort_field}{type} eq 'text') | ||
| 837 | { | ||
| 838 | require BenchmarkAnything::Storage::Backend::SQL::Search; | ||
| 839 | $or_es->indices->put_mapping | ||
| 840 | ( | ||
| 841 | index => $s_index, | ||
| 842 | type => $s_type, | ||
| 843 | body => { $s_type => { properties => { $sort_field => { type => 'text', | ||
| 844 | fielddata => BenchmarkAnything::Storage::Backend::SQL::Search::json_true(), | ||
| 845 | }}}} | ||
| 846 | ); | ||
| 847 | } | ||
| 848 | } | ||
| 849 | |||
| 850 | # ===== search ===== | ||
| 851 | my $hr_es_answer = $or_es->search(index => $s_index, type => $s_type, body => $hr_es_query); | ||
| 852 | |||
| 853 | if ( | ||
| 854 | !$hr_es_answer->{timed_out} and | ||
| 855 | !$hr_es_answer->{_shards}{failed} | ||
| 856 | ) | ||
| 857 | { | ||
| 858 | my @ar_es_result = map { $_->{_source} } @{$hr_es_answer->{hits}{hits} || []}; | ||
| 859 | return \@ar_es_result; | ||
| 860 | } | ||
| 861 | } else { | ||
| 862 | print STDERR "Did not get Elasticsearch query, fall back to SQL.\n" if $debug; | ||
| 863 | } | ||
| 864 | |||
| 865 | # Else no-op, continue with relational backend query. | ||
| 866 | } | ||
| 867 | |||
| 868 | my $ar_result = $or_self | ||
| 869 | ->search( $hr_search ) | ||
| 870 | ->fetchall_arrayref({}) | ||
| 871 | ; | ||
| 872 | |||
| 873 | if ( $or_self->{cache} ) { | ||
| 874 | $or_self->{cache}->set( "search_array||$s_key" => $ar_result ); | ||
| 875 | } | ||
| 876 | |||
| 877 | return $ar_result; | ||
| 878 | |||
| 879 | } | ||
| 880 | |||
| 881 | sub search_hash { | ||
| 882 | |||
| 883 | my ( $or_self, $hr_search ) = @_; | ||
| 884 | |||
| 885 | my $s_key; | ||
| 886 | if ( $or_self->{cache} ) { | ||
| 887 | require JSON::XS; | ||
| 888 | $s_key = JSON::XS::encode_json($hr_search); | ||
| 889 | if ( my $hr_search_data = $or_self->{cache}->get( "search_hash||$s_key" ) ) { | ||
| 890 | return $hr_search_data; | ||
| 891 | } | ||
| 892 | } | ||
| 893 | |||
| 894 | if (! $hr_search->{keys} ) { | ||
| 895 | require Carp; | ||
| 896 | Carp::confess(q#cannot get hash search result without 'keys'#); | ||
| 897 | return; | ||
| 898 | } | ||
| 899 | |||
| 900 | my $hr_result = $or_self | ||
| 901 | ->search( $hr_search ) | ||
| 902 | ->fetchall_hashref($hr_search->{keys}) | ||
| 903 | ; | ||
| 904 | |||
| 905 | if ( $or_self->{cache} ) { | ||
| 906 | $or_self->{cache}->set( "search_hash||$s_key" => $hr_result ) | ||
| 907 | } | ||
| 908 | |||
| 909 | return $hr_result; | ||
| 910 | |||
| 911 | } | ||
| 912 | |||
| 913 | sub subsume { | ||
| 914 | |||
| 915 | my ( $or_self, $hr_options ) = @_; | ||
| 916 | |||
| 917 | for my $s_parameter (qw/ subsume_type /) { | ||
| 918 | if (! $hr_options->{$s_parameter}) { | ||
| 919 | require Carp; | ||
| 920 | Carp::confess("missing parameter '$s_parameter'"); | ||
| 921 | return; | ||
| 922 | } | ||
| 923 | } | ||
| 924 | |||
| 925 | # check if subsume type exists | ||
| 926 | my $hr_subsume_type = $or_self->{query} | ||
| 927 | ->select_subsume_type( $hr_options->{subsume_type} ) | ||
| 928 | ->fetchrow_hashref() | ||
| 929 | ; | ||
| 930 | if (! $hr_subsume_type ) { | ||
| 931 | require Carp; | ||
| 932 | Carp::confess("subsume type '$hr_options->{subsume_type}' not exists"); | ||
| 933 | return; | ||
| 934 | } | ||
| 935 | if ( $hr_subsume_type->{bench_subsume_type_rank} == 1 ) { | ||
| 936 | require Carp; | ||
| 937 | Carp::confess("cannot subsume with type '$hr_options->{subsume_type}'"); | ||
| 938 | return; | ||
| 939 | } | ||
| 940 | |||
| 941 | # looking for values with with a higher rank subsume type | ||
| 942 | if ( | ||
| 943 | $or_self->{query} | ||
| 944 | ->select_check_subsumed_values({ | ||
| 945 | date_to => $hr_options->{date_to}, | ||
| 946 | date_from => $hr_options->{date_from}, | ||
| 947 | subsume_type_id => $hr_subsume_type->{bench_subsume_type_id}, | ||
| 948 | }) | ||
| 949 | ->rows() | ||
| 950 | ) { | ||
| 951 | require Carp; | ||
| 952 | Carp::confess( | ||
| 953 | "cannot use subsume type '$hr_options->{subsume_type}' " . | ||
| 954 | 'because a higher rank subsume type is already used for this date period' | ||
| 955 | ); | ||
| 956 | return; | ||
| 957 | } | ||
| 958 | |||
| 959 | # look if excluded additional types really exists | ||
| 960 | my @a_excluded_adds; | ||
| 961 | if ( $hr_options->{exclude_additionals} ) { | ||
| 962 | for my $s_additional_type ( @{$hr_options->{exclude_additionals}} ) { | ||
| 963 | if ( | ||
| 964 | my $hr_addtype = $or_self->{query} | ||
| 965 | ->select_addtype( $s_additional_type ) | ||
| 966 | ->fetchrow_hashref() | ||
| 967 | ) { | ||
| 968 | push @a_excluded_adds, $hr_addtype->{bench_additional_type_id} | ||
| 969 | } | ||
| 970 | else { | ||
| 971 | require Carp; | ||
| 972 | Carp::confess( "additional type '$s_additional_type' not exists" ); | ||
| 973 | return; | ||
| 974 | } | ||
| 975 | } | ||
| 976 | } | ||
| 977 | |||
| 978 | # get all data points for subsume | ||
| 979 | my $or_data_values = $or_self->{query}->select_data_values_for_subsume({ | ||
| 980 | date_to => $hr_options->{date_to}, | ||
| 981 | date_from => $hr_options->{date_from}, | ||
| 982 | exclude_additionals => \@a_excluded_adds, | ||
| 983 | subsume_type_id => $hr_subsume_type->{bench_subsume_type_id}, | ||
| 984 | }); | ||
| 985 | |||
| 986 | require DateTime::Format::Strptime; | ||
| 987 | my $or_strp = DateTime::Format::Strptime->new( pattern => '%F %T', ); | ||
| 988 | |||
| 989 | my @a_rows; | ||
| 990 | my $i_counter = 0; | ||
| 991 | my $i_sum_value = 0; | ||
| 992 | my $b_backup = ((not exists $hr_options->{backup}) || $hr_options->{backup}) ? 1 : 0; | ||
| 993 | my $s_last_key = q##; | ||
| 994 | |||
| 995 | while ( my $hr_values = $or_data_values->fetchrow_hashref() ) { | ||
| 996 | |||
| 997 | my $s_act_key = join '__', | ||
| 998 | $hr_values->{bench_id}, | ||
| 999 | $or_strp->parse_datetime( $hr_values->{created_at} )->strftime( $hr_subsume_type->{datetime_strftime_pattern} ), | ||
| 1000 | $hr_values->{additionals} || q##, | ||
| 1001 | ; | ||
| 1002 | |||
| 1003 | if ( $s_last_key ne $s_act_key ) { | ||
| 1004 | |||
| 1005 | if ( $i_counter ) { | ||
| 1006 | $or_self->$fn_add_subsumed_point({ | ||
| 1007 | rows => \@a_rows, | ||
| 1008 | VALUE => $i_sum_value / $i_counter, | ||
| 1009 | backup => $b_backup, | ||
| 1010 | type_id => $hr_subsume_type->{bench_subsume_type_id} | ||
| 1011 | }); | ||
| 1012 | } | ||
| 1013 | |||
| 1014 | @a_rows = (); | ||
| 1015 | $i_counter = 0; | ||
| 1016 | $i_sum_value = 0; | ||
| 1017 | $s_last_key = $s_act_key; | ||
| 1018 | |||
| 1019 | } | ||
| 1020 | |||
| 1021 | $i_counter += 1; | ||
| 1022 | $i_sum_value += $hr_values->{bench_value}; | ||
| 1023 | |||
| 1024 | push @a_rows, $hr_values; | ||
| 1025 | |||
| 1026 | } | ||
| 1027 | |||
| 1028 | if ( $i_counter ) { | ||
| 1029 | $or_self->$fn_add_subsumed_point({ | ||
| 1030 | rows => \@a_rows, | ||
| 1031 | VALUE => $i_sum_value / $i_counter, | ||
| 1032 | backup => $b_backup, | ||
| 1033 | type_id => $hr_subsume_type->{bench_subsume_type_id} | ||
| 1034 | }); | ||
| 1035 | } | ||
| 1036 | |||
| 1037 | return 1; | ||
| 1038 | |||
| 1039 | } | ||
| 1040 | |||
| 1041 | sub _get_additional_key_id { | ||
| 1042 | |||
| 1043 | my ( $or_self, $s_key ) = @_; | ||
| 1044 | |||
| 1045 | return $or_self->{query}->select_additional_key_id($s_key)->fetch->[0]; | ||
| 1046 | |||
| 1047 | } | ||
| 1048 | |||
| 1049 | sub default_columns { | ||
| 1050 | |||
| 1051 | my ( $or_self ) = @_; | ||
| 1052 | |||
| 1053 | return $or_self->{query}->default_columns; | ||
| 1054 | |||
| 1055 | } | ||
| 1056 | |||
| 1057 | sub benchmark_operators { | ||
| 1058 | |||
| 1059 | my ( $or_self ) = @_; | ||
| 1060 | |||
| 1061 | return $or_self->{query}->benchmark_operators; | ||
| 1062 | |||
| 1063 | } | ||
| 1064 | |||
| 1065 | sub init_search_engine | ||
| 1066 | { | ||
| 1067 | my ( $or_self, $b_force) = @_; | ||
| 1068 | |||
| 1069 | require BenchmarkAnything::Storage::Backend::SQL::Search; | ||
| 1070 | BenchmarkAnything::Storage::Backend::SQL::Search::init_search_engine (@_); | ||
| 1071 | } | ||
| 1072 | |||
| 1073 | sub sync_search_engine | ||
| 1074 | { | ||
| 1075 | require BenchmarkAnything::Storage::Backend::SQL::Search; | ||
| 1076 | BenchmarkAnything::Storage::Backend::SQL::Search::sync_search_engine (@_); | ||
| 1077 | } | ||
| 1078 | |||
| 1079 | 1; | ||
| 1080 | |||
| 1081 | __END__ | ||
| 1082 | |||
| 1083 | =pod | ||
| 1084 | |||
| 1085 | =encoding UTF-8 | ||
| 1086 | |||
| 1087 | =head1 NAME | ||
| 1088 | |||
| 1089 | BenchmarkAnything::Storage::Backend::SQL - Autonomous SQL backend to store benchmarks | ||
| 1090 | |||
| 1091 | =head1 SYNOPSIS | ||
| 1092 | |||
| 1093 | require YAML::Syck; | ||
| 1094 | require BenchmarkAnything::Storage::Backend::SQL; | ||
| 1095 | my $or_bench = BenchmarkAnything::Storage::Backend::SQL->new({ | ||
| 1096 | dbh => $or_dbh, | ||
| 1097 | debug => 0, | ||
| 1098 | config => YAML::Syck::LoadFile('~/conf/tapper_benchmark.conf'), | ||
| 1099 | }); | ||
| 1100 | |||
| 1101 | my $b_success = $or_bench->add_single_benchmark({ | ||
| 1102 | NAME => 'testbenchmark', | ||
| 1103 | UNIT => 'example unit', | ||
| 1104 | testplanid => 813, | ||
| 1105 | DATA => [ | ||
| 1106 | { | ||
| 1107 | VALUE => 123.45, | ||
| 1108 | testrun_id => 123, | ||
| 1109 | machine => 'mx1.small', | ||
| 1110 | benchmark_date => '2013-09-25 12:12:00', | ||
| 1111 | },{ | ||
| 1112 | VALUE => 122.88, | ||
| 1113 | testrun_id => 123, | ||
| 1114 | machine => 'mx1.large', | ||
| 1115 | benchmark_date => '2013-09-23 13:02:14', | ||
| 1116 | }, | ||
| 1117 | ... | ||
| 1118 | ], | ||
| 1119 | },{ | ||
| 1120 | force => 1, | ||
| 1121 | }); | ||
| 1122 | |||
| 1123 | my $b_success = $or_bench->add_multi_benchmark([ | ||
| 1124 | { | ||
| 1125 | NAME => 'testbenchmark', | ||
| 1126 | UNIT => 'example unit', | ||
| 1127 | VALUE => 123.45, | ||
| 1128 | testrun_id => 123, | ||
| 1129 | machine => 'mx1.small', | ||
| 1130 | benchmark_date => '2013-09-25 12:12:00', | ||
| 1131 | },{ | ||
| 1132 | NAME => 'testbenchmark', | ||
| 1133 | UNIT => 'example unit', | ||
| 1134 | VALUE => 122.88, | ||
| 1135 | testrun_id => 123, | ||
| 1136 | machine => 'mx1.large', | ||
| 1137 | benchmark_date => '2013-09-23 13:02:14', | ||
| 1138 | }, | ||
| 1139 | ... | ||
| 1140 | ],{ | ||
| 1141 | force => 1, | ||
| 1142 | }); | ||
| 1143 | |||
| 1144 | my $or_benchmark_points = $or_bench->search({ | ||
| 1145 | select => [ | ||
| 1146 | 'testrun_id', | ||
| 1147 | 'machine', | ||
| 1148 | ], | ||
| 1149 | where => [ | ||
| 1150 | ['!=', 'machine', 'mx1.small' ], | ||
| 1151 | ['=' , 'bench' , 'testbenchmark' ], | ||
| 1152 | ], | ||
| 1153 | order_by => [ | ||
| 1154 | 'machine', | ||
| 1155 | ['testrun_id','ASC',{ numeric => 1 }] | ||
| 1156 | ], | ||
| 1157 | limit => 2, | ||
| 1158 | offset => 1, | ||
| 1159 | }); | ||
| 1160 | |||
| 1161 | while my $hr_data_point ( $or_benchmark_points->fetchrow_hashref() ) { | ||
| 1162 | ... | ||
| 1163 | } | ||
| 1164 | |||
| 1165 | my $b_success = $or_bench->subsume({ | ||
| 1166 | subsume_type => 'month', | ||
| 1167 | exclude_additionals => [qw/ benchmark_date /], | ||
| 1168 | date_from => '2013-01-01 00:00:00', | ||
| 1169 | date_to => '2014-01-01 00:00:00', | ||
| 1170 | }); | ||
| 1171 | |||
| 1172 | =head1 DESCRIPTION | ||
| 1173 | |||
| 1174 | B<BenchmarkAnything::Storage::Backend::SQL> is a module for adding benchmark points in a standardised | ||
| 1175 | way to the the database. A search function with complexe filters already exists. | ||
| 1176 | |||
| 1177 | =head2 Class Methods | ||
| 1178 | |||
| 1179 | =head3 new | ||
| 1180 | |||
| 1181 | Create a new B<BenchmarkAnything::Storage::Backend::SQL> object. | ||
| 1182 | |||
| 1183 | my $or_bench = BenchmarkAnything::Storage::Backend::SQL->new({ | ||
| 1184 | dbh => $or_dbh, | ||
| 1185 | debug => 0, | ||
| 1186 | verbose=> 0, | ||
| 1187 | config => YAML::Syck::LoadFile('~/conf/tapper_benchmark.conf'), | ||
| 1188 | searchengine => ... # optional, see below at "Elasticsearch support" | ||
| 1189 | }); | ||
| 1190 | |||
| 1191 | =over 4 | ||
| 1192 | |||
| 1193 | =item dbh | ||
| 1194 | |||
| 1195 | A B<DBI> database handle. | ||
| 1196 | |||
| 1197 | =item config [optional] | ||
| 1198 | |||
| 1199 | Containing the path to the BenchmarkAnything::Storage::Backend::SQL-Configuration-File. See | ||
| 1200 | B<Configuration> for details. | ||
| 1201 | |||
| 1202 | =item debug [optional] | ||
| 1203 | |||
| 1204 | Setting C<debug> to a true value results in multiple debugging informations | ||
| 1205 | written to STDOUT. The default is 0. | ||
| 1206 | |||
| 1207 | =item verbose [optional] | ||
| 1208 | |||
| 1209 | Setting C<verbose> to a true value provides more logs or status | ||
| 1210 | information. The default is 0. | ||
| 1211 | |||
| 1212 | =back | ||
| 1213 | |||
| 1214 | =head3 add_single_benchmark | ||
| 1215 | |||
| 1216 | Add one or more data points to a single benchmark to the database. | ||
| 1217 | |||
| 1218 | my $b_success = $or_bench->add_single_benchmark({ | ||
| 1219 | NAME => 'testbenchmark', | ||
| 1220 | UNIT => 'example unit', | ||
| 1221 | data => [ | ||
| 1222 | { | ||
| 1223 | VALUE => 123.45, | ||
| 1224 | },{ | ||
| 1225 | VALUE => 122.88, | ||
| 1226 | testrun_id => 123, | ||
| 1227 | machine => 'mx1.large', | ||
| 1228 | benchmark_date => '2013-09-23 13:02:14', | ||
| 1229 | },{ | ||
| 1230 | VALUE => 122.88, | ||
| 1231 | testrun_id => 123, | ||
| 1232 | }, | ||
| 1233 | ... | ||
| 1234 | ], | ||
| 1235 | },{ | ||
| 1236 | force => 1 | ||
| 1237 | }); | ||
| 1238 | |||
| 1239 | =over 4 | ||
| 1240 | |||
| 1241 | =item 1st Parameter Hash => NAME | ||
| 1242 | |||
| 1243 | The name of the benchmark for grouping benchmark data points. | ||
| 1244 | |||
| 1245 | =item 1st Parameter Hash => data | ||
| 1246 | |||
| 1247 | This parameter contains the benchmark data points. It's an array of hashes. The | ||
| 1248 | element C<VALUE> is the only required element in this hashes. The C<VALUE> is | ||
| 1249 | the benchmark data point value. | ||
| 1250 | |||
| 1251 | =item 1st Parameter Hash => UNIT [optional] | ||
| 1252 | |||
| 1253 | Containing a unit for benchmark data point values. | ||
| 1254 | |||
| 1255 | =item 2nd Parameter Hash => force [optional] | ||
| 1256 | |||
| 1257 | Ignore forgivable errors while writing. | ||
| 1258 | |||
| 1259 | =back | ||
| 1260 | |||
| 1261 | =head3 add_multi_benchmark | ||
| 1262 | |||
| 1263 | Add one or more data points for multiple benchmarks to the database. | ||
| 1264 | |||
| 1265 | my $b_success = $or_bench->add_multi_benchmark([ | ||
| 1266 | { | ||
| 1267 | NAME => 'testbenchmark 1', | ||
| 1268 | UNIT => undef, | ||
| 1269 | VALUE => 123.45, | ||
| 1270 | },{ | ||
| 1271 | NAME => 'testbenchmark 2', | ||
| 1272 | VALUE => 122.88, | ||
| 1273 | testrun_id => 123, | ||
| 1274 | machine => 'mx1.large', | ||
| 1275 | benchmark_date => '2013-09-23 13:02:14', | ||
| 1276 | },{ | ||
| 1277 | NAME => 'testbenchmark 1', | ||
| 1278 | UNIT => 'example unit', | ||
| 1279 | VALUE => 122.88, | ||
| 1280 | testrun_id => 123, | ||
| 1281 | }, | ||
| 1282 | ... | ||
| 1283 | ],{ | ||
| 1284 | force => 1 | ||
| 1285 | }); | ||
| 1286 | |||
| 1287 | =over 4 | ||
| 1288 | |||
| 1289 | =item 1st Parameter Array of Hashes => NAME | ||
| 1290 | |||
| 1291 | The name of the benchmark for grouping benchmark data points. | ||
| 1292 | |||
| 1293 | =item 1st Parameter Hash => VALUE | ||
| 1294 | |||
| 1295 | The value is the benchmark data point value. | ||
| 1296 | |||
| 1297 | =item 1st Parameter Hash => UNIT [optional] | ||
| 1298 | |||
| 1299 | Containing a unit for benchmark data point values. | ||
| 1300 | |||
| 1301 | =item 1st Parameter Hash => all others | ||
| 1302 | |||
| 1303 | All other elements in the hashes are additional values added to this data point. | ||
| 1304 | |||
| 1305 | =item 2nd Parameter Hash => force [optional] | ||
| 1306 | |||
| 1307 | Ignore forgivable errors while writing. | ||
| 1308 | |||
| 1309 | =back | ||
| 1310 | |||
| 1311 | =head3 search | ||
| 1312 | |||
| 1313 | Search for benchmark data points in the database. Function returns a DBI | ||
| 1314 | Statement Handle. | ||
| 1315 | |||
| 1316 | my $or_benchmark_points = $or_bench->search({ | ||
| 1317 | select => [ | ||
| 1318 | 'testrun_id', | ||
| 1319 | 'machine', | ||
| 1320 | ], | ||
| 1321 | where => [ | ||
| 1322 | ['!=', 'machine', 'mx1.small' ], | ||
| 1323 | ['=' , 'NAME' , 'testbenchmark' ], | ||
| 1324 | ], | ||
| 1325 | where_sql => q#, | ||
| 1326 | AND NOT( | ||
| 1327 | ${testrun_id} = 123 | ||
| 1328 | OR ${VALUE} = '144' | ||
| 1329 | ) | ||
| 1330 | #, | ||
| 1331 | limit => 2, | ||
| 1332 | offset => 1, | ||
| 1333 | order_by => [ | ||
| 1334 | 'machine', | ||
| 1335 | ['testrun_id','ASC'] | ||
| 1336 | ], | ||
| 1337 | }); | ||
| 1338 | |||
| 1339 | =over 4 | ||
| 1340 | |||
| 1341 | =item select [optional] | ||
| 1342 | |||
| 1343 | An Array of Strings or Array References containing additional selected columns. | ||
| 1344 | The default selected columns are: | ||
| 1345 | NAME - name of benchmark | ||
| 1346 | UNIT - benchmark unit [optional] | ||
| 1347 | VALUE - value of benchmark data point | ||
| 1348 | VALUE_ID - unique benchmark data point identifier | ||
| 1349 | CREATED - benchmark data point created date in format YYYY-MM-DD HH:II:SS | ||
| 1350 | |||
| 1351 | Add additional data "testrun_id" and "machine" as columns to selection. | ||
| 1352 | |||
| 1353 | ... | ||
| 1354 | select => [ | ||
| 1355 | 'testrun_id', | ||
| 1356 | 'machine', | ||
| 1357 | ], | ||
| 1358 | ... | ||
| 1359 | |||
| 1360 | Do the same as above. | ||
| 1361 | |||
| 1362 | ... | ||
| 1363 | select => [ | ||
| 1364 | ['','testrun_id'], | ||
| 1365 | ['','machine'], | ||
| 1366 | ], | ||
| 1367 | ... | ||
| 1368 | |||
| 1369 | Get the maximum "testrun_id" of all selected data points. All other columns | ||
| 1370 | without an aggregation become the C<default_aggregation> from | ||
| 1371 | BenchmarkAnything::Storage::Backend::SQL-Configuration. Possible aggregation types are: | ||
| 1372 | |||
| 1373 | - min = minimum | ||
| 1374 | - max = maximum | ||
| 1375 | - avg = average | ||
| 1376 | - gem = geometric mean | ||
| 1377 | - sum = summary | ||
| 1378 | - cnt = count | ||
| 1379 | - cnd = distinct value count | ||
| 1380 | |||
| 1381 | ... | ||
| 1382 | select => [ | ||
| 1383 | ['max','testrun_id'], | ||
| 1384 | 'machine', | ||
| 1385 | ], | ||
| 1386 | ... | ||
| 1387 | |||
| 1388 | A aggregation is also possible for the default columns. | ||
| 1389 | |||
| 1390 | ... | ||
| 1391 | select => [ | ||
| 1392 | ['max','testrun_id'], | ||
| 1393 | ['avg','VALUE'], | ||
| 1394 | ], | ||
| 1395 | ... | ||
| 1396 | |||
| 1397 | =item where [optional] | ||
| 1398 | |||
| 1399 | An Array of Array References containing restrictions for benchmark data points. | ||
| 1400 | |||
| 1401 | ... | ||
| 1402 | where => [ | ||
| 1403 | ['!=', 'machine', 'mx1.small' ], | ||
| 1404 | ['=' , 'NAME' , 'testbenchmark' ], | ||
| 1405 | ], | ||
| 1406 | ... | ||
| 1407 | |||
| 1408 | 1. Parameter in Sub-Array = restriction operator | ||
| 1409 | |||
| 1410 | = - equal | ||
| 1411 | != - not equal | ||
| 1412 | < - lower | ||
| 1413 | > - greater | ||
| 1414 | <= - lower equal | ||
| 1415 | >= - greater equal | ||
| 1416 | like - SQL LIKE | ||
| 1417 | not_like - SQL NOT LIKE | ||
| 1418 | is_empty - empty string or undef or null | ||
| 1419 | |||
| 1420 | 2. Parameter in Sub-Array = restricted column | ||
| 1421 | |||
| 1422 | A restriction is possible for additional values and the default columns. | ||
| 1423 | |||
| 1424 | 3 - n. Parameters in Sub-Array = value for restriction | ||
| 1425 | |||
| 1426 | In general there is just a single value. For '=' and '!=' a check for multiple | ||
| 1427 | values is possible. In SQL it is implemented with IN and NOT IN. | ||
| 1428 | |||
| 1429 | =item where_sql [optional] | ||
| 1430 | |||
| 1431 | A String containing an additional where clause. Please use this feature just if | ||
| 1432 | the "where" parameter is not sufficient to restrict. | ||
| 1433 | |||
| 1434 | =item order_by [optional] | ||
| 1435 | |||
| 1436 | An Array of Strings or an Array of Array References determining the order of | ||
| 1437 | returned benchmark data points. | ||
| 1438 | |||
| 1439 | Array of Strings: | ||
| 1440 | column to sort with default order direction "ASC" (ascending) | ||
| 1441 | |||
| 1442 | Array of Array References | ||
| 1443 | 1. Element: column to sort | ||
| 1444 | 2. Element: order direction with possible values "ASC" (ascending) and "DESC" (descending) | ||
| 1445 | 3. Element: hash of additional options. Possible values: | ||
| 1446 | numeric: Set a true value for a numeric sort | ||
| 1447 | |||
| 1448 | ... | ||
| 1449 | order_by => [ | ||
| 1450 | 'machine', | ||
| 1451 | ['benchmark_date','DESC'] | ||
| 1452 | ['testrun_id','ASC',{numeric => 1}] | ||
| 1453 | ], | ||
| 1454 | ... | ||
| 1455 | |||
| 1456 | =item limit [optional] | ||
| 1457 | |||
| 1458 | An integer value which determine the number of returned benchmark data points. | ||
| 1459 | |||
| 1460 | =item offset [optional] | ||
| 1461 | |||
| 1462 | An integer value which determine the number of omitted benchmark data points. | ||
| 1463 | |||
| 1464 | =back | ||
| 1465 | |||
| 1466 | =head3 search_array | ||
| 1467 | |||
| 1468 | Returning all benchmark data points as Array of Hashes. | ||
| 1469 | |||
| 1470 | my $or_benchmark_points = $or_bench->search_array({ | ||
| 1471 | select => [ | ||
| 1472 | 'testrun_id', | ||
| 1473 | 'machine', | ||
| 1474 | ], | ||
| 1475 | where => [ | ||
| 1476 | ['!=', 'machine', 'mx1.small' ], | ||
| 1477 | ['=' , 'NAME' , 'testbenchmark' ], | ||
| 1478 | ], | ||
| 1479 | limit => 2, | ||
| 1480 | offset => 1, | ||
| 1481 | order_by => [ | ||
| 1482 | 'machine', | ||
| 1483 | ['testrun_id','ASC'] | ||
| 1484 | ], | ||
| 1485 | }); | ||
| 1486 | |||
| 1487 | =head3 search_hash | ||
| 1488 | |||
| 1489 | Returning all benchmark data points as Hash of Hashes. As compared to search | ||
| 1490 | C<search_array> this function needs the parameter C<keys>. C<keys> is an Array | ||
| 1491 | of Strings which determine the columns used as the keys for the nested hashes. | ||
| 1492 | Every "key" create a new nested hash. | ||
| 1493 | |||
| 1494 | my $or_benchmark_points = $or_bench->search_array({ | ||
| 1495 | keys => [ | ||
| 1496 | 'testrun_id', | ||
| 1497 | 'machine', | ||
| 1498 | 'VALUE_ID', | ||
| 1499 | ], | ||
| 1500 | select => [ | ||
| 1501 | 'testrun_id', | ||
| 1502 | 'machine', | ||
| 1503 | ], | ||
| 1504 | where => [ | ||
| 1505 | ['!=', 'machine', 'mx1.small' ], | ||
| 1506 | ['=', 'NAME' , 'testbenchmark' ], | ||
| 1507 | ['like', 'some_key', 'some%value' ], | ||
| 1508 | ['not_like', 'another_key', 'another%value' ], | ||
| 1509 | ['is_empty', 'parameter1', 1 ], # check parameter1 is empty - Elasticsearch backend only | ||
| 1510 | ['is_empty', 'parameter2', 0 ], # check parameter2 is not empty - Elasticsearch backend only | ||
| 1511 | ], | ||
| 1512 | limit => 2, | ||
| 1513 | offset => 1, | ||
| 1514 | order_by => [ | ||
| 1515 | 'machine', | ||
| 1516 | ['testrun_id','ASC'] | ||
| 1517 | ], | ||
| 1518 | }); | ||
| 1519 | |||
| 1520 | =head3 get_stats | ||
| 1521 | |||
| 1522 | Returns a hash with info about the storage, like how many data points, | ||
| 1523 | how many metrics, how many additional keys, are stored. | ||
| 1524 | |||
| 1525 | my $stats = $or_bench->get_stats(); | ||
| 1526 | |||
| 1527 | =head3 get_single_benchmark_point | ||
| 1528 | |||
| 1529 | Get a single data point from the database including all essential | ||
| 1530 | fields (NAME, VALUE, UNIT, VALUE_ID, CREATED) and all additional | ||
| 1531 | fields. | ||
| 1532 | |||
| 1533 | my $point = $or_bench->get_single_benchmark_point($value_id); | ||
| 1534 | |||
| 1535 | =head3 get_full_benchmark_points | ||
| 1536 | |||
| 1537 | Get C<$count> data points from the database including all essential | ||
| 1538 | fields (NAME, VALUE, UNIT, VALUE_ID, CREATED) and all additional | ||
| 1539 | fields, beginning with C<$value_id>. | ||
| 1540 | |||
| 1541 | my $point = $or_bench->get_full_benchmark_points($value_id, $count); | ||
| 1542 | |||
| 1543 | =head3 list_benchmark_names | ||
| 1544 | |||
| 1545 | Get a list of all benchmark NAMEs, optionally matching a given pattern | ||
| 1546 | (SQL LIKE syntax, i.e., using C<%> as placeholder. | ||
| 1547 | |||
| 1548 | $benchmarkanythingdata = $or_bench->list_benchmark_names($pattern); | ||
| 1549 | |||
| 1550 | =head3 list_additional_keys | ||
| 1551 | |||
| 1552 | Get a list of all additional key names, optionally matching a given | ||
| 1553 | pattern (SQL LIKE syntax, i.e., using C<%> as placeholder. | ||
| 1554 | |||
| 1555 | $benchmarkanythingdata = $or_bench->list_additional_keys($pattern); | ||
| 1556 | |||
| 1557 | =head3 enqueue_multi_benchmark | ||
| 1558 | |||
| 1559 | As a low-latency alternative to directly calling | ||
| 1560 | L</add_multi_benchmark> there is a queuing functionality. | ||
| 1561 | |||
| 1562 | The C<enqueue_multi_benchmark> function simply writes the raw incoming | ||
| 1563 | data structure serialized (and compressed) into a single row and | ||
| 1564 | returns. The complementary function to this is | ||
| 1565 | C<process_queued_multi_benchmark> which takes these values over using | ||
| 1566 | the real C<add_multi_benchmark> internally. | ||
| 1567 | |||
| 1568 | =head3 process_queued_multi_benchmark | ||
| 1569 | |||
| 1570 | This is part 2 of the low-latency queuing alternative to directly | ||
| 1571 | calling L</add_multi_benchmark>. | ||
| 1572 | |||
| 1573 | It transactionally marks a single raw entry as being processed and | ||
| 1574 | then takes over its values by calling C<add_multi_benchmark>. It | ||
| 1575 | preserves the order of entries by inserting each chunk sequentially, | ||
| 1576 | to not confuse the IDs to the careful reader. After the bundle is | ||
| 1577 | taken over it is marked as processed. | ||
| 1578 | |||
| 1579 | This function only handles one single raw entry. It is expected to | ||
| 1580 | called from co-operating multiple worker tasks or multiple times from | ||
| 1581 | a wrapper. | ||
| 1582 | |||
| 1583 | Currently the original raw values are B<not> deleted immediately, just | ||
| 1584 | for safety reasons, until the transactional code is death-proof (and | ||
| 1585 | certified by Stuntman Mike). There is a dedicated funtion L/gc> for | ||
| 1586 | that cleanup. | ||
| 1587 | |||
| 1588 | The function returns the ID of the processed raw entry. | ||
| 1589 | |||
| 1590 | =head3 gc | ||
| 1591 | |||
| 1592 | This calls garbage collection, in particular deletes raw entries | ||
| 1593 | created by C<process_queued_multi_benchmark> and already processed by | ||
| 1594 | C<process_queued_multi_benchmark>. | ||
| 1595 | |||
| 1596 | It is separated from those processing just for safety reasons until | ||
| 1597 | the transactional code in there is waterproof. | ||
| 1598 | |||
| 1599 | The gc function can cleanup more stuff in the future. | ||
| 1600 | |||
| 1601 | =head3 subsume | ||
| 1602 | |||
| 1603 | This is a maintenance function for reducing the number of data points in the | ||
| 1604 | database. Calling this function reduces the rows in the benchmark values table | ||
| 1605 | by building an average value for all benchmark data points grouped by specfic | ||
| 1606 | columns. By default all old grouped columns will be added to backup tables for | ||
| 1607 | rebuilding the original state. | ||
| 1608 | It is highly recommended to do this periodically for better search performance. | ||
| 1609 | |||
| 1610 | my $b_success = $or_bench->subsume({ | ||
| 1611 | subsume_type => 'month', | ||
| 1612 | exclude_additionals => [qw/ benchmark_date /], | ||
| 1613 | date_from => '2013-01-01 00:00:00', | ||
| 1614 | date_to => '2014-01-01 00:00:00', | ||
| 1615 | backup => 0, | ||
| 1616 | }); | ||
| 1617 | |||
| 1618 | =over 4 | ||
| 1619 | |||
| 1620 | =item subsume_type | ||
| 1621 | |||
| 1622 | The subsume of benchmark data points is made by group with the following | ||
| 1623 | elements: | ||
| 1624 | |||
| 1625 | - bench_id | ||
| 1626 | - additional data values ( Example: testrun_id, machine ) | ||
| 1627 | - specific data range ( subsume_type ). | ||
| 1628 | The possible subsume types are stored in the | ||
| 1629 | extrapolation_type_table ( BenchmarkAnything::Storage::Backend::SQL-Configuration ). By default there | ||
| 1630 | are the following types: "second", "minute", "hour", "day", "week", "month", | ||
| 1631 | "year". | ||
| 1632 | |||
| 1633 | =item date_from | ||
| 1634 | |||
| 1635 | Begin of subsume period. | ||
| 1636 | |||
| 1637 | =item date_to | ||
| 1638 | |||
| 1639 | End of subsume period. | ||
| 1640 | |||
| 1641 | =item exclude_additionals | ||
| 1642 | |||
| 1643 | Array Reference of additional values that should be excluded from grouping. | ||
| 1644 | |||
| 1645 | =item backup | ||
| 1646 | |||
| 1647 | By default all subsumed rows will be inserted to backup tables. If this | ||
| 1648 | isn't desired a false value must be passed. | ||
| 1649 | |||
| 1650 | =back | ||
| 1651 | |||
| 1652 | =head3 init_search_engine( $force ) | ||
| 1653 | |||
| 1654 | Initializes the configured search engine (Elasticsearch). If the index | ||
| 1655 | already exists it does nothing, except when you set C<$force> to a | ||
| 1656 | true value which deletes and re-creates the index. This is necessary | ||
| 1657 | for example to apply new type mappings. | ||
| 1658 | |||
| 1659 | After a successful (re-)init you need to run C<sync_search_engine>. | ||
| 1660 | |||
| 1661 | During (re-init) and sync you should disable querying by setting | ||
| 1662 | |||
| 1663 | searchengine.elasticsearch.enable_query: 0 | ||
| 1664 | |||
| 1665 | =head3 sync_search_engine( $force, $start, $count) | ||
| 1666 | |||
| 1667 | Sync C<$count> (default 10000) entries from the relational backend | ||
| 1668 | into the search engine (Elasticsearch) for indexing, beginning at | ||
| 1669 | C<$start> (default 1). Already existing entries in Elasticsearch are | ||
| 1670 | skipped unless C<$force> is set to a true value. | ||
| 1671 | |||
| 1672 | =head1 NAME | ||
| 1673 | |||
| 1674 | BenchmarkAnything::Storage::Backend::SQL - Save and search benchmark points by database | ||
| 1675 | |||
| 1676 | =head1 Configuration | ||
| 1677 | |||
| 1678 | The following elements are required in configuration: | ||
| 1679 | |||
| 1680 | =over 4 | ||
| 1681 | |||
| 1682 | =item default_aggregation | ||
| 1683 | |||
| 1684 | Default aggregation used for non aggregated columns if an aggregation on any | ||
| 1685 | other column is found. | ||
| 1686 | |||
| 1687 | =item tables | ||
| 1688 | |||
| 1689 | Containing the names of the tables used bei B<BenchmarkAnything::Storage::Backend::SQL> | ||
| 1690 | |||
| 1691 | tables => { | ||
| 1692 | unit_table => 'bench_units', | ||
| 1693 | benchmark_table => 'benchs', | ||
| 1694 | benchmark_value_table => 'bench_values', | ||
| 1695 | subsume_type_table => 'bench_subsume_types', | ||
| 1696 | benchmark_backup_value_table => 'bench_backup_values', | ||
| 1697 | additional_type_table => 'bench_additional_types', | ||
| 1698 | additional_value_table => 'bench_additional_values', | ||
| 1699 | additional_relation_table => 'bench_additional_relations', | ||
| 1700 | additional_type_relation_table => 'bench_additional_type_relations', | ||
| 1701 | backup_additional_relation_table => 'bench_backup_additional_relations', | ||
| 1702 | } | ||
| 1703 | |||
| 1704 | =item select_cache [optional] | ||
| 1705 | |||
| 1706 | In case of a true value the module cache some select results | ||
| 1707 | |||
| 1708 | =back | ||
| 1709 | |||
| 1710 | =head3 default_columns | ||
| 1711 | |||
| 1712 | Returns the hash about those columns that are by default part of each | ||
| 1713 | single data point (NAME, UNIT, VALU, VALUE_ID, CREATED, each with its | ||
| 1714 | internal column name). These default columns might go away in the | ||
| 1715 | future, but for now some systems need this internal information. | ||
| 1716 | |||
| 1717 | =head3 benchmark_operators | ||
| 1718 | |||
| 1719 | Returns the list of operators supported by the query language. This is | ||
| 1720 | provided for frontend systems that support creating queries | ||
| 1721 | automatically. | ||
| 1722 | |||
| 1723 | =head3 json_true | ||
| 1724 | |||
| 1725 | Auxiliary function for Elasticsearch JSON data. | ||
| 1726 | |||
| 1727 | =head3 json_false | ||
| 1728 | |||
| 1729 | Auxiliary function for Elasticsearch JSON data. | ||
| 1730 | |||
| 1731 | =head2 Elasticsearch support | ||
| 1732 | |||
| 1733 | =head3 Config | ||
| 1734 | |||
| 1735 | You can pass through a config entry for an external search engine | ||
| 1736 | (currently only Elasticsearch) to the constructor: | ||
| 1737 | |||
| 1738 | my $or_bench = BenchmarkAnything::Storage::Backend::SQL->new({ | ||
| 1739 | dbh => $or_dbh, | ||
| 1740 | searchengine => { | ||
| 1741 | elasticsearch => | ||
| 1742 | # | ||
| 1743 | # which index/type to use | ||
| 1744 | index => "myapp", | ||
| 1745 | type => "benchmarkanything", | ||
| 1746 | # | ||
| 1747 | # queries use the searchengine | ||
| 1748 | enable_query => 1, | ||
| 1749 | # | ||
| 1750 | # should each single added value be stored immediately | ||
| 1751 | # (maybe there is another bulk sync mechanism) | ||
| 1752 | index_single_added_values_immediately => 1, | ||
| 1753 | # | ||
| 1754 | # which nodes to use | ||
| 1755 | nodes => [ 'localhost:9200' ], | ||
| 1756 | # | ||
| 1757 | # (OPTIONAL) | ||
| 1758 | # Your additional application specific mappings, used when | ||
| 1759 | # index is created. | ||
| 1760 | # | ||
| 1761 | # WARNING: You are allowed to overwrite the pre-defined | ||
| 1762 | # defaults for the built-in fields (NAME, VALUE, VALUE_ID, | ||
| 1763 | # UNIT, CREATED) as it is a RIGHT_PRECEDENT hash merge with | ||
| 1764 | # your additional_mappings on the right side. So if you | ||
| 1765 | # touch the internal fields you better know what you are | ||
| 1766 | # doing. | ||
| 1767 | additional_mappings => { | ||
| 1768 | # type as defined above in elasticsearch.type | ||
| 1769 | benchmarkanything => { | ||
| 1770 | # static key <properties> | ||
| 1771 | properties => { | ||
| 1772 | # field | ||
| 1773 | tapper_report => { | ||
| 1774 | type => long, | ||
| 1775 | }, | ||
| 1776 | tapper_testrun => { | ||
| 1777 | type => long, | ||
| 1778 | }, | ||
| 1779 | tapper_testplan => { | ||
| 1780 | type => long, | ||
| 1781 | }, | ||
| 1782 | }, | ||
| 1783 | }, | ||
| 1784 | }, | ||
| 1785 | }, | ||
| 1786 | }); | ||
| 1787 | |||
| 1788 | With such a config and an already set up Elasticsearch you can use the | ||
| 1789 | lib as usual but it is handling all things transparently behind the | ||
| 1790 | scenes to index and query the data with Elasticsearch. The relational | ||
| 1791 | SQL storage is still used as the primary storage. | ||
| 1792 | |||
| 1793 | =head3 Index | ||
| 1794 | |||
| 1795 | When C<index_single_added_values_immediately> is set, every single | ||
| 1796 | added entry is fetched right after insert (to get all transformations | ||
| 1797 | and added metadata) and sent to elasticsearch for index. | ||
| 1798 | |||
| 1799 | Please note, this immediate indexing adds an overhead to insert | ||
| 1800 | time. You could as well switch-off this setting and take care of | ||
| 1801 | indexing the data at another time. Then again, for instance the | ||
| 1802 | C<::Frontend::HTTP> already takes care of bulk-adding new data | ||
| 1803 | asynchronously, so the overhead should be hidden in there, so just | ||
| 1804 | switch-on the feature and don't worry too much. | ||
| 1805 | |||
| 1806 | =head3 Search | ||
| 1807 | |||
| 1808 | When C<enable_query> is set, the BenchmarkAnything queries are | ||
| 1809 | transformed into corresponding Elasticsearch queries, sent to | ||
| 1810 | Elastisearch, and the result is taken directly from its answers. | ||
| 1811 | |||
| 1812 | =head1 AUTHOR | ||
| 1813 | |||
| 1814 | Roberto Schaefer <schaefr@amazon.com> | ||
| 1815 | |||
| 1816 | =head1 COPYRIGHT AND LICENSE | ||
| 1817 | |||
| 1818 | This software is Copyright (c) 2020 by Amazon.com, Inc. or its affiliates. | ||
| 1819 | |||
| 1820 | This is free software, licensed under: | ||
| 1821 | |||
| 1822 | The (two-clause) FreeBSD License | ||
| 1823 | |||
| 1824 | =cut |