File.open('filename').each_line do |line|
color, x, y = line.split
end
会不会是 js 一些操作耗时或者读取外部资源耗时?
MongoDB 没有固定模式,所以要读取一条数据才能确定它有什么字段和类型。
Mongoid 的话用 ModelName.fields
可以获得自己在 Model 里面定义的字段信息。
whenever,了解下系统的 cron
#7 楼 @jfreebird 期待你拼装出一个 Rails。
#8 楼 @sanivbyfish 晕,我以为在说 ActiveRecord。
mongoid 是 order_by http://mongoid.org/en/origin/docs/options.html#sorting
#6 楼 @sanivbyfish 贴一下你加上 order 后的代码
#4 楼 @sanivbyfish commects 不是一个 associations(has_many)吗?
post.commects.order("xxx DESC").each
如果所有的 comments 都需要这个 order,可以加 default_scope
#2 楼 @beihuiguixian 是的。
可以放在 lib/ 下面。
喜欢 Mailgun 的 api 和文档,Postmark 网站设计太刺了。
推荐楼主一本书 http://book.douban.com/subject/3288908/ 集体智慧编程
编程不是目的,编程去解决问题才是目的,所以要知道你要解决什么问题才知道需要用什么知识。
顺便,我把模板从 erb 换到 slim,发现性能有提升
- 128000.times do
= rand(8999)+1000
Server Software: nginx/1.2.3
Server Hostname: localhost
Server Port: 4000
Document Path: /
Document Length: 512450 bytes
Concurrency Level: 10
Time taken for tests: 6.919 seconds
Complete requests: 100
Failed requests: 0
Write errors: 0
Total transferred: 51319300 bytes
HTML transferred: 51245000 bytes
Requests per second: 14.45 [#/sec] (mean)
Time per request: 691.885 [ms] (mean)
Time per request: 69.188 [ms] (mean, across all concurrent requests)
Transfer rate: 7243.47 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.1 0 0
Processing: 454 672 103.5 677 911
Waiting: 454 670 103.8 673 910
Total: 454 672 103.5 677 911
Percentage of the requests served within a certain time (ms)
50% 677
66% 709
75% 753
80% 778
90% 808
95% 864
98% 880
99% 911
100% 911 (longest request)
Started GET "/" for 127.0.0.1 at 2012-11-24 22:26:31 +0800
Processing by MainController#index as */*
Rendered main/index.html.slim within layouts/application (703.5ms)
Completed 200 OK in 905ms (Views: 704.9ms | ActiveRecord: 0.0ms)
Rendered main/index.html.slim within layouts/application (482.9ms)
Completed 200 OK in 685ms (Views: 484.5ms | ActiveRecord: 0.0ms)
循环放在 View 里面的日志
Started GET "/" for 127.0.0.1 at 2012-11-24 22:20:45 +0800
Processing by MainController#index as */*
Rendered main/index.html.erb within layouts/application (1670.9ms)
Completed 200 OK in 1873ms (Views: 1672.4ms | ActiveRecord: 0.0ms)
移出后的日志
Started GET "/" for 127.0.0.1 at 2012-11-24 22:23:24 +0800
Processing by MainController#index as */*
Rendered main/index.html.erb within layouts/application (1.6ms)
Completed 200 OK in 410ms (Views: 3.1ms | ActiveRecord: 0.0ms)
可见效率是有差异的。不过我觉得一般不会用到 128000 这么大的循环,View 按正常写就行了。
测试了一下,用 passenger start -p 4000 -e production --max-pool-size 100 --min-instances 10 启动
ab -c 10 -n 100 http://localhost:4000/
按原文的代码
Server Software: nginx/1.2.3
Server Hostname: localhost
Server Port: 4000
Document Path: /
Document Length: 512451 bytes
Concurrency Level: 10
Time taken for tests: 21.563 seconds
Complete requests: 100
Failed requests: 0
Write errors: 0
Total transferred: 51319400 bytes
HTML transferred: 51245100 bytes
Requests per second: 4.64 [#/sec] (mean)
Time per request: 2156.277 [ms] (mean)
Time per request: 215.628 [ms] (mean, across all concurrent requests)
Transfer rate: 2324.22 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.1 0 0
Processing: 1273 2107 311.6 2128 2994
Waiting: 1273 2106 311.6 2128 2994
Total: 1273 2107 311.7 2128 2994
Percentage of the requests served within a certain time (ms)
50% 2128
66% 2290
75% 2336
80% 2362
90% 2460
95% 2544
98% 2747
99% 2994
100% 2994 (longest request)
把 view 里面的循环移到 controller
class MainController < ApplicationController
def index
sleep(0.2)
@content = 128000.times.map{rand(8999)+1000}.join
end
end
<%= @content %>
结果有了不少改善
Server Software: nginx/1.2.3
Server Hostname: localhost
Server Port: 4000
Document Path: /
Document Length: 512451 bytes
Concurrency Level: 10
Time taken for tests: 4.817 seconds
Complete requests: 100
Failed requests: 0
Write errors: 0
Total transferred: 51319400 bytes
HTML transferred: 51245100 bytes
Requests per second: 20.76 [#/sec] (mean)
Time per request: 481.690 [ms] (mean)
Time per request: 48.169 [ms] (mean, across all concurrent requests)
Transfer rate: 10404.33 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.1 0 0
Processing: 363 466 97.4 442 768
Waiting: 362 465 97.2 442 768
Total: 363 466 97.4 442 768
Percentage of the requests served within a certain time (ms)
50% 442
66% 480
75% 502
80% 513
90% 593
95% 735
98% 767
99% 768
100% 768 (longest request)
结论是
我是毕业半年后才找工作,没提三方协议。