corn 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 65c8f764fa897611e644e7d5ad89afeeb9a88317
4
+ data.tar.gz: dc42b9d354000c87e9b90e1b43f1557399a7814d
5
+ SHA512:
6
+ metadata.gz: 08901a2e5162aa0c346816a6b51899c9097d1c5917701919c63bd7903127cc1ee352103ce7c7fa447b5b7f8601b800245eea49d680a76de12cc9dd05471e5f43
7
+ data.tar.gz: eb38dd6ecaaa95436dc77f35f3e13123c7696d798e629532bb6cd8a0f4927e18963d4798ea9cd21c465d9d0dc7920e96df269d6009861057ebfcb5d66e022952
@@ -0,0 +1,15 @@
1
+ # Corn
2
+
3
+ ## Examples:
4
+
5
+ setup environments:
6
+
7
+ CORN_CLIENT_ID
8
+ CORN_BUILD_ID
9
+
10
+ ```ruby
11
+
12
+ # test/test_helper.rb, first line
13
+ require 'corn/setup'
14
+
15
+ ```
@@ -0,0 +1,79 @@
1
+ require 'corn/test_unit'
2
+ require 'net/http'
3
+ require 'net/https'
4
+ require 'logger'
5
+
6
+ module Corn
7
+ module_function
8
+
9
+ def host
10
+ ENV['CORN_HOST']
11
+ end
12
+
13
+ def client_id
14
+ ENV['CORN_CLIENT_ID'] || 'corn client id'
15
+ end
16
+
17
+ def build_id
18
+ ENV['CORN_BUILD_ID'] || 'corn build id'
19
+ end
20
+
21
+ def logger
22
+ @logger ||= Logger.new(STDOUT)
23
+ end
24
+
25
+ def setup
26
+ Test::Unit::TestCase.send(:include, Corn::TestUnit)
27
+ end
28
+
29
+ def benchmark(*labels, &block)
30
+ reports = []
31
+ yield(reporter(reports))
32
+
33
+ log_error { report(labels, reports) }
34
+ end
35
+
36
+ def reporter(reports)
37
+ lambda do |label, &block|
38
+ start_at = Time.now
39
+ begin
40
+ block.call
41
+ ensure
42
+ realtime = Time.now - start_at
43
+ reports << [label, start_at.to_i, realtime]
44
+ end
45
+ end
46
+ end
47
+
48
+ def report(labels, reports)
49
+ data = [['client_id', client_id],
50
+ ['build_id', build_id]]
51
+ data.concat(labels.map {|l| ['labels[]', l]})
52
+ data.concat(reports.map {|r| ['reports[]', r.join(",")]})
53
+ http_post(File.join(host, 'benchmarks'), data)
54
+ end
55
+
56
+ def http_post(url, data)
57
+ uri = URI(url)
58
+ Net::HTTP.start(uri.host, uri.port) do |http|
59
+ http.use_ssl = uri.scheme == 'https'
60
+ req = Net::HTTP::Post.new(uri.path)
61
+ req.set_form_data(data)
62
+ res = http.request req
63
+ case res
64
+ when Net::HTTPSuccess, Net::HTTPRedirection
65
+ # OK
66
+ else
67
+ res.error!
68
+ end
69
+ end
70
+ end
71
+
72
+ def log_error(&block)
73
+ yield
74
+ rescue Exception => e
75
+ logger.debug do
76
+ "Report error: #{e.message}:\n#{e.backtrace.join("\n")}"
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,3 @@
1
+ require 'corn'
2
+
3
+ Corn.setup
@@ -0,0 +1,42 @@
1
+ require 'test/unit/testcase'
2
+
3
+ module Corn
4
+ module TestUnit18
5
+ def self.included(base)
6
+ base.send(:alias_method, :run_without_corn, :run)
7
+ base.send(:alias_method, :run, :run_with_corn)
8
+ end
9
+
10
+ def run_with_corn(result, &block)
11
+ Corn.benchmark(name) do |report|
12
+ __run_with_corn__(report, result, &block)
13
+ end
14
+ end
15
+
16
+ def __run_with_corn__(report, result, &block)
17
+ yield(Test::Unit::TestCase::STARTED, name)
18
+ @_result = result
19
+ begin
20
+ report.call(:setup) { setup }
21
+ report.call(@method_name) { __send__(@method_name) }
22
+ rescue AssertionFailedError => e
23
+ add_failure(e.message, e.backtrace)
24
+ rescue Exception
25
+ raise if Test::Unit::TestCase::PASSTHROUGH_EXCEPTIONS.include? $!.class
26
+ add_error($!)
27
+ ensure
28
+ begin
29
+ report.call(:teardown) { teardown }
30
+ rescue AssertionFailedError => e
31
+ add_failure(e.message, e.backtrace)
32
+ rescue Exception
33
+ raise if Test::Unit::TestCase::PASSTHROUGH_EXCEPTIONS.include? $!.class
34
+ add_error($!)
35
+ end
36
+ end
37
+ result.add_run
38
+ yield(Test::Unit::TestCase::FINISHED, name)
39
+ end
40
+ end
41
+ TestUnit = TestUnit18
42
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: corn
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Xiao Li
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2014-01-29 00:00:00 Z
13
+ dependencies: []
14
+
15
+ description: TBD
16
+ email:
17
+ - swing1979@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - README.md
26
+ - lib/corn.rb
27
+ - lib/corn/setup.rb
28
+ - lib/corn/test_unit.rb
29
+ homepage: https://github.com/xli/corn
30
+ licenses:
31
+ - MIT
32
+ metadata: {}
33
+
34
+ post_install_message:
35
+ rdoc_options: []
36
+
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - &id001
42
+ - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - *id001
48
+ requirements: []
49
+
50
+ rubyforge_project:
51
+ rubygems_version: 2.1.9
52
+ signing_key:
53
+ specification_version: 4
54
+ summary: TBD
55
+ test_files: []
56
+