hessian2 2.0.5 → 2.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +10 -10
- data/README.md +203 -199
- data/lib/hessian2.rb +14 -14
- data/lib/hessian2/client.rb +57 -57
- data/lib/hessian2/constants.rb +164 -164
- data/lib/hessian2/fault.rb +3 -3
- data/lib/hessian2/handler.rb +18 -18
- data/lib/hessian2/hessian_client.rb +3 -3
- data/lib/hessian2/parser.rb +619 -619
- data/lib/hessian2/type_wrapper.rb +49 -49
- data/lib/hessian2/version.rb +3 -3
- data/lib/hessian2/writer.rb +506 -504
- data/spec/binary_spec.rb +51 -51
- data/spec/boolean_spec.rb +26 -26
- data/spec/class_wrapper_spec.rb +52 -52
- data/spec/create_monkeys.rb +14 -14
- data/spec/date_spec.rb +45 -45
- data/spec/double_spec.rb +78 -78
- data/spec/int_spec.rb +54 -54
- data/spec/list_spec.rb +66 -66
- data/spec/long_spec.rb +68 -68
- data/spec/map_spec.rb +36 -36
- data/spec/null_spec.rb +17 -17
- data/spec/object_spec.rb +78 -78
- data/spec/ref_spec.rb +43 -43
- data/spec/spec_helper.rb +23 -23
- data/spec/string_spec.rb +61 -61
- data/spec/struct_wrapper_spec.rb +47 -47
- data/spec/type_wrapper_spec.rb +102 -102
- data/test/app.rb +24 -24
- data/test/async/em_http_asleep.rb +25 -25
- data/test/async/em_http_sleep.rb +25 -25
- data/test/async/monkey.asleep.rb +27 -27
- data/test/async/mysql2_aquery.rb +37 -37
- data/test/fault/monkey.undefined_method.rb +5 -5
- data/test/fault/monkey.wrong_arguments.rb +5 -5
- data/test/fiber_concurrency/em_http_asleep.rb +17 -17
- data/test/fiber_concurrency/em_http_sleep.rb +17 -17
- data/test/fiber_concurrency/monkey.asleep.fiber_aware.rb +18 -18
- data/test/fiber_concurrency/mysql2_query.rb +29 -29
- data/test/fiber_concurrency/net_http_asleep.rb +19 -19
- data/test/fiber_concurrency/net_http_sleep.rb +19 -19
- data/test/fibered_rainbows/Gemfile +15 -15
- data/test/fibered_rainbows/config.ru +11 -11
- data/test/fibered_rainbows/rainbows.rb +13 -13
- data/test/monkey_service.rb +16 -16
- data/test/prepare.rb +7 -7
- data/test/thread_concurrency/active_record_execute.rb +29 -29
- data/test/thread_concurrency/monkey.asleep.rb +22 -22
- data/test/thread_concurrency/net_http_asleep.rb +24 -24
- data/test/thread_concurrency/net_http_sleep.rb +24 -24
- data/test/threaded_rainbows/Gemfile +13 -13
- data/test/threaded_rainbows/config.ru +9 -9
- data/test/threaded_rainbows/rainbows.rb +13 -13
- metadata +46 -4
data/spec/type_wrapper_spec.rb
CHANGED
@@ -1,102 +1,102 @@
|
|
1
|
-
require File.expand_path('../spec_helper', __FILE__)
|
2
|
-
|
3
|
-
module Hessian2
|
4
|
-
describe TypeWrapper do
|
5
|
-
hash = { id: nil, born_at: Time.new(2009, 5, 8), name: '大鸡', price: 99.99 }
|
6
|
-
|
7
|
-
it "should raise error" do
|
8
|
-
expect(lambda{ Hessian2::TypeWrapper.new(:unknown) }).to raise_error
|
9
|
-
end
|
10
|
-
|
11
|
-
|
12
|
-
it "should wrap nil" do
|
13
|
-
bin = Hessian2.write(Hessian2::TypeWrapper.new(:unknown, nil))
|
14
|
-
|
15
|
-
expect(bin).to eq('N')
|
16
|
-
expect(Hessian2.parse(bin)).to eq(nil)
|
17
|
-
end
|
18
|
-
|
19
|
-
|
20
|
-
it "should wrap long" do
|
21
|
-
[ 59, '59' ].each do |val|
|
22
|
-
[ 'L', 'l', 'Long', 'long', :long ].each do |type|
|
23
|
-
bin = Hessian2.write(Hessian2::TypeWrapper.new(type, val))
|
24
|
-
|
25
|
-
b1, b0 = bin[0, 2].unpack('CC')
|
26
|
-
expect(((b1 - 0xf8) << 8) + b0).to eq(Integer(val))
|
27
|
-
expect(Hessian2.parse(bin)).to eq(Integer(val))
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
|
33
|
-
it "should wrap int" do
|
34
|
-
[ 0x7f_fff_fff, '0x7f_fff_fff' ].each do |val|
|
35
|
-
[ 'I', 'i', 'Integer', 'int', :int ].each do |type|
|
36
|
-
bin = Hessian2.write(Hessian2::TypeWrapper.new(type, val))
|
37
|
-
|
38
|
-
expect(bin[0]).to eq('I')
|
39
|
-
expect(bin[1, 4].unpack('l>').first).to eq(Integer(val))
|
40
|
-
expect(Hessian2.parse(bin)).to eq(Integer(val))
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
|
46
|
-
it "should wrap binary" do
|
47
|
-
val = 'b' * 59
|
48
|
-
[ 'B', 'b', 'Binary', 'bin', :bin ].each do |type|
|
49
|
-
bin = Hessian2.write(Hessian2::TypeWrapper.new(type, val))
|
50
|
-
|
51
|
-
b1, b0 = bin[0, 2].unpack('CC')
|
52
|
-
expect(256 * (b1 - 0x34) + b0).to eq(val.size)
|
53
|
-
expect(bin.size).to eq(2 + val.size)
|
54
|
-
expect(Hessian2.parse(bin)).to eq(val)
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
|
59
|
-
it "should wrap monkey" do
|
60
|
-
[ Monkey.new(hash), AnotherMonkey.new(hash) ].each do |val|
|
61
|
-
bin = Hessian2.write(Hessian2::TypeWrapper.new('com.sun.java.Monkey', val))
|
62
|
-
|
63
|
-
_hash = Hessian2.parse(bin, nil, symbolize_keys: true)
|
64
|
-
expect([ _hash[:born_at], _hash[:name], _hash[:price] ]).to eq([ hash[:born_at], hash[:name], hash[:price] ])
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
|
69
|
-
it "should wrap long array" do
|
70
|
-
bin = Hessian2.write(Hessian2::TypeWrapper.new('[long', [ 59, 69, 79, 89, 99 ]))
|
71
|
-
|
72
|
-
expect(Hessian2.parse(bin)).to eq([ 59, 69, 79, 89, 99 ])
|
73
|
-
end
|
74
|
-
|
75
|
-
|
76
|
-
it "should wrap int array" do
|
77
|
-
bin = Hessian2.write(Hessian2::TypeWrapper.new('[int', [ 0x7f_fff_ffb, 0x7f_fff_ffc, 0x7f_fff_ffd, 0x7f_fff_ffe, 0x7f_fff_fff ]))
|
78
|
-
|
79
|
-
expect(Hessian2.parse(bin)).to eq([ 0x7f_fff_ffb, 0x7f_fff_ffc, 0x7f_fff_ffd, 0x7f_fff_ffe, 0x7f_fff_fff ])
|
80
|
-
end
|
81
|
-
|
82
|
-
|
83
|
-
it "should wrap binary array" do
|
84
|
-
bin = Hessian2.write(Hessian2::TypeWrapper.new('[bin', [ 'b' * 59, 'b' * 69, 'b' * 79, 'b' * 89, 'b' * 99 ]))
|
85
|
-
|
86
|
-
expect(Hessian2.parse(bin)).to eq([ 'b' * 59, 'b' * 69, 'b' * 79, 'b' * 89, 'b' * 99 ])
|
87
|
-
end
|
88
|
-
|
89
|
-
|
90
|
-
it "should wrap monkey array" do
|
91
|
-
bin = Hessian2.write(Hessian2::TypeWrapper.new('[com.sun.java.Monkey', [ nil, Monkey.new(hash), AnotherMonkey.new(hash) ]))
|
92
|
-
|
93
|
-
_hash1, _hash2, _hash3 = Hessian2.parse(bin, nil, symbolize_keys: true)
|
94
|
-
expect(_hash1).to eq(nil)
|
95
|
-
[ _hash2, _hash3 ].each do |_hash|
|
96
|
-
expect([ _hash[:born_at], _hash[:name], _hash[:price] ]).to eq([ hash[:born_at], hash[:name], hash[:price] ])
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
|
101
|
-
end
|
102
|
-
end
|
1
|
+
require File.expand_path('../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
module Hessian2
|
4
|
+
describe TypeWrapper do
|
5
|
+
hash = { id: nil, born_at: Time.new(2009, 5, 8), name: '大鸡', price: 99.99 }
|
6
|
+
|
7
|
+
it "should raise error" do
|
8
|
+
expect(lambda{ Hessian2::TypeWrapper.new(:unknown) }).to raise_error
|
9
|
+
end
|
10
|
+
|
11
|
+
|
12
|
+
it "should wrap nil" do
|
13
|
+
bin = Hessian2.write(Hessian2::TypeWrapper.new(:unknown, nil))
|
14
|
+
|
15
|
+
expect(bin).to eq('N')
|
16
|
+
expect(Hessian2.parse(bin)).to eq(nil)
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
it "should wrap long" do
|
21
|
+
[ 59, '59' ].each do |val|
|
22
|
+
[ 'L', 'l', 'Long', 'long', :long ].each do |type|
|
23
|
+
bin = Hessian2.write(Hessian2::TypeWrapper.new(type, val))
|
24
|
+
|
25
|
+
b1, b0 = bin[0, 2].unpack('CC')
|
26
|
+
expect(((b1 - 0xf8) << 8) + b0).to eq(Integer(val))
|
27
|
+
expect(Hessian2.parse(bin)).to eq(Integer(val))
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
it "should wrap int" do
|
34
|
+
[ 0x7f_fff_fff, '0x7f_fff_fff' ].each do |val|
|
35
|
+
[ 'I', 'i', 'Integer', 'int', :int ].each do |type|
|
36
|
+
bin = Hessian2.write(Hessian2::TypeWrapper.new(type, val))
|
37
|
+
|
38
|
+
expect(bin[0]).to eq('I')
|
39
|
+
expect(bin[1, 4].unpack('l>').first).to eq(Integer(val))
|
40
|
+
expect(Hessian2.parse(bin)).to eq(Integer(val))
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
|
46
|
+
it "should wrap binary" do
|
47
|
+
val = 'b' * 59
|
48
|
+
[ 'B', 'b', 'Binary', 'bin', :bin ].each do |type|
|
49
|
+
bin = Hessian2.write(Hessian2::TypeWrapper.new(type, val))
|
50
|
+
|
51
|
+
b1, b0 = bin[0, 2].unpack('CC')
|
52
|
+
expect(256 * (b1 - 0x34) + b0).to eq(val.size)
|
53
|
+
expect(bin.size).to eq(2 + val.size)
|
54
|
+
expect(Hessian2.parse(bin)).to eq(val)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
|
59
|
+
it "should wrap monkey" do
|
60
|
+
[ Monkey.new(hash), AnotherMonkey.new(hash) ].each do |val|
|
61
|
+
bin = Hessian2.write(Hessian2::TypeWrapper.new('com.sun.java.Monkey', val))
|
62
|
+
|
63
|
+
_hash = Hessian2.parse(bin, nil, symbolize_keys: true)
|
64
|
+
expect([ _hash[:born_at], _hash[:name], _hash[:price] ]).to eq([ hash[:born_at], hash[:name], hash[:price] ])
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
|
69
|
+
it "should wrap long array" do
|
70
|
+
bin = Hessian2.write(Hessian2::TypeWrapper.new('[long', [ 59, 69, 79, 89, 99 ]))
|
71
|
+
|
72
|
+
expect(Hessian2.parse(bin)).to eq([ 59, 69, 79, 89, 99 ])
|
73
|
+
end
|
74
|
+
|
75
|
+
|
76
|
+
it "should wrap int array" do
|
77
|
+
bin = Hessian2.write(Hessian2::TypeWrapper.new('[int', [ 0x7f_fff_ffb, 0x7f_fff_ffc, 0x7f_fff_ffd, 0x7f_fff_ffe, 0x7f_fff_fff ]))
|
78
|
+
|
79
|
+
expect(Hessian2.parse(bin)).to eq([ 0x7f_fff_ffb, 0x7f_fff_ffc, 0x7f_fff_ffd, 0x7f_fff_ffe, 0x7f_fff_fff ])
|
80
|
+
end
|
81
|
+
|
82
|
+
|
83
|
+
it "should wrap binary array" do
|
84
|
+
bin = Hessian2.write(Hessian2::TypeWrapper.new('[bin', [ 'b' * 59, 'b' * 69, 'b' * 79, 'b' * 89, 'b' * 99 ]))
|
85
|
+
|
86
|
+
expect(Hessian2.parse(bin)).to eq([ 'b' * 59, 'b' * 69, 'b' * 79, 'b' * 89, 'b' * 99 ])
|
87
|
+
end
|
88
|
+
|
89
|
+
|
90
|
+
it "should wrap monkey array" do
|
91
|
+
bin = Hessian2.write(Hessian2::TypeWrapper.new('[com.sun.java.Monkey', [ nil, Monkey.new(hash), AnotherMonkey.new(hash) ]))
|
92
|
+
|
93
|
+
_hash1, _hash2, _hash3 = Hessian2.parse(bin, nil, symbolize_keys: true)
|
94
|
+
expect(_hash1).to eq(nil)
|
95
|
+
[ _hash2, _hash3 ].each do |_hash|
|
96
|
+
expect([ _hash[:born_at], _hash[:name], _hash[:price] ]).to eq([ hash[:born_at], hash[:name], hash[:price] ])
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
|
101
|
+
end
|
102
|
+
end
|
data/test/app.rb
CHANGED
@@ -1,24 +1,24 @@
|
|
1
|
-
require File.expand_path('../monkey_service', __FILE__)
|
2
|
-
|
3
|
-
set :logging, false
|
4
|
-
|
5
|
-
get '/' do
|
6
|
-
status 405
|
7
|
-
'post me'
|
8
|
-
end
|
9
|
-
|
10
|
-
post '/' do
|
11
|
-
MonkeyService.handle(request.body.read)
|
12
|
-
end
|
13
|
-
|
14
|
-
route :get, :post, '/sleep' do
|
15
|
-
sleep 1
|
16
|
-
|
17
|
-
'wake'
|
18
|
-
end
|
19
|
-
|
20
|
-
route :get, :post, '/asleep' do
|
21
|
-
EM::Synchrony.sleep(1)
|
22
|
-
|
23
|
-
'awake'
|
24
|
-
end
|
1
|
+
require File.expand_path('../monkey_service', __FILE__)
|
2
|
+
|
3
|
+
set :logging, false
|
4
|
+
|
5
|
+
get '/' do
|
6
|
+
status 405
|
7
|
+
'post me'
|
8
|
+
end
|
9
|
+
|
10
|
+
post '/' do
|
11
|
+
MonkeyService.handle(request.body.read)
|
12
|
+
end
|
13
|
+
|
14
|
+
route :get, :post, '/sleep' do
|
15
|
+
sleep 1
|
16
|
+
|
17
|
+
'wake'
|
18
|
+
end
|
19
|
+
|
20
|
+
route :get, :post, '/asleep' do
|
21
|
+
EM::Synchrony.sleep(1)
|
22
|
+
|
23
|
+
'awake'
|
24
|
+
end
|
@@ -1,25 +1,25 @@
|
|
1
|
-
require File.expand_path('../../prepare', __FILE__)
|
2
|
-
require 'eventmachine'
|
3
|
-
require 'em-synchrony/em-http'
|
4
|
-
|
5
|
-
EM.run do
|
6
|
-
@number_of.times do |i|
|
7
|
-
puts i
|
8
|
-
http = EM::HttpRequest.new("http://127.0.0.1:8080/asleep").apost
|
9
|
-
http.callback do |r|
|
10
|
-
puts 'callback'
|
11
|
-
@results << r.response
|
12
|
-
EM.stop if @results.size >= @number_of
|
13
|
-
end
|
14
|
-
|
15
|
-
http.errback do |r|
|
16
|
-
puts "errback #{r.error}"
|
17
|
-
EM.stop
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
puts "results.size #{@results.size}"
|
22
|
-
end
|
23
|
-
|
24
|
-
puts @results.inspect
|
25
|
-
puts "results.size #{@results.size}"
|
1
|
+
require File.expand_path('../../prepare', __FILE__)
|
2
|
+
require 'eventmachine'
|
3
|
+
require 'em-synchrony/em-http'
|
4
|
+
|
5
|
+
EM.run do
|
6
|
+
@number_of.times do |i|
|
7
|
+
puts i
|
8
|
+
http = EM::HttpRequest.new("http://127.0.0.1:8080/asleep").apost
|
9
|
+
http.callback do |r|
|
10
|
+
puts 'callback'
|
11
|
+
@results << r.response
|
12
|
+
EM.stop if @results.size >= @number_of
|
13
|
+
end
|
14
|
+
|
15
|
+
http.errback do |r|
|
16
|
+
puts "errback #{r.error}"
|
17
|
+
EM.stop
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
puts "results.size #{@results.size}"
|
22
|
+
end
|
23
|
+
|
24
|
+
puts @results.inspect
|
25
|
+
puts "results.size #{@results.size}"
|
data/test/async/em_http_sleep.rb
CHANGED
@@ -1,25 +1,25 @@
|
|
1
|
-
require File.expand_path('../../prepare', __FILE__)
|
2
|
-
require 'eventmachine'
|
3
|
-
require 'em-synchrony/em-http'
|
4
|
-
|
5
|
-
EM.run do
|
6
|
-
@number_of.times do |i|
|
7
|
-
puts i
|
8
|
-
http = EM::HttpRequest.new("http://127.0.0.1:8080/sleep").apost
|
9
|
-
http.callback do |r|
|
10
|
-
puts 'callback'
|
11
|
-
@results << r.response
|
12
|
-
EM.stop if @results.size >= @number_of
|
13
|
-
end
|
14
|
-
|
15
|
-
http.errback do |r|
|
16
|
-
puts "errback #{r.error}"
|
17
|
-
EM.stop
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
puts "results.size #{@results.size}"
|
22
|
-
end
|
23
|
-
|
24
|
-
puts @results.inspect
|
25
|
-
puts "results.size #{@results.size}"
|
1
|
+
require File.expand_path('../../prepare', __FILE__)
|
2
|
+
require 'eventmachine'
|
3
|
+
require 'em-synchrony/em-http'
|
4
|
+
|
5
|
+
EM.run do
|
6
|
+
@number_of.times do |i|
|
7
|
+
puts i
|
8
|
+
http = EM::HttpRequest.new("http://127.0.0.1:8080/sleep").apost
|
9
|
+
http.callback do |r|
|
10
|
+
puts 'callback'
|
11
|
+
@results << r.response
|
12
|
+
EM.stop if @results.size >= @number_of
|
13
|
+
end
|
14
|
+
|
15
|
+
http.errback do |r|
|
16
|
+
puts "errback #{r.error}"
|
17
|
+
EM.stop
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
puts "results.size #{@results.size}"
|
22
|
+
end
|
23
|
+
|
24
|
+
puts @results.inspect
|
25
|
+
puts "results.size #{@results.size}"
|
data/test/async/monkey.asleep.rb
CHANGED
@@ -1,27 +1,27 @@
|
|
1
|
-
require File.expand_path('../../prepare', __FILE__)
|
2
|
-
require 'eventmachine'
|
3
|
-
|
4
|
-
client = Hessian2::Client.new('http://127.0.0.1:8080/', async: true)
|
5
|
-
|
6
|
-
EM.run do
|
7
|
-
@number_of.times do |i|
|
8
|
-
puts i
|
9
|
-
http = client.asleep
|
10
|
-
http.callback do |r|
|
11
|
-
puts 'callback'
|
12
|
-
@results << Hessian2.parse_rpc(r.response)
|
13
|
-
EM.stop if @results.size >= @number_of
|
14
|
-
end
|
15
|
-
|
16
|
-
http.errback do |r|
|
17
|
-
puts "errback #{r.error}"
|
18
|
-
@results << nil
|
19
|
-
EM.stop
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
puts "results.size #{@results.size}"
|
24
|
-
end
|
25
|
-
|
26
|
-
puts @results.inspect
|
27
|
-
puts "results.size #{@results.size}"
|
1
|
+
require File.expand_path('../../prepare', __FILE__)
|
2
|
+
require 'eventmachine'
|
3
|
+
|
4
|
+
client = Hessian2::Client.new('http://127.0.0.1:8080/', async: true)
|
5
|
+
|
6
|
+
EM.run do
|
7
|
+
@number_of.times do |i|
|
8
|
+
puts i
|
9
|
+
http = client.asleep
|
10
|
+
http.callback do |r|
|
11
|
+
puts 'callback'
|
12
|
+
@results << Hessian2.parse_rpc(r.response)
|
13
|
+
EM.stop if @results.size >= @number_of
|
14
|
+
end
|
15
|
+
|
16
|
+
http.errback do |r|
|
17
|
+
puts "errback #{r.error}"
|
18
|
+
@results << nil
|
19
|
+
EM.stop
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
puts "results.size #{@results.size}"
|
24
|
+
end
|
25
|
+
|
26
|
+
puts @results.inspect
|
27
|
+
puts "results.size #{@results.size}"
|
data/test/async/mysql2_aquery.rb
CHANGED
@@ -1,37 +1,37 @@
|
|
1
|
-
require 'em-synchrony'
|
2
|
-
require 'em-synchrony/mysql2'
|
3
|
-
require 'em-synchrony/fiber_iterator'
|
4
|
-
require 'yaml'
|
5
|
-
|
6
|
-
options = YAML.load_file(File.expand_path('../../../spec/database.yml', __FILE__))
|
7
|
-
puts options.inspect
|
8
|
-
|
9
|
-
number_of = 10
|
10
|
-
concurrency = 2
|
11
|
-
connection_pool_size = 4
|
12
|
-
results = []
|
13
|
-
|
14
|
-
db = EM::Synchrony::ConnectionPool.new(size: connection_pool_size) do
|
15
|
-
Mysql2::EM::Client.new(options)
|
16
|
-
end
|
17
|
-
|
18
|
-
EM.synchrony do
|
19
|
-
EM::Synchrony::FiberIterator.new(0...number_of, concurrency).each do |i|
|
20
|
-
puts i
|
21
|
-
aquery = db.aquery('select sleep(1)')
|
22
|
-
aquery.callback do |r|
|
23
|
-
puts 'callback'
|
24
|
-
results << r.first
|
25
|
-
EM.stop if results.size >= number_of
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
puts "results.size #{results.size}"
|
30
|
-
end
|
31
|
-
|
32
|
-
puts results.inspect
|
33
|
-
puts "results.size #{results.size}"
|
34
|
-
|
35
|
-
# time ruby test/async/mysql2_aquery.rb
|
36
|
-
# 并发数 >= db连接池的场合,aquery要等空闲连接出来,耗时和query一样
|
37
|
-
# 并发数 < db连接池的场合,aquery可以马上用空闲连接发起下一波查询,也就更早拿到callback,比query快
|
1
|
+
require 'em-synchrony'
|
2
|
+
require 'em-synchrony/mysql2'
|
3
|
+
require 'em-synchrony/fiber_iterator'
|
4
|
+
require 'yaml'
|
5
|
+
|
6
|
+
options = YAML.load_file(File.expand_path('../../../spec/database.yml', __FILE__))
|
7
|
+
puts options.inspect
|
8
|
+
|
9
|
+
number_of = 10
|
10
|
+
concurrency = 2
|
11
|
+
connection_pool_size = 4
|
12
|
+
results = []
|
13
|
+
|
14
|
+
db = EM::Synchrony::ConnectionPool.new(size: connection_pool_size) do
|
15
|
+
Mysql2::EM::Client.new(options)
|
16
|
+
end
|
17
|
+
|
18
|
+
EM.synchrony do
|
19
|
+
EM::Synchrony::FiberIterator.new(0...number_of, concurrency).each do |i|
|
20
|
+
puts i
|
21
|
+
aquery = db.aquery('select sleep(1)')
|
22
|
+
aquery.callback do |r|
|
23
|
+
puts 'callback'
|
24
|
+
results << r.first
|
25
|
+
EM.stop if results.size >= number_of
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
puts "results.size #{results.size}"
|
30
|
+
end
|
31
|
+
|
32
|
+
puts results.inspect
|
33
|
+
puts "results.size #{results.size}"
|
34
|
+
|
35
|
+
# time ruby test/async/mysql2_aquery.rb
|
36
|
+
# 并发数 >= db连接池的场合,aquery要等空闲连接出来,耗时和query一样
|
37
|
+
# 并发数 < db连接池的场合,aquery可以马上用空闲连接发起下一波查询,也就更早拿到callback,比query快
|