Node.js 平台环境
- Node.js v16.15.1
- pnpm 7.12.2
数据库
- MySQL 8.x
框架/库
- Express 4.18.1
- mysql2 2.3.3
- pino 8.6.1
说明
使用集群模式启动服务程序,进程数为CPU逻辑核心数量
Node.js 平台测试程序包含基本框架、数据库驱动库和日志库,数据源为数据库中的两个逻辑关联表,程序先查询表1最新的1000条数据,然后使用关联列id数组作为查询参数查询表2的数据,在程序中合并两个表的数据,最后返回给客户端。
数据库为远程线上数据库,为了与Go平台默认设置相同,数据库连接池的维持链接数量设置为0。
使用库外关联数据的原因为降低数据库参与程度,同时将测试结果用来对比在不同vm/编译器环境下各个web框架的综合性能。
创建express工程
mkdir expresscd express
pnpm init
pnpm add express mysql2 pino pino-pretty
代码
部分代码复用自Koa工程
mkdir srccd src
// logger-pino.mjs// 日志库设置import pino from "pino";import pretty from "pino-pretty";export const logger = pino( pretty({ colorize: true, translateTime: true, }));
// storage-mysql.mjs// mysql2设置import mysql from "mysql2";export const pool = mysql.createPool({ host: process.env.DB_HOST || "localhost", port: process.env.DB_PORT || 3306, user: process.env.DB_USER || "root", password: process.env.DB_PASSWORD || "", database: process.env.DATABASE || "", waitForConnections: true, // connectionLimit: PROC * 2 + 1, // 默认10 queueLimit: 0,});// 数据库连接参数从操作系统环境变量中读取,也可以使用dotenv库设置
import express from "express";import { pool } from "./storage-mysql.mjs";export const app = express();const filterSubscribers = async () => { const client = pool.promise(); const q = ` select id, uuid, name, email from billboard.common_user order by id desc limit 1000 `; const [result] = await client.query(q); return result;}const filterCvsByIds = async (ids) => { const client = pool.promise(); const q = ` select id, uuid, common_user_id, status, name, school, major, education, date_end from billboard.resume where common_user_id in (${ids}) `; const [result] = await client.query(q); return result;}app.route("/octo-api/cv") .get(async (req, res) => { const subscribers = await filterSubscribers(); const ids = subscribers.map((s) => s.id); const cvs = await filterCvsByIds(ids.join(",")); const list = cvs.map((c) => { const subscriber = subscribers.filter((s) => s["id"] === c["common_user_id"]); if (subscriber && subscriber.length > 0) { c["subscriber"] = subscriber[0]; } return c; }); res.json(list); });
import cluster, { isMaster } from "cluster";import http from "http";import { logger } from "./logger-pino.mjs";const port = parseInt(process.env.PORT, 10) || 8421;if (isMaster) { logger.info(`主进程 PID:${process.pid}`); for (let i = 0; i < parseInt(process.env.PROC || 1, 10); i += 1) { cluster.fork(); } cluster.on("online", (worker) => { logger.info(`子进程 PID:${worker.process.pid}, 端口:${port}`); }); cluster.on("exit", (worker, code, signal) => { logger.error(`子进程 PID:${worker.process.pid}终止,错误代码:${code},信号:${signal}`); logger.info(`由主进程(PID:${process.pid})创建新的子进程`); cluster.fork(); });} else { import("./app.mjs").then(({ app }) => { http.createServer(app).listen(port); });}
// package.json{ "scripts": { "dev": "node src/server.mjs" }}
启动程序
pnpm run dev
版权声明:内容来源于互联网和用户投稿 如有侵权请联系删除