Files
seaweedFS/weed/mq/offset/benchmark_test.go
Chris Lu 995dfc4d5d chore: remove ~50k lines of unreachable dead code (#8913)
* chore: remove unreachable dead code across the codebase

Remove ~50,000 lines of unreachable code identified by static analysis.

Major removals:
- weed/filer/redis_lua: entire unused Redis Lua filer store implementation
- weed/wdclient/net2, resource_pool: unused connection/resource pool packages
- weed/plugin/worker/lifecycle: unused lifecycle plugin worker
- weed/s3api: unused S3 policy templates, presigned URL IAM, streaming copy,
  multipart IAM, key rotation, and various SSE helper functions
- weed/mq/kafka: unused partition mapping, compression, schema, and protocol functions
- weed/mq/offset: unused SQL storage and migration code
- weed/worker: unused registry, task, and monitoring functions
- weed/query: unused SQL engine, parquet scanner, and type functions
- weed/shell: unused EC proportional rebalance functions
- weed/storage/erasure_coding/distribution: unused distribution analysis functions
- Individual unreachable functions removed from 150+ files across admin,
  credential, filer, iam, kms, mount, mq, operation, pb, s3api, server,
  shell, storage, topology, and util packages

* fix(s3): reset shared memory store in IAM test to prevent flaky failure

TestLoadIAMManagerFromConfig_EmptyConfigWithFallbackKey was flaky because
the MemoryStore credential backend is a singleton registered via init().
Earlier tests that create anonymous identities pollute the shared store,
causing LookupAnonymous() to unexpectedly return true.

Fix by calling Reset() on the memory store before the test runs.

* style: run gofmt on changed files

* fix: restore KMS functions used by integration tests

* fix(plugin): prevent panic on send to closed worker session channel

The Plugin.sendToWorker method could panic with "send on closed channel"
when a worker disconnected while a message was being sent. The race was
between streamSession.close() closing the outgoing channel and sendToWorker
writing to it concurrently.

Add a done channel to streamSession that is closed before the outgoing
channel, and check it in sendToWorker's select to safely detect closed
sessions without panicking.
2026-04-03 16:04:27 -07:00

306 lines
7.6 KiB
Go

package offset
import (
"fmt"
"testing"
"time"
"github.com/seaweedfs/seaweedfs/weed/pb/schema_pb"
)
// BenchmarkOffsetAssignment benchmarks sequential offset assignment
func BenchmarkOffsetAssignment(b *testing.B) {
storage := NewInMemoryOffsetStorage()
partition := &schema_pb.Partition{
RingSize: 1024,
RangeStart: 0,
RangeStop: 31,
UnixTimeNs: time.Now().UnixNano(),
}
manager, err := NewPartitionOffsetManager("test-namespace", "test-topic", partition, storage)
if err != nil {
b.Fatalf("Failed to create partition manager: %v", err)
}
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
manager.AssignOffset()
}
})
}
// BenchmarkBatchOffsetAssignment benchmarks batch offset assignment
func BenchmarkBatchOffsetAssignment(b *testing.B) {
storage := NewInMemoryOffsetStorage()
partition := &schema_pb.Partition{
RingSize: 1024,
RangeStart: 0,
RangeStop: 31,
UnixTimeNs: time.Now().UnixNano(),
}
manager, err := NewPartitionOffsetManager("test-namespace", "test-topic", partition, storage)
if err != nil {
b.Fatalf("Failed to create partition manager: %v", err)
}
batchSizes := []int64{1, 10, 100, 1000}
for _, batchSize := range batchSizes {
b.Run(fmt.Sprintf("BatchSize%d", batchSize), func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
manager.AssignOffsets(batchSize)
}
})
}
}
// BenchmarkOffsetSubscription benchmarks subscription operations
func BenchmarkOffsetSubscription(b *testing.B) {
storage := NewInMemoryOffsetStorage()
registry := NewPartitionOffsetRegistry(storage)
subscriber := NewOffsetSubscriber(registry)
partition := &schema_pb.Partition{
RingSize: 1024,
RangeStart: 0,
RangeStop: 31,
UnixTimeNs: time.Now().UnixNano(),
}
// Pre-assign offsets
registry.AssignOffsets("test-namespace", "test-topic", partition, 10000)
b.Run("CreateSubscription", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
subscriptionID := fmt.Sprintf("bench-sub-%d", i)
_, err := subscriber.CreateSubscription(
subscriptionID,
"test-namespace", "test-topic",
partition,
schema_pb.OffsetType_RESET_TO_EARLIEST,
0,
)
if err != nil {
b.Fatalf("Failed to create subscription: %v", err)
}
subscriber.CloseSubscription(subscriptionID)
}
})
// Create subscription for other benchmarks
sub, err := subscriber.CreateSubscription(
"bench-sub",
"test-namespace", "test-topic",
partition,
schema_pb.OffsetType_RESET_TO_EARLIEST,
0,
)
if err != nil {
b.Fatalf("Failed to create subscription: %v", err)
}
b.Run("GetOffsetRange", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
sub.GetOffsetRange(100)
}
})
b.Run("AdvanceOffset", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
sub.AdvanceOffset()
}
})
b.Run("GetLag", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
sub.GetLag()
}
})
b.Run("SeekToOffset", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
offset := int64(i % 9000) // Stay within bounds
sub.SeekToOffset(offset)
}
})
}
// BenchmarkSMQOffsetIntegration benchmarks the full integration layer
func BenchmarkSMQOffsetIntegration(b *testing.B) {
storage := NewInMemoryOffsetStorage()
integration := NewSMQOffsetIntegration(storage)
partition := &schema_pb.Partition{
RingSize: 1024,
RangeStart: 0,
RangeStop: 31,
UnixTimeNs: time.Now().UnixNano(),
}
b.Run("PublishRecord", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
key := fmt.Sprintf("key-%d", i)
integration.PublishRecord("test-namespace", "test-topic", partition, []byte(key), &schema_pb.RecordValue{})
}
})
b.Run("PublishRecordBatch", func(b *testing.B) {
batchSizes := []int{1, 10, 100}
for _, batchSize := range batchSizes {
b.Run(fmt.Sprintf("BatchSize%d", batchSize), func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
records := make([]PublishRecordRequest, batchSize)
for j := 0; j < batchSize; j++ {
records[j] = PublishRecordRequest{
Key: []byte(fmt.Sprintf("batch-%d-key-%d", i, j)),
Value: &schema_pb.RecordValue{},
}
}
integration.PublishRecordBatch("test-namespace", "test-topic", partition, records)
}
})
}
})
// Pre-populate for subscription benchmarks
records := make([]PublishRecordRequest, 1000)
for i := 0; i < 1000; i++ {
records[i] = PublishRecordRequest{
Key: []byte(fmt.Sprintf("pre-key-%d", i)),
Value: &schema_pb.RecordValue{},
}
}
integration.PublishRecordBatch("test-namespace", "test-topic", partition, records)
b.Run("CreateSubscription", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
subscriptionID := fmt.Sprintf("integration-sub-%d", i)
_, err := integration.CreateSubscription(
subscriptionID,
"test-namespace", "test-topic",
partition,
schema_pb.OffsetType_RESET_TO_EARLIEST,
0,
)
if err != nil {
b.Fatalf("Failed to create subscription: %v", err)
}
integration.CloseSubscription(subscriptionID)
}
})
b.Run("GetHighWaterMark", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
integration.GetHighWaterMark("test-namespace", "test-topic", partition)
}
})
b.Run("GetPartitionOffsetInfo", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
integration.GetPartitionOffsetInfo("test-namespace", "test-topic", partition)
}
})
}
// BenchmarkConcurrentOperations benchmarks concurrent offset operations
func BenchmarkConcurrentOperations(b *testing.B) {
storage := NewInMemoryOffsetStorage()
integration := NewSMQOffsetIntegration(storage)
partition := &schema_pb.Partition{
RingSize: 1024,
RangeStart: 0,
RangeStop: 31,
UnixTimeNs: time.Now().UnixNano(),
}
b.Run("ConcurrentPublish", func(b *testing.B) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
i := 0
for pb.Next() {
key := fmt.Sprintf("concurrent-key-%d", i)
integration.PublishRecord("test-namespace", "test-topic", partition, []byte(key), &schema_pb.RecordValue{})
i++
}
})
})
// Pre-populate for concurrent reads
for i := 0; i < 1000; i++ {
key := fmt.Sprintf("read-key-%d", i)
integration.PublishRecord("test-namespace", "test-topic", partition, []byte(key), &schema_pb.RecordValue{})
}
b.Run("ConcurrentRead", func(b *testing.B) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
integration.GetHighWaterMark("test-namespace", "test-topic", partition)
}
})
})
b.Run("ConcurrentMixed", func(b *testing.B) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
i := 0
for pb.Next() {
if i%10 == 0 {
// 10% writes
key := fmt.Sprintf("mixed-key-%d", i)
integration.PublishRecord("test-namespace", "test-topic", partition, []byte(key), &schema_pb.RecordValue{})
} else {
// 90% reads
integration.GetHighWaterMark("test-namespace", "test-topic", partition)
}
i++
}
})
})
}
// BenchmarkMemoryUsage benchmarks memory usage patterns
func BenchmarkMemoryUsage(b *testing.B) {
b.Run("InMemoryStorage", func(b *testing.B) {
storage := NewInMemoryOffsetStorage()
partition := &schema_pb.Partition{
RingSize: 1024,
RangeStart: 0,
RangeStop: 31,
UnixTimeNs: time.Now().UnixNano(),
}
manager, err := NewPartitionOffsetManager("test-namespace", "test-topic", partition, storage)
if err != nil {
b.Fatalf("Failed to create partition manager: %v", err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
manager.AssignOffset()
// Note: Checkpointing now happens automatically in background every 2 seconds
}
// Clean up background goroutine
manager.Close()
})
}