2016年8月30日 星期二

Unity Test 單元測試





Principle 

is to isolate each part of the program and show that the individual parts are correct. A unit test provides a strict, written contract that the piece of code must satisfy. As a result, it affords several benefits.

Ideally, each test case is independent from the others. Substitutes such as method stubsmock objects, fakes, and test harnesses can be used to assist testing a module in isolation


Benefits


  • Find problems early
  • TDD helps you to realise when to stop coding. Your tests give you confidence that you've done enough for now and can stop tweaking and move on to the next thing.
  • Unit Tests help you really understand the design of the code you are working on. Instead of writing code to do something, you are starting by outlining all the conditions you are subjecting the code to and what outputs you'd expect from that.
  • Contrary to popular belief unit testing does not mean writing twice as much code, or coding slower. It's faster and more robust than coding without tests once you've got the hang of it.



limitation


  • 測試不可能發現所有的程式錯誤,單元測試也不例外。
  • 單元測試只測試程式單元自身的功能。因此,它不能發現整合錯誤、效能問題、或者其他系統級別的問題。
  • 單元測試只能表明測到的問題,不能表明不存在未測試到的錯誤。







2016年8月27日 星期六

code refactoring







代碼重構 code refactoring




There are two general categories of benefits to the activity of refactoring.

  1. Maintainability. It is easier to fix bugs because the source code is easy to read and the intent of its author is easy to grasp.[4] This might be achieved by reducing large monolithic routines into a set of individually concise, well-named, single-purpose methods. It might be achieved by moving a method to a more appropriate class, or by removing misleading comments.
  2. Extensibility. It is easier to extend the capabilities of the application if it uses recognizable design patterns, and it provides some flexibility where none before may have existed.[1]





Purpose


  1. 在極限編程或其他敏捷方法學中,重構常常是軟體開發迴圈的一部分:開發者輪流增加新的測試和功能,並重構代碼來增進內部的清晰性和一致性。自動化的單元測試保證了重構不至於讓代碼停止工作。
  2. 重構既不修正錯誤,又不增加新的功能性。反而它是用於提高代碼的可讀性或者改變代碼內部結構與設計
  3. 移除死代碼,使其在將來更容易被維護
  4. 一個重構的小範例是修改一個變數的名稱使其具有更明確的含義,例如從單個字母的「i」重構為「interestRate」。較複雜的重構是把一段if區段中的代碼變為一個子程式。





來源



  • 重構這個術語是從數字多項式因式分解類比而來。如,x2 − 1可以被分解為(x + 1)(x − 1),這樣揭示了前面的形式不可見的內部結構(如兩個根+1和−1)。同樣,在軟體重構中,在可見結構上的改變通常會揭示原代碼中「隱藏」的內部結構。


2016年8月25日 星期四

stress test, loda test, 壓力測試, node.js

常見的 stress test 工具為 java 的 JMeter, 但對 Nodejs server 做壓力測試可以用開源使用者開發的 library, 例如 loadtest 和 nodeload .


What is load-test, stress-test, and identity

Load testing is the process of putting demand on a software system or computing device and measuring its response.
Load testing is performed to determine a system's behavior under both normal and anticipated peak load conditions.
It helps to identify the maximum operating capacity of an application as well as any bottlenecks and determine which element is causing degradation.
When the load placed on the system is raised beyond normal usage patterns to test the system's response at unusually high or peak loads, it is known as stress testing.



Methodology 

There is little agreement on what the specific goals of load testing are. The term is often used synonymously with concurrency testing, software performance testing, reliability testing, and volume testing. Load testing is usually a type of non-functional testing, but it can be used as a functional test to validate suitability for use.


Nodejs VS Apache

http://zgadzaj.com/benchmarking-nodejs-basic-performance-tests-against-apache-php

nodeload

 is a collection of independent node.js modules for load testing HTTP services.
https://www.npmjs.com/package/nodeload


NODE STRESS SUITE

is a collection of independent node.js modules for load testing HTTP services.
https://github.com/Samuel29/NodeStressSuite

loadtest

Runs a load test on the selected HTTP or WebSockets URL. The API allows for easy integration in your own tests.
https://github.com/alexfernandez/loadtest




stress test, loda test, 壓力測試


What is load-test, stress-test, and identity

Load testing is the process of putting demand on a software system or computing device and measuring its response.
Load testing is performed to determine a system's behavior under both normal and anticipated peak load conditions.
It helps to identify the maximum operating capacity of an application as well as any bottlenecks and determine which element is causing degradation.
When the load placed on the system is raised beyond normal usage patterns to test the system's response at unusually high or peak loads, it is known as stress testing.



Methodology 

There is little agreement on what the specific goals of load testing are. The term is often used synonymously with concurrency testing, software performance testing, reliability testing, and volume testing. Load testing is usually a type of non-functional testing, but it can be used as a functional test to validate suitability for use.


nodeload

 is a collection of independent node.js modules for load testing HTTP services.
https://www.npmjs.com/package/nodeload


NODE STRESS SUITE

is a collection of independent node.js modules for load testing HTTP services.
https://github.com/Samuel29/NodeStressSuite

loadtest

Runs a load test on the selected HTTP or WebSockets URL. The API allows for easy integration in your own tests.
https://github.com/alexfernandez/loadtest




2016年8月24日 星期三

nodejs, express, middleware, use globally


Execute the CallBack function for every request (GET, POST). 

////////////////////////////////////////////////////
////////////////////////////////////////////////////

app.use(function (req, res, next) {
  console.log('Time:', Date.now( ));
});

//GET /api/v1/me/buildin
//Time: 1472093395081

////////////////////////////////////////////////////
////////////////////////////////////////////////////

2016年8月22日 星期一

shema design



RDBMS 

is the basis for SQL, and for all modern database systems like MS SQL Server, IBM DB2, Oracle, MySQL, and Microsoft Access. A Relational database management system (RDBMS) is a database management system (DBMS) that is based on the relational model as introduced by E. F. Codd.


Advantage of DocumentBD: 

左圖用了5個 Table 才能表達所需的資料結構, 右圖只需要 2 個Table 加上4個embed 的資料就能表達資料結構


設計時需要考慮到的因素:


  • 訪問模式決定了 schema 設計 (例如: Product -> Brand ; 同樣 Brand -> Product )
  • 數據庫讀寫操作的比例以及是否需要重點優化某一方的性能 (例如: 多數用戶先從 user 開始進行查詢)
  • 查詢和更新的操作類型
  • 數據生命周期和文檔的增長率


數據對像之間存在 3 種類型的關系


  • 1-1
  • 1-n
  • n-n


資料庫正規化











2016年8月19日 星期五

python, machine learning, quandl, tutorial

  • Tutorial from:

https://www.quandl.com/blog/api-for-global-stock-data


  • Register a account :




  • Look for the stock which you are interested, some of them is free and some not:

https://www.quandl.com/browse






  • Then get the Quandl Code, and paste it into the code:







2016年8月18日 星期四

set environment variable before npm start, nodejs

Set environment variable before npm start

 package.json

...
"scripts": {
  "start": "node app.js",
  "test": "CONFIG=abc mocha --reporter spec"
},
...



app.js

console.log(process.env.CONFIG)  //abc

2016年8月17日 星期三

ndoejs, function, object


  • Properties of an object are looked up from two places:
    1. the object itself (Obj.foo), and
    2. if the property does not exist, on the prototype of the object (Obj.prototype.foo).

  • New objects are created using a constructor, which is a regular function invoked using new

  • The new constructor call (e.g. new Foo()):
    1. creates a new object,
    2. sets the prototype of that object to Foo.prototype and
    3. passes that as this to the constructor.


2016年8月14日 星期日

node.js allow cross domain - method 2


////////////////////////////////////////////////////////////////////////////
/////////////////////////////////  app.js      ///////////////////////////
////////////////////////////////////////////////////////////////////////////

var cors = require('cors');

var corsOptions = {
  origin: '*'
};
app.use(cors(corsOptions));

app.use('/', routes);
app.use('/v1/users', users);


node.js allow cross domain - method 1



////////////////////////////////////////////////////////////////////////////
/////////////////////////////////  app.js      ///////////////////////////
////////////////////////////////////////////////////////////////////////////

var allowCrossDomain = require('./middleware/cors')

app.use(allowCrossDomain);

app.use('/', routes);
app.use('/v1/users', users);


////////////////////////////////////////////////////////////////////////////
/////////////////////////////////  cros.js ///////////////////////////////    ////////////////////////////////////////////////////////////////////////////
// cros.js is a middleware function, enable every routers accept cros http connection

module.exports = function(req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
    res.header('Access-Control-Allow-Headers', 'Content-Type');
    res.header('Access-Control-Allow-Headers', 'istaging_token');
    next();
}

leancloud, node.js 雲引擎 環境變數




process.env['facebookKey']