Giter Club home page Giter Club logo

gruptor's Introduction

Gruptor

Gruptor is a basic port of Disruptor (JAVA) . It's very fast by using lock-free ringbuffer

Usage

Gruptor

const DefaultBufferSize = 1024  // must be 2,4,8,..1024...
const DefaultBufferMask = DefaultBufferSize -1

var ringBuffer [DefaultBufferSize]int64  // specified an ringBuffer 

// implements your own Consumer
type AConsumer struct {
}

func (c *AConsumer) Consume(lo, hi int64) {
	for lo <= hi {
		event := ringBuffer[lo&DefaultBufferMask]
		if event != lo {
			warning := fmt.Sprintf("\nRace condition--Sequence: %d, Event: %d\n", lo, event)
			fmt.Printf(warning)
			panic(warning)
		}
		lo++
	}
}

// Create the gruptor
g := gruptor.NewBuilder(DefaultBufferSize).HandleEventWith(&AConsumer{}).Build()
g.Start()
defer g.Stop()


// using Writer to acquiring next sequence
w := g.Writer()
var sequence int64

for sequence < 100000 {
	sequence = w.Next()
	ringBuffer[sequence&DefaultBufferMask] = sequence
	w.Commit(sequence, sequence)
}

GruptorX

// TODO add some example
goos: darwin
goarch: amd64
pkg: gruptor/benchmarks
BenchmarkCompositeBarrierWithOneCursor_Read-8            	2000000000	         1.82 ns/op	       0 B/op	       0 allocs/op
BenchmarkCompositeBarrierWithMoreCursor_Read-8           	200000000	         6.35 ns/op	       0 B/op	       0 allocs/op
BenchmarkBlockingOneGoroutine-8                          	30000000	        55.1 ns/op	       0 B/op	       0 allocs/op
BenchmarkBlockingTwoGoroutines-8                         	20000000	        84.1 ns/op	       0 B/op	       0 allocs/op
BenchmarkBlockingThreeGoroutinesWithContendedWrite-8     	10000000	       167 ns/op	       0 B/op	       0 allocs/op
BenchmarkUnBlockingOneGoroutine-8                        	100000000	        16.9 ns/op	       0 B/op	       0 allocs/op
BenchmarkUnBlockingTwoGoroutines-8                       	100000000	        17.4 ns/op	       0 B/op	       0 allocs/op
BenchmarkUnBlockingThreeGoroutinesWithContendedWrite-8   	100000000	        63.1 ns/op	       0 B/op	       0 allocs/op
BenchmarkCursor_Load-8                                   	2000000000	         0.81 ns/op	       0 B/op	       0 allocs/op
BenchmarkCursor_Store-8                                  	2000000000	         0.80 ns/op	       0 B/op	       0 allocs/op
BenchmarkCursor_Read-8                                   	2000000000	         0.83 ns/op	       0 B/op	       0 allocs/op
BenchmarkRingBuffer_Get-8                                	500000000	         2.92 ns/op	       0 B/op	       0 allocs/op
BenchmarkRingBuffer_Published-8                          	500000000	         2.90 ns/op	       0 B/op	       0 allocs/op
BenchmarkRingBuffer_Set-8                                	30000000	        52.0 ns/op	       8 B/op	       1 allocs/op
BenchmarkCustomRingBuffer_Get-8                          	1000000000	         2.25 ns/op	       0 B/op	       0 allocs/op
BenchmarkCustomRingBuffer_Set-8                          	500000000	         3.00 ns/op	       0 B/op	       0 allocs/op
BenchmarkCustomRingBufferWithEvent_Get-8                 	500000000	         3.89 ns/op	       0 B/op	       0 allocs/op
BenchmarkCustomRingBufferWithEvent_Set-8                 	30000000	        55.7 ns/op	       8 B/op	       1 allocs/op
BenchmarkNoTypeSwitch-8                                  	2000000000	         1.04 ns/op	       0 B/op	       0 allocs/op
BenchmarkUsingTypeSwitch-8                               	2000000000	         1.52 ns/op	       0 B/op	       0 allocs/op
BenchmarkUsingTypeAssertion-8                            	1000000000	         2.29 ns/op	       0 B/op	       0 allocs/op

BenchmarkGruptor_OneWriterOneConsumer-8                      	100000000	        19.7 ns/op	       0 B/op	       0 allocs/op
BenchmarkGruptor_OneWriterOneConsumerMoreCPU-8               	50000000	        32.3 ns/op	       0 B/op	       0 allocs/op
BenchmarkGruptor_OneWriterMultiConsumer-8                    	50000000	        32.8 ns/op	       0 B/op	       0 allocs/op
BenchmarkGruptor_MultiWriterOneConsumer-8                    	10000000	       168 ns/op	       0 B/op	       0 allocs/op
BenchmarkGruptor_MultiWriterMultiConsumer-8                  	10000000	       184 ns/op	       0 B/op	       0 allocs/op
BenchmarkGruptor_MultiWriterOneConsumerInMultiGoroutines-8   	 5000000	       241 ns/op	       0 B/op	       0 allocs/op

gruptor's People

Contributors

proliming avatar

Stargazers

Prashant Surti avatar  avatar fightingbamboo avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.