Class: TDriver::Hooking

Inherits:
Object show all
Defined in:
lib/tdriver/util/hooking/hooking.rb

Constant Summary collapse

@@non_wrappable_methods =
[ 'instance' ]
@@wrapped_methods =

default values

{}
@@wrappee_count =
0
@@benchmark =
{}
@@logger_instance =
nil

Class Method Summary collapse

Class Method Details

.benchmarkObject

TODO: document me



287
288
289
290
291
# File 'lib/tdriver/util/hooking/hooking.rb', line 287

def self.benchmark

  @@benchmark

end

.benchmark=(value) ⇒ Object

TODO: document me



294
295
296
297
298
# File 'lib/tdriver/util/hooking/hooking.rb', line 294

def self.benchmark=( value )

  @@benchmark = value

end

.hook_methods(_base) ⇒ Object

Function to hook all instance and static methods of target Class/Module

params

base

Target Class or Module

returns



334
335
336
337
338
339
340
341
342
# File 'lib/tdriver/util/hooking/hooking.rb', line 334

def self.hook_methods( _base )

  hook_static_methods( _base )

  hook_instance_methods( _base )

  nil

end

.log(text, *arguments) ⇒ Object

Function to create logger event - this method is called from wrapper

params

text

Text sent from wrapper

arguments

Not in use

returns



322
323
324
325
326
327
328
# File 'lib/tdriver/util/hooking/hooking.rb', line 322

def self.log( text, *arguments )

  @@logger_instance.debug( text.to_s ) if @@logger_instance

  nil

end

.logger_instanceObject

TODO: document me



301
302
303
304
305
# File 'lib/tdriver/util/hooking/hooking.rb', line 301

def self.logger_instance

  @@logger_instance

end

.logger_instance=(logger_instance) ⇒ Object

Function to set logger instance used by wrapper

params

logger_instance

Instance of TDriver logger

returns



311
312
313
314
315
# File 'lib/tdriver/util/hooking/hooking.rb', line 311

def self.logger_instance=( logger_instance )

  @@logger_instance = logger_instance

end


361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# File 'lib/tdriver/util/hooking/hooking.rb', line 361

def self.print_benchmark( rules = {} )

  total_run_time = 0

  # :sort => :total_time || :times_called || :average_time

  rules = { :sort => :total_time, :order => :ascending, :show_uncalled_methods => true }.merge( rules )

  puts "%-80s %8s %15s %15s %9s %15s" % [ 'Name:',  'Calls:', 'Time total:', 'W/O subcalls:', '%/run', 'Total/call:' ]
  puts "%-80s %8s %15s %15s %9s %15s" % [ '-' * 80, '-' * 8,  '-' * 15,      '-' * 15,        '-' * 8, '-' * 15      ]

  table = @@benchmark

  # calculate average time for method
  table.each{ | key, value |
  
    table[ key ][ :average_time ] = ( value[ :times_elapsed_total ] == 0 || value[ :times_called ] == 0 ) ? 0 : value[ :time_elapsed_total ] / value[ :times_called ] 
    
    total_run_time += value[ :time_elapsed ]

  }

  table = table.sort{ | method_a, method_b | 

    case rules[ :sort ]

      when :name
        method_a[ 0 ] <=> method_b[ 0 ]

      when :times_called
        method_a[ 1 ][ :times_called ] <=> method_b[ 1 ][ :times_called ]

      when :total_time
        method_a[ 1 ][ :time_elapsed_total ] <=> method_b[ 1 ][ :time_elapsed_total ]

      when :total_time_no_subs
        method_a[ 1 ][ :time_elapsed ] <=> method_b[ 1 ][ :time_elapsed ]

      when :percentage
      
        ( ( method_a[ 1 ][ :time_elapsed ].to_f / total_run_time.to_f ) * 100 ) <=> ( ( method_b[ 1 ][ :time_elapsed ].to_f / total_run_time.to_f ) * 100 )

      when :average_time
        method_a[ 1 ][ :average_time ] <=> method_b[ 1 ][ :average_time ]

    else

      raise ArgumentError.new("Invalid sorting rule, valid rules are :name, :times_called, :total_time, :total_time_no_subs, :percentage or :average_time")

    end

  }

  case rules[ :order ]

    # do nothing
    when :ascending

    when :descending
      table = table.reverse

  else

    raise ArgumentError.new("Invalid sort order rule, valid rules are :ascending, :descending")  

  end

  total_percentage = 0.0
  total_time_elapsed_total = 0.0
  total_average = 0.0
  total_calls = 0

  table.each{ | method | 

    puts "%-80s %8s %15.8f %15.8f %8.3f%% %15.8f" % [ 
      method[ 0 ], 
      method[ 1 ][ :times_called ], 
      method[ 1 ][ :time_elapsed_total ], 
      method[ 1 ][ :time_elapsed ], 
      
      ( ( method[ 1 ][ :time_elapsed ].to_f / total_run_time.to_f ) * 100 ),
      
      method[ 1 ][ :average_time ] 
    ] unless !rules[ :show_uncalled_methods ] && method[ 1 ][ :times_called ] == 0

    total_percentage += ( ( method[ 1 ][ :time_elapsed ].to_f / total_run_time.to_f ) * 100 )

    total_calls += method[ 1 ][ :times_called ]
    total_time_elapsed_total += method[ 1 ][ :time_elapsed_total ]        
    total_average += method[ 1 ][ :average_time ]

  }

  puts "%-80s %8s %15s %15s %9s %15s" % [ '-' * 80, '-' * 8, '-' * 15, '-' * 15, '-' * 8, '-' * 15 ]

  puts "%-80s %8s %15.6f %15.6f %8.3f%% %15.8f" % [ 'Total:', total_calls, total_time_elapsed_total, total_run_time, total_percentage, total_average ]

end

.update_method_benchmark(method_name, time_elapsed_in_subcalls, total_time_elapsed) ⇒ Object

Function to update method runtime & calls count for benchmark

params

method_name

Name of the target method

time_elapsed_in_subcalls
total_time_elapsed


349
350
351
352
353
354
355
356
357
358
359
# File 'lib/tdriver/util/hooking/hooking.rb', line 349

def self.update_method_benchmark( method_name, time_elapsed_in_subcalls, total_time_elapsed )

  @@benchmark[ method_name ].tap{ | hash | 

    hash[ :time_elapsed       ] += total_time_elapsed - time_elapsed_in_subcalls
    hash[ :time_elapsed_total ] += total_time_elapsed
    hash[ :times_called       ] += 1  

  }

end

.wrappee_countObject

TODO: document me



258
259
260
261
262
# File 'lib/tdriver/util/hooking/hooking.rb', line 258

def self.wrappee_count

  @@wrappee_count

end

.wrappee_count=(value) ⇒ Object

TODO: document me



265
266
267
268
269
# File 'lib/tdriver/util/hooking/hooking.rb', line 265

def self.wrappee_count=( value )

  @@wrappee_count = value 

end

.wrappee_methodsObject

TODO: document me



272
273
274
275
276
# File 'lib/tdriver/util/hooking/hooking.rb', line 272

def self.wrappee_methods

  @@wrappee_methods

end

.wrappee_methods=(value) ⇒ Object

TODO: document me



279
280
281
282
283
# File 'lib/tdriver/util/hooking/hooking.rb', line 279

def self.wrappee_methods=( value )

  @@wrappee_methods = value 

end