golfw

package module
v0.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 1, 2021 License: MIT Imports: 3 Imported by: 3 modules, 3 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

golfw

Go line feed writer

MIT license made-with-Go GoDoc GoReportCard

Description

Library provides WriteCloser structure that is an io.WriteCloser that buffers output to ensure it only emits bytes to the underlying io.WriteCloser on line feed boundaries.

Example

See a full example in the examples/log-rotator/ directory.

func Example() error {
    // Flush completed lines to os.Stdout at least every 512 bytes.
    lf, err := golfw.NewWriteCloser(os.Stdout, 512)
    if err != nil {
        return err
    }

    // Give copy buffer some room.
    _, rerr := io.CopyBuffer(lf, os.Stdin, make([]byte, 4096))

    // Clean up
    cerr := lf.Close()
    if rerr == nil {
        return cerr
    }
    return rerr
}

Benchmarks

War and Peace

I created a small log-rotator program in examples/log-rotator/main.go that uses composition to wrap lumberjack.Logger with golfw.WriteCloser, and streamed 1000 copies of War and Peace through the UNIX cat utility and the resulting log-rotator program

I created an input file by creating a large 3.2 GiB file with 1000 copies of War and Peace:

$ cd examples/log-rotator
$ go build
$ wget https://www.gutenberg.org/files/2600/2600-h/2600-h.htm
$ for i in $(seq 1000); do cat 2600-h.htm >> 2600-h-1000.htm ; done

I ran the benchmarks using hyperfine.

$ hyperfine --export-markdown ../../BENCHMARKS.md --prepare 'rm -f *.log' --warmup 10 \
            'cat 2600-h-1000.htm > stdout.log' \
            'cat 2600-h-1000.htm | ./log-rotator'
Benchmark #1: cat 2600-h-1000.htm > stdout.log
  Time (mean ± σ):      3.921 s ±  0.025 s    [User: 8.3 ms, System: 3840.0 ms]
  Range (min … max):    3.891 s …  3.964 s    10 runs

Benchmark #2: cat 2600-h-1000.htm | ./log-rotator
  Time (mean ± σ):      4.507 s ±  0.062 s    [User: 239.4 ms, System: 6198.0 ms]
  Range (min … max):    4.379 s …  4.595 s    10 runs

Summary
  'cat 2600-h-1000.htm > stdout.log' ran
    1.15 ± 0.02 times faster than 'cat 2600-h-1000.htm | ./log-rotator'
Command Mean [s] Min [s] Max [s] Relative
cat 2600-h-1000.htm > stdout.log 3.911 ± 0.043 3.861 3.984 1.00
cat 2600-h-1000.htm | ./log-rotator 4.510 ± 0.050 4.431 4.611 1.15 ± 0.02

Using mean run times, there is a 15% pipeline penalty using golfw.WriteCloser on top of lumberjack.Logger compared to merely using the cat UNIX utility, but shown below, the penalty from golfw.WriteCloser is between 1% and 5%.

golfw.WriteCloser compared to io.Discard

To determine how much overhead golfw.WriteCloser adds to a data pipeline, I created a benchmark file benchmark_test.go to stream one megabyte of sequential bytes directly to io.Discard, and another benchmark that streams the same bytes through golfw.WriteCloser to io.Discard. On my development system, streaming through golfw.WriteCloser measured between 3% more efficient, and 4% less efficient than merely streaming through io.Discard, depending on the selected flush threshold.

$ go test -bench=.
goos: linux
goarch: amd64
pkg: github.com/karrick/golfw
cpu: AMD Ryzen Threadripper 3960X 24-Core Processor 
BenchmarkWriteCloser-48    	       4	 278117669 ns/op
BenchmarkDevNull-48        	       4	 286373981 ns/op
PASS
ok  	github.com/karrick/golfw	4.503s

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type WriteCloser

type WriteCloser struct {
	// contains filtered or unexported fields
}

WriteCloser is an io.WriteCloser that buffers output to ensure it only emits bytes to the underlying io.WriteCloser on line feed boundaries.

func NewWriteCloser

func NewWriteCloser(iowc io.WriteCloser, flushThreshold int) (*WriteCloser, error)

NewWriteCloser returns new WriteCloser with the specified flush threshold. Whenever the buffer is greater than the specified threshold, it flushes the buffer, up to and including the final LF byte, to the underlying io.WriteCloser.

func Example() error {
    // Flush completed lines to os.Stdout at least every 512 bytes.
    lf, err := golfw.NewWriteCloser(os.Stdout, 512)
    if err != nil {
        return err
    }

    // Give copy buffer some room.
    _, rerr := io.CopyBuffer(lf, os.Stdin, make([]byte, 4096))

    // Clean up
    cerr := lf.Close()
    if rerr == nil {
        return cerr
    }
    return rerr
}

func (*WriteCloser) Close

func (lbf *WriteCloser) Close() error

Close writes all data in its buffer to the underlying io.WriteCloser, including bytes without a trailing LF, then closes the underlying io.WriteCloser. This will either return any error caused by writing the bytes to the underlying io.WriteCloser, or an error caused by closing it. Use this method when done with a WriteCloser to prevent data loss.

func (*WriteCloser) Write

func (lbf *WriteCloser) Write(p []byte) (int, error)

Write appends bytes from p to internal buffer, flushing buffer up to and including the final LF when buffer length exceeds programmed threshold.

Directories

Path Synopsis
examples
line-buffer module
log-roller module

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址