blob: c35719a71fdf66fdd1a4b0d7bf11ce17b6fada8b [file] [log] [blame]
Alexander Polcyn4736e012017-04-14 17:32:00 -07001#!/usr/bin/env ruby
2
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003# Copyright 2015 gRPC authors.
Alexander Polcyn4736e012017-04-14 17:32:00 -07004#
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
Alexander Polcyn4736e012017-04-14 17:32:00 -07008#
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009# http://www.apache.org/licenses/LICENSE-2.0
Alexander Polcyn4736e012017-04-14 17:32:00 -070010#
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
Alexander Polcyn4736e012017-04-14 17:32:00 -070016
Alexander Polcynd1143ab2017-04-16 10:26:44 -070017# For GRPC::Core classes, which use the grpc c-core, object init
18# is interesting because it's related to overall library init.
Alexander Polcyn4736e012017-04-14 17:32:00 -070019
20require_relative './end2end_common'
21
Alexander Polcynb2c0b7b2017-04-27 00:26:25 -070022def construct_many(test_proc)
23 thds = []
24 4.times do
25 thds << Thread.new do
26 20.times do
27 test_proc.call
28 end
Alexander Polcyn4736e012017-04-14 17:32:00 -070029 end
Alexander Polcynb2c0b7b2017-04-27 00:26:25 -070030 end
31 20.times do
32 test_proc.call
33 end
34 thds.each(&:join)
35end
Alexander Polcyn4736e012017-04-14 17:32:00 -070036
Alexander Polcynb2c0b7b2017-04-27 00:26:25 -070037def run_gc_stress_test(test_proc)
38 GC.disable
39 construct_many(test_proc)
Alexander Polcyndeeed7f2017-04-16 13:31:58 -070040
Alexander Polcynb2c0b7b2017-04-27 00:26:25 -070041 GC.enable
42 construct_many(test_proc)
43
Alexander Polcyn010ea652017-09-01 09:06:11 -070044 GC.start
Alexander Polcynb2c0b7b2017-04-27 00:26:25 -070045 construct_many(test_proc)
46end
47
Alexander Polcynfd4cbb72017-05-17 09:57:25 -070048def run_concurrency_stress_test(test_proc)
Alexander Polcynfd4cbb72017-05-17 09:57:25 -070049 100.times do
Alexander Polcyn916893b2017-05-17 10:05:39 -070050 Thread.new do
Alexander Polcynfd4cbb72017-05-17 09:57:25 -070051 test_proc.call
52 end
53 end
54
Alexander Polcyn916893b2017-05-17 10:05:39 -070055 test_proc.call
56
Alexander Polcyn5c6dda82017-05-17 13:08:34 -070057 fail 'exception thrown while child thread initing class'
Alexander Polcynfd4cbb72017-05-17 09:57:25 -070058end
59
60# default (no gc_stress and no concurrency_stress)
61def run_default_test(test_proc)
62 thd = Thread.new do
63 test_proc.call
64 end
65 test_proc.call
Alexander Polcyn5c6dda82017-05-17 13:08:34 -070066 thd.join
Alexander Polcynfd4cbb72017-05-17 09:57:25 -070067end
68
Alexander Polcynb2c0b7b2017-04-27 00:26:25 -070069def get_test_proc(grpc_class)
Alexander Polcyn4736e012017-04-14 17:32:00 -070070 case grpc_class
71 when 'channel'
Alexander Polcynb2c0b7b2017-04-27 00:26:25 -070072 return proc do
Alexander Polcynd1143ab2017-04-16 10:26:44 -070073 GRPC::Core::Channel.new('dummy_host', nil, :this_channel_is_insecure)
74 end
Alexander Polcyn4736e012017-04-14 17:32:00 -070075 when 'server'
Alexander Polcynb2c0b7b2017-04-27 00:26:25 -070076 return proc do
Alexander Polcynd1143ab2017-04-16 10:26:44 -070077 GRPC::Core::Server.new({})
78 end
Alexander Polcyn4736e012017-04-14 17:32:00 -070079 when 'channel_credentials'
Alexander Polcynb2c0b7b2017-04-27 00:26:25 -070080 return proc do
Alexander Polcynd1143ab2017-04-16 10:26:44 -070081 GRPC::Core::ChannelCredentials.new
82 end
Alexander Polcyn4736e012017-04-14 17:32:00 -070083 when 'call_credentials'
Alexander Polcynb2c0b7b2017-04-27 00:26:25 -070084 return proc do
Alexander Polcynd1143ab2017-04-16 10:26:44 -070085 GRPC::Core::CallCredentials.new(proc { |noop| noop })
86 end
Alexander Polcyn4736e012017-04-14 17:32:00 -070087 when 'compression_options'
Alexander Polcynb2c0b7b2017-04-27 00:26:25 -070088 return proc do
Alexander Polcynd1143ab2017-04-16 10:26:44 -070089 GRPC::Core::CompressionOptions.new
90 end
Alexander Polcyn4736e012017-04-14 17:32:00 -070091 else
92 fail "bad --grpc_class=#{grpc_class} param"
93 end
Alexander Polcynb2c0b7b2017-04-27 00:26:25 -070094end
Alexander Polcyndeeed7f2017-04-16 13:31:58 -070095
Alexander Polcynb2c0b7b2017-04-27 00:26:25 -070096def main
97 grpc_class = ''
Alexander Polcynfd4cbb72017-05-17 09:57:25 -070098 stress_test = ''
Alexander Polcynb2c0b7b2017-04-27 00:26:25 -070099 OptionParser.new do |opts|
100 opts.on('--grpc_class=P', String) do |p|
101 grpc_class = p
102 end
Alexander Polcynfd4cbb72017-05-17 09:57:25 -0700103 opts.on('--stress_test=P') do |p|
104 stress_test = p
Alexander Polcynb2c0b7b2017-04-27 00:26:25 -0700105 end
106 end.parse!
107
108 test_proc = get_test_proc(grpc_class)
109
Alexander Polcynfd4cbb72017-05-17 09:57:25 -0700110 # the different test configs need to be ran
111 # in separate processes, since each one tests
112 # clean shutdown in a different way
113 case stress_test
114 when 'gc'
115 p 'run gc stress'
Alexander Polcynb2c0b7b2017-04-27 00:26:25 -0700116 run_gc_stress_test(test_proc)
Alexander Polcynfd4cbb72017-05-17 09:57:25 -0700117 when 'concurrency'
118 p 'run concurrency stress'
119 run_concurrency_stress_test(test_proc)
120 when ''
121 p 'run default'
122 run_default_test(test_proc)
123 else
124 fail "bad --stress_test=#{stress_test} param"
Alexander Polcynb2c0b7b2017-04-27 00:26:25 -0700125 end
Alexander Polcyn4736e012017-04-14 17:32:00 -0700126end
127
128main