-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Significant performance refactor of running_output buffers
- Loading branch information
Showing
4 changed files
with
233 additions
and
194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package internal_models | ||
|
||
import ( | ||
"github.com/influxdata/telegraf" | ||
) | ||
|
||
type Buffer struct { | ||
buf chan telegraf.Metric | ||
// total dropped metrics | ||
drops int | ||
// total metrics added | ||
total int | ||
} | ||
|
||
func NewBuffer(size int) *Buffer { | ||
return &Buffer{ | ||
buf: make(chan telegraf.Metric, size), | ||
} | ||
} | ||
|
||
func (b *Buffer) IsEmpty() bool { | ||
return len(b.buf) == 0 | ||
} | ||
|
||
func (b *Buffer) Len() int { | ||
return len(b.buf) | ||
} | ||
|
||
func (b *Buffer) Drops() int { | ||
return b.drops | ||
} | ||
|
||
func (b *Buffer) Total() int { | ||
return b.total | ||
} | ||
|
||
func (b *Buffer) Add(metrics ...telegraf.Metric) { | ||
for i, _ := range metrics { | ||
b.total++ | ||
select { | ||
case b.buf <- metrics[i]: | ||
default: | ||
b.drops++ | ||
<-b.buf | ||
b.buf <- metrics[i] | ||
} | ||
} | ||
} | ||
|
||
func (b *Buffer) Batch(batchSize int) []telegraf.Metric { | ||
n := min(len(b.buf), batchSize) | ||
out := make([]telegraf.Metric, n) | ||
for i := 0; i < n; i++ { | ||
out[i] = <-b.buf | ||
} | ||
return out | ||
} | ||
|
||
func min(a, b int) int { | ||
if b < a { | ||
return b | ||
} | ||
return a | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.