mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-10-16 20:44:03 +00:00
https://github.com/XTLS/Xray-core/pull/7130#issuecomment-6116258188 https://github.com/XTLS/Xray-core/pull/7130#issuecomment-6120742739 https://github.com/XTLS/Xray-core/pull/7130#issuecomment-6137264458 https://github.com/XTLS/Xray-core/pull/7156#issuecomment-6202933664 https://github.com/XTLS/Xray-core/pull/7156#issuecomment-6203239736
83 lines
1.5 KiB
Go
83 lines
1.5 KiB
Go
package tun
|
|
|
|
import (
|
|
"runtime"
|
|
"testing"
|
|
|
|
"github.com/xtls/xray-core/common/buf"
|
|
)
|
|
|
|
const benchBatch = 64
|
|
|
|
// Raw cost of queueing and draining a small burst, as one flow's reader does.
|
|
func BenchmarkQueueBurstChan(b *testing.B) {
|
|
ch := make(chan *buf.Buffer, udpQueueLimit)
|
|
p := &buf.Buffer{}
|
|
b.ReportAllocs()
|
|
for i := 0; i < b.N; i++ {
|
|
for j := 0; j < benchBatch; j++ {
|
|
ch <- p
|
|
}
|
|
for j := 0; j < benchBatch; j++ {
|
|
<-ch
|
|
}
|
|
}
|
|
}
|
|
|
|
func BenchmarkQueueBurstPacketQueue(b *testing.B) {
|
|
q := newPacketQueue(udpQueueLimit)
|
|
p := &buf.Buffer{}
|
|
b.ReportAllocs()
|
|
for i := 0; i < b.N; i++ {
|
|
for j := 0; j < benchBatch; j++ {
|
|
q.push(p)
|
|
}
|
|
for j := 0; j < benchBatch; j++ {
|
|
q.pop()
|
|
}
|
|
}
|
|
}
|
|
|
|
// Producer and consumer on different goroutines; the producer yields when the
|
|
// queue is full instead of spinning, like a blocking channel send would.
|
|
func BenchmarkQueueStreamChan(b *testing.B) {
|
|
ch := make(chan *buf.Buffer, udpQueueLimit)
|
|
p := &buf.Buffer{}
|
|
done := make(chan struct{})
|
|
go func() {
|
|
for range ch {
|
|
}
|
|
close(done)
|
|
}()
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
ch <- p
|
|
}
|
|
close(ch)
|
|
<-done
|
|
}
|
|
|
|
func BenchmarkQueueStreamPacketQueue(b *testing.B) {
|
|
q := newPacketQueue(udpQueueLimit)
|
|
p := &buf.Buffer{}
|
|
done := make(chan struct{})
|
|
go func() {
|
|
for {
|
|
if _, ok := q.pop(); !ok {
|
|
break
|
|
}
|
|
}
|
|
close(done)
|
|
}()
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
for !q.push(p) {
|
|
runtime.Gosched()
|
|
}
|
|
}
|
|
q.close()
|
|
<-done
|
|
}
|