Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Package writer implements batch writing.

License

NotificationsYou must be signed in to change notification settings

hslam/writer

Repository files navigation

PkgGoDevBuild StatuscodecovGo Report CardLICENSE

Package writer implements batch writing.

Feature

  • Shared Buffer
  • Automatic Batch Writing
Write Throughputs

write-throughputs

Get started

Install

go get github.com/hslam/writer

Import

import "github.com/hslam/writer"

Usage

Example

package mainimport ("fmt""github.com/hslam/writer""io""sync""sync/atomic""time")funcmain() {RunParallel(1,false)RunParallel(16,false)RunParallel(32,false)RunParallel(1,true)RunParallel(16,true)RunParallel(32,true)}funcRunParallel(numint,batchbool) {start:=time.Now()r,w:=io.Pipe()size:=0done:=make(chanstruct{})gofunc() {buf:=make([]byte,65536)for {n,err:=r.Read(buf)iferr!=nil {break}size+=n}close(done)}()worker:=&Worker{msg:make([]byte,512)}ifbatch {worker.w=writer.NewWriter(w,worker.concurrency,512*64,false)}else {worker.w=w}wg:= sync.WaitGroup{}fori:=0;i<num;i++ {wg.Add(1)goworker.run(&wg)}wg.Wait()worker.w.Close()w.Close()<-donefmt.Printf("time - %.1fs,\tbatch - %t,\tparallel - %d,\twrite - %dMByte/s\n",float64(time.Now().Sub(start))/1E9,batch,num,size/1E6)}typeWorkerstruct {countint64w     io.WriteClosermsg   []byte}func (w*Worker)concurrency()int {returnint(atomic.LoadInt64(&w.count))}func (w*Worker)run(wg*sync.WaitGroup) {deferwg.Done()t:=time.NewTimer(time.Second)for {atomic.AddInt64(&w.count,1)w.w.Write(w.msg)atomic.AddInt64(&w.count,-1)select {case<-t.C:returndefault:}}}

Output

time - 1.0s,batch - false,parallel - 1,write - 554MByte/stime - 1.0s,batch - false,parallel - 16,write - 384MByte/stime - 1.0s,batch - false,parallel - 32,write - 384MByte/stime - 1.0s,batch - true,parallel - 1,write - 485MByte/stime - 1.0s,batch - true,parallel - 16,write - 962MByte/stime - 1.0s,batch - true,parallel - 32,write - 2013MByte/s

License

This package is licensed under a MIT license (Copyright (c) 2020 Meng Huang)

Author

writer was written by Meng Huang.


[8]ページ先頭

©2009-2025 Movatter.jp