Class: InternetHakai::ScenarioBuilder

Inherits:
BaseHandler show all
Defined in:
lib/internethakai/scenario_builder.rb

Constant Summary collapse

UNIQUE_BY_THREAD =
false
DEFAULT_CONFIG =
{
    "loop" => 1,
    "rev" => true,
    "max_scenario" => 1,
    "max_request" => 1,
    "log_level" => 3,
    "ranking" => 5,
    "follow_redirect" => true,
    "queue" => "SimpleQueue",
    "action_handler" => "RequestAction",
    "client_handler" => "ClientHandler",
    "response_handler" => "TimeRegister",
    "request_pool" => "RequestPool",
    "http_client" => "HttpClient",
    "scenario_executer" => "ScenarioExecuter",
    "logger" => "Logger",
    "encoding" => "Shift_JIS",
    "save_report" => false,
    "reporter" => "TsvReporter",
    "show_report" => false,
    "sleep" => false,
    "timeout" => 10,
    "actions" => [],
}

Instance Method Summary collapse

Methods inherited from BaseHandler

clear, clearall, get_class, get_config, get_handler, get_instance, get_instance_thread, get_thread_id, #initialize, #on_create, set_config, set_thread_id, unique_by_thread?

Constructor Details

This class inherits a constructor from InternetHakai::BaseHandler

Instance Method Details

#get_configObject



30
31
32
# File 'lib/internethakai/scenario_builder.rb', line 30

def get_config
    @config
end

#load(path, setting = nil) ⇒ Object



155
156
157
158
159
160
161
162
163
# File 'lib/internethakai/scenario_builder.rb', line 155

def load path, setting=nil
    begin
        basedir = File::expand_path(File::dirname(path))
        str = File::open(path){|io|io.read}
        load_config(str, basedir, setting)
    rescue
        raise $!
    end
end

#load_config(str, basedir = '.', setting = nil) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/internethakai/scenario_builder.rb', line 164

def load_config str, basedir='.', setting=nil
    default_config = DEFAULT_CONFIG
    if(RUBY_VERSION.to_f >= 1.9)
        str.force_encoding('UTF-8')
    end
    if str.include?('%%BASEDIR%%')
        puts "WARNING: %%BASEDIR%% is obsolete"
        str.gsub!('%%BASEDIR%%', basedir)
    end
    @config = ::YAML::load(str)
    @config = @config.merge(setting) if setting
    raise "invalid file" unless @config
    @config = default_config.merge(@config)
    prepare_config
    if(@config["require"])
        begin
            require @config["require"]
        rescue
            @logger.run("class load error: #{@config['require']}", 1)
        end
    end
    @config
end

#prepare_configObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/internethakai/scenario_builder.rb', line 33

def prepare_config
    baseconfig = @config
    basehost, baseport, = Util::parse_url(baseconfig['domain'])
    if baseconfig['domain'].index('https://')==0
        baseconfig['ssl'] = true
        baseport = 443
    end
    basehostaddress = IPSocket::getaddress(basehost)
    baseconfig['host'] = basehostaddress
    baseconfig['host_name'] = basehost unless baseconfig['host_name']
    baseconfig['port'] = baseport
    baseconfig['encoding'] ||= 'UTF-8'

    #後方互換性のため
    if baseconfig.has_key?('fork') && baseconfig['fork'].has_key?('max_process') && baseconfig['fork']['max_process'] > 1 && !baseconfig.has_key?('max_process')
        baseconfig['max_process'] = baseconfig['fork']['max_process']
    else
        baseconfig['max_process'] ||= 1
    end
    #後方互換性のため
    if baseconfig.has_key?('concurrency') && baseconfig['max_scenario'] == 1
        baseconfig['max_scenario'] = baseconfig['concurrency']
    end

    headers = baseconfig.has_key?('header') ? baseconfig['header'] : Hash::new
    headers['User-Agent'] = baseconfig['user_agent'] if baseconfig.has_key?('user_agent')
    headers['Host'] = baseconfig['host_name']
    baseconfig['header'] = headers
    hosts = []
    hosts << [basehostaddress, basehost, baseport]
    hosts_cache = {}
    hosts_cache[hosts[0].join(':')] = true
    idx = 0

    base = baseconfig.clone
    base.delete('actions')
    actions = []
    @config["actions"].each do |actconfig|
        if actconfig.is_a? String
            path = actconfig
            actconfig = Hash::new
            actconfig['path'] = path
        end
        url = actconfig['path']
        actconfig['path'] = url
        actconfig['method'] = 'GET' unless actconfig.has_key? 'method'
        actconfig['class'] = baseconfig['action_handler'] unless actconfig.has_key? 'class'
        classname = actconfig["class"]
        command = BaseHandler::get_class(classname)
        raise "invalid class" unless command < BaseAction
        actconfig['class_obj'] = command

        if (!url.match( /^http:\/\// ))
            url = baseconfig['domain'].to_s + url.to_s
            actconfig['host'] = basehostaddress
            actconfig['host_name'] = basehost
            actconfig['port'] = baseport
        else
            host, port, path = Util::parse_url(url)
            #名前解決
            address = IPSocket::getaddress(host)
            actconfig['host'] = address
            actconfig['path'] = path
            actconfig['host_name'] = host unless baseconfig['host_name']
            actconfig['port'] = port
            info = [address, host, port]
            headers = actconfig.has_key?('header') ? actconfig['header'] : Hash::new
            headers['User-Agent'] = baseconfig['user_agent'] if baseconfig.has_key?('user_agent')
            headers['Host'] = actconfig['host_name']
            actconfig['header'] = headers
            key = info.join(':')
            unless hosts_cache.has_key?(key)
                hosts << info
                hosts_cache[key] = true
            end
        end
        if(actconfig.has_key?('post_params'))
            actconfig['post_string'] = Util::hash2poststring(actconfig['post_params'])
        end
        unless actconfig.has_key?('type')
            actconfig['type'] = actconfig['path'].gsub( /\?.*$/, '' ).gsub( /[#;].*/, '' )
        end
        if actconfig.has_key?('expr')
            if RUBY_VERSION >= "1.9"
                s = actconfig['expr'].encode(baseconfig['encoding'])
                actconfig['compiled_expr'] = Regexp::compile(s)
            else
                actconfig['compiled_expr'] = Regexp::compile(actconfig['expr'])
            end
        end
        actconfig['url'] = url
        time = actconfig['times'] || 1
        actconfig['times'] = time unless actconfig.has_key?('times')
        idx += 1
        actions << actconfig
    end
    scenario_size = actions.inject(0){|sum, i|
        sum + i['times']
    }
    @config['scenario_size'] = scenario_size
    @config['actions'] = actions
    @config['hosts'] = hosts
    #concurrency はデフォルトでスレッドと同じ
    if !@config.has_key?('max_scenario') || @config['max_scenario'] < @config['max_request']
        @config['max_scenario'] = @config['max_request']
    end
    @config['max_scenario'] = 1 if @config['max_scenario'] <= 0

    @config['max_scenario_show'] = @config['max_scenario']
    @config['max_request_show'] = @config['max_request']
    if @config['max_process'] > 1
        #fork する場合は、concurrency をプロセス数で割る
        process = @config['max_process']
        @config['max_process'] = process
        @config['max_scenario'] = (@config['max_scenario'] / process).round
        @config['max_scenario'] = 1 if @config['max_scenario'] <= 0
        @config['max_request'] = (@config['max_request']/ process).round
        @config['max_request'] = 1 if @config['max_request'] <= 0
    end
    ch = BaseHandler::get_class(@config["client_handler"])
    @config = ch::on_config_load(@config)
end