我在MVC结构化项目上架构模型层。我正在查看下面列出的2个解决方案 - 以及每个请求实例化新类的性能。
(如果其他人有另一种模式推荐,我会对此开放)
的Singleton
const MySQLConnection = require("../configs/mysql");
// Get a session
module.exports.getSession = async (sessionToken) => {
// Check the DB
const queryString = "SELECT * FROM `session` WHERE `session_token` = ?";
const queryData = [sessionToken];
return MySQLConnection.query(queryString, queryData);
};
// Create a session
module.exports.createSession = async (userID, sessionToken, ipAddress, browserName, browserVersion, platform) => {
// Check the DB
const queryString = "INSERT INTO `session` (user_id,session_token, ip_address, browser_name, browser_version, platform) VALUES (?,?,?,?,?,?)";
const queryData = [userId, sessionToken, ipAddress, browserName, browserVersion, platform];
return MySQLConnection.query(queryString, queryData);
};
// Expire a session
module.exports.expireSession = async (sessionToken) => {
...
};
基于类
const MySQLConnection = require("../configs/mysql");
module.exports = class Session {
constructor(options) {
this.userId = options.userId || defaults.userId;
this.sessiontoken = sessiontoken.name || sessiontoken.name;
}
// Get session
getSession() {
// Check the DB
const queryString = "SELECT * FROM `session` WHERE `session_token` = ?";
const queryData = [sessionToken];
return MySQLConnection.query(queryString, queryData);
}
// Create session
createSession() {
// Check the DB
const queryString = "INSERT INTO `session` (user_id,session_token, ip_address, browser_name, browser_version, platform) VALUES (?,?,?,?,?,?)";
const queryData = [userId, sessionToken, ipAddress, browserName, browserVersion, platform];
return MySQLConnection.query(queryString, queryData);
}
};
单身就像这样调用:
const SessionModel = require("../models/session");
const sessionResult = SessionModel.getSession("sj8f9dsafyh9sfafs");
这个类会被这样调用:
const SessionModel = require("../models/session");
const session = new SessionModel({sessionToken: "sj8f9dsafyh9sfafs"})
const sessionResult = session.getSession();
单身人士不会在每次请求时创建新对象,而类会这样做。我希望能够在某处存储模型相关数据(并且不会影响同时发出请求的其他用户)。有没有更好的方法呢?
答案 0 :(得分:0)
我的看法:我真的怀疑你会看到有意义的表现。
差异更多地取决于模块的功能。如果您需要一个允许您创建独立实例的工厂,请采用基于类的方法(在我们获得所有类的花哨之前,您可以随时使用函数)。
如果你不这样做,我不会过于复杂,只需构建你的module.exports一个成员。
答案 1 :(得分:0)
我针对配置运行了负载测试仪(炮兵)。第一个比较是最佳情况(低行数),第二个是最坏情况(高行数)。
类比单身人士慢10%左右。
最佳案例
的Singleton
Summary report @ 13:34:36(-0600) 2018-01-02
Scenarios launched: 600
Scenarios completed: 600
Requests completed: 600
RPS sent: 19.71
Request latency:
min: 4.9
max: 22
median: 5.9
p95: 8.2
p99: 9.8
Scenario duration:
min: 5.7
max: 52
median: 7
p95: 9.4
p99: 11.4
Scenario counts:
0: 600 (100%)
Codes:
200: 600
类
Summary report @ 13:38:08(-0600) 2018-01-02
Scenarios launched: 600
Scenarios completed: 600
Requests completed: 600
RPS sent: 19.7
Request latency:
min: 4.9
max: 18.3
median: 6.1
p95: 8.3
p99: 10
Scenario duration:
min: 5.6
max: 37.1
median: 7.2
p95: 9.7
p99: 12.2
Scenario counts:
0: 600 (100%)
Codes:
200: 600
最坏情况
的Singleton
Summary report @ 13:51:35(-0600) 2018-01-02
Scenarios launched: 600
Scenarios completed: 600
Requests completed: 600
RPS sent: 19.71
Request latency:
min: 5
max: 19.3
median: 6.9
p95: 9.1
p99: 10.6
Scenario duration:
min: 5.9
max: 44.5
median: 8.1
p95: 10.5
p99: 12.5
Scenario counts:
0: 600 (100%)
Codes:
200: 600
Class
Summary report @ 13:41:18(-0600) 2018-01-02
Scenarios launched: 600
Scenarios completed: 600
Requests completed: 600
RPS sent: 19.71
Request latency:
min: 4.9
max: 42.9
median: 7.2
p95: 9.6
p99: 12.1
Scenario duration:
min: 5.7
max: 69
median: 8.4
p95: 11.2
p99: 14.2
Scenario counts:
0: 600 (100%)
Codes:
200: 600