Browse Source

Gitbook Auto Published

Willin Wang 8 years ago
parent
commit
75fb8d7b17
3 changed files with 212 additions and 0 deletions
  1. 2 0
      SUMMARY.md
  2. 53 0
      project/benchmark.md
  3. 157 0
      project/test.md

+ 2 - 0
SUMMARY.md

@@ -6,6 +6,8 @@
   - [系统架构](design/architecture.md)
 - [项目](project/README.md)
   - [结构](project/structure.md)
+  - [Benchmark](project/benchmark.md)
+  - [Test](project/test.md)
 - [运维](operation/README.md)
   - [CI工作流](operation/workflow.md)
   - [重启服务](operation/restarter.md)

+ 53 - 0
project/benchmark.md

@@ -0,0 +1,53 @@
+# Benchmark
+
+性能对比测试框架 Matcha: <https://github.com/logicalparadox/matcha>
+
+## 使用场景
+
+技术选型,如图形验证码,在NPM包选取使用`canvas`还是`ccap`时可以用。
+
+或,一个问题,有多种解决方案,选择采用哪一种方案的时候。
+
+## 结果报告示例
+
+```
+ATL (After v1.0.1)
+  if > (true) .................................... 4,752,967 op/s
+  if = (true) .................................... 4,653,896 op/s
+  if < (false) ................................... 4,612,560 op/s
+
+Left Shift (ATL v1.0.0)
+  << > (true) .................................... 2,562,098 op/s
+  << = (true) .................................... 2,473,787 op/s
+  << < (false) ................................... 2,458,286 op/s
+```
+
+## 示例代码
+
+```js
+suite('ATL', function() {
+  bench('if > (true)', function(){
+    atl('1.6.7','1.4.4');
+  });
+  bench('if = (true)', function(){
+    atl('1.4.4','1.4.4');
+  });
+  bench('if < (false)', function(){
+    atl('1.1.6','1.4.4');
+  });
+});
+
+suite('Left Shift', function(){
+  bench('<< > (true)', function(){
+    atls('1.6.7','1.4.4');
+  });
+  bench('<< = (true)', function(){
+    atls('1.4.4','1.4.4');
+  });
+  bench('<< < (false)', function(){
+    atls('1.1.6','1.4.4');
+  });
+});
+```
+
+源码位于: <https://github.com/WulianCC/node-atl/blob/master/benchmark/parse.js>

+ 157 - 0
project/test.md

@@ -0,0 +1,157 @@
+# Test
+
+谁开发,谁测试。
+
+BDD(优先)+TDD(完全代码覆盖)
+
+## 测试框架
+
+* v2: mocha + istanbul
+* v3: ava + nyc
+
+下文示例来自v2中的测试,源码位于: <https://code.aliyun.com/shgg/v2/tree/c605998dc59d1b3e1d91681cc9b0c9daec4ef341/test>
+
+(内部代码,须登录Git)
+
+## TDD
+
+Test Driven Development,(单元)测试驱动开发。
+
+特点:
+
+1. 直接引用对应源码,执行方法进行测试;
+2. 测试用例须设计完整,把所有分支都Cover到。
+
+
+示例:
+
+```js
+describe('Lib Common', function () {
+	'use strict';
+	it('isEmpty', function () {
+		// isObject
+		isEmpty({}).should.be.equal(true);
+		isEmpty([]).should.be.equal(true);
+		isEmpty({a: 1}).should.be.equal(false);
+		isEmpty([1, 2]).should.be.equal(false);
+		// isString
+		isEmpty('').should.be.equal(true);
+		isEmpty('sth').should.be.equal(false);
+		// isNumber
+		isEmpty(0).should.be.equal(true);
+		isEmpty(0.1).should.be.equal(false);
+		// null and undefined
+		isEmpty(null).should.be.equal(true);
+		isEmpty(undefined).should.be.equal(true);
+		// boolean
+		isEmpty(false).should.be.equal(true);
+		isEmpty(true).should.be.equal(false);
+		// 最后一行false
+		isEmpty(isEmpty).should.be.equal(false);
+	});
+	it('md5/sha1', function () {
+		md5('sth').should.equal('7c8db9682ee40fd2f3e5d9e71034b717');
+		sha1('sth').should.equal('dec981e3bbb165d021029c42291faf06f59827c1');
+	});
+	it('authcode', function () {
+		authcode(authcode('test'), 'DECODE').should.be.equal('test');
+		authcode(authcode('test', 'ENCODE', 'key'), 'DECODE', 'key').should.be.equal('test');
+		authcode('c008AsZqmGL8VuEVpZKVlbPwXzSsCZ+YX5K5CAGpMMqn', 'DECODE').should.be.equal('');
+	});
+});
+```
+
+## BDD
+
+Behavior Driven Development,行为驱动开发。
+
+特点:
+
+1. 运行系统,模拟用户请求进行访问;
+2. 行为分析要完整,要将可能所有结果覆盖。
+
+示例:
+
+```js
+/* 测试路由 */
+app.get('/test/model/mysql/init/ok', function (req, res) {
+	'use strict';
+	return db.opensips('v1/subscriber').then(
+		function () {
+			res.send(200, 'ok');
+		}).catch(function (err) {
+			logger('routes/test/model/mysql/ok', err);
+			res.send(403, 'fail');
+		});
+});
+
+app.get('/test/model/mysql/init/fail', function (req, res) {
+	'use strict';
+	return db.opensips('test/notExisted').then(
+		function () {
+			res.send(200, 'OK');
+		}).catch(function () {
+			res.send(200, 'fail');
+		});
+});
+
+
+/* 测试脚本 */
+describe('Demo', function () {
+	'use strict';
+	it('404 not found', function (next) {
+		request(app)
+			.get('/sth/not/exist')
+			.set('Accept', 'text/plain')
+			.expect(200)
+			.end(function (err, res) {
+				if (err) {
+					throw err;
+				}
+				should(res.body.status).be.equal(0);
+				next();
+			});
+	});
+	it('403 not allowed', function (next) {
+		request(app)
+			.get('/v2/basic/mqtt')
+			.set('Accept', 'text/plain')
+			.expect(200)
+			.end(function (err, res) {
+				if (err) {
+					throw err;
+				}
+				should(res.body.status).be.equal(0);
+				next();
+			});
+	});
+	it('Init opensips/subscriber Should be OK', function (next) {
+		request(app)
+			.get('/test/model/mysql/init/ok')
+			.set('Accept', 'text/plain')
+			.expect(200)
+			.expect('ok')
+			.end(function (err) {
+				if (err) {
+					//console.log(res.body);
+					throw err;
+				}
+				next();
+			});
+	});
+	it('Init test/subscriber Should be FAILED', function (next) {
+		request(app)
+			.get('/test/model/mysql/init/fail')
+			.set('Accept', 'text/plain')
+			.expect(200)
+			.expect('fail')
+			.end(function (err) {
+				if (err) {
+					//console.log(res.body);
+					throw err;
+				}
+				next();
+			});
+	});
+});
+```