<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by smallnest on Medium]]></title>
        <description><![CDATA[Stories by smallnest on Medium]]></description>
        <link>https://medium.com/@smallnest?source=rss-3b7dcf0cd088------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*dZmmqP59rEb03qt_ohzC4Q.jpeg</url>
            <title>Stories by smallnest on Medium</title>
            <link>https://medium.com/@smallnest?source=rss-3b7dcf0cd088------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 21:40:51 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@smallnest/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to Efficiently Batch Read Data from Go Channels]]></title>
            <link>https://medium.com/@smallnest/how-to-efficiently-batch-read-data-from-go-channels-7fe70774a8a5?source=rss-3b7dcf0cd088------2</link>
            <guid isPermaLink="false">https://medium.com/p/7fe70774a8a5</guid>
            <category><![CDATA[go]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[channel]]></category>
            <dc:creator><![CDATA[smallnest]]></dc:creator>
            <pubDate>Sat, 23 Dec 2023 15:42:44 GMT</pubDate>
            <atom:updated>2023-12-23T15:42:44.092Z</atom:updated>
            <content:encoded><![CDATA[<p>In recent years, Go language has received widespread attention for its efficient concurrency model. Goroutines and channels are the basis for building concurrent programs in Go. Channels can be used to safely pass data and signals between goroutines.</p><p>In many scenarios, we need to batch read data from channels for processing. For example, batch writing data to databases, batch sending messages to Kafka, or batch writing to the network, etc. This batch processing method can significantly improve program performance.</p><p>However, Go channels do not provide native batch reading methods. So how can we efficiently read batch data from channels? This article will introduce a mature solution.</p><h3>Simplify Batch Reading with chanx Library</h3><p>To facilitate batch reading of channel data, the <a href="https://github.com/smallnest/exp/blob/master/chanx/batcher.go">chanx</a> library implements the Batch method:</p><pre>func Batch[T any](ctx context.Context, ch &lt;-chan T, batchSize int, fn func([]T))</pre><p>It is very simple to use:</p><ul><li>Pass in a context for cancellation/timeout control</li><li>Pass in a reading channel ch</li><li>Set batch size batchSize</li><li>Pass in a handling function fn to process each batch of data</li></ul><p>Example:</p><pre>func TestBatch(t *testing.T) {<br> ch := make(chan int, 10)<br> for i := 0; i &lt; 10; i++ {<br>  ch &lt;- i<br> }<br><br> count := 0<br> go Batch[int](context.Background(), ch, 5, func(batch []int) {<br>  if len(batch) != 5 {<br>   assert.Fail(t, &quot;expected batch size 5, got %d&quot;, len(batch))<br>  }<br>  count += len(batch)<br> })<br> time.Sleep(time.Second)<br> close(ch)<br> assert.Equal(t, 10, count)<br>}</pre><p>In the above example, we write 10 int data to the channel, and then call Batch to batch read from the channel. We set the batch size to 5, that is, read up to 5 data from the channel each time. The read data will be passed to the function fn for processing.</p><p>This allows us to easily read and process batch data.</p><h3>FlatBatch Supports Batch Reading Batched Data</h3><p>The <a href="https://github.com/smallnest/exp/blob/master/chanx/batcher.go">chanx</a> library also provides the FlatBatch method, which supports batch reading batched data from channels:</p><pre>func FlatBatch[T any](ctx context.Context, ch &lt;-chan []T, batchSize int, fn func([]T))</pre><p>The channel parameter of FlatBatch is &lt;-chan []T, which means each data in the channel is a sub slice. It will unfold the data in the read sub slices according to the set batch size into a large slice, and then pass it to the handling function.</p><p>The below code is an example:</p><pre><br>func TestFlatBatch(t *testing.T) {<br> ch := make(chan []int, 10)<br> for i := 0; i &lt; 10; i++ {<br>  ch &lt;- []int{i, i}<br> }<br><br> count := 0<br> go FlatBatch[int](context.Background(), ch, 5, func(batch []int) {<br>  assert.NotEmpty(t, batch)<br>  count += len(batch)<br> })<br> time.Sleep(time.Second)<br> close(ch)<br> assert.Equal(t, 20, count)<br>}</pre><h3>Implementation</h3><p>To implement efficient batch reading, there are several key issues to consider:</p><ol><li>Cannot read the channel indefinitely, must set read size, otherwise it will exhaust memory</li><li>Cannot wait indefinitely if the channel has no data temporarily, which will block reading</li><li>But cannot check the channel too frequently either, which will cause CPU spinning</li></ol><p>A preliminary implementation is to use a read buffer batch to store the read data. If the buffer is full, call the handler; if the channel is closed, also process the data in the buffer.</p><p>However, this would cause a starvation problem if the channel has no new data for long.</p><pre> var batch = make([]T, 0, batchSize)<br> for {<br>  select {<br>  case &lt;-ctx.Done():<br>   if len(batch) &gt; 0 {<br>    fn(batch)<br>   }<br>   return<br>  case v, ok := &lt;-ch:<br>   if !ok { // closed<br>    fn(batch)<br>    return<br>   }<br><br>   batch = append(batch, v)<br>   if len(batch) == batchSize { // full<br>    fn(batch)<br>    batch = make([]T, 0, batchSize) // reset<br>   }<br>  }<br> }<br>}</pre><p>We can add a default branch in select to process buffered data when no data:</p><pre>func Batch[T any](ctx context.Context, ch &lt;-chan T, batchSize int, fn func([]T)) {<br>    var batch = make([]T, 0, batchSize)<br>    for {<br>        select {<br>        case &lt;-ctx.Done():<br>            if len(batch) &gt; 0 {<br>                fn(batch)<br>            }<br>            return<br>        case v, ok := &lt;-ch:<br>            if !ok { // closed<br>                fn(batch)<br>                return<br>            }<br><br>            batch = append(batch, v)<br>            if len(batch) == batchSize { // full<br>                fn(batch)<br>                batch = make([]T, 0, batchSize) // reset<br>            }<br>        default:<br>            if len(batch) &gt; 0 {<br>                fn(batch)<br>                batch = make([]T, 0, batchSize) // reset<br>            }<br>        }<br>    }<br>}</pre><p>But this will cause CPU spinning problem, as default branch will be called frequently when no data.</p><p>One improvement is to add a time.After timer in default branch to process buffered data every period of time:</p><pre>func Batch[T any](ctx context.Context, ch &lt;-chan T, batchSize int, fn func([]T)) {<br>    var batch = make([]T, 0, batchSize)<br>    for {<br>        select {<br>        case &lt;-ctx.Done():<br>            if len(batch) &gt; 0 {<br>                fn(batch)<br>            }<br>            return<br>        case v, ok := &lt;-ch:<br>            if !ok { // closed<br>                fn(batch)<br>                return<br>            }<br><br>            batch = append(batch, v)<br>            if len(batch) == batchSize { // full<br>                fn(batch)<br>                batch = make([]T, 0, batchSize) // reset<br>            }<br>        case &lt;-time.After(100 * time.Millisecond):<br>            if len(batch) &gt; 0 {<br>                fn(batch)<br>                batch = make([]T, 0, batchSize) // reset<br>            }<br>        }<br>    }<br>}</pre><p>However, this has the following problems:</p><ul><li>memory leaks because a lot of timers exist</li><li>data in the channel have not been read during timer SLEEP</li></ul><p>Is there a better implementation? The answer is yes. <a href="https://pkg.go.dev/github.com/smallnest/exp/chanx">chanx</a> library adopts a clever implementation:</p><pre>func Batch[T any](ctx context.Context, ch &lt;-chan T, batchSize int, fn func([]T)) {<br> var batch = make([]T, 0, batchSize)<br> for {<br>  select {<br>  case &lt;-ctx.Done():<br>   if len(batch) &gt; 0 {<br>    fn(batch)<br>   }<br>   return<br>  case v, ok := &lt;-ch:<br>   if !ok { // closed<br>    fn(batch)<br>    return<br>   }<br><br>   batch = append(batch, v)<br>   if len(batch) == batchSize { // full<br>    fn(batch)<br>    batch = make([]T, 0, batchSize) // reset<br>   }<br>  default:<br>   if len(batch) &gt; 0 { // partial<br>    fn(batch)<br>    batch = make([]T, 0, batchSize) // reset<br>   } else { // empty<br>    // wait for more<br>    select {<br>    case &lt;-ctx.Done():<br>     if len(batch) &gt; 0 {<br>      fn(batch)<br>     }<br>     return<br>    case v, ok := &lt;-ch:<br>     if !ok {<br>      return<br>     }<br><br>     batch = append(batch, v)<br>    }<br><br>   }<br>  }<br> }<br>}</pre><p>The handling in default branch is crucial:</p><ul><li>If batch not empty, process batch data immediately</li><li>If batch empty, block waiting for channel or ctx signal</li></ul><p>This implementation prevents both starvation and CPU spinning. It also minimizes inefficient waiting and reduces read latency.</p><p>Through carefully designed read logic, we can achieve efficient, low-latency batch reading of channel data. This batch processing pattern can improve the performance and resilience of many Go programs.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7fe70774a8a5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Go K/V databases Benchmark]]></title>
            <link>https://medium.com/@smallnest/go-k-v-databases-benchmark-cd051279ef22?source=rss-3b7dcf0cd088------2</link>
            <guid isPermaLink="false">https://medium.com/p/cd051279ef22</guid>
            <category><![CDATA[go]]></category>
            <category><![CDATA[benchmark]]></category>
            <category><![CDATA[databas]]></category>
            <dc:creator><![CDATA[smallnest]]></dc:creator>
            <pubDate>Wed, 06 Mar 2019 05:21:37 GMT</pubDate>
            <atom:updated>2019-03-07T08:29:36.439Z</atom:updated>
            <content:encoded><![CDATA[<h3>Go K/V Databases Benchmark</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*11DoUz1iZt4Fs6R_5SB_NA.jpeg" /></figure><p>There are several well-known k/v database in Go ecosystem. They can be used as persistent caches, storages, databases, etc. All of them have common features such as Get, Set (Update), Delete, and some of them have customized features such as transaction, bucket, column families.</p><p>For those common features, how about their benchmarks? This post will give you some test results in Solid State Drive (<strong>SSD</strong>) or <strong>Hard Disk</strong> Drive cases.</p><p>My test code is cloned from <a href="https://github.com/tidwall/kvbench">https://github.com/tidwall/kvbench</a>. tidwall/kvbench implements a redis protocol server to serve Get/Set operations by using k/v databases. I have implemented a cli tool to test those k/v databases without any redis protocol server. It can test each k/v databases and output test results in console.</p><p>And I also add more k/v database such as badger, rocksdb, bbolt and goleveldb. Now the test contains the below k/v databases:</p><ul><li><a href="https://github.com/facebook/rocksdb">Rocksdb</a></li><li><a href="https://github.com/dgraph-io/badger">badger</a></li><li><a href="https://github.com/syndtr/goleveldb">goleveldb</a></li><li><a href="https://github.com/boltdb/bolt">bolt</a></li><li><a href="https://github.com/etcd-io/bbolt">bbolt</a></li><li><a href="https://github.com/tidwall/buntdb">buntdb</a></li><li><a href="https://github.com/cznic/kv">cznic/kv</a></li><li>map (in-memory) with <a href="https://redis.io/topics/persistence">AOF persistence</a></li><li>btree (in-memory) with <a href="https://redis.io/topics/persistence">AOF persistence</a></li></ul><blockquote>Size of keys is 9 bytes and Size of value is 256 bytes.<br>Test four cases: concurrent only set, concurrent only get, one set and concurrent get, concurrent delete.<br>Use<em> </em>fsync<em> to control whether the write operation should be sync into the disk. For memory case this option is useless.<br>Tests are executed both in HDD and SDD. Some databases are </em>optimized for SDD.</blockquote><h3><strong>HDD Case</strong></h3><p>The server uses Seagate HDD:</p><pre>Vendor:               SEAGATE<br>Product:              ST900MM0168<br>User Capacity:        900,185,481,216 bytes [900 GB]<br>Rotation Rate:        10500 rpm</pre><p>40 logical cpu cores, 32G memory.</p><p><strong>nofsync</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mFeOItVgo-Z4fT59b_Af4w.png" /><figcaption>fsync=false, throughtputs</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jwDmY97529hjPowPZYHeWA.png" /></figure><p>Btree and map are always great. <br>Rocksdb looks good, buntdb Get operations is very high. <br>The set operations are also high for all databases although the gap between them is still large.</p><h4>fsync</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uU25IZEXvs5ODwh1s7-5Kg.png" /><figcaption>fsync=true, throughputs</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aH-ED36D5pCcsL0jUtidKQ.png" /></figure><p>The write operations have fallen sharply. Rocksdb and badger can reach 1000 op/s but others can’t reach 100 op/s. (Ignore btree/memory and map/memory because they won’t sync data to disk)</p><h3>SSD Case</h3><p>The server uses SSD:</p><pre>Vendor:               AVAGO<br>Product:              AVAGO<br>Revision:             4.66<br>User Capacity:        298,999,349,248 bytes [298 GB]<br>Device Speed: 6.0Gb/s<br>Link Speed: 12.0Gb/s<br>Media Type: Solid State Device</pre><p>20 logical cpu cores, 128G memory.</p><p><strong>nofsync</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*07hRXCxoqkMAfGyQameLPA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o73ezQ2_3QZb_-11FG25kA.png" /></figure><p><strong>fsync</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*h_bXZmZbXZhERelow3qiZw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bGT1oHFySzdF70HvGO-B6w.png" /></figure><p>For some databases such as rocksdb, badger, performance of write operations improved significantly and performance of others database are also improved.</p><p>In mixed concurrent case (one set, multiple get), performance of the get operation in mixed case is far less than performance of the get operation in read-only case.</p><p>The code is at <a href="https://github.com/smallnest/kvbench">github</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cd051279ef22" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Make your methods as services in 10 seconds]]></title>
            <link>https://medium.com/@smallnest/make-your-methods-as-services-in-10-seconds-14ec74b86210?source=rss-3b7dcf0cd088------2</link>
            <guid isPermaLink="false">https://medium.com/p/14ec74b86210</guid>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[rpc]]></category>
            <dc:creator><![CDATA[smallnest]]></dc:creator>
            <pubDate>Tue, 13 Feb 2018 07:39:10 GMT</pubDate>
            <atom:updated>2018-02-13T07:50:18.965Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/200/1*mTQy06ArZiadeSMXk297mw.png" /></figure><p><a href="https://github.com/smallnest/rpcx">rpcx</a> is a service-governance rpc framework in Go, which is inspired by Alibaba <a href="https://github.com/alibaba/dubbo">Dubbo</a>.</p><p>rpcx has been deployed in products and it handles billions of service calls every day more than half a year. Compared to the previous implementation in Java, the system only uses half of machines saves hundreds of thousands of Hong Kong dollars a month for the company’s cost.</p><p>One of the design philosophy of the rpcx framework is <strong>simply</strong>. Users don’t need to spend a lot of time on learning frameworks. And rpcx do not need proto files and only use raw golang methods and functions just like golang std rpc lib. Only **ten** lines of code just can start a rpc server.</p><p>Recently I learned from atih Arslan’s <a href="https://github.com/fatih/gomodifytags">gomodifytags</a> , and golang tools implementation. I decided to write a tool to help developers to create the rpcx server.</p><p>So <a href="https://github.com/smallnest/rpcx/tree/master/tool/xgen">xgen</a> was born.</p><p><strong>xgen</strong> can search given packages and get types that can be registered as rpcx services. Then it uses templates to create the code of server and register those types as services.</p><p>You can use <em>go install github.com/smallnest/rpcx/tool/xgen</em> to install xgen in your <em>$GOPATH</em>.</p><p>Once you has installed <em>xgen</em>, you can view its help info:</p><pre><br>$ xgen -h<br>Usage of xgen:<br> -o string<br> specify the filename of the output<br> -pkg<br> process the whole package instead of just the given file<br> -r string<br> registry type. support etcd, consul, zookeeper, mdns<br> -tags string<br> build tags to add to generated file</pre><p>It can run as <em>xgen -o output.go -pkg a/b/c/d -r etcd -tags etcd service1.go service2.go</em>.</p><p>If you use <em>-pkg</em> option, xgen will ignore parameters such as service1.go and only search services in packages indicated by <em>-pkg</em>.</p><p><em>-o</em> is used that which file to output. If not set, the generated code are printed in `Stdout`.</p><p><em>-r</em> can be used to add registry such as <em>zookeeper</em>、<em>etcd</em>、<em>consul</em> and <em>mdns</em>. If not set, no registry is added in the server and clients only use IPAddress and port to connect the server directly.</p><p><em>-tags</em> is used to add <a href="https://golang.org/pkg/go/build/#hdr-Build_Constraints">Build Constraints</a>.</p><p>After introduce this tool, let’s write an example.</p><p>In <a href="https://github.com/rpcx-ecosystem/rpcx-examples3/tree/master/xgen">rpcx-examples3/xgen</a>, there is a <em>server.go</em> file, which contains three services <em>Arith</em>、<em>Echo</em>、<em>TimeService</em>.</p><pre><br>package xgen</pre><pre>import (<br> “context”<br> “fmt”<br> “time”</pre><pre>example “github.com/rpcx-ecosystem/rpcx-examples3”<br>)</pre><pre>type Arith int</pre><pre>func (t *Arith) Mul(ctx context.Context, args example.Args, reply *example.Reply) error {<br> reply.C = args.A * args.B<br> return nil<br>}</pre><pre>func (t *Arith) Add(ctx context.Context, args *example.Args, reply *example.Reply) error {<br> reply.C = args.A + args.B<br> return nil<br>}</pre><pre>type Echo string</pre><pre>func (s *Echo) Echo(ctx context.Context, args string, reply *string) error {<br> *reply = fmt.Sprintf(“Hello %s from %s”, args, *reply)<br> return nil<br>}</pre><pre>type TimeS struct{}</pre><pre>func (s *TimeS) Time(ctx context.Context, args time.Time, reply *time.Time) error {<br> *reply = time.Now()<br> return nil<br>}</pre><p>Those tree types follows rules of rpcx services:</p><p>- exported method of exported type<br>- three arguments, the first is of context.Context, both of exported type for three arguments<br>- the third argument is a pointer<br>- one return value, of type error</p><p>Now you can use <em>xgen</em> to generate server code for those services。</p><pre>xgen -o cmd/main.go -r “etcd” -pkg github.com/rpcx-ecosystem/rpcx-examples3/xgen</pre><p>or the below if you are in the directory.</p><pre><br>xgen -o cmd/main.go -r “etcd” ./server.go<br></pre><p>The server code is generated in cmd/main.go and you can run it: <em>go run -tags “etcd” cmd/main.go</em>.</p><p>It is just so simple.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=14ec74b86210" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Go web framework benchmark]]></title>
            <link>https://medium.com/@smallnest/go-web-framework-benchmark-93a34403ef0a?source=rss-3b7dcf0cd088------2</link>
            <guid isPermaLink="false">https://medium.com/p/93a34403ef0a</guid>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[benchmark]]></category>
            <dc:creator><![CDATA[smallnest]]></dc:creator>
            <pubDate>Thu, 07 Apr 2016 02:12:32 GMT</pubDate>
            <atom:updated>2016-04-07T02:12:32.923Z</atom:updated>
            <content:encoded><![CDATA[<p>I created <a href="https://github.com/smallnest/go-web-framework-benchmark">a benchmark suite</a> aims to compare the performance of Go web frameworks. It is inspired by <a href="https://github.com/julienschmidt/go-http-routing-benchmark">Go HTTP Router Benchmark</a> but this benchmark suite is different with that. Go HTTP Router Benchmark suit aims to compare the performance of <strong>routers</strong> but this Benchmark suit aims to compare the whole HTTP request processing.</p><h3>Tested web frameworks</h3><ul><li><a href="https://golang.org/pkg/net/http/">default http</a></li><li><a href="https://github.com/Unknwon/macaron">macaron</a></li><li><a href="https://github.com/ant0ine/go-json-rest">go-json-rest</a></li><li><a href="https://github.com/astaxie/beego">beego</a></li><li><a href="https://github.com/bmizerany/pat">pat</a></li><li><a href="https://github.com/celrenheit/lion">lion</a></li><li><a href="https://github.com/dimfeld/httptreemux">httptreemux</a></li><li><a href="https://github.com/emicklei/go-restful">go-restful</a></li><li><a href="https://github.com/gin-gonic/gin">gin</a></li><li><a href="https://github.com/go-martini/martini">martini</a></li><li><a href="https://github.com/go-playground/lars">lars</a></li><li><a href="https://github.com/go-zoo/bone">bone</a></li><li><a href="https://github.com/gocraft/web">gocraft</a></li><li><a href="https://github.com/gorilla/mux">gorilla</a></li><li><a href="https://github.com/julienschmidt/httprouter">httprouter</a></li><li><a href="https://github.com/kataras/iris">iris</a></li><li><a href="https://github.com/lunny/tango">tango</a></li><li><a href="https://github.com/mailgun/route">vulcan</a></li><li><a href="https://github.com/mikespook/possum">possum</a></li><li><a href="https://github.com/naoina/denco">denco</a></li><li><a href="https://github.com/pilu/traffic">traffic</a></li><li><a href="https://github.com/plimble/ace">ace</a></li><li><a href="https://github.com/rcrowley/go-tigertonic">go-tigertonic</a></li><li><a href="https://github.com/valyala/fasthttp">fasthttp</a></li><li><a href="https://github.com/vanng822/r2router">r2router</a></li><li><a href="https://github.com/zenazn/goji/web">goji</a></li><li><a href="http://goji.io/">gojiv2</a></li><li><a href="http://gopkg.in/labstack/echo.v1">echo</a></li></ul><h3>Motivation</h3><p>When I investigated performance of Go web frameworks, I found <a href="https://github.com/julienschmidt/go-http-routing-benchmark">Go HTTP Router Benchmark</a>, created by Julien Schmidt. He also developed a high performance http router: <a href="https://github.com/julienschmidt/httprouter">httprouter</a>. I had thought I got the performance result until I created a piece of codes to mock the real business logics:</p><pre>api.Get(&quot;/rest/hello&quot;, func(c *XXXXX.Context) {<br>        sleepTime := strconv.Atoi(os.Args[1]) //10ms<br>        if sleepTime &gt; 0 {<br>            time.Sleep(time.Duration(sleepTime) * time.Millisecond)<br>        }<br><br>        c.Text(&quot;Hello world&quot;)<br>    })</pre><p>When I use the above codes to test those web frameworks, the token time of route selection is not so important in the whole http request processing, although performance of route selection of web frameworks are very different.</p><p>So I create this project to compare performance of web frameworks including connection, route selection, handler processing. It mocks business logics and can set a special processing time.</p><p>The you can get some interesting results if you use it to test.</p><h3>Implementation</h3><p>When you test a web framework, this test suit will starts a simple http server implemented by this web framework. It is a real http server and only contains GET url: “/hello”.</p><p>When this server processes this url, it will sleep n milliseconds in this handler. It mocks the business logics such as:</p><ul><li>read data from sockets</li><li>write data to disk</li><li>access databases</li><li>access cache servers</li><li>invoke other microservices</li><li>……</li></ul><p>It contains a test.sh that can do those tests automatically.</p><p>It uses wrk to test.</p><h3>Basic Test</h3><p>The first test case is to mock 0 ms, 10 ms, 100 ms, 500 ms processing time in handlers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/707/0*C2adOlq3v4frtPSw.png" /></figure><p>the concurrency clients are 5000.</p><h3>Concurrency Test</h3><p>In 30 ms processing time, the tets result for 100, 1000, 5000 clients is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/870/0*SpY-aWA_o8kS4Amk.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=93a34403ef0a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>