wasm runtime performance test

The previous article shared the wasmer runtime. The compiled openssl.wasm file can run independently. This article shares the performance comparison between openssl.wasm and native openssl.

Test hardware

  • Host MacBook Pro
  • CPU 6-Core Intel Core i7 2.2 GHz/Enable Hyper-Threading
  • RAM 16GB
  • Disk SSD

testing software

  • Openssl LibreSSL 2.8.3
  • Openssl WASM openssl installation package version openssl 1.1.1d
  • WASMER 1.0.0-alpha4

testing method

Encrypt and decrypt 100/300/500 MB multiple times to get the average result. Note that some modes here are not standard, so there are not all simulations, just for reference.

openssl test command

#!/bin/bash

key="A665A45920422F9D417E4867EFDC4FB8"
iv="4632527467615238616971576a466653"
#file100、file300、file500
fileName="file100"
ecbEnFileName="ecb-en-${fileName}"
ecbDeFileName="ecb-de-${fileName}"
cbcEnFileName="cbc-en-${fileName}"
cbcDeFileName="cbc-de-${fileName}"
ctrEnFileName="ctr-en-${fileName}"
ctrDeFileName="ctr-de-${fileName}"
gcmEnFileName="gcm-en-${fileName}"
gcmDeFileName="gcm-dn-${fileName}"
echo "------start openssl-aes-128 encryption-------"
echo "------ECB Encryption-------"
time openssl enc -aes-128-ecb -in ${fileName} -out ${ecbEnFileName} -K ${key} -p
echo "------CBC Encryption-------"
time openssl enc -aes-128-cbc -in ${fileName} -out ${cbcEnFileName} -K ${key} -iv ${iv} -p
echo "------CTR Encryption-------"
time openssl enc -aes-128-ctr -in ${fileName} -out ${ctrEnFileName} -K ${key} -iv ${iv} -p
echo "------GCM Encryption-------"
time openssl enc -aes-128-gcm -in ${fileName} -out ${gcmEnFileName} -K ${key} -iv ${iv} -p

echo "#############################################################"
echo "------start openssl-aes-128 Decrypt-------"
echo "------ECB Decrypt-------"
time openssl enc -aes-128-ecb -d -in ${ecbEnFileName} -out ${ecbDeFileName} -K ${key} -p
echo "------CBC Decrypt-------"
time openssl enc -aes-128-cbc -d -in ${cbcEnFileName} -out ${cbcDeFileName} -K ${key} -iv ${iv} -p
echo "------CTR Decrypt-------"
time openssl enc -aes-128-ctr -d -in ${ctrEnFileName} -out ${ctrDeFileName} -K ${key} -iv ${iv} -p
echo "------GCM Decrypt-------"
time openssl enc -aes-128-gcm -d -in ${gcmEnFileName} -out ${gcmDeFileName} -K ${key} -iv ${iv} -p

openssl.wasm test command

#!/bin/bash

key="A665A45920422F9D417E4867EFDC4FB8"
iv="4632527467615238616971576a466653"
#file100、file300、file500
fileName="file500"
ecbEnFileName="aes/ecb-en-${fileName}"
ecbDeFileName="aes/ecb-de-${fileName}"
cbcEnFileName="aes/cbc-en-${fileName}"
cbcDeFileName="aes/cbc-de-${fileName}"
ctrEnFileName="aes/ctr-en-${fileName}"
ctrDeFileName="aes/ctr-de-${fileName}"

# 创建需要的文件,wasm文件运行,不会把数据写入磁盘,所以需要提前准备文件
touch ${ecbEnFileName}
touch ${ecbDeFileName}
touch ${cbcEnFileName}
touch ${cbcDeFileName}
touch ${ctrEnFileName}
touch ${ctrDeFileName}

echo "------start openssl-aes-128 encryption-------"
echo "------ECB Encryption-------"
time wasmer run openssl.wasm --dir=. -- enc -aes-128-ecb -in ${fileName} -out ${ecbEnFileName} --nosalt -K ${key}
echo "------CBC Encryption-------"
time wasmer run openssl.wasm --dir=. -- enc -aes-128-cbc -in ${fileName} -out ${cbcEnFileName} --nosalt -K ${key} -iv ${iv}
echo "------CTR Encryption-------"
time wasmer run openssl.wasm --dir=. -- enc -aes-128-ctr -in ${fileName} -out ${ctrEnFileName} --nosalt -K ${key} -iv ${iv}

echo "#############################################################"
echo "------start openssl-aes-128 Decrypt-------"
echo "------ECB Decrypt-------"
time wasmer run openssl.wasm --dir=. -- enc -aes-128-ecb -d -in ${ecbEnFileName} -out ${ecbDeFileName} --nosalt -K ${key}
echo "------CBC Decrypt-------"
time wasmer run openssl.wasm --dir=. -- enc -aes-128-cbc -d -in ${cbcEnFileName} -out ${cbcDeFileName} --nosalt -K ${key} -iv ${iv}
echo "------CTR Decrypt-------"
time wasmer run openssl.wasm --dir=. -- enc -aes-128-ctr -d -in ${ctrEnFileName} -out ${ctrDeFileName} --nosalt -K ${key} -iv ${iv}

Test Results

Openssl

encryption

Mode/file size 100M 300M 500M key data size
ECB 0.197s 0.970s 1.418s 128bit
CBC 0.244s 1.421s 1.278s 128bit
CTR 0.203s 0.587s 1.145s 128bit
GCM 0.303s 1.123s 1.710s 128bit

Decrypt

Mode/file size 100M 300M 500M key data size
ECB 0.309s 1.014s 1.275s 128bit
CBC 0.260s 0.946s 1.630s 128bit
CTR 0.322s 1.081s 1.591s 128bit
GCM 0.429s 1.283s 1.842s 128bit

Openssl.wasm

encryption

Mode/file size 100M 300M 500M key data size
ECB 1.775s 5.326s 8.996s 128bit
CBC 1.805s 5.563s 9.452s 128bit
CTR 2.011 6.133s 10.350s 128bit

Decrypt

Mode/file size 100M 300M 500M key data size
ECB 1.894s 5.780s 9.186s 128bit
CBC 1,880s 5.892s 10.123s 128bit
CTR 2.168s 6.297s 11.424s 128bit

in conclusion

According to the time-consuming comparison of openssl.wasm and openssl according to different encryption and decryption modes and files of different sizes, the speed of openssl.wasm is not as efficient as that of the native openssl. I am also curious about the specific reasons. There are not many information on the Internet. My personal understanding from previous research on wasm may be because the wasm virtual machine causes the running speed to be not so fast.

The definition of WebAssembly is a binary instruction format of a stack-based virtual machine. The stack virtual machine model is a common memory management structure, which pushes data and operators onto the stack and pops them out for successive execution. The binary instruction of wasm is a virtual instruction set. The virtual instruction set (V-ISA) can be regarded as a series of platform-independent custom operators (such as JVM). In contrast, the physical instruction set (ISA) is strongly dependent on the physical system. (Such as Intel's x86-64).

Because WASM relies on the V-ISA instruction set, it completely relies on the operating instructions provided by the host environment. So if you want to run on a real operating system, you must have a "virtual machine" to interpret these instructions and shield the differences between specific system calls. That is, a WASM VM similar to JVM is needed, and WASI is the system interface that defines these abstract system calls abstraction layer, WASM platform. So as to realize the characteristic of "compile once, run everywhere".

Another feature is the security of WASM, which uses a sandbox mechanism. This means that the code cannot directly interact with the operating system. So how does it use system resources? The host (which may be a browser or WASM runtime) puts the function into a sandbox that the code can use. That is, the virtual machine is fully responsible for the control of permissions, and all system calls are controlled by the periphery to isolate the sandbox environment, which can avoid security problems caused by third-party libraries.

From the perspective of portability and security, compared with openssl installed directly on the host, it makes sense that openssl.wasm is less efficient. Not only that, but the speed of wasm on different platforms is also different. Wasm is still in the development stage, and interested friends can continue to pay attention to wasm dynamics.

scenes to be used

The scenario of applying WebAssembly in the browser, such as:

  • Porting programs written in C, C++, Rust and other languages ​​to the browser
  • game:
    • Small games that need to be opened quickly
    • AAA level game with a lot of resources.
    • Game Portal (Agent/Original Game Platform)
  • Graphics and image processing fields, such as page games, data visualization, etc.
  • Encryption tool
  • CAD software
  • Audio and video codec, etc.

Non-browser application scenarios, some are also listed in the official documents, but they are all relatively abstract

  • Edge cloud of Fastly and other service providers
  • Node and npm (see translation)

    • "WebAssembly can solve one of the biggest problems in Node - how to get close to native speed and reuse code written in other languages ​​(such as C and C++) like native modules, while still maintaining portability and security. Standardization This system interface is the first step in achieving this goal."
    • "Npm is extremely interested in that WebAssembly has the potential to expand the npm ecosystem while greatly simplifying the process of running native code in server-side JavaScript applications. We look forward to the results of this process."
  • Plug-in system. No matter the browser, IDE or any other plug-in system, if WebAssembly is adopted, it can not only ensure the security and avoid the unauthorized code of the plug-in, but also decouple from the development language, which is conducive to the prosperity of the plug-in ecology.

For more usage scenarios, please refer to the official document: http://webassembly.org.cn/docs/use-cases/

Future direction

  • The process of adding subsequent features to the WebAssembly standard
    • Directly manipulate the DOM
    • Concurrency of shared memory
    • SIMD (single instruction, multiple data)
    • Exception handling
    • Other improvements-make it easier for developers
      • First-class developer tools
      • Garbage collection
      • ES6 module integration.

Reference documents


Netwarps is composed of a senior cloud computing and distributed technology development team in China. The team has very rich experience in the financial, power, communications and Internet industries. Netwarps currently has R&D centers in Shenzhen and Beijing, with a team size of 30+, most of which are technicians with more than ten years of development experience, from professional fields such as the Internet, finance, cloud computing, blockchain, and scientific research institutions.
Netwarps focuses on the development and application of secure storage technology products. The main products include decentralized file system (DFS) and decentralized computing platform (DCP), and are committed to providing distributed storage and distributed based on decentralized network technology. The computing platform has the technical characteristics of high availability, low power consumption and low network, and is suitable for scenarios such as the Internet of Things and Industrial Internet.
Official account: Netwarps

Guess you like

Origin blog.51cto.com/14915984/2570480