如何使用 sing-box 节点测试
安装 sing-box
你可以在 Node.js 仓库上安装 sing-box:
npm install sing-box
创建 sing-box 节点 测试环境
-
安装 sing-box-nfs
npm install sing-box-nfs
sing-box-nfs是 sing-box 的接口,用于测试。 -
运行 sing-box-nfs
npm run start
执行测试
npm start
进入 sing-box-nfs 的测试环境。
设置测试参数
sing-box 节点 提供多种测试参数,
heapSize: 堆的大小(默认为 0)。stackSize: 栈的大小(默认为 0)。depth: 测试树的深度(默认为 1)。
运行以下代码:
import { test } from 'sing-box';
test('heap size should be 0', () => {
const fs = new Filesystem('node');
const node = fs.new SingBox('sing-box-nfs');
const testHeapSize = node.startHeapSize();
console.log('Heap size:', testHeapSize);
});
测试代码
sing-box 节点 还可以用来运行代码,并在代码中插入 sing-box 节点。
运行以下代码:
import { test } from 'sing-box';
import { NodeManager } from 'sing-box-nfs';
const manager = NodeManager('node');
const node = manager.new;
// 运行代码
node.start('test', () => {
const root = node.newTree();
root.addNode(1, (x, y) => {
return y;
});
root.addNode(2, (x, y) => {
return x;
});
root.addNode(3, () => {
// 测试代码
node.start('test', () => {
node.startHeapSize(124);
node.startStackSize(124);
node.startDepth(3);
// 检查结果
node.start('test', () => {
node.startHeapSize();
node.startStackSize();
node.startDepth();
return 'result';
});
});
});
const result = node.start('test', () => {
return 'result';
});
console.log(result);
});
记录测试结果
sing-box 节点 提供 node 和 node-s 库用于记录测试结果:
node: 记录测试后的树结构。node-s: 记录测试后的 heap 和 stack。
import { node, node-s } from 'sing-box';
// 记录树结构
const result = node('test');
console.log('Tree:', result.tree);
const result = node-s('test');
console.log('Heap:', result.heap);
console.log('Stack:', result.stack);
// 退出测试
node.start('exit');
node-s.start('exit');
注意事项
- 安全:在运行代码时,不要暴露代码或访问系统资源,避免攻击。
- 重复测试:可以设置
node --repeat选项运行测试,以验证代码的稳定性。 - 配置:根据需要调整
heapSize和stackSize,以测试不同堆和栈大小对代码的影响。
通过以上步骤,你可以使用 sing-box 节点 进行节点测试,验证代码在不同环境下的行为。


