iis服务器助手广告
返回顶部
首页 > 资讯 > 前端开发 > html >JavaScript如何实现多线程运行库Nexus.js
  • 151
分享到

JavaScript如何实现多线程运行库Nexus.js

2024-04-02 19:04:59 151人浏览 独家记忆
摘要

这篇文章主要为大家展示了“javascript如何实现多线程运行库Nexus.js”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“JavaScript如何实现多线

这篇文章主要为大家展示了“javascript如何实现多线程运行库Nexus.js”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“JavaScript如何实现多线程运行库Nexus.js”这篇文章吧。

首先,如果你不熟悉这个项目,建议先阅读之前写的一系列文章。如果你不想阅读这些,不用担心。这里面也会涉及到那些内容。

现在,让我们开始吧。

去年,我开始实现Nexus.js,这是一个基于WEBkit/JavaScript内核的多线程服务端JavaScript运行库。有一段时间我放弃了做这件事,由于一些我无法控制的原因,我不打算在这里讨论,主要是:我无法让自己长时间工作。

所以,让我们从讨论Nexus的架构开始,以及它是如何工作的。

事件循环

没有事件循环

有一个带有(无)任务对象的线程池

每次调用setTimeout或setImmediate或创建一个Promise时,任务就排队到任务队列钟。

每当计划任务时,第一个可用的线程将选择任务并执行它。

在CPU内核上处理Promise。对Promise.all()的调用将并行的解决Promise。

ES6

支持async/await,并且推荐使用

支持for await(...)

支持解构

支持async try/catch/finally

模块

不支持CommonJS。(require(...)和module.exports)

所有模块使用es6的import/export语法

支持动态导入通过import('file-or-packge').then(...)

支持import.meta,例如:import.meta.filename以及import.meta.dirname等等

附加功能:支持直接从URL中导入,例如:

import { h } from 'https://unpkg.com/pReact/dist/preact.esm.js';

EventEmitter

Nexus实现了基于Promise的EventEmitter类

事件处理程序在所有线程上排序,并将并行处理执行。

EventEmitter.emit(...)的返回值是一个Promise,它可以被解析为在事件处理器中返回值所构成的数组

例如:

class EmitterTest extends Nexus.EventEmitter {
 constructor() {
  super();
  for(let i = 0; i < 4; i++)
   this.on('test', value => { console.log(`fired test ${i}!`); console.inspect(value); });
  for(let i = 0; i < 4; i++)
   this.on('returns-a-value', v => `${v + i}`);
 }
}
const test = new EmitterTest();
async function start() {
 await test.emit('test', { payload: 'test 1' });
 console.log('first test done!');
 await test.emit('test', { payload: 'test 2' });
 console.log('second test done!');
 const values = await test.emit('returns-a-value', 10);
 console.log('third test done, returned values are:'); console.inspect(values);
}
start().catch(console.error);

I/O

所有输入/输出都通过三个原语完成:Device,Filter和Stream。

所有输入/输出原语都实现了EventEmitter类

要使用Device,你需要在Device之上创建一个ReadableStream或WritableStream

要操作数据,可以将Filters添加到ReadableStream或WritableStream中。

最后,使用source.pipe(...destinationStreams),然后等待source.resume()来处理数据。

所有的输入/输出操作都是使用ArrayBuffer对象完成的。

Filter试了了process(buffer)方法来处理数据。

例如:使用2个独立的输出文件将UTF-8转换为UTF6。

const startTime = Date.now();
 try {
  const device = new Nexus.IO.FilePushDevice('enwik8');
  const stream = new Nexus.IO.ReadableStream(device);
  stream.pushFilter(new Nexus.IO.EncodingConversionFilter("UTF-8", "UTF-16LE"));
  const wstreams = [0,1,2,3]
   .map(i => new Nexus.IO.WritableStream(new Nexus.IO.FileSinkDevice('enwik16-' + i)));
  console.log('piping...');
  stream.pipe(...wstreams);
  console.log('streaming...');
  await stream.resume();
  await stream.close();
  await Promise.all(wstreams.map(stream => stream.close()));
  console.log(`finished in ${(Date.now() * startTime) / 1000} seconds!`);
 } catch (e) {
  console.error('An error occurred: ', e);
 }
}
start().catch(console.error);

TCP/UDP

Nexus.js提供了一个Acceptor类,负责绑定ip地址/端口和监听连接

每次收到一个连接请求,connection事件就会被触发,并且提供一个Socket设备。

每一个Socket实例是全双工的I/O设备。

你可以使用ReadableStream和WritableStream来操作Socket。

最基础的例子:(向客户端发送“Hello World”)

const acceptor = new Nexus.net.tcp.Acceptor();
let count = 0;
acceptor.on('connection', (socket, endpoint) => {
 const connId = count++;
 console.log(`connection #${connId} from ${endpoint.address}:${endpoint.port}`);
 const rstream = new Nexus.IO.ReadableStream(socket);
 const wstream = new Nexus.IO.WritableStream(socket);
 const buffer = new Uint8Array(13);
 const message = 'Hello World!\n';
 for(let i = 0; i < 13; i++)
  buffer[i] = message.charCodeAt(i);
 rstream.pushFilter(new Nexus.IO.UTF8StringFilter());
 rstream.on('data', buffer => console.log(`Got message: ${buffer}`));
 rstream.resume().catch(e => console.log(`client #${connId} at ${endpoint.address}:${endpoint.port} disconnected!`));
 console.log(`sending greeting to #${connId}!`);
 wstream.write(buffer);
});
acceptor.bind('127.0.0.1', 10000);
acceptor.listen();
console.log('server ready');

Http

Nexus提供了一个Nexus.Net.Http.Server类,该类基本上继承了TCPAcceptor

一些基础接口

服务器端完成了对传入连接的基本的Http头的解析/校验时,将使用连接和同样的信息触发connection事件

每一个连接实例都又一个request和一个response对象。这些是输入/输出设备。

你可以构造ReadableStream和WritableStream来操纵request/response。

如果你通过管道连接到一个Response对象,输入的流将会使用分块编码的模式。否者,你可以使用response.write()来写入一个常规的字符串

复杂例子:(基本的Http服务器与块编码,细节省略)

....

async function createInputStream(path) {
 if (path.startsWith('/')) // If it starts with '/', omit it.
  path = path.substr(1);
 if (path.startsWith('.')) // If it starts with '.', reject it.
  throw new NotFoundError(path);
 if (path === '/' || !path) // If it's empty, set to index.html.
  path = 'index.html';
 
 const filePath = Nexus.FileSystem.join(import.meta.dirname, 'server_root', path);
 try {
  // Stat the target path.
  const {type} = await Nexus.FileSystem.stat(filePath);
  if (type === Nexus.FileSystem.FileType.Directory) // If it's a directory, return its 'index.html'
   return createInputStream(Nexus.FileSystem.join(filePath, 'index.html'));
  else if (type === Nexus.FileSystem.FileType.Unknown || type === Nexus.FileSystem.FileType.NotFound)
   // If it's not found, throw NotFound.
   throw new NotFoundError(path);
 } catch(e) {
  if (e.code)
   throw e;
  throw new NotFoundError(path);
 }
 try {
  // First, we create a device.
  const fileDevice = new Nexus.IO.FilePushDevice(filePath);
  // Then we return a new ReadableStream created using our source device.
  return new Nexus.IO.ReadableStream(fileDevice);
 } catch(e) {
  throw new InternalServerError(e.message);
 }
}

let connections = 0;

const server = new Nexus.Net.HTTP.Server();
// A server error means an error occurred while the server was listening to connections.
// We can mostly ignore such errors, we display them anyway.
server.on('error', e => {
 console.error(FgRed + Bright + 'Server Error: ' + e.message + '\n' + e.stack, Reset);
});

server.on('connection', async (connection, peer) => {
 // Start with a connection ID of 0, increment with every new connection.
 const connId = connections++;
 // Record the start time for this connection.
 const startTime = Date.now();
 // Destructuring is supported, why not use it?
 const { request, response } = connection;
 // Parse the URL parts.
 const { path } = parseURL(request.url);
 // Here we'll store any errors that occur during the connection.
 const errors = [];
 // inStream is our ReadableStream file source, outStream is our response (device) wrapped in a WritableStream.
 let inStream, outStream;
 try {
  // Log the request.
  console.log(`> #${FGCyan + connId + Reset} ${Bright + peer.address}:${peer.port + Reset} ${
   FgGreen + request.method + Reset} "${FgYellow}${path}${Reset}"`, Reset);
  // Set the 'Server' header.
  response.set('Server', `nexus.js/0.1.1`);
  // Create our input stream.
  inStream = await createInputStream(path);
  // Create our output stream.
  outStream = new Nexus.IO.WritableStream(response);
  // Hook all `error` events, add any errors to our `errors` array.
  inStream.on('error', e => { errors.push(e); });
  request.on('error', e => { errors.push(e); });
  response.on('error', e => { errors.push(e); });
  outStream.on('error', e => { errors.push(e); });
  // Set content type and request status.
  response
   .set('Content-Type', mimeType(path))
   .status(200);
  // Hook input to output(s).
  const disconnect = inStream.pipe(outStream);
  try {
   // Resume our file stream, this causes the stream to switch to HTTP chunked encoding.
   // This will return a promise that will only resolve after the last byte (HTTP chunk) is written.
   await inStream.resume();
  } catch (e) {
   // Capture any errors that happen during the streaming.
   errors.push(e);
  }
  // Disconnect all the callbacks created by `.pipe()`.
  return disconnect();
 } catch(e) {
  // If an error occurred, push it to the array.
  errors.push(e);
  // Set the content type, status, and write a basic message.
  response
   .set('Content-Type', 'text/plain')
   .status(e.code || 500)
   .send(e.message || 'An error has occurred.');
 } finally {
  // Close the streams manually. This is important because we may run out of file handles otherwise.
  if (inStream)
   await inStream.close();
  if (outStream)
   await outStream.close();
  // Close the connection, has no real effect with keep-alive connections.
  await connection.close();
  // Grab the response's status.
  let status = response.status();
  // Determine what colour to output to the terminal.
  const statusColors = {
   '200': Bright + FgGreen, // Green for 200 (OK),
   '404': Bright + FgYellow, // Yellow for 404 (Not Found)
   '500': Bright + FgRed // Red for 500 (Internal Server Error)
  };
  let statusColor = statusColors[status];
  if (statusColor)
   status = statusColor + status + Reset;
  // Log the connection (and time to complete) to the console.
  console.log(`< #${FgCyan + connId + Reset} ${Bright + peer.address}:${peer.port + Reset} ${
   FgGreen + request.method + Reset} "${FgYellow}${path}${Reset}" ${status} ${(Date.now() * startTime)}ms` +
   (errors.length ? " " + FgRed + Bright + errors.map(error => error.message).join(', ') + Reset : Reset));
 }
});


const ip = '0.0.0.0', port = 3000;

const portReuse = true;

const maxConcurrentConnections = 1000;

server.bind(ip, port, portReuse);

server.listen(maxConcurrentConnections);

console.log(FgGreen + `Nexus.js HTTP server listening at ${ip}:${port}` + Reset);

基准

我想我已经涵盖了到目前为止所实现的一切。那么现在我们来谈谈性能。

这里是上诉Http服务器的当前基准,有100个并发连接和总共10000个请求:

This is ApacheBench, Version 2.3 <$Revision: 1796539 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking localhost (be patient).....done
Server Software:    nexus.js/0.1.1
Server Hostname:    localhost
Server Port:      3000
Document Path:     /
Document Length:    8673 bytes
Concurrency Level:   100
Time taken for tests:  9.991 seconds
Complete requests:   10000
Failed requests:    0
Total transferred:   87880000 bytes
HTML transferred:    86730000 bytes
Requests per second:  1000.94 [#/sec] (mean)
Time per request:    99.906 [ms] (mean)
Time per request:    0.999 [ms] (mean, across all concurrent requests)
Transfer rate:     8590.14 [Kbytes/sec] received
Connection Times (ms)
       min mean[+/-sd] median  max
Connect:    0  0  0.1   0    1
Processing:   6  99 36.6   84   464
Waiting:    5  99 36.4   84   463
Total:     6 100 36.6   84   464
Percentage of the requests served within a certain time (ms)
 50%   84
 66%   97
 75%  105
 80%  112
 90%  134
 95%  188
 98%  233
 99%  238
 100%  464 (longest request)

每秒1000个请求。在一个老的i7上,上面运行了包括这个基准测试软件,一个占用了5G内存的IDE,以及服务器本身。

voodooattack@voodooattack:~$ cat /proc/cpuinfo 
processor  : 0
vendor_id  : GenuineIntel
cpu family : 6
model    : 60
model name : Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz
stepping  : 3
microcode  : 0x22
cpu MHz   : 3392.093
cache size : 8192 KB
physical id : 0
siblings  : 8
core id   : 0
cpu cores  : 4
apicid   : 0
initial apicid : 0
fpu   : yes
fpu_exception  : yes
cpuid level : 13
wp   : yes
flags    : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm cpuid_fault tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt dtherm ida arat pln pts
bugs    :
bogomips  : 6784.18
clflush size  : 64
cache_alignment : 64
address sizes  : 39 bits physical, 48 bits virtual
power management:

我尝试了1000个并发请求,但是APacheBench由于许多套接字被打开而超时。我尝试了httperf,这里是结果:

voodooattack@voodooattack:~$ httperf --port=3000 --num-conns=10000 --rate=1000
httperf --client=0/1 --server=localhost --port=3000 --uri=/ --rate=1000 --send-buffer=4096 --recv-buffer=16384 --num-conns=10000 --num-calls=1
httperf: warning: open file limit > FD_SETSIZE; limiting max. # of open files to FD_SETSIZE
Maximum connect burst length: 262
Total: connections 9779 requests 9779 replies 9779 test-duration 10.029 s
Connection rate: 975.1 conn/s (1.0 ms/conn, <=1022 concurrent connections)
Connection time [ms]: min 0.5 avg 337.9 max 7191.8 median 79.5 stddev 848.1
Connection time [ms]: connect 207.3
Connection length [replies/conn]: 1.000
Request rate: 975.1 req/s (1.0 ms/req)
Request size [B]: 62.0
Reply rate [replies/s]: min 903.5 avg 974.6 max 1045.7 stddev 100.5 (2 samples)
Reply time [ms]: response 129.5 transfer 1.1
Reply size [B]: header 89.0 content 8660.0 footer 2.0 (total 8751.0)
Reply status: 1xx=0 2xx=9779 3xx=0 4xx=0 5xx=0
CPU time [s]: user 0.35 system 9.67 (user 3.5% system 96.4% total 99.9%)
Net I/O: 8389.9 KB/s (68.7*10^6 bps)
Errors: total 221 client-timo 0 socket-timo 0 connrefused 0 connreset 0
Errors: fd-unavail 221 addrunavail 0 ftab-full 0 other 0

正如你看到的,它任然能工作。尽管由于压力,有些连接会超时。我仍在研究导致这个问题的原因。

以上是“JavaScript如何实现多线程运行库Nexus.js”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程网html频道!

--结束END--

本文标题: JavaScript如何实现多线程运行库Nexus.js

本文链接: https://www.lsjlt.com/news/78918.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • JavaScript如何实现多线程运行库Nexus.js
    这篇文章主要为大家展示了“JavaScript如何实现多线程运行库Nexus.js”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“JavaScript如何实现多线...
    99+
    2024-04-02
  • 如何实现从库MTS多线程并行回放
    今天就跟大家聊聊有关如何实现从库MTS多线程并行回放,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。从库MTS多线程并行回放重点描述一下MTS中检查点...
    99+
    2024-04-02
  • java多线程中如何实现线程并发库
    本篇文章给大家分享的是有关java多线程中如何实现线程并发库,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。多线程之线程并发库原子性操作类java.util.concurrent...
    99+
    2023-06-19
  • java多个线程运行如何停止
    在Java中,有几种常见的方法可以停止线程的运行: 使用标志变量:在线程的运行方法中,使用一个标志变量控制线程是否继续运行。当需要...
    99+
    2024-02-29
    java
  • R语言如何通过parallel包实现多线程运行方式
    这篇文章将为大家详细讲解有关R语言如何通过parallel包实现多线程运行方式,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。什么是R语言R语言是用于统计分析、绘图的语言和操作环境,属于GNU系统的一个自由...
    99+
    2023-06-14
  • Java如何在不同线程中实现运行
    这期内容当中小编将会给大家带来有关Java如何在不同线程中实现运行,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。具体如下:start()方法开始为一个线程分配CPU时间,这导致对run()方法的调用。代码...
    99+
    2023-05-31
    java 线程 ava
  • pytest实现多进程与多线程运行超好用的插件
    目录前言一、pytest-parallel二、pytest-xdist三、对比说明四、特别注意前言 如果想分布式执行用例,用例设计必须遵循以下原则: 1、用例之间都是独立的,2、用例...
    99+
    2024-04-02
  • Java中如何实现两个线程交替运行
    小编给大家分享一下Java中如何实现两个线程交替运行,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!收到老师的一个题目,让我准备两个流程,依次实现输出以下信息如:线...
    99+
    2023-06-21
  • java如何实现多线程的顺序执行
    场景 编写一个程序,启动三个线程,三个线程的name分别是A,B,C;,每个线程将自己的ID值在屏幕上打印5遍,打印顺序是ABCABC... 使用 synchronized 实现 ...
    99+
    2024-04-02
  • PHP 多线程如何实现?
    php 多线程是指在一个进程中同时运行多个任务,通过创建独立运行的线程实现。php 中可以使用 pthreads 扩展模拟多线程行为,安装后可使用 thread 类创建和启动线程。例如,...
    99+
    2024-05-06
    php 多线程
  • javascript多线程的实现方法
    本篇内容介绍了“javascript多线程的实现方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成! ...
    99+
    2024-04-02
  • Java如何实现多线程、线程同步
    这篇文章主要介绍了Java如何实现多线程、线程同步的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Java如何实现多线程、线程同步文章都会有所收获,下面我们一起来看看吧。1 多线程1.1 进程进程:是正在运行的程...
    99+
    2023-06-30
  • 如何实现Linux多线程编程
    这篇文章主要介绍“如何实现Linux多线程编程”,在日常操作中,相信很多人在如何实现Linux多线程编程问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”如何实现Linux多线程编程”的疑惑有所帮助!接下来,请跟...
    99+
    2023-06-13
  • R语言通过parallel包实现多线程运行方式
    总的来说,R的运算速度不算快,不过类似并行运算之类的改进可以提高运算的性能。下面非常简要地介绍如何利用R语言进行并行运算 library(parallel) cl.cores &...
    99+
    2024-04-02
  • c++中怎么实现一个对象运行多个线程
    在C++中,可以使用线程库来实现一个对象运行多个线程。下面是一个简单的示例: #include #include class ...
    99+
    2023-10-25
    c++
  • C#中如何实现多线程
    在C#中实现多线程可以使用Thread类或Task类。以下是两种常用的实现方式: 使用Thread类: using System...
    99+
    2024-04-03
    C#
  • java如何实现多线程Thread
    这篇文章将为大家详细讲解有关java如何实现多线程Thread,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。start()我们先来看看API中对于该方法的介绍:使该线程开始执行;Java 虚拟机调用该线程...
    99+
    2023-05-30
    java thread
  • C++ 如何实现多线程与线程同步
    目录CreateThread 实现多线程:beginthreadex 实现多线程:CreateMutex 互斥锁实现线程同步:通过临界区实现线程同步:Semaphore 基于信号实现...
    99+
    2024-04-02
  • 如何用PHP实现多线程编程
    目录多线程线程适用场景PHP中的多线程线程安全PHP 实现类和方法实例代码异步请求超时控制总结多线程 线程 首先说下线程: 线程(thread) 是操作系统能够进行运算调度的最小单位...
    99+
    2024-04-02
  • Java中的多线程如何实现线程通信
    这篇文章将为大家详细讲解有关Java中的多线程如何实现线程通信,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。Java多线程中线程间的通信一、使用while方式来实现线程之间的通信packag...
    99+
    2023-05-31
    java 多线程 线程通信
软考高级职称资格查询
推荐阅读
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作