shitou's blog 

  • Home
  • About
  • Feed
  • 十人族
  • MIX BOY
  • 塌客

Category "ruby on rails"

14
Sep

大量数据的批量操作

如果你有10,000,000个用户,每周需要給他们发送最新的推广邮件,你可能会从数据库中这样操作:
User.each do |user|
    NewsLetter.weekly_deliver(user)
end
但这样做会让ruby从数据库中一次取出10,000,000条数据,并创建10,000,000个对象,服务器内存会大量消耗,因此这样做是不行的,
从rails 2.3开始你就可以使用find_each和find_in_batches方法了,这两个方法专门用于分批查询数据和处理,默认情况下find_each和find_in_batches每次会查出1000条数据记录,你可以可以指定每次取出的数量:batch_size,
User.find_each do |user|
    NewsLetter.weekly_deliver(user)
end
find_in_batches是返还一个数据对象:
User.find_in_batches(:batch_size => 5000) do |users|
    users.each { |user| NewsLetter.weekly_deliver(user) }
end


Tags:

2010-09-14 23:51:39, 898 reviews, comment

send to mailbox

Your email:

20
Feb

Rails 3

Rails 3 beta 已经发布了, 变化非常大, 细节就不说了, 看这里, 这里, Rails每次的版本变化都是一件头疼的事,gems, plugins版本依赖是个大问题, 升级不是件容易的事情, 况且这次的改动非常大, Rails 3 使用Ruby 1.8.7或者1.9

 

------------------------

顺带贴下ruby 1.9的变化

 

Ruby 1.9
View more presentations from Wen-Tien Chang.
Tags:

2010-02-20 09:15:32, 662 reviews, comment

send to mailbox

Your email:

04
Nov

10 Cool Things in Rails 2.3

Rails 2.3 10个比较棒的新特性

 

感觉Rails封装的越来越深了,太智能并不一定是好事

 

Tags:

2009-11-04 10:27:20, 618 reviews, comment

send to mailbox

Your email:

22
Oct

Rails Benchmark

命令行工具


benckmark

#ruby script/performance/benchmarker 3 "Product.find :all"

执行3次Product.find :all, 输出结果中real列为实际运行所消耗的时间

profiler

#ruby script/performance/profiler "Product.find :all" 10

执行10次Product.find :all, 列出所有涉及到的类库的运行时间

 

测试用例


比如测试View中某段代码的执行时间,

<% benchmark("Showing projects partial") do %>

   <%= render :partial => @projects %>

<% end %>

 

Model中测试,

Project.benchmark("Creating project") do

  project = Project.create("name" => "stuff")

   project.create_manager("name" => "David")

   project.milestones << Milestone.find(:all)

end

 

日志


Rails生成的日志,

Processing ItemsController#index (for 127.0.0.1 at 2009-01-08 03:06:39) [GET]

Rendering template within layouts/items

Rendering items/index

Completed in 5ms (View: 2, DB: 0) | 200 OK [http://0.0.0.0/items]

总的请求时间是5ms, View占了2ms, DB为0, 其他3ms为Controller占用,可以用shell来分析反应慢的请求

 

具体见这里

 

Tags:

2009-10-22 12:45:24, 753 reviews, comment

send to mailbox

Your email:

19
Oct

发送异常到邮箱

搞了ExceptionNotifier,觉得不好用,还是自己写吧

首先要配置好ActionMailer,戳我, 戳我, 戳我, 戳我

然后

#lib/exception_mail_notify.rb
class ExceptionMailNotify < ActionMailer::Base

  @@receivers = []
  @@subject_prefix = ''
  
  def self.receivers=(r)
    @@receivers = r
  end

  def self.subject_prefix=(s)
    @@subject_prefix = s
  end

  def self.receivers
    @@receivers
  end

  def app_exception_notify(recipient, body)
    from 'server@abc.com'
    recipients recipient
    subject "[#{@@subject_prefix}Exception] #{Time.now}"
    content_type "text/html"
    body body
  end
  
end

class ApplicationController < ActionController::Base

  protected

  def rescue_action(exception)
    if RAILS_ENV == 'production'
      error = "

" + exception.class.to_s + "

" + "

" + exception.message.inspect + "

" + exception.backtrace.join('
') ExceptionMailNotify.deliver_app_exception_notify ExceptionMailNotify.receivers.join(', '), error end super exception end end #app/controllers/application_controller.rb #收件人 ExceptionMailNotify.receivers = %w[a@abc.com b@abc.com] #邮件主题前缀 ExceptionMailNotify.subject_prefix = 'WWW '

 

你也可以写在其他controller里,对某一controller启作用

 

Tags: log

2009-10-19 16:34:57, 729 reviews, comment

send to mailbox

Your email:

09
Oct

Rails日志分析工具

Request-Log-Analyzer, 一个分析rails日志的工具,格式化输出结果,直接gem install request-log-analyzer; request-log-analyzer production.log;  就行了, 方便做性能分析使用,下面是测试的输出结果:

http://www.ccok.me/photo/image/48/medium/Screen_shot_2009-10-09_at_4.26.11_PM.png

 

Tags: log

2009-10-09 16:15:57, 875 reviews, comment

send to mailbox

Your email:

上一页 1 2 3 4 5 下一页

Tags

json Impactjs Canvas fun gen_server superfly PS3 Webgame HTML5 jquery SSH tenerer MooseFS gearman-ruby Gearman MongoDB MochiChat TCP U-ka saegusa IN db command Mai Kuraki Norah Jones log iPhoneException ACG Mac Safari objective-c CouchDB LVS AJAX debian 推荐 AMQP google mail bug gettext Erlang 北京 iptables 架构 tips mysql backup function 我看 postfix 监控 SEO cache Etag memcache thread 进程 线程 无锡 yield file column mixboy xml rss gems ruby shitou shell lighttpd 安全 csrf 公司 nginx linux 模块 apache webserver 朋友 大学 生活 尼古拉斯凯奇 movie 文件同步 笑笑 歌词 auto complete plugin rails music ubuntu blog

Category

  • HTML5[3]
  • iPhone[17]
  • Erlang[11]
  • google[13]
  • 生活[45]
  • 音乐[13]
  • 电影[11]
  • linux[29]
  • web server[6]
  • mail server[3]
  • cluster[1]
  • system manage[9]
  • ruby[20]
  • ruby on rails[28]
  • 开源[4]

Episode

  • MongoDB
  • MochiChat
  • iPhoneException
  • shell
  • thread
  • memcache


Popular Posts

  • 准备开始学习Erlang了(恶狼, 二郎..)
  • Ruby遍历MemCached的key
  • 服务器监控小脚本
  • MySQL Innodb备份
  • [转载]5个有用的ruby gems

Recommended Posts

  • Tri-survive - HTML5 Game
  • Cut the rope - HTML5版
  • json_formatter
  • 在gen_server中spawn新的进程
  • Superfly - Wildflowers
  • 继续凸墙 for Mac OS
  • MongoDB Beijing 2011
  • MongoDB删除map_reduce生成的tmp collection
  • Mai Kuraki - Future Kiss
  • Erlang OOP
  • 用SSH tunnel凸墙
  • Google的语法高亮工具包
  • 大量数据的批量操作
  • 再次被和谐-_-
  • 十人族: 上线了

Friends' blogs

  • levy
  • sphance
  • andreas
  • yangkunlun
  • {:dev=>:wxianfeng}
  • bheye
  • joeydarko

Login

   注册

留言 查看留言

留言

   取消

留言 查看留言


Statistics

  • 访问次数: 316242
  • 今天访问: 37
  • 日志: 213
  • 评论: 4967
  • 音乐: 9
  • 用户: 1758


少女,不点下广告吗!

 

all by shitou

blog comments powered by Disqus

close