-
-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathprocessors.rs
More file actions
233 lines (213 loc) · 8.09 KB
/
processors.rs
File metadata and controls
233 lines (213 loc) · 8.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
use std::sync::Arc;
use std::time::{Duration, SystemTime};
use chrono::DateTime;
use criterion::measurement::WallTime;
use criterion::{black_box, BenchmarkGroup, BenchmarkId, Criterion, Throughput};
use once_cell::sync::Lazy;
use parking_lot::Mutex;
use rust_snuba::{
BatchSizeCalculation, BrokerConfig, ClickhouseConfig, ConsumerStrategyFactoryV2, EnvConfig,
KafkaMessageMetadata, MessageProcessorConfig, ProcessingFunction, ProcessingFunctionType,
ProcessorConfig, StatsDBackend, StorageConfig, TopicConfig, PROCESSORS,
};
use sentry_arroyo::backends::kafka::types::KafkaPayload;
use sentry_arroyo::backends::local::broker::LocalBroker;
use sentry_arroyo::backends::local::LocalConsumer;
use sentry_arroyo::backends::storages::memory::MemoryMessageStorage;
use sentry_arroyo::backends::ConsumerError;
use sentry_arroyo::metrics;
use sentry_arroyo::processing::strategies::run_task_in_threads::ConcurrencyConfig;
use sentry_arroyo::processing::strategies::ProcessingStrategyFactory;
use sentry_arroyo::processing::{Callbacks, ConsumerState, RunError, StreamProcessor};
use sentry_arroyo::types::{Partition, Topic};
use sentry_arroyo::utils::clock::SystemClock;
use uuid::Uuid;
#[cfg(not(target_env = "msvc"))]
use tikv_jemallocator::Jemalloc;
#[cfg(not(target_env = "msvc"))]
#[global_allocator]
static GLOBAL: Jemalloc = Jemalloc;
const MSG_COUNT: usize = 5_000;
static RUNTIME: Lazy<tokio::runtime::Runtime> = Lazy::new(|| {
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.unwrap()
});
static PROCESSOR_CONFIG: Lazy<ProcessorConfig> = Lazy::new(ProcessorConfig::default);
fn create_factory(
concurrency: usize,
schema: &str,
python_class_name: &str,
) -> Box<dyn ProcessingStrategyFactory<KafkaPayload>> {
let storage = StorageConfig {
name: "test".into(),
clickhouse_table_name: "test".into(),
clickhouse_cluster: ClickhouseConfig {
host: "test".into(),
port: 1234,
secure: false,
http_port: 1234,
user: "test".into(),
password: "test".into(),
database: "test".into(),
},
message_processor: MessageProcessorConfig {
python_class_name: python_class_name.into(),
python_module: "test".into(),
},
};
let processing_concurrency =
ConcurrencyConfig::with_runtime(concurrency, RUNTIME.handle().to_owned());
let clickhouse_concurrency =
ConcurrencyConfig::with_runtime(concurrency, RUNTIME.handle().to_owned());
let commitlog_concurrency =
ConcurrencyConfig::with_runtime(concurrency, RUNTIME.handle().to_owned());
let replacements_concurrency =
ConcurrencyConfig::with_runtime(concurrency, RUNTIME.handle().to_owned());
let factory = ConsumerStrategyFactoryV2 {
storage_config: storage,
env_config: EnvConfig::default(),
logical_topic_name: schema.into(),
max_batch_size: 1_000,
max_batch_time: Duration::from_millis(10),
max_batch_size_calculation: BatchSizeCalculation::Rows,
processing_concurrency,
clickhouse_concurrency,
commitlog_concurrency,
replacements_concurrency,
async_inserts: false,
python_max_queue_depth: None,
use_rust_processor: true,
health_check_file: None,
enforce_schema: false,
commit_log_producer: None,
replacements_config: None,
physical_consumer_group: "test-group".to_owned(),
physical_topic_name: Topic::new("test"),
accountant_topic_config: TopicConfig {
physical_topic_name: "shared-resources-usage".to_string(),
logical_topic_name: "shared-resources-usage".to_string(),
broker_config: BrokerConfig::default(),
quantized_rebalance_consumer_group_delay_secs: None,
},
stop_at_timestamp: None,
batch_write_timeout: None,
join_timeout_ms: None,
health_check: "arroyo".to_string(),
use_row_binary: false,
blq_producer_config: None,
blq_topic: None,
};
Box::new(factory)
}
fn create_stream_processor(
concurrency: usize,
schema: &str,
python_class_name: &str,
messages: usize,
) -> StreamProcessor<KafkaPayload> {
let factory = create_factory(concurrency, schema, python_class_name);
let consumer_state = ConsumerState::new(factory, None);
let topic = Topic::new("test");
let partition = Partition::new(topic, 0);
let storage: MemoryMessageStorage<KafkaPayload> = Default::default();
let clock = SystemClock {};
let mut broker = LocalBroker::new(Box::new(storage), Box::new(clock));
broker.create_topic(topic, 1).unwrap();
let schema = sentry_kafka_schemas::get_schema(schema, None).unwrap();
let payloads = schema.examples();
for payload in payloads.iter().cycle().take(messages) {
let payload = KafkaPayload::new(None, None, Some(payload.payload().to_vec()));
broker.produce(&partition, payload).unwrap();
}
let consumer = LocalConsumer::new(
Uuid::nil(),
Arc::new(Mutex::new(broker)),
"test_group".to_string(),
true,
&[topic],
Callbacks(consumer_state.clone()),
);
let consumer = Box::new(consumer);
StreamProcessor::new(consumer, consumer_state)
}
fn run_fn_bench(
bencher: &mut BenchmarkGroup<WallTime>,
schema: &str,
processor_fn: ProcessingFunction,
) {
let metadata = KafkaMessageMetadata {
partition: 0,
offset: 1,
timestamp: DateTime::from(SystemTime::now()),
};
let schema = sentry_kafka_schemas::get_schema(schema, None).unwrap();
let payloads = schema.examples();
bencher
.warm_up_time(Duration::from_millis(500))
.throughput(Throughput::Elements(payloads.len() as u64))
.bench_function(BenchmarkId::from_parameter("-"), |b| {
b.iter(|| {
for payload in payloads {
let payload = KafkaPayload::new(None, None, Some(payload.payload().to_vec()));
let processed =
processor_fn(payload, metadata.clone(), &PROCESSOR_CONFIG).unwrap();
black_box(processed);
}
})
});
}
fn run_processor_bench(
bencher: &mut BenchmarkGroup<WallTime>,
concurrency: usize,
schema: &str,
python_class_name: &str,
) {
bencher
.throughput(Throughput::Elements(MSG_COUNT as u64))
.warm_up_time(Duration::from_millis(500))
.sample_size(10)
.bench_with_input(
BenchmarkId::from_parameter(concurrency),
&concurrency,
|b, &s| {
b.iter(|| {
let mut processor = black_box(create_stream_processor(
s,
schema,
python_class_name,
MSG_COUNT,
));
loop {
let res = processor.run_once();
if matches!(res, Err(RunError::Poll(ConsumerError::EndOfPartition))) {
processor.shutdown();
break;
}
}
})
},
);
}
fn main() {
// this sends to nowhere, but because it's UDP we won't error.
metrics::init(StatsDBackend::new("127.0.0.1", 8081, "snuba.consumer")).unwrap();
let mut c = Criterion::default().configure_from_args();
for (python_class_name, topic_name, processor_fn_type) in PROCESSORS {
let mut group = c.benchmark_group(*topic_name);
match processor_fn_type {
ProcessingFunctionType::ProcessingFunction(processor_fn) => {
run_fn_bench(&mut group, topic_name, *processor_fn);
for concurrency in [1, 4, 16] {
run_processor_bench(&mut group, concurrency, topic_name, python_class_name);
}
group.finish();
}
_ => {
// TODO: Support processing function with replacements
}
}
}
c.final_summary()
}