Class: RPerft::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/rperft/client.rb

Defined Under Namespace

Classes: TestResult

Instance Method Summary collapse

Constructor Details

#initialize(suite_name) ⇒ Client

Returns a new instance of Client.



23
24
25
26
27
# File 'lib/rperft/client.rb', line 23

def initialize(suite_name)
  @suite_name    = suite_name
  @test_results  = []
  @configuration = nil
end

Instance Method Details

#add_result(description, repetitions, elapsed_seconds, options = {}) ⇒ Object



55
56
57
58
59
# File 'lib/rperft/client.rb', line 55

def add_result(description, repetitions, elapsed_seconds, options={})
  result = TestResult.new(description, repetitions, elapsed_seconds)
  result.tags = options[:tags] || []
  @test_results << result
end

#configure!(config_path = nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rperft/client.rb', line 29

def configure!(config_path=nil)
  # If a config path wasn't specified, let's assume it's in the working
  # directory.
  config_path ||= File.join(Dir.pwd, ".perft-config")
  @configuration = YAML.load_file(config_path)

  @host    = @configuration["host"]
  @project = @configuration["project"]
  @machine = @configuration["machine"]
  @api_key = @configuration["api_key"]

  RPerft::Client.base_uri @host
  RPerft::Client.basic_auth @machine, @api_key
end

#configured?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/rperft/client.rb', line 44

def configured?
  !!@configuration
end

#run_test(description, repetitions, options = {}, &block) ⇒ Object



48
49
50
51
52
53
# File 'lib/rperft/client.rb', line 48

def run_test(description, repetitions, options={}, &block)
  measurement = Benchmark.measure do
    repetitions.times(&block)
  end
  add_result(description, repetitions, measurement.total, options)
end

#submit_results(options = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rperft/client.rb', line 61

def submit_results(options={})
  configure! if !configured?

  changeset = nil
  comment   = nil

  if options[:final] || (`git status --porcelain`).empty?
    changeset, comment = `git log --oneline HEAD^..HEAD`.split(/\s+/, 2)
  end

  changes = `git diff HEAD HEAD^`

  results = @test_results.map do |result|
    {
      :description     => result.description,
      :elapsed_seconds => result.elapsed_seconds,
      :repetitions     => result.repetitions,
      :tags            => result.tags
    }
  end

  RPerft::Client.post("/projects/#{@project}/#{CGI.escape(@suite_name)}", {
    :body => {
      :results   => results,
      :changeset => changeset,
      :version   => options[:version],
      :comment   => comment,
      :changes   => changes,
      :append    => options[:append]
    },
    :headers => {
      "Content-Type" => "application/x-www-form-urlencoded"
    }
  })
end