Added a channel version..
This commit is contained in:
		
							
								
								
									
										20
									
								
								flake.go
									
									
									
									
									
								
							
							
						
						
									
										20
									
								
								flake.go
									
									
									
									
									
								
							| @@ -54,8 +54,28 @@ func NewFlakeNode(node int64) (*Node, error) { | ||||
| } | ||||
|  | ||||
| // high performance generator | ||||
| // well, that w as the idea... | ||||
| func (n *Node) Generator(c chan Flake) { | ||||
|  | ||||
| 	ticker := time.NewTicker(time.Millisecond) | ||||
| 	now := int64(time.Now().UnixNano() / 1000000) | ||||
| 	for { | ||||
|  | ||||
| 		n.step = 0 | ||||
|  | ||||
| 		select { | ||||
| 		case c <- Flake((now-n.epoch)<<TimeShift | (n.node << NodeShift) | (n.step)): | ||||
|  | ||||
| 			n.step = (n.step + 1) & StepMask | ||||
|  | ||||
| 			if n.step == 0 { | ||||
| 				// wait for ticker.. | ||||
| 			} | ||||
| 		case <-ticker.C: | ||||
| 			now++ | ||||
| 			// continue | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // Return a freshly generated Flake ID | ||||
|   | ||||
| @@ -6,6 +6,20 @@ import ( | ||||
|  | ||||
| ////////////////////////////////////////////////////////////////////////////// | ||||
|  | ||||
| // Benchmarks Presence Update event with fake data. | ||||
| func BenchmarkGenerateChan(b *testing.B) { | ||||
|  | ||||
| 	node, _ := NewFlakeNode(1) | ||||
| 	c := make(chan Flake) | ||||
| 	go node.Generator(c) | ||||
|  | ||||
| 	b.ReportAllocs() | ||||
| 	for n := 0; n < b.N; n++ { | ||||
| 		<-c | ||||
| 	} | ||||
|  | ||||
| } | ||||
|  | ||||
| // Benchmarks Presence Update event with fake data. | ||||
| func BenchmarkGenerate(b *testing.B) { | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user