em_hessian2 2.0.0

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,75 @@
1
+ lib_path = File.expand_path('../../lib', __FILE__)
2
+ $:.unshift(lib_path)
3
+ require 'hessian2'
4
+ require File.expand_path('../monkey', __FILE__)
5
+
6
+ monkey = Monkey.new
7
+ monkey.name = '阿门'
8
+ monkey.age = 7
9
+ monkey.description = '阿门啦啦啦'
10
+
11
+ monkey2 = Monkey.new
12
+ monkey2.name = '大鸡'
13
+ monkey2.age = 6
14
+ monkey2.description = '大鸡啦啦啦'
15
+
16
+ hash = {name: '阿门', age: 7, description: '阿门啦啦啦'}
17
+ hash2 = {name: '大鸡', age: 6, description: '大鸡啦啦啦'}
18
+
19
+ aash = {'name' => '阿门', 'age' => 7, 'description' => '阿门啦啦啦'}
20
+ aash2 = {'name' => '大鸡', 'age' => 6, 'description' => '大鸡啦啦啦'}
21
+
22
+ wmonkey = Hessian2::ClassWrapper.new('example.Monkey', monkey)
23
+ whash = Hessian2::ClassWrapper.new('example.Monkey', hash)
24
+ waash = Hessian2::ClassWrapper.new('example.Monkey', aash)
25
+ wmonkeys = Hessian2::ClassWrapper.new('[example.Monkey', [monkey, monkey2])
26
+ whashes = Hessian2::ClassWrapper.new('[example.Monkey', [hash, hash2])
27
+ waashes = Hessian2::ClassWrapper.new('[example.Monkey', [aash, aash2])
28
+
29
+ puts 'wrap monkey'
30
+
31
+ monkeybin = Hessian2.write(wmonkey)
32
+ puts monkeybin.inspect
33
+
34
+ sonkey = Hessian2.parse(monkeybin)
35
+ puts sonkey.inspect
36
+
37
+ puts 'wrap hash'
38
+
39
+ hashbin = Hessian2.write(whash)
40
+ puts hashbin.inspect
41
+
42
+ sonkey = Hessian2.parse(hashbin)
43
+ puts sonkey.inspect
44
+
45
+ puts 'wrap aash'
46
+
47
+ aashbin = Hessian2.write(waash)
48
+ puts aashbin.inspect
49
+
50
+ sonkey = Hessian2.parse(aashbin)
51
+ puts sonkey.inspect
52
+
53
+ puts 'wrap monkeys'
54
+
55
+ monkeysbin = Hessian2.write(wmonkeys)
56
+ puts monkeysbin.inspect
57
+
58
+ sonkeys = Hessian2.parse(monkeysbin)
59
+ puts sonkeys.inspect
60
+
61
+ puts 'wrap hashes'
62
+
63
+ hashesbin = Hessian2.write(whashes)
64
+ puts hashesbin.inspect
65
+
66
+ sonkeys = Hessian2.parse(hashesbin)
67
+ puts sonkeys.inspect
68
+
69
+ puts 'wrap aashes'
70
+
71
+ aashesbin = Hessian2.write(waashes)
72
+ puts aashesbin.inspect
73
+
74
+ sonkeys = Hessian2.parse(aashesbin)
75
+ puts sonkeys.inspect
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib_path = File.expand_path('../../lib', __FILE__)
3
+ $:.unshift(lib_path)
4
+ require 'hessian2'
5
+ require File.expand_path('../monkey', __FILE__)
6
+
7
+ c1 = Hessian2::Client.new('http://127.0.0.1:9292/monkey')
8
+ list1 = (1..7).to_a
9
+ map1 = { name: '阿门', age: 7 }
10
+ cmonkey1 = Hessian2::ClassWrapper.new('example.Monkey', map1)
11
+ monkeys = []
12
+ 0x11.times do |i|
13
+ monkeys << Hessian2::ClassWrapper.new("example.Monkey#{i}", map1)
14
+ end
15
+ now = Time.new(2013, 1, 12, 14, 59, 59)
16
+
17
+ begin c1.undefined_method; rescue Hessian2::Fault => e; puts e.message; end
18
+
19
+ begin c1.set_string_0('', 'undefined argument'); rescue Hessian2::Fault => e; puts e.message; end
20
+
21
+ Hessian2.parse_bytes([0x5a].each)
@@ -0,0 +1,93 @@
1
+ lib_path = File.expand_path('../../lib', __FILE__)
2
+ $:.unshift(lib_path)
3
+
4
+ require 'hessian2'
5
+ require File.expand_path('../another_monkey', __FILE__)
6
+ require File.expand_path('../establish_connection', __FILE__)
7
+ require File.expand_path('../monkey', __FILE__)
8
+
9
+ hash = { born_at: Time.new(2005, 3, 4), name: '阿门', price: 59.59 }
10
+ hash2 = { born_at: Time.new(2009, 5, 8), name: '大鸡', price: 99.99 }
11
+
12
+ monkey = Monkey.new(hash)
13
+ monkey2 = Monkey.new(hash2)
14
+
15
+ aash = monkey.attributes
16
+ aash2 = monkey2.attributes
17
+
18
+ aonkey = AnotherMonkey.new(hash)
19
+ aonkey2 = AnotherMonkey.new(hash2)
20
+
21
+ whash = Hessian2::StructWrapper.new(MonkeyStruct, hash)
22
+ wmonkey = Hessian2::StructWrapper.new(MonkeyStruct, monkey)
23
+ waash = Hessian2::StructWrapper.new(MonkeyStruct, aash)
24
+ waonkey = Hessian2::StructWrapper.new(MonkeyStruct, aonkey)
25
+
26
+ whashes = Hessian2::StructWrapper.new([MonkeyStruct], [hash, hash2])
27
+ wmonkeys = Hessian2::StructWrapper.new([MonkeyStruct], [monkey, monkey2])
28
+ waashes = Hessian2::StructWrapper.new([MonkeyStruct], [aash, aash2])
29
+ waonkeys = Hessian2::StructWrapper.new([MonkeyStruct], [aonkey, aonkey2])
30
+
31
+ puts 'wrap hash'
32
+
33
+ hashbin = Hessian2.write(whash)
34
+ puts hashbin.inspect
35
+
36
+ sonkey = Hessian2.parse(hashbin, MonkeyStruct)
37
+ puts sonkey.inspect
38
+
39
+ puts 'wrap monkey'
40
+
41
+ monkeybin = Hessian2.write(wmonkey)
42
+ puts monkeybin.inspect
43
+
44
+ sonkey = Hessian2.parse(monkeybin, MonkeyStruct)
45
+ puts sonkey.inspect
46
+
47
+ puts 'wrap aash'
48
+
49
+ aashbin = Hessian2.write(waash)
50
+ puts aashbin.inspect
51
+
52
+ sonkey = Hessian2.parse(aashbin, MonkeyStruct)
53
+ puts sonkey.inspect
54
+
55
+ puts 'wrap aonkey'
56
+
57
+ aonkeybin = Hessian2.write(waonkey)
58
+ puts aonkeybin.inspect
59
+
60
+ sonkey = Hessian2.parse(aonkeybin, MonkeyStruct)
61
+ puts sonkey.inspect
62
+
63
+ puts 'wrap hashes'
64
+
65
+ hashesbin = Hessian2.write(whashes)
66
+ puts hashesbin.inspect
67
+
68
+ sonkeys = Hessian2.parse(hashesbin, [MonkeyStruct])
69
+ puts sonkeys.inspect
70
+
71
+ puts 'wrap monkeys'
72
+
73
+ monkeysbin = Hessian2.write(wmonkeys)
74
+ puts monkeysbin.inspect
75
+
76
+ sonkeys = Hessian2.parse(monkeysbin, [MonkeyStruct])
77
+ puts sonkeys.inspect
78
+
79
+ puts 'wrap aashes'
80
+
81
+ aashesbin = Hessian2.write(waashes)
82
+ puts aashesbin.inspect
83
+
84
+ sonkeys = Hessian2.parse(aashesbin, [MonkeyStruct])
85
+ puts sonkeys.inspect
86
+
87
+ puts 'wrap aonkeys'
88
+
89
+ aonkeysbin = Hessian2.write(waonkeys)
90
+ puts aonkeysbin.inspect
91
+
92
+ sonkeys = Hessian2.parse(aonkeysbin, [MonkeyStruct])
93
+ puts sonkeys.inspect
@@ -0,0 +1,75 @@
1
+ lib_path = File.expand_path('../../lib', __FILE__)
2
+ $:.unshift(lib_path)
3
+ require 'hessian2'
4
+ require File.expand_path('../monkey', __FILE__)
5
+
6
+ monkey = Monkey.new
7
+ monkey.name = '阿门'
8
+ monkey.age = 7
9
+ monkey.description = '阿门啦啦啦'
10
+
11
+ monkey2 = Monkey.new
12
+ monkey2.name = '大鸡'
13
+ monkey2.age = 6
14
+ monkey2.description = '大鸡啦啦啦'
15
+
16
+ hash = {name: '阿门', age: 7, description: '阿门啦啦啦'}
17
+ hash2 = {name: '大鸡', age: 6, description: '大鸡啦啦啦'}
18
+
19
+ aash = {'name' => '阿门', 'age' => 7, 'description' => '阿门啦啦啦'}
20
+ aash2 = {'name' => '大鸡', 'age' => 6, 'description' => '大鸡啦啦啦'}
21
+
22
+ wmonkey = Hessian2::ClassWrapper.new('example.Monkey', monkey)
23
+ whash = Hessian2::ClassWrapper.new('example.Monkey', hash)
24
+ waash = Hessian2::ClassWrapper.new('example.Monkey', aash)
25
+ wmonkeys = Hessian2::ClassWrapper.new('[example.Monkey', [monkey, monkey2])
26
+ whashes = Hessian2::ClassWrapper.new('[example.Monkey', [hash, hash2])
27
+ waashes = Hessian2::ClassWrapper.new('[example.Monkey', [aash, aash2])
28
+
29
+ puts 'wrap monkey'
30
+
31
+ monkeybin = Hessian2.write(wmonkey)
32
+ puts monkeybin.inspect
33
+
34
+ sonkey = Hessian2.parse(monkeybin)
35
+ puts sonkey.inspect
36
+
37
+ puts 'wrap hash'
38
+
39
+ hashbin = Hessian2.write(whash)
40
+ puts hashbin.inspect
41
+
42
+ sonkey = Hessian2.parse(hashbin)
43
+ puts sonkey.inspect
44
+
45
+ puts 'wrap aash'
46
+
47
+ aashbin = Hessian2.write(waash)
48
+ puts aashbin.inspect
49
+
50
+ sonkey = Hessian2.parse(aashbin)
51
+ puts sonkey.inspect
52
+
53
+ puts 'wrap monkeys'
54
+
55
+ monkeysbin = Hessian2.write(wmonkeys)
56
+ puts monkeysbin.inspect
57
+
58
+ sonkeys = Hessian2.parse(monkeysbin)
59
+ puts sonkeys.inspect
60
+
61
+ puts 'wrap hashes'
62
+
63
+ hashesbin = Hessian2.write(whashes)
64
+ puts hashesbin.inspect
65
+
66
+ sonkeys = Hessian2.parse(hashesbin)
67
+ puts sonkeys.inspect
68
+
69
+ puts 'wrap aashes'
70
+
71
+ aashesbin = Hessian2.write(waashes)
72
+ puts aashesbin.inspect
73
+
74
+ sonkeys = Hessian2.parse(aashesbin)
75
+ puts sonkeys.inspect
@@ -0,0 +1,8 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib_path = File.expand_path('../../lib', __FILE__)
3
+ $:.unshift(lib_path)
4
+ require 'hessian2'
5
+
6
+ c1 = Hessian2::Client.new("http://#{ARGV[1] || '127.0.0.1'}:9292/monkey")
7
+
8
+ puts c1.wait_taka(ARGV[0] || 'taka')
@@ -0,0 +1,50 @@
1
+ lib_path = File.expand_path('../../lib', __FILE__)
2
+ $:.unshift(lib_path)
3
+ require 'hessian2'
4
+ require 'benchmark'
5
+
6
+ class User
7
+ attr_accessor :first_name, :last_name
8
+ end
9
+
10
+ UserStruct = Struct.new(:first_name, :last_name)
11
+
12
+ user = User.new
13
+ user.first_name = "Lloyd"
14
+ user.last_name = "Christmas"
15
+
16
+ puts 'hes'
17
+ hes = Hessian2.write(user)
18
+ puts hes.inspect
19
+ puts hes.size
20
+
21
+ huser = Hessian2.parse(hes)
22
+ puts huser.first_name
23
+ puts huser.last_name
24
+
25
+ puts 'hes2'
26
+ hes2 = Hessian2.write(Hessian2::StructWrapper.new(UserStruct, user))
27
+ puts hes2.inspect
28
+ puts hes2.size
29
+
30
+ huser2 = Hessian2.parse(hes2, UserStruct)
31
+ puts huser2.first_name
32
+ puts huser2.last_name
33
+
34
+ number_of = 10000
35
+
36
+ Benchmark.bmbm do |x|
37
+
38
+ x.report "hes" do
39
+ number_of.times do
40
+ Hessian2.parse(hes)
41
+ end
42
+ end
43
+
44
+ x.report "hes2" do
45
+ number_of.times do
46
+ Hessian2.parse(hes2, UserStruct)
47
+ end
48
+ end
49
+
50
+ end
@@ -0,0 +1,182 @@
1
+ lib_path = File.expand_path('../../lib', __FILE__)
2
+ $:.unshift(lib_path)
3
+
4
+ require 'benchmark'
5
+ require 'hessian2'
6
+ require 'memcached'
7
+ require 'msgpack'
8
+ require 'protobuf'
9
+ require 'redis'
10
+ require 'redis/connection/hiredis'
11
+ require 'yajl'
12
+ require File.expand_path('../defs.pb', __FILE__)
13
+ require File.expand_path('../establish_connection', __FILE__)
14
+ require File.expand_path('../monkey', __FILE__)
15
+
16
+ options = YAML.load_file(File.expand_path('../options.yml', __FILE__))
17
+ redis_opts = options['redis']
18
+ redis = Redis.new(driver: :hiredis, host: redis_opts['host'], port: redis_opts['port'],
19
+ password: redis_opts['password'], db: redis_opts['db'] || 0)
20
+ cache = Memcached.new(options['cache_connstr'])
21
+
22
+ id = 59
23
+ monkey = Monkey.find(id)
24
+ attrs = monkey.attributes
25
+ attrs2 = {}
26
+ attrs.each do |k, v|
27
+ attrs2[k] = case v
28
+ when Time
29
+ v.to_i
30
+ when BigDecimal
31
+ v.to_f
32
+ else
33
+ v
34
+ end
35
+ end
36
+
37
+ values = []
38
+ values2 = []
39
+ MonkeyStruct.members.each do |m|
40
+ values << attrs[m.to_s]
41
+ values2 << attrs2[m.to_s]
42
+ end
43
+
44
+ puts monkey.inspect
45
+ puts "attrs: #{attrs.inspect}"
46
+ puts "attrs2: #{attrs2.inspect}"
47
+ puts "values: #{values.inspect}"
48
+ puts "values2: #{values2.inspect}"
49
+ puts
50
+
51
+ # hessian2
52
+
53
+ hes = Hessian2.write(Hessian2::StructWrapper.new(MonkeyStruct, monkey))
54
+ heskey = "monkey#{monkey.id}.hes"
55
+ redis.set(heskey, hes)
56
+ cache.set(heskey, hes)
57
+
58
+ puts heskey
59
+ puts hes.inspect
60
+ puts hes.size
61
+ puts Hessian2.parse(hes, MonkeyStruct).inspect
62
+ puts
63
+
64
+ # marshal
65
+
66
+ mar = Marshal.dump(values)
67
+ markey = "monkey#{monkey.id}.mar"
68
+ redis.set(markey, mar)
69
+ cache.set(markey, mar)
70
+
71
+ puts markey
72
+ puts mar.inspect
73
+ puts mar.size
74
+ puts MonkeyStruct.new(*Marshal.load(mar)).inspect
75
+ puts
76
+
77
+ # yajl
78
+
79
+ json = Yajl::Encoder.encode(values2)
80
+ jsonkey = "monkey#{monkey.id}.json"
81
+ redis.set(jsonkey, json)
82
+ cache.set(jsonkey, json)
83
+
84
+ puts jsonkey
85
+ puts json.inspect
86
+ puts json.size
87
+ puts MonkeyStruct.new(*Yajl::Parser.parse(json)).inspect
88
+ puts
89
+
90
+ # msgpack
91
+
92
+ msg = MessagePack.dump(values2)
93
+ msgkey = "monkey#{monkey.id}.msg"
94
+ redis.set(msgkey, msg)
95
+ cache.set(msgkey, msg)
96
+
97
+ puts msgkey
98
+ puts msg.inspect
99
+ puts msg.size
100
+ puts MonkeyStruct.new(*MessagePack.unpack(msg)).inspect
101
+ puts
102
+
103
+ # protobuf
104
+
105
+ pro = Proto::Monkey.new(attrs2).serialize_to_string
106
+ prokey = "monkey#{monkey.id}.pro"
107
+ redis.set(prokey, pro)
108
+ cache.set(prokey, pro)
109
+
110
+ puts prokey
111
+ puts pro.inspect
112
+ puts pro.size
113
+ puts Proto::Monkey.new.parse_from_string(pro).inspect
114
+ puts
115
+
116
+ # benchmark
117
+
118
+ raise "#{monkey.name} not cached" unless [
119
+ Hessian2.parse(redis.get(heskey), MonkeyStruct).name,
120
+ Hessian2.parse(cache.get(heskey), MonkeyStruct).name,
121
+ monkey.name ].uniq.size == 1
122
+
123
+ raise 'born_at not match' unless [
124
+ Hessian2.parse(hes, MonkeyStruct).born_at,
125
+ MonkeyStruct.new(*Marshal.load(mar)).born_at,
126
+ Time.at(MonkeyStruct.new(*Yajl::Parser.parse(json)).born_at),
127
+ Time.at(MonkeyStruct.new(*MessagePack.unpack(msg)).born_at),
128
+ Time.at(Proto::Monkey.new.parse_from_string(pro).born_at) ].uniq.size == 1
129
+
130
+ number_of = ARGV.first ? ARGV.first.to_i : 5959
131
+
132
+ Benchmark.bmbm do |x|
133
+
134
+ x.report "redis hes" do
135
+ number_of.times do
136
+ Hessian2.parse(redis.get(heskey), MonkeyStruct)
137
+ end
138
+ end
139
+
140
+ x.report "memcached hes" do
141
+ number_of.times do
142
+ Hessian2.parse(cache.get(heskey), MonkeyStruct)
143
+ end
144
+ end
145
+
146
+ x.report "mysql2" do
147
+ number_of.times do
148
+ Monkey.find(id)
149
+ end
150
+ end
151
+
152
+ x.report "hes" do
153
+ number_of.times do
154
+ Hessian2.parse(hes, MonkeyStruct).born_at
155
+ end
156
+ end
157
+
158
+ x.report "mar" do
159
+ number_of.times do
160
+ MonkeyStruct.new(*Marshal.load(mar)).born_at
161
+ end
162
+ end
163
+
164
+ x.report "json" do
165
+ number_of.times do
166
+ Time.at(MonkeyStruct.new(*Yajl::Parser.parse(json)).born_at)
167
+ end
168
+ end
169
+
170
+ x.report "msg" do
171
+ number_of.times do
172
+ Time.at(MonkeyStruct.new(*MessagePack.unpack(msg)).born_at)
173
+ end
174
+ end
175
+
176
+ x.report "pro" do
177
+ number_of.times do
178
+ Time.at(Proto::Monkey.new.parse_from_string(pro).born_at)
179
+ end
180
+ end
181
+
182
+ end