Module: TestProf::AnyFixture

Extended by:
Logging
Defined in:
lib/test_prof/any_fixture.rb,
lib/test_prof/any_fixture/dsl.rb

Overview

Make DB fixtures from blocks.

Defined Under Namespace

Modules: DSL Classes: Cache

Constant Summary collapse

INSERT_RXP =
/^INSERT INTO ([\S]+)/

Constants included from Logging

Logging::COLORS

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Logging

build_log_msg, colorize, log

Class Attribute Details

.reporting_enabledObject

Returns the value of attribute reporting_enabled.



45
46
47
# File 'lib/test_prof/any_fixture.rb', line 45

def reporting_enabled
  @reporting_enabled
end

Class Method Details

.cleanObject

Clean all affected tables (but do not reset cache)



62
63
64
65
66
67
68
69
70
# File 'lib/test_prof/any_fixture.rb', line 62

def clean
  disable_referential_integrity do
    tables_cache.keys.reverse_each do |table|
      ActiveRecord::Base.connection.execute %(
        DELETE FROM #{table}
      )
    end
  end
end

.register(id) ⇒ Object

Register a block of code as a fixture, returns the result of the block execution



53
54
55
56
57
58
59
# File 'lib/test_prof/any_fixture.rb', line 53

def register(id)
  cache.fetch(id) do
    ActiveSupport::Notifications.subscribed(method(:subscriber), "sql.active_record") do
      yield
    end
  end
end

.report_statsObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/test_prof/any_fixture.rb', line 84

def report_stats
  msgs = []

  msgs <<
    <<-MSG.strip_heredoc
      AnyFixture usage stats:
    MSG

  first_column = cache.stats.keys.map(&:size).max + 2

  msgs << format(
    "%#{first_column}s  %12s  %9s  %12s",
    'key', 'build time', 'hit count', 'saved time'
  )

  msgs << ""

  total_spent = 0.0
  total_saved = 0.0
  total_miss = 0.0

  cache.stats.to_a.sort_by { |(_, v)| -v[:hit] }.each do |(key, stats)|
    total_spent += stats[:time]

    saved = stats[:time] * stats[:hit]

    total_saved += saved

    total_miss += stats[:time] if stats[:hit].zero?

    msgs << format(
      "%#{first_column}s  %12s  %9d  %12s",
      key, stats[:time].duration, stats[:hit],
      saved.duration
    )
  end

  msgs <<
    <<-MSG.strip_heredoc

      Total time spent: #{total_spent.duration}
      Total time saved: #{total_saved.duration}
      Total time wasted: #{total_miss.duration}
    MSG

  log :info, msgs.join("\n")
end

.reporting_enabled?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/test_prof/any_fixture.rb', line 47

def reporting_enabled?
  reporting_enabled == true
end

.resetObject

Reset all information and clean tables



73
74
75
76
77
# File 'lib/test_prof/any_fixture.rb', line 73

def reset
  clean
  tables_cache.clear
  cache.clear
end

.subscriber(_event, _start, _finish, _id, data) ⇒ Object



79
80
81
82
# File 'lib/test_prof/any_fixture.rb', line 79

def subscriber(_event, _start, _finish, _id, data)
  matches = data.fetch(:sql).match(INSERT_RXP)
  tables_cache[matches[1]] = true if matches
end