2009年2月27日 星期五

Performance Research (2)

Maximizing Parallel Downloads in the Carpool Lane
Our rule of thumb is to increase the number of parallel downloads by using at least two, but no more than four hostnames. Once again, this underscores the number one rule for improving response times: reduce the number of components in the page.


Less is More — Serving Files Faster by Combining Them
* Combine scripts and stylesheets to reduce HTTP requests.
* Look at different types of file combinations.
* Avoid users from having to download more than they really need.
* Pay special attention to the order in which files are combined.

2009年2月25日 星期三

Yahoo performance research

Performance Research, Part 1: What the 80/20 Rule Tells Us about Reducing HTTP Requests
The conclusion is the same: Reducing the number of HTTP requests has the biggest impact on reducing response time and is often the easiest performance improvement to make. In the next article we’ll look at the impact of caching, and some surprising real-world findings.


Performance Research, Part 2: Browser Cache Usage - Exposed!

Conclusion: Keep in mind the empty cache user experience. It might be more prevalent than you think!

Performance Research, Part 3: When the Cookie Crumbles
* Eliminate unnecessary cookies.
* Keep cookie sizes as low as possible to minimize the impact on the user response time.
* Be mindful of setting cookies at the appropriate domain level so other sub-domains are not affected.
* Set an Expires date appropriately. An earlier Expires date or none removes the cookie sooner, improving the user response time.

composite key

Composite Primary Keys -> Ruby on Rails => ActiveRecord
When to use composite key

2009年2月24日 星期二

install passenger, mod_rails

在機車的環境裝遇到問題啦, 明明apache 裝了 可是它卻找不到, 又是path 問題
請參考這篇How To Install Phusion Passenger / mod_rails / mod_passenger


我的目錄是apache2 也是export 這兩個就行了
export APXS2=/usr/local/apache2/bin/apxs
export APR_CONFIG=/usr/local/apache2/bin/apr-1-config

p3p, cookie

iframe跨域session丟失問題
Session variables are lost if you use FRAMESET in Internet Explorer 6
Privacy in Internet Explorer 6

在abc.com 的網域下, 嵌了一個 def.com 的 iframe, abc 下的 cookie, def 讀不到合情合理, 但是會發現我在def.com 下 想寫一個在def.com domain 下的cookie 在ie也會有問題, 這是因為ie privacy policy 的原因

reference:
IE6/IE7支持的P3P(Platform for Privacy Preferences Project (P3P) specification)協議默認阻止第三方無隱私安全聲明的cookie,Firefox目前還不支持P3P安全特性,firefox中自然也不存在此問題了。Mircosoft對此的具體描述可以參見 Privacy in Internet Explorer 6


p3p不能讓其他域讀取到第三方域的cookie
只是使 第三方cookie在能夠在本域內被存取
比如一個 www.cctv.com中有個服務應用 是 cnc.com域的,
通常情況下用戶的隱私策略設置為中或者更高 在cnc.com域的cookie是不能夠正常讀取的,服務端能夠改變 用戶對cnc.com域的隱私策略使cnc.com域下的cookie正常存取

example

Accessing session data using session_id

Accessing session data using session_id

cookies in rails

Rails Cookies Mangles the Hash Interface
cookies api
Session and cookie in rails


cookies 不是單純的 hash 而已, 簡單看這個例子就可以明白

So,
cookies[] gives you value from the incoming cookie.
cookies[]= sets value in the outgoing cookie.

The server needs to send the cookie to the browser and the browser has to return the cookie. Then you can read the value.

cookies[:its] = "1999"

*server response
*browser request

p cookies[:its]
#=> "1999"

2009年2月18日 星期三

instanceof, typeof

instanceof, typeof

typeof 會返回的結果只有 number,boolean,string,function,object,undefined,
對於Array,Null等特殊對象使用typeof一律返回object,這正是typeof的侷限性。, 注意:undefined 是 undefined, null 是 object

而如果是Object的話, 可以用instanceof 進一步確認是什麼object


至於如何辨識他是什麼Object, 可以利用toString, 但是因為常常被overwrite 掉, 所以就必須依賴prototype, 不過這招也只有內建的型態才有用

2009年2月17日 星期二

outerHTML

$('#here').attr('outerHTML');

object.innerHTML: object所包含的元素的HTML,不包含object
object.outerHTML: object及它所包含元素的HTML,包含object

jquery 的 html 是innerHTML, 想連自己也拿到就是利用attr('outerHTML')

javascript, iframe

How to access iframe in jQuery
外面拿iframe 裡面的東西


get dimensions of the parent from the child iframe

在iframe裡面拿外面的東西


IE has a security precaution
想把iframe 裡面東西跟parent 搬來搬去, 不行
出錯在appendChild的時候, 因為dom對象不能跨document傳

2009年2月15日 星期日

javascript memo: Classes, Constructors, and Prototypes

This solution works better but is still not optimal. Every rectangle created will have three properties. The width and height properties may be different for each rectangle, but the area of every single Rectangle object always refers to the same function (someone might change it, of course, but you usually intend the methods of an object to be constant). It is inefficient to use regular properties for methods that are intended to be shared by all objects of the same class (that is, all objects created with the same constructor).


Note that inheritance occurs automatically as part of the process of looking up a property value. Properties are not copied from the prototype object into new objects; they merely appear as if they were properties of those objects. This has two important implications. First, the use of prototype objects can dramatically decrease the amount of memory required by each object because the object can inherit many of its properties. The second implication is that an object inherits properties even if they are added to its prototype after the object is created. This means that it is possible (though not necessarily a good idea) to add new methods to existing classes.


Inherited properties behave just like regular properties of an object. They are enumerated by the for/in loop and can be tested with the in operator. You can distinguish them only with the Object.hasOwnProperty( ) method:

javascript memo: function



Arguments

callee:
The Arguments object defines a callee property that refers to the function that is currently being executed.

Using Object Properties as Arguments

instanceof和typeof


apply and call

The Function() Constructor

2009年2月14日 星期六

jquery ajax

pecifying the Data Type for AJAX Requests
Ajax Event
jquery ajax api

注意一下的參數
cache
Added in jQuery 1.2, if set to false it will force the pages that you request to not be cached by the browser.

complete

A function to be called when the request finishes (after success and error callbacks are executed). The function gets passed two arguments: The XMLHttpRequest object and a string describing the type of success of the request.



contentType
When sending data to the server, use this content-type. Default is "application/x-www-form-urlencoded", which is fine for most cases.

data
Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key i.e. {foo:["bar1", "bar2"]} becomes '&foo=bar1&foo=bar2'.

processData
By default, data passed in to the data option as an object (technically, anything other than a string) will be processed and transformed into a query string, fitting to the default content-type "application/x-www-form-urlencoded". If you want to send DOMDocuments, or other non-processed data, set this option to false.

password
A password to be used in response to an HTTP access authentication request.

username
A username to be used in response to an HTTP access authentication request.

2009年2月13日 星期五

const_defined? & const_get, class_eval, instance_methods

Module api

2009年2月9日 星期一

javascript memo(2) scripting browser window

14.2.2 loading new document

下面這句話 測不出來
In most cases, document.location is the same as location.href. When there is a server redirect, however, document.location contains the URL as loaded, and location.href contains the URL as originally requested.


14.2.3 The History Object
Netscape and Mozilla-based browsers also support back( ) and forward( ) methods on the Window object itself. These nonportable methods perform the same action as the browser's Back and Forward buttons. When frames are used, window.back( ) may perform a different action than history.back( ).



open 可以設定參數, open 會傳回代表開啟的 window object, opener 則是代表開啟被開啟視窗的window object, 如果視窗不是由javascript 開啟, 是由user 開啟, opener 屬性就會是 null

window.close, 一般而言只能關閉由妳javascript 產生的 window, 由user 產生的會丟出confirm box

window object 會在其代表視窗關掉之後依然存在, 所以有時有必要check close attribute 來確認視窗是否已經關閉
window.closed




function 是在其所定義的地方執行, 不是在被呼叫的地方, 所以妳如果呼叫的是上層的function
那該function 其所在的document 或是 window 當然也是上層的, 所以如果要用某些function 取用自己的window, document 還是要自己include 該 function

2009年2月8日 星期日

javascript memo(1), same-origin policy

The definitive Guide 5th
13.8.2 The Same-Origin Policy

就是要相同domain, protocol, port
script 的source 跟same-origin policy 沒關係, 重點是它被embedded 的地方

假設domain B 下的 document 包含了一個script, 而這個script 的 source domain 是在domain A 下, 這個script 仍可以控制這個domain B 下的 document(就像我們可以引入放在google 的jquery 一樣) if the script opens a new window and loads a second document from domain B, the script also has full access to the content of that second document. But if the script opens a third window and loads a document from domain C (or even from domain A) into it, the same-origin policy comes into effect and prevents the script from accessing this document.



13.2.1
在同個HTML網頁裡, 即時是不同的script 區塊, file, 後loading 的script 一樣可以用在它先前load 進來的 function

13.2.2
<[script src="../../scripts/util.js"]><[/script]>

有src 的script tag, 相當於把 file 裡的script, 直接放在

13.2.5
<[noscript]> tag

13.4.1
bookmarklet

13.5.2 onload Event Handler
After the document is parsed, all scripts have run, 還有其他所有像是image 就是所有東西都載入了就會呼叫onload, window.load(){//blah}, 所以這時如果在這邊叫了document.write 就會覆蓋掉所有的內容

13.5.4 onunload Event Handler
離開網頁時會trigger 的

13.6.5 conditional Comments in internet Explorer

2009年2月5日 星期四

Activerecord memo (3)

ActiveRecord::Base
save, save!, create, create!, update_attribute, update_attributes, update, update_all, delete, delete_all, destroy, destroy_all

1. save vs save!
save 成功 => true
save 失敗 => fasle

另外 save(perform_validation = true), save 還可以丟個參數進去, save(false) 的話, 一堆validate 都不跑, 可是會跑before_*, 失敗一樣是return false

save! 成功 => true
save! 失敗 => validate 失敗:RecordInvalid execption,
before_* 失敗 : RecordNotSaved



2. create vs create!
create 成功 => ActiveRecord object


create 失敗 => ActiveRecord object, 所以要確認成功失敗可以用 errors.empty? 檢查


create! 成功 => ActiveRecord object
create! 失敗 => RecordInvalid



3. 如果是資料庫的validate 就會爆 StatementInvalid
例如有這樣的設定 t.column :user_id, :integer, :null => false
沒填user_id 就會爆StatementInvalid



4. update_attribute vs update_attributes
除了可以更新update的多寡之外,
還有 update_attribute 是 save(false), 它不跑 validate

同樣成功true, 失敗false



5 update, update_all
丟的參數差很多
update(id, attributes)
update_all(updates, conditions = nil, options = {})





6.delete_all, delete, destroy, destroy_all
delete 是不會跑before_, after_ destroy 還有 dependent 也是不跑的

destroy_all(conditions = nil)
delete_all(conditions = nil)




7. ConnectionAdapters::DatabaseStatements

connection.select_all



8. ActiveRecord::ConnectionAdapters::AbstractAdapter

ActiveRecord::Base.connection returns an AbstractAdapter object,

Activerecord memo (2)

Find的參數
1.select

2.group

統計的method
有average, maximum, minimum, sum, count
1. count, group

2. maximum

memo: find_by_sql, count_by_sql

find_or_initialize_by, find_or_create_by

2009年2月4日 星期三

Activerecord memo (1)

1. 如果table name不合 rails 的規則時, 可以自己設定
2. ActirveRecord 提供了一些跟欄位相關的method
3. 除了table column的屬性之外, model 也可以拿到其他的屬性

4. Facade column, 有些時候欄位的格式不是最好用的, 如上面的浮點數 string, 這時就可以利用read_attribute 和 write_attribute來達到

5. before_type_cast, ActiveRecord 會把 attribute reader (無論是attr_reader 或 read_attribute)讀回來的東西強制轉成Ruby type(根據database 的column type 去決定想轉成啥),
6. primary_key 也可以自己指定, 而我們仍是用 id 這樣的屬性去呼叫這個primary key

7. find object 的時候如果是直接照主鍵去找的才會丟 RecordNotFound

2009年2月3日 星期二

iframe,src,domain

iframe 的 src 連去的這個 document 它的 domain, protocol 會影響, 例如

abc.iwakela.com

如果A 這個document的domain 跟外面不同, 意思就是說沒有設定的話, 外面的domain 是abc.iwakela.com, 而iframe的是 www.iwakela.com

要在A裡面去用像 parent.frames["B"] 來取B, 只也能是拿到B iframe, 可是取用其函式或屬性就就是必須domain等相同, 所以也是等於啥都拿不到, 在domain或port 都不同的情況下, 唯一能用的就是像 A.location.href 這樣的 redirect function
所以最好是手動去將雙方設成iwakela.com 這樣的domain (document.domain = "iwakela.com")

當然如果有用到https,雖然 document.domain 都是 iwakela.com,結果也會無法互相叫用了, 因為port 不同阿 XD


parent.frames['A'] 跟 parent.frames['B'] 的 domain 和 port 都一樣的話,所以可以通這兩個就可以互通(就算跟外面, 也就是parent 不同也沒差), 從 B 拿 parent 的所有東西都拿不到,但是 frames 可以拿得到,這是瀏覽器刻意支援,目的應該就是要滿足類似我們這種需求

結論
同 document.domain 且 同 port 時
, 你可以存取所有跟你相同 document.domain 的屬性和函式
不同 document.domain 或不同 port 時, 你只能呼叫它 redirect,其他幾乎什麼都不行

AP與DBMaker的搭配 – Performance Tuning初探

AP與DBMaker的搭配 – Performance Tuning初探

節錄

Database的原理,與上述的情形頗為類似,其中所謂的三層館藏,指的是實際存放資料的硬碟或其他儲存媒體,「工作平台」指的是記憶體。所有資料庫的搜尋或傳給客戶端的資料,都必須一定要先放在記憶體,搜尋動作會損耗CPU資源,但資料的處理、搜尋都必須先放在記憶體中,此處意指記憶體有 3000個單位(pages)。

所謂「客戶詢問」,指的便是由客戶端來的各式各樣的query,而無論客戶端的query如何,資料庫一定得將資料找到,並傳回客戶端。



索引如同其他資料頁一樣,必須先載入至記憶體後,CPU才能針對其中的內容,進行「比較」運算;而由於磁碟速度極慢,DBMaker會將常用的資料頁(含索引頁)儘量放在記憶體中,如此一來,當要再搜尋某資料頁時,就有相對較大的可能,可以在記憶體中找到,而不須到磁碟上去搜尋。



在定義索引時,自然也是以「排除效果」(filter effect)大的為佳,在條件判斷過程中,並不是只搜尋索引頁,而是有可能進一步搜尋資料頁,所以要想辦法讓第一次搜尋索引頁時,儘可能第一時間排除所有「不可能」的資料,上述中,作者的排除效果顯然就沒有書名好,也可以想作,書名比較「unique」



有許多SA,在設計之初,將所有query可能用到的欄位,都一一設定成索引。由上述的推演可以看出,這樣的作法不見得明智,理論上,DBMaker會從最符合的索引開始搜尋(如上述的書藉-作者索引),但搜尋所花的工夫,卻還不如用單一的作者索引來得快。在設計索引時,必須要將這些因素考量在內,複合鍵有時會造成索引變大,甚至過肥, I/O加重的結果,反而拖慢了performance。

有些query是很難去定義出一個好的index,這時只要是「還好」的索引,可能就夠了,上述的 query,若將全部欄位建立成索引,可能會造成索引肥大,不見得好,建議將排除效果的欄位先建立好,若是能將所有搜尋的資料頁降低到25%以下,就算是不錯的索引,有些SA甚至會定義「血型」、「性別」為索引,這種索引的排除效果都不是甚佳,除非必要,這樣的索引應儘量避免。

另外常見的情形,便是一個常被變動的表格,本身資料量也很大,也常被查詢,表格本身可能有十個以上的索引,因為常變動,任何的變動都會造成索引值須被更新,十個以上的索引頁,都須被放入記憶體以便進行「新增索引」的動作,想想,這會造成系統效能多大的負擔?!



記憶體的大小調整,是所有DBA在系統效能調整時,必須先考量的,有些評比報告,常將兩個不同記憶體大小的資料庫效能加以比較,在先天上將造成了不公平,評比的結果自然也讓人存疑,在linux journal中文版雜誌2001年9月號,針對某家資料庫的效能調整,全文都在探討如何放大記憶體,由此可見記憶體大小對performance的重要。
記憶體當然能放大就儘量放大,讓所有資料能儘量在記憶體中,減少I/O的次數。但這樣也須付出代價,便是當系統毀損時,變動的資料都可能還在記憶體中,雖然DBMaker有LOG可記錄所有變動,但還是會讓recovery的時間拉長。有些個案甚至造成了資料庫的毀損,所以在設計系統時,亦將此因素一併考量,DBMaker在4.0後,會針對系統可用的資源,自動幫使用者劃好可用記憶體,但若使用者本身有其他考量,當然也可自己設定,而不依賴系統的調整; DBMaker要調整記憶體大小,相當容易,只要去調整dmconfig.ini中的DB_NBUFS即可,詳細內容,請參閱Administrator Guide。

sql index

index introduction
clustered index vs nonclustered index

節錄

聚簇索引非常象目錄表,目錄表的順序與實際的頁碼順序是一致的。非聚簇索引則更象書的標準索引表,索引表中的順序通常與實際的頁碼順序是不一致的。一本書也許有多個索引。例如,它也許同時有主題索引和作者索引。同樣,一個表可以有多個非聚簇索引。

聚簇索引的順序就是數據的物理存儲順序,而對非聚簇索引的解釋是:索引順序與數據物理排列順序無關。正式因為如此,所以一個表最多只能有一個聚簇索引。

在SQL Server中,索引是通過二叉樹的數據結構來描述的,我們可以這麼理解聚簇索引:索引的葉節點就是數據節點。而非聚簇索引的葉節點仍然是索引節點,只不過有一個指針指向對應的數據塊。

索引屬性

這兩種類型的索引都有兩個重要屬性:你可以用兩者中任一種類型同時對多個字段建立索引(復合索引)﹔兩種類型的索引都可以指定為唯一索引。

你可以對多個字段建立一個復合索引,甚至是復合的聚簇索引。假如有一個表記錄了你的網點訪問者的姓和名字。如果你希望根據完整姓名從表中取資料,你需要建立一個同時對姓字段和名字字段進行的索引。這和分別對兩個字段建立單獨的索引是不同的。當你希望同時對不止一個字段進行查詢時,你應該建立一個對多個字段的索引。如果你希望對各個字段進行分別查詢,你應該對各字段建立獨立的索引。

兩種類型的索引都可以被指定為唯一索引。如果對一個字段建立了唯一索引,你將不能向這個字段輸入重復的值。一個標識字段會自動成為唯一值字段,但你也可以對其它類型的字段建立唯一索引。假設你用一個表來保存你的網點的會員密碼,你當然不希望兩個會員有相同的密碼。通過強製一個字段成為唯一值字段,你可以防止這種情況的發生。


一、索引塊與數據塊的區別

大家都知道,索引可以提高檢索效率,因為它的二叉樹結構以及佔用空間小,所以訪問速度塊。讓我們來算一道數學題:如果表中的一條記錄在磁盤上佔用 1000字節的話,我們對其中10字節的一個字段建立索引,那麼該記錄對應的索引塊的大小只有10字節。我們知道,SQL Server的最小空間分配單元是「頁(Page)」,一個頁在磁盤上佔用8K空間,那麼這一個頁可以存儲上述記錄8條,但可以存儲索引800條。現在我們要從一個有8000條記錄的表中檢索符合某個條件的記錄,如果沒有索引的話,我們可能需要遍歷8000條×1000字節/8K字節=1000個頁面才能夠找到結果。如果在檢索字段上有上述索引的話,那麼我們可以在8000條×10字節/8K字節=10個頁面中就檢索到滿足條件的索引塊,然後根據索引塊上的指針逐一找到結果數據塊,這樣IO訪問量要少的多。


二、索引優化技術

是不是有索引就一定檢索的快呢?答案是否。有些時候用索引還不如不用索引快。比如說我們要檢索上述表中的所有記錄,如果不用索引,需要訪問8000 條×1000字節/8K字節=1000個頁面,如果使用索引的話,首先檢索索引,訪問8000條×10字節/8K字節=10個頁面得到索引檢索結果,再根據索引檢索結果去對應數據頁面,由於是檢索所有數據,所以需要再訪問8000條×1000字節/8K字節=1000個頁面將全部數據讀取出來,一共訪問了 1010個頁面,這顯然不如不用索引快。

Migration 基礎知識

1. migration 裡要建立浮點數, decimal, float 都可以, 也建議加上 precision, scale 兩個 options

2.要建立one-way migration, 可以在down method 裡丟出 ActiveRecord::IrreversibleMigration 這樣的execption

3. options 的參數可以讓我們指定底層資料庫的選項, 例如 :options => 'default charset=utf8'

4. ActiveRecord::Base.connect 的boject 可以呼叫所有的migration method, 所以我們在model, controller, view 裡也都可以用它, BTW

2009年2月2日 星期一

Time,Date

time api

require,require_dependency,extend,include

extend vs include
* extend - class level
* include - instance level

require vs require_dependency
require_dependency wiki

require 是 ruby 的, require_dependency 是rails 的, 在development mode 時, 每個request 都會在load 一下

javascript,iframe,name