Comparison of Golang/goroutine and swoole/coroutine performance tests

Source: Internet
Author: User

Some of the characteristics and advantages of the process I will not say, many articles on the Internet are very thorough.

The
process can be understood as a purely user-state thread, which is switched through collaboration rather than preemption. With respect to the process or thread, all operations of the coprocessor can be done in the user state, creating and switching the consumption lower. Developers can use the synchronous code writing method to achieve the effect and performance of asynchronous Io, avoid the discrete code logic caused by the traditional asynchronous callback and fall into the multi-layer callback to cause the code to be unable to maintain.

1, Golang::goroutine

Recently in learning go, some of the high-level features are really for the higher concurrency, the self-contained net/http package on the processing of the request is also transparent in the context of the association, the real out-of-the-box.

Server.go

package mainimport (    "fmt"    "net/http"    "time"    "log"    "runtime"    "bytes"    "strconv")func main() {    // 注册请求 handler    http.HandleFunc("/", func (responseWrite http.ResponseWriter, request *http.Request) {        log.Println("goroutine: ", GetGID(), "start")        // 模拟2秒的IO耗时操作 会发生协程切换        time.Sleep(2 * time.second)        // 协程继续执行        log.Println("goroutine: ", GetGID(), "end")        // 结束请求        fmt.Fprintln(responseWrite, "

The Golang comes with a net/http package that creates a process to handle the request, so the blocked 2 second does not suspend the main process, and the main process still receives the request and creates a coprocessor to process the request.

2, Swoole::coroutine

The ability of the swoole to start the built-in coprocessor (Coroutine) in 2.0 provides the ability to have a co-capable IO interface (unified in the namespace swoolecoroutine*).

server.php

<?php//high concurrent multi-process also requires a certain amount of memory ini_set (' Mermory_limit ', ' 1024M '); $server = new Swoole\http\server (' 127.0.0.1 ', 8082);// Number of worker processes set to 1 This is consistent with the Golang process model//Swoole worker The default number of Max_coroutine is 3000 per business load//@reference https://wiki.swoole.com/w  Iki/page/950.html$server->set ([' worker_num ' = 1,]); $server->on ("Start", function ($server) {echo "server Start ... ". Php_eol;}); /Processing request $server->on (' request ', function ($request, $response) {echo "Goroutine:". Swoole\coroutine::getuid (). "Start".        Php_eol; /** * Here we need to use swoole\coroutine::sleep () to simulate IO time-consuming trigger co-switchover * real business scenarios in which you may be using the following coprocessor client * coroutine\client coroutine\  Http\client coroutine\http2\client * Coroutine\redis coroutine\socket coroutine\mysql Coroutine\PostgreSQL * php    Sleep () is not able to trigger the process switch will be synchronized blocking * This is why the swoole in the process to use the specified IO client to play the performance of the coprocessor */Swoole\coroutine::sleep (2); echo "Goroutine:". Swoole\coroutine::getuid (). "End".        Php_eol; $response->end ("

The Swoole Server will be transparent to the same golang as net/http, but we need to use some pre-provided methods or clients in Swoole to trigger the co-operation to perform the high performance of the process.
Swoole Client: https://wiki.swoole.com/wiki/...

3, AB pressure measurement

Server configuration slightly slag, vsphere on a small self-service server 1 core 2G, do not care

Pressure measurement 200 Concurrent 5000 requests

ab -c 200 -n 5000 -k http://127.0.0.1:8081ab -c 200 -n 5000 -k http://127.0.0.1:8082

Go

Swoole

Pressure measurement 500 Concurrent 5000 requests

ab -c 500 -n 5000 -k http://127.0.0.1:8081ab -c 500 -n 5000 -k http://127.0.0.1:8082

Go

Swoole

Pressure measurement 1000 Concurrent 5000 requests

ab -c 1000 -n 5000 -k http://127.0.0.1:8081ab -c 1000 -n 5000 -k http://127.0.0.1:8082

Go

Swoole

Pressure Measurement 2000 Concurrent 5000 requests

ab -c 2000 -n 5000 -k http://127.0.0.1:8081ab -c 2000 -n 5000 -k http://127.0.0.1:8082

Go

Swoole

Can be seen from the above groups of data, Golang::goroutine and Swoole::coroutine two of the performance of the basic no gap, and the co-process in high concurrency can be more intuitive to reflect the excellent performance.

The same 5000 of requests, 200-500-1000-2000 processing time is getting smaller, it means that I do not eat, it is your hair too slow. When 2000 is concurrent, my 1-core 2G server can run to 600-concurrency in a 2-second blocking scenario.

But Swoole still to occupy more memory, I test machine memory is smaller, no way to use swoole perfect run c10k, memory is not enough, no way to create more concurrent processing request, but go can also, back in the next look at the Go::goroutine is not occupied memory smaller bar, Run a look first.

Go 10k Concurrent 200k Requests

Go 20k concurrent 1000k requests

AB Maximum concurrent analog is 20k, for reference only

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.