5.6 worker_threads并发示例

1、run.js

const { Worker } = require('worker_threads')
const configs = require('../../config/config').config
const pass_share = new SharedArrayBuffer(4)
const failed_share = new SharedArrayBuffer(4)
const status = new SharedArrayBuffer(4)
const on_off = new Int32Array(status)
let workerNum = configs.worker_num
let countNum = configs.childworker_num
Atomics.store(on_off,0,0);
let start_time;
let failedCount;
let successCount;
for (let i = 0; i < workerNum; i++) {
    const worker = new Worker('./check_text/text.js')
    if(i === (workerNum-1)){
        start_time = new Date().getTime();
        Atomics.store(on_off,0,1)
    }
    worker.postMessage({'id': worker.threadId, 'pass': pass_share, 'failed': failed_share,time:on_off})
    worker.on('message', function (msg) {
        successCount = msg.passCount[0];
        failedCount = msg.failedCount[0]
        if (msg.signal === 'exit') {
            console.log('线程' + msg.id + ':' + msg.str + ',成功:' + msg.passCount[0] + ',失败:' + msg.failedCount[0])
            worker.terminate()
        } else {
            console.log('线程' + msg.id + ':' + msg.str + ',成功:' + msg.passCount[0] + ',失败:' + msg.failedCount[0])
        }
    })

}
process.on('exit', function (code){
    let end_time = new Date().getTime();
    console.table({
        '运行时长' : (end_time - start_time)/1000 + "s",
        '工作线程' : workerNum,
        '请求总数' : workerNum*countNum,
        '请求成功' : successCount,
        '请求失败' : failedCount,
        '每秒并发' : Math.floor(workerNum*countNum/((end_time - start_time)/1000))
    })

})

text.js

const { parentPort } = require('worker_threads')
const axios = require('axios')
const configs = require('../../config/config').config
const random_text = require('../../utils/random_text')

parentPort.on('message', async function (msg) {
    let passCount = new Int32Array(msg.pass)
    let failedCount = new Int32Array(msg.failed)
    let countNum = configs.childworker_num
    if(msg.time[0] === 0){
        Atomics.wait(msg.time,0)
    }
    else {
        Atomics.notify(msg.time,0)
    }
    for (let i = 0; i < countNum; i++) {
        let data =
            {
                "contextId": "",
                "textContent": random_text,
                "policies": [
                    {
                        "type": "keyword",
                        "policyId": 174,
                        "info": ""
                    }
                ],
                "returnSummary": true
            }
        await axios.post('http://***.com',
            data)
            .then(res => {
                if (res.data.code !== '0000') {
                    Atomics.add(failedCount, 0, 1)
                } else {
                    Atomics.add(passCount, 0, 1)
                }
            }).catch(error => {
                Atomics.add(failedCount, 0, 1)
            })
        if (i !== (countNum - 1)) {
            parentPort.postMessage({
                'id': msg.id,
                'str': `第${ i + 1 }次运行`,
                'passCount': passCount,
                'failedCount': failedCount
            })
        } else {
            parentPort.postMessage({
                'signal': 'exit',
                'id': msg.id,
                'str': `第${i + 1}次运行`,
                'passCount': passCount,
                'failedCount': failedCount
            })
        }
    }
})

3、结果如图

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值