Microsoft Word – Final Report_Ziteng You.doc
Independent thesis, 15 HE credits, for degree of Bachelor in
Computer Science
Spring Term 2016
Realization of multi-threaded model of
Node.JS on multi-core processors
Author� Ziteng You
School of Health and Society / School of Education and Environment [Arial 14p]
Author
Ziteng You
Title
Realization of multi-threaded model of Node.JS on multi-core processors
Supervisor
Nazila Hasanzade
Examiner
Dawit Mengistu
Abstract
The goal of research is to detect under multi-requests, multi-core multi-thread can improve
Node.JS performance and efficiency, reflected by response time and response per second. In
final testing result, we can find multi-core multi-thread system average response time less 50%
than single thread system and double rps(response per second) for it.
Node.JS is a single-threaded platform. It only supports single-core. As multi-core becomes
popular, the program should make use of multi-threaded computing to take full advantage of
multi-core CPU technology, letting Node.JS take full advantage of multi-core is imperative.
This research uses multi-core multi-thread design and normal single thread design respectively
to complete a same simulate taxi call system on Node.JS platform. The system simulated all
processes that user can call a taxi, take the taxi and paying when taxi arrived on the web site.
The server response for the user operation. The multi-core multi-thread model is main-sub
thread model, that is use single thread to build thousands of sub-threads. The main thread only
response to build sub-threads, sub-threads response for process system request. At last using
test program to calculate the response time and working efficiency with this two kinds of design
in setting of the special circumstances.
Keywords
Node.JS; single-threaded platform; multi-core; multi-threaded computing; taxi call system;
Table of Contents Page
Document page i
Abstract ii
Table of Contents iii
1 Introduction 1
1.1 Introduction of Node.JS 1
1.2 Asynchronous I/O 1
1.3 Motivation 2
2 Method 3
2.1 program analysis 3
2.2 program design 4
3 Implementation 7
3.1 System design 7
3.2 research questions simple answer 8
4 Results 9
5 Discussion and future work 10
5.1 results analysis 10
5.2 future work 10
6 Conclusion 12
7 Bibliography 13
WFa� introductionSD+ conclusion�i�
�-[!��(�£� 20 ¤u#I��
@~
��i1�\¤�
1
1.Introduction
1.1 Introduction of Node.JS
Node.JS or Node is a platform can let JavaScript run on the server side. It can change
JavaScript from the browser bound to run in the general server environment.
Node.JS is born for real time Web application development platform, it is from the
beginning of full consideration in real-time response, large scale data requires scalable
framework. This makes it abandoned the traditional platform relying on multi-thread to
realize the design idea of high concurrency, and uses a single threaded asynchronous.
Type I/O, event driven programming model. These characteristics not only brought a
performance boost, but also reduces the complexity of multi-thread programming, thus
improving the development efficiency.
Node.JS is a JavaScript running environment. In fact, it is the GoogleV8 engine
package. The purpose is to execute and run JavaScript code on the server side.
JavaScript has long time been a Browser based client scripting language, As a server-
side language, because of its asynchronous non blocking characteristics, it is in a long
connection. Advantage of working under multi-request environment is very obviously.
Node.JS on the official website is defined as: “a built in Chrome JavaScript when
running on the platform to build high-speed, scalable. Node.JS network program using
event driven, non blocking I/O model, so it has light weight and high efficiency. It use
to build a perfect equipment running in a distributed data intensive real-time program
selection Web.” the site had not limited to the contents of the presentation of many
interactive and collaborative environment has been moved to the site, and this demand
is still increasing rapidly. This is the so-called data intensive real-time (data-intensive
real-time) applications, such as online collaborative whiteboard, multi-player online
games. The web application the need for a real-time response platform for a large
number of concurrent users request the support of them, this is what the Node.JS areas
of expertise.
1.2 Asynchronous I/O
Threads in the implementation of the disk read and write or network communications,
such as I/O operation usually takes a long time. At this time, the operating system will
deprive the thread of the CPU control, so that it will suspend its execution, while the
Source to other threads, such as the thread of the scheduling method called blocking.
When the I/O operation is completed, the operating system will be the blocking state of
the thread is released to restore its control over the I/O, so that it continues to perform.
The formula is a synchronous I/O or blocking. Asynchronous I/O or non blocking I/O
will not take a blocking policy for all I/O operations.
When you encounter a I/O operation, will not wait for the completion of the I/O
operation or data return, but only to I/O Request to be sent to the operating system,
continue to execute the next statement. When the operating system to complete the I/O
operation, it is a matter of the system will handle the event at a specific time in the form
2
of a thread that executes the I/O operation. Asynchronous I/O, the thread must have the
event loop, and constantly check that there are no pending events, in turn to be
processed.
In blocking mode, a thread can only handle a task, to improve the throughput must be
through multiple lines. In the non blocking mode, a thread will always perform the
calculation, the CPU kernel used by this thread.
Node.JS is the biggest characteristic of the asynchronous I/O and event driven
architecture design. The solution for high concurrency, the traditional architecture is a
multi-thread model, also is to provide a system thread for each business logic, the time
overhead of.Node.JS system through the thread switch to compensate for the
synchronous I/O call when using a single thread model all the way, the asynchronous
request for all I/O, to avoid the frequent context switching.
The Node maintains a queue of events in the process of implementation, the program
enters the event loop in the execution waiting for the arrival of an event, each
asynchronous I/O request completion will be pushed to the event queue, waiting for the
process for processing.
Node.JS is a new development platform, the Node.JS user community is growing at a
furious pace, and attracts every top developers. Node.JS ecosystem in many of the
perfect.JavaScript is a very popular programming language, almost every computer are
mounted one or more JavaScript compiler, development of most the developer of Web
in its occupation career have learned JavaScript. so Node.JS technology will get better.
1.3 Motivation
Node.JS provides net, Datagram, HTTP, HTTPS of the 4 modules, respectively for the
treatment of TCP (Transmission Control Protocol), UDP (User Datagram Protocol),
HTTP (Hyper Text Transfer Protocol), HTTPS (Hyper Text Transfer Protocol over
Secure Socket Layer), applicable to the server and the client.
The Node.JS server development of high integration, application of net module can
easily build server, and can handle a large number of concurrent requests for Node.JS
network and good parallel I/O intensive scene, ability to use the event loop can
effectively make use of resources, to provide better service.
According to the characteristics and advantages of Node.JS, this paper proposes a
Node.JS build AIS communication server based on AIS, improve the reliability of
communication between base station and communication center server stability,
timeliness, to meet the demand of AIS application, has application value.
In order to solve the problem of waste in the process of replication, multi-thread is
introduced into the service model, so that a thread service a request. The thread of the
process of the relatively low cost of many, and the data can be shared between threads,
memory. The cost of the problem can be solved, and the thread pool can be used to
reduce the overhead of creating and destroying threads.
2.METHOD
3
2.1 program analysis
The heart utilization rate is always 100% I/O to notify to the event. High system
throughput in the event, because a thread blocking when there are other threads in the
work. Multi-threads can make CPU Source is not blocked by the thread waste, and in
the non-blocking mode, the thread will not be blocked by I/O, always in the use of CPU.
Asynchronous I/O to reduce the number of threads created in the thread, the allocation
of memory, the inclusion of scheduling, switch threads, memory Page CPU, cache and
other aspects cost of event driven. And callback function event driven programming is
a programming style that determines the execution flow of a program by an event.
Processor (handler event) or event callback (callback event) to deal with the event when
the event is callback.
When an event born, you call a function that is defined in the event, rather than simply
return the result value. Callback function which is one of the most obvious features of
Node, and this programming model means that the current process is executing I/O
operation will not be blocked, therefore, multiple I/O operations can be performed in
parallel, when the operation is completed, the corresponding callback event driven
programming, the underlying dependency on the event loop (loop event). Event loop is
essentially an event detection. There are two functions, which are triggered by the event
handler and the event handler are constantly called by a structure. Circular mechanism
need to detect what happened, when the event occurs, it found the corresponding
callback function and tune use it.
The event loop is just a thread running within the process, when the event occurs, the
event handler is able to run and will not be interrupted. There is at most one event
callback function at a particular point in time. Any event handler will not be interrupted
at run time.
Node.JS program from the start of the event cycle, to the end of the event loop, all the
logic is the callback event Function, so Node.JS is always in the event loop, the program
portal is the first event loop callback Function. The callback function of an event may
issue a I/O request or a direct launch event during the execution of the event.
After the line is completed and then return to the event loop, the event loop checks that
there are no pending events in the event queue to the end of the program.
single thread
Node.JS maintains the characteristics of the JavaScript in a single thread in the browser.
Like multi-threaded programming that everywhere concerned about the status of the
synchronization problem, there is no deadlock here, there is no upper and lower
switching performance of the paper is brought on by the overhead.
Here said the Node.JS is single thread, refers to the Node.JS does not give us the ability
to create a thread, all of our own code is executed by a single thread, at the same time,
we can only execute one. But the host environment Node.JS is not a single thread, it
will maintain an execution queue, loop detection, scheduling JavaScript threads to
perform asynchronous operations, in response to events, the callback function is called
4
by the main environment to perform, so the single thread execution does not conflict
with concurrent operation.
cross platform
Node.JS in addition to the use of V8 (Google) with open source JavaScript engine for
Chrome Google, as the JavaScript engine, but also the use of efficient libev and libeio
library support for event driven and asynchronous I/O Node.JS biggest feature is the
use of asynchronous I/O and event driven design framework. Node.JS use the single
threaded model, for all of the I/O are using asynchronous request mode, to avoid the
frequent context switch. Node.JS in the execution of the process will maintain an event
queue, the program in the implementation of the event loop.
Waiting for the next event, each asynchronous I/O request is completed and will be
pushed to the event queue, waiting for the event.
With the social networking era coming, various forms of social networking applications
are continuing to emerge, the mainstream social networking applications including
Facebook, Twitter and Google+, etc., each carrying hundreds of millions of daily levels
of online traffic. Facing such a huge number of users concurrent access, how to continue
to provide high-speed, stable, smooth high-quality user experience will become one of
biggest problems to be solved for network applications.
To overcome concurrent challenges, we need to strengthen and improve the overall
system design and architecture from different dimensions, from inside and outside to
improve application concurrency ability comprehensively Both strengthen internal
systems architecture and optimize key algorithms, but also with third-party applications
external auxiliary components to improve concurrency. Compared to traditional mufti-
threaded programming model and language, application service platform based on
incident response mechanisms, whether from memory utilization, asynchronous I/O,
and ease of programming all have an unparalleled advantage, maximize effective use
of existing resources with enhancing system application concurrency.
This thesis is mainly based on Node.JS technology, combined with load balancing,
distributed storage and caching techniques, presents a innovative solution to solve
massive concurrent access by an efficient, stable, and scalable asynchronous and
concurrent architecture. Through technical research and real-time high concurrent
access performance tests, verify feasibility and innovation of network application
asynchronous concurrent architecture. Finally, problems and deficiencies arising in the
work are summarized, and prospected system future development.
2.2 program design
��?����£�*’�l�ucp��e q�d7� �u�(t+�
�
cp�£$<,1-2¤u#I�
The system is a simulate taxi call system, it connect to Google map API, simulate
taxis and all actions on Google map.
5
There are two kinds of model, one is multi-core multi-thread model, other one is
single thread model. Both of them will using to complete same taxi call system.
Figure 1. two different thread model
The multi-core multi-thread model is main-sub thread model, that is use single thread
to build thousands of sub-threads. The main thread only response to build sub-threads,
it averagely build sub-threads on each cores. Sub-threads response for process system
request. The single thread model is a single thread process a request on the core, and
next request needs waiting before it complete last request.
Server uses the express module to define a plurality of different HTTP post request.
Client sends the location to server, server returns the nearest taxi to client, client
displayed those taxi on Google map. Client send the destination to server, server
calculate the travel distance, return to the number of kilometers and prices. Web client
through the acquisition position function of HTML5 to getting user location, users can
also click other place on the map to change the position. Client is responsible for
interaction with the user, send post request to the server side, and display the return
data.
There are some classes function definition in Table 1.
Table 1. Classes definition
index.js main script, define the request that the server can handle
generate.js define function that generates some points around a center
6
public/carbooking.html client html page and js code
package.json define information of this app and its dependencies
node_modules all the library I make use of
node_modules/body-parser used to parse http request body
node_modules/express make use of this module to define http request handler
7
3.Implementation
�gB��+�§�)u��eG�^� node.js.�{��R}�
>�{u
node.jsO2��eg50s���node.jsV¦;�.�{�Y��e���
��{-E�{umT��e9¥v�)�`+/�br�h��uz|�;�
¥v�o�V¦�¢d�
X&P�d /�br�u�4�=�research
questions ��w]8���e9_�C�Hsu�{����£9<, 1-2¤
u#I�
3.1 System design
Users use computer or mobile phone browser open the web page, web browser
connect to Google map API, user can find taxis near their location on the map.
Then user can click “book”button, system will distribute a recently taxi and hiding
other taxis on the map.
User can click anywhere on the map for his destination address, and the taxi will
come to take him.
After arriving at the destination, the user click the “leave” button to finish this trip on
web page, and the system will show the mileage and price according to the distance
on website.
step 1 step 2
8
step 3 step 4
Figure 2. Program running
The test program is testing when system get 100 client parallel requests and each
client send request 10 times continuously, show rps and response time of two different
systems when they process each task (getposition, book, getprice, carbooking.html).
3.2 research questions simple answer
1. What are the main issues to be considered to customize Node.JS for multi-core
platforms?
The main difficulty of customize Node.JS for multi-core platform is that Node.JS itself
is running on a single thread, it can’t be changed to multi-thread directly. To let Node.JS
run on multi-core needs to deal with a lot of sub process task allocation and coordination.
2. How can a multi-threaded extension of Node.JS be realized?
Multi-thread design is the main process only responsible for establish a number of sub
processes , the actual logic processing are complete by sub processes .
Main process establish a number of sub process, these sub processes sharing server
port, when network requests come in, request processing tasks are assigned to each
sub process to realize processing of multiple requests at same time, to improve
efficiency.
3.What is the performance gain obtained from multi-threaded Node.JS compared with
the single-threaded version?
When system need response for huge multi-requests, multi-threaded system will have
much more rps and much less response time than single-thread system.
9
4.Results
�#IAL�£$<,1-2¤u#I�results analysts�Q�9���
Because it is a simulate system, so we run it by CMD. ‘node index.js’ is for debug,
‘node test.js’ is for run test program.
When we running multi-core multi-thread system, main thread build sub-threads shown
as ‘master’, next line shown how many cores have been used when system running.
When single thread system running, it will show nothing.
Figure 3. Program debug
The test program running as follow:
There are 4 tasks will be test when system working. ‘done’ means how many times
client send requests, blue ‘200’ is status code, ‘OK’ means how many times system
system processed requests. ‘rps’ is response per second, ‘response’ is response time.
1
0
Figure 4. Testing
5.Discussion and future
5.1 results analysts
���Q�ediscussionu�(�Q�e(j¥vu��q�6x ¥vuJn�
£$<,1¤u#I�
In test result we can see when system process task ‘book’, ‘getPrice’, ‘carbooking.html’,
the muti-core multi-threads system just cost 64%, 31%, 44% response time of single
thread system, and 121%, 151%, 208% rps of it. The reason of both of two system have
nearly getposition efficiency is the muti-core multi-threads system need time to create
sub-threads when it get requests, so it will cost some time.
Table 2. Test results
1
1
getposition book getPrice carbooking.ht
ml
min(multi) 169 53 16 47
min(single) 169 80 84 131
max(multi) 200 53 49 62
max(single) 185 84 116 131
avg(multi) 184 53 30 57
avg(single) 170 83 96 131
rps(multi) 46 144 153 158
rps(single) 54 119 86 76
5.2 future work
The solution for high concurrency, the traditional architecture is a multi-thread model,
is also a business logic with a system thread, thread switch through the system to
compensate for the synchronous I/O call time. This method will bring the server
hardware resource consumption.
Node.JS method for solving multiple request, lies in the way that the connection to the
server for example, in the Node.JS of each HTTP connection will launch an operation
in the Node.JS engine in the process of the event, and not for each connection to
generate a new OS process. This approach is based on event driven can better save the
server memory resources.
In addition, for those simple server, realized by Node.JS. Node.JS will be easy than use
of the C ten + to solve the long connection request, many problems caused by the cost,
so in some projects, such as online games, online chat, real-time message push function,
SNS real-time communication, real-time monitoring system, can consider the
application development of Node.JS.
In recent years, with the rapid development of the domestic shipping industry, the
quantity of ships in navigable waters has grown rapidly. However, at the same time, the
increasing quantity of ships and traffic density has a serious impact on the ecological
environment and traffic safety of the navigable waters. In order to guarantee the safety
of navigation, strengthen the management of the ship, The AIS base station network
system covered river and coastal areas waters has been established in China. The signal
has covered the important navigable waters and ports of coastal and inland countrywide.
AIS server is designed to receive AIS data with the feature of large quantity and real
time. Nevertheless, in order to avoid occurrences of data congestion, missing, etc.
During communication, it requires the communication links with the AIS base stations
stable, reliable, and able to meet the needs of real-time communication. Currently, the
most of AIS communication servers are developed with VC++ technology. They
receive a variety of information via the network port or the serial interface of the PC
linking with AIS base station, which is sent by sailing vessels through the network.
1
2
Node.JS is a platform which allows JavaScript to run on the server side. It has
abandoned the design ideas of the traditional platform which rely on mufti-threading to
achieve high concurrency. However, it adopted single-threaded, asynchronous I/O,
event-driven programming model. It has turned out to be suitable for server design with
the characteristic of high concurrency, real-time response and ultra-large-scale data.
6.Conclusion
�FaN�>�#I1�%uf”����
This degree work is finding a suitable multi-core multi-thread system for Node.JS
platform to letting Node.JS take full advantage of multi-core. From research result we
can see when client have huge multi-request, multi-core multi-thread system is more
high performance and efficiently than single thread system. That means if multi-core
multi-thread Node.JS can be using effectively, Node.JS can be using on
more broader field. Hope to see that Node.JS can get a wider range of using.
1
3
7.Bibliography
�bru�(£�����£�A>���9�b�Ut+br�u#I��
£�9�=kdbru3y�M¡9���:�KZ�
www.hkr.se/sv/lrc/biblioteket/att-hantera-referenser/vancouver/
http://www.ub.gu.se/skriva/referensstilar/
�
[1] Karagz,Mehmet Fatih.Design and implementation of RESTful wireless sensor
network
gateways using Node. js framework:proceeding of the 20th European Wireless
Conference,
1
4
Turgut, Cevahir, 2014: 517-522[C].
[2] Marak Squires. Ten node applications that need to exist[J].Nodejitsu Blog,
2011, (02):38-41.
[3] Stefan Tilkov, Steve Vinoski. Node. js:Using JavaScript to Build High Performance
Network Programs[J].IEEE Internet computing,2010, 14(06):80-83.
[4] Liu Feng. Site selection and networking scheme of AIS base station for inland
waterway J]. China Science and technology information, 2011, (11): 97-104.
[5] Liu Chang. Research on Key Technologies of automatic identification system (AIS)
for ships [D]. Dalian: Dalian Maritime University, 2013
[6] Douglas Crockford.JavaScript:The World’s Most Misunderstood
Language[J].Douglas Crockford, 2001, (10):6-15.
[7] Tao Limin, Xuchang, such as automatic identification system (AIS) of the base
station network research (J]. navigation technology, 2004, (04): 31-33.
[8] http://zh.wikipedia.org/wiki/V8_(JavaScript)#cite-note-comic-2.
[9] http://baike. baidu. com�iew/1303868. Htm.
[10] http://baike.baidu.com/link?url=AOihiTGyW8Ebx1-
OkBsEfGSMzmOVibimCnYIe90W8H
gXetYRk9GMCSS9kLqPL9Ppk5tfgK6-eGGhkKacfSUzvK. BYVoid. Node. JS
[11] http://my.oschina.net/u/1454562/blog/285926.
[12] Bruno Terkaly,Ricardo Villalobos.Real-World Scenarios for Node.JS
Azure[J].MSDN magazine, 2013, 28 (3):26-32.
[13] http://~.csdn.net/article/2012-05-03/2805296.
[14] j s Node. in Pak Ling. [M]. Beijing: People’s Posts and Telecommunications Press,
2013
[15] Kai Lei�Yining Ma�Zhi Tan. Performance Comparison and Evaluation of
Web
Development Technologies in PHP, Python, and Node.JS:proceeding of IEEE 17th
International Conference on Computational Science and Engineering (CSE2014)�
Chengdu,
2014: 661-668[C]�IEEE Computer Society Washington, DC, USA, c2014.
[16] Pedro Teixeira, Hu Xunqiang.Node., JS senior programming [M]. Beijing:
Tsinghua University press, 2013
[17] Shelley Powers, xiasiyu:.Node study guide [M].J Beijing: People’s Posts and
Telecommunications Press, 2014
[18] Liu Xinwen. Distributed AIS ship real time dynamic monitoring system based on
electronic chart “D]. Dalian: Dalian Maritime University,
Two thousand and twelve
[19]Tang Feifei. Application of universal automatic identification system (AIS) to
shore station network D]. Dalian: Dalian Maritime University, 2008
[20] Tang. The principle of computer composition [M]. Beijing: Higher Education
Press, 2008
Bryant, OHara Len, Gong Yili, et al. Understanding the computer system M]. Beijing:
mechanical industry press,http://blog, sina. com. cn/s/blog-68ffc7a401018221. html.
[21] Tilkov Stefan, Vinoski Steve. Node. js:Using JavaScriptto Build High-
1
5
Performance
Vetwork Programs[J].IEEE INTERNET COMPUTING.2010, 14(6):80-83.
[22] http://~.infoq.com/cn/articles/what�is-Node.JS.
[23] http://~.infoq.com/cn/news/2011/08/enterprise-Node.JS.
[24]http://www.infoq.com/cn/news/2011/05/nodeparty-hangzhou.
[25] Liu Pai. Design and implementation of a thermal measurement and monitoring
system based on JS D]. “Node. Beijing: Beijing University of Posts and
Telecommunications, 2014
[26] Zhang Jin, Pan Bichao, Liu Shiwen. Based on the Node. j s campus lost mobile
phone software design [J]. computer soft disk Piece and application, 2014, (09): 262-
263.
[27] Peng Na. Design and Implementation Based on the JS Node. blog system D].
Dalian: Dalian University of Technology, 2013
[28] Huang Danhua.Node. JS developer combat Xiangjie [M]. Beijing: Tsinghua
University press, 2014
[29]Mike Cantelon Marc Harter [33] T. J.Holowaychuk Nathan Rajlich, Wu Xinghai
translated j s.Node.
[30] George Ornbo.Node. j s Fu Qiang et al. Introduction to the classic [M]. Beijing:
People’s Posts and Telecommunications Press, 2013 Tom Hughes-Croucher Mike,
Wilson
[31]. Zheng Dachan translated.Node that is to learn that the use of [M]. Beijing: People’s
Posts and Telecommunications Press, 2013
[32] http://~.cnblogs.com/dolphinX/p/3475090.html.
[33] Xiao-Feng Gu} Le Yang, Shaoquan Wu.A real-time stream system based on
Node.JS:
proceeding of 2014 11th International Computer Conference on Wavelet Active Media
Technology and Information Processing (ICCWAMTIP)�Chengdu} China, 2014:
479-82[C].
[34] Kai Lei�Yining Ma�Zhi Tan. Performance Comparison and Evaluation of
Web
Development Technologies in PHP,Python,and Node.JS:proceeding of IEEE 17th
[35] International Conference on Computational Science and Engineering (CSE2014)�
Chengdu, 2014: 661-668[C].IEEE Computer Society Washington, DC, USA, c2014.
Programming in Windows