Thursday, October 7, 2010

Switching to Snow Leopard, mysql and mongrel problems

for mysql, i had to re-install mysql using the 64bit version.

uninstall previous mysql: http://obscuredclarity.blogspot.com/2009/08/uninstall-mysql-on-mac-os-x.html

install mysql, follow this: http://hivelogic.com/articles/compiling-mysql-on-snow-leopard

use mysql version 5.1.39

for the mysql gem, use this:

> export ARCHFLAGS="-arch i386 -arch x86_64" ;sudo gem install --no-rdoc --no-ri mysql -v 2.7 -- --with-mysql-dir=/usr/local/mysql --with-mysql-config=/usr/local/mysql/bin/mysql_config

then..mongrel hangs without error, to fix, use this (thanks Darian Shimy):

> sudo gem uninstall mongrel fastthread
> sudo gem install mongrel

i hope this helps future me and some troubled person out there.

UPDATE:
But wait there's more

to fix ruby-debug
> sudo gem uninstall ruby-debug
> sudo gem uninstall ruby-debug-base
> sudo gem uninstall linecache
> sudo env ARCHFLAGS="-arch x86_64" gem install ruby-debug

not sure if the ARCHFLAGS was necessary..

Wednesday, September 29, 2010

httperf notes

Benchmark
- not on a live site
- proper database content (ours is not yet full)
- what data is needed
- how to use/analyze collected data
- how many requests
- parallels?
- dedicated machine to run benchmarking
- done per page (choose pages)
- num-conn: to increase the number of data samples
- rate: specifies the number of HTTP requests/second sent to the Web server -- indicates the number of concurrent clients accessing the server

- when the request/reply rate drops, that's when either client/server cant handle anymore connections, check the client on a fast server to see if it's the client that can't handle the requests

httperf

Short runs
httperf --hog --server samplesite.com --num-conn 100 --uri '/'
Reply rate [replies/s]: min 5.2 avg 6.5 max 7.4 stddev 1.2 (3 samples)
Reply rate [replies/s]: min 3.8 avg 4.8 max 5.8 stddev 1.0 (4 samples)
Reply rate [replies/s]: min 4.4 avg 4.7 max 5.2 stddev 0.5 (3 samples)
Reply rate [replies/s]: min 3.2 avg 4.7 max 6.4 stddev 1.4 (4 samples)
Reply rate [replies/s]: min 7.2 avg 7.5 max 7.8 stddev 0.4 (2 samples)
Reply rate [replies/s]: min 0.6 avg 4.8 max 7.2 stddev 2.9 (4 samples)
Reply rate [replies/s]: min 6.2 avg 6.5 max 6.8 stddev 0.3 (3 samples)
Reply rate [replies/s]: min 6.6 avg 6.9 max 7.2 stddev 0.4 (2 samples)
Reply rate [replies/s]: min 7.0 avg 7.1 max 7.2 stddev 0.1 (2 samples)
Reply rate [replies/s]: min 5.8 avg 6.1 max 6.6 stddev 0.4 (3 samples)
Reply rate [replies/s]: min 5.6 avg 6.1 max 7.0 stddev 0.8 (3 samples)
Reply rate [replies/s]: min 1.2 avg 3.9 max 5.6 stddev 1.6 (5 samples)
Reply rate [replies/s]: min 4.4 avg 5.6 max 6.4 stddev 1.1 (3 samples)
Reply rate [replies/s]: min 4.4 avg 5.4 max 6.0 stddev 0.9 (3 samples)
Reply rate [replies/s]: min 5.6 avg 6.4 max 7.0 stddev 0.7 (3 samples)

Long run
httperf --hog --server samplesite.com --num-conn 1000 --uri '/'
Reply rate [replies/s]: min 2.2 avg 5.0 max 6.8 stddev 1.0 (39 samples)

Load test (?)
httperf --hog --server coolmovies.binaries.sg --num-conn 50 --rate 7

Friday, July 2, 2010

disable asset timestamp in rails

put in config/environment.rb

ENV['RAILS_ASSET_ID'] = ''

Friday, June 25, 2010

Escape Single Quotes in Ruby Strings

Got this from http://snippets.dzone.com/posts/show/880

class String
  def escape_single_quotes
    self.gsub(/'/, "\\\\'")
  end
end 
Usefull when passing strings to javascript. I placed mine in config/initializers

Wednesday, June 16, 2010

URI in JS

use: encodeURIComponent() to values passed in params especially in passing strings


Notes on complex forms

Complex Forms
- virtual attribute
++++++++++++++++++++++++++++
         NEW/CREATE
++++++++++++++++++++++++++++
*controller
[NEW]
1. Build object

  @customer.addresses.build

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

*view
[MAIN]
1. form_for
2. Make div of object which would have many instances (ie:addresses)

  #addresses
    = render :partial => 'address', :collection => @customer.addresses
  = add_address_link "Add Address"


[PARTIAL]
new method:

  <% new_or_existing = task.new_record? ? 'new' : 'existing' %>
  <% prefix = "project[#{new_or_existing}_task_attributes][]" %>

  <% fields_for prefix, task do |task_form| -%>
   
      Task: <%= task_form.text_field :name %>
      <%= link_to_function "remove" , "$(this).up('.task').remove()" %>
   
  <% end -%>

---
1. fields_for

  - fields_for "customer[address_attributes][]", address do |af|


2. Add link to remove

  = link_to_function "Remove","$(this).up('.tablecontainer').remove()"

**tablecontainer will be removed
--------------

*helper
1. For adding another class instance

  def add_address_link(name)
    link_to_function name do |page|
      page.insert_html :bottom, :addresses, :partial => 'address', :object => NinAddress.new
    end
  end


========================
insert_html(position, id, *options_for_render)

Inserts HTML at the specified position relative to the DOM element identified by the given id.

position may be one of:
:top:    HTML is inserted inside the element, before the elementës existing content.
:bottom: HTML is inserted inside the element, after the elementës existing content.
:before: HTML is inserted immediately preceding the element.
:after:     HTML is inserted immediately following the element.

Note:
- use ':bottom' if inserting new table

  #div_name
    = render...
  = add_link


- use ':after' if inserting new row

  %tr#div_name
    = render ...**
  = add_link

** render starts with %tr as well
==========================

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

*model
1. define virtual attribute
new:
def new_task_attributes=(task_attributes)


  def address_attributes=(address_attributes)
    address_attributes.each do |attributes|
      addresses.build(attributes)
    end
  end

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

+++++++++++++++++++++++++++
        EDIT/UPDATE
+++++++++++++++++++++++++++

*controller
def edit
  @project = Project.find(params[:id])
end
def update
  params[:project][:existing_task_attributes] ||= {}
  @project = Project.find(params[:id])
  if @project.update_attributes(params[:project])
    flash[:notice] = "Successfully updated project and tasks."
    redirect_to project_path(@project)
  else
    render :action => 'edit'
  end
end

*model
validates_associated :tasks
after_update :save_tasks

def existing_task_attributes=(task_attributes)
  tasks.reject(&:new_record?).each do |task|
  attributes = task_attributes[task.id.to_s]
    if attributes
      task.attributes = attributes
    else
      tasks.delete(task)
    end
  end
end

def save_tasks
  tasks.each do |task|
    task.save(false)
end
end

END ^_^

Wednesday, June 9, 2010

git: delete last commit

git reset --hard HEAD~1
 
HEAD~1 is the commit before the HEAD branch. You could also use the SHA-1 hash of the
commit you want to revert to.
 
Using --hard would remove all untracked files in your working directory so BACKUP! 
 
git revert HEAD
 
If however you have pushed the wrong commit already. Use this. This one makes another 
commit from HEAD (or whatever commit you want to revert to). Then push it again.