关于RoR的性能研究 一

1、我们新建一个RoR项目

$ rails new hello_word
$ cd hello_word/

$ rails g controller hello say_hello

2、编辑hello_controller.rb如下

$ gedit app/controllers/hello_controller.rb

class HelloController < ApplicationController
  def say_hello
    info = ""
    info += "当前进程号:" + Process.pid.to_s + "\n"
    info += "当前线程号:" + Thread.current.object_id.to_s + "\n"
    info += "开始时间:" + Time.now.to_s + "\n"
    sleep(10)
    info += "结束时间:" + Time.now.to_s
    render plain: info
  end
end

3、启动

$ rails server -b 0.0.0.0 -p 3000 -e development

[6143] Puma starting in cluster mode...
[6143] * Version 3.11.4 (ruby 2.4.1-p111), codename: Love Song
[6143] * Min threads: 5, max threads: 5
[6143] * Environment: development
[6143] * Process workers: 2
[6143] * Phased restart available
[6143] * Listening on tcp://0.0.0.0:3000
[6143] Use Ctrl-C to stop
[6143] - Worker 0 (pid: 6157) booted, phase: 0

[6143] - Worker 1 (pid: 6161) booted, phase: 0

可以看到Puma启动了2个进程,每个进程里面有5个线程来运行hello/say_hello

4、在浏览器内快速开启5个请求

http://192.168.0.37:3000/hello/say_hello

输出如下:

当前进程号:6161
当前线程号:38784120
开始时间:2018-06-20 01:30:05 -0700
结束时间:2018-06-20 01:30:15 -0700
当前进程号:6161
当前线程号:38784120
开始时间:2018-06-20 01:30:15 -0700
结束时间:2018-06-20 01:30:25 -0700
当前进程号:6161
当前线程号:38784120
开始时间:2018-06-20 01:30:25 -0700
结束时间:2018-06-20 01:30:35 -0700
当前进程号:6161
当前线程号:38783900
开始时间:2018-06-20 01:30:27 -0700
结束时间:2018-06-20 01:30:37 -0700
当前进程号:6161
当前线程号:38783520
开始时间:2018-06-20 01:30:27 -0700
结束时间:2018-06-20 01:30:37 -0700

可见,实际执行hello/say_hello的有1个进程,3个线程

进程6161线程38784120:30:05~30:15,30:15~30:25,30:25~30:35

进程6161线程38783900:30:27~30:37

进程6161线程38783520:30:27~30:37

5、更改Puma配置

改为1个进程1个线程再执行

$ gedit config/puma.rb
threads_count = ENV.fetch("RAILS_MAX_THREADS") {1}

workers ENV.fetch("WEB_CONCURRENCY") {1}

$ rails server -b 0.0.0.0 -p 3000 -e development

[6355] Puma starting in cluster mode...
[6355] * Version 3.11.4 (ruby 2.4.1-p111), codename: Love Song
[6355] * Min threads: 1, max threads: 1
[6355] * Environment: development
[6355] * Process workers: 1
[6355] * Phased restart available
[6355] * Listening on tcp://0.0.0.0:3000
[6355] Use Ctrl-C to stop
[6355] - Worker 0 (pid: 6373) booted, phase: 0

结果如下

当前进程号:6373
当前线程号:27653500
开始时间:2018-06-20 01:49:00 -0700
结束时间:2018-06-20 01:49:10 -0700
当前进程号:6373
当前线程号:27653500
开始时间:2018-06-20 01:49:11 -0700
结束时间:2018-06-20 01:49:21 -0700
当前进程号:6373
当前线程号:27653500
开始时间:2018-06-20 01:49:21 -0700
结束时间:2018-06-20 01:49:31 -0700
当前进程号:6373
当前线程号:27653500
开始时间:2018-06-20 01:49:31 -0700
结束时间:2018-06-20 01:49:41 -0700
当前进程号:6373
当前线程号:27653500
开始时间:2018-06-20 01:49:41 -0700
结束时间:2018-06-20 01:49:51 -0700

6、结论

· RoR是支持多进程/多线程部署的

· RoR的进程/线程调度取决于web服务器

· RoR的性能很大程度上依赖于web服务器

猜你喜欢

转载自blog.csdn.net/YongYu_IT/article/details/80747865