-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add latency benchmark #16
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
41 changes: 41 additions & 0 deletions
41
app/src/main/java/org/astraea/performance/latency/CloseableThread.java
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,41 @@ | ||
package org.astraea.performance.latency; | ||
|
||
import java.io.Closeable; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
abstract class CloseableThread implements Runnable, Closeable { | ||
private final AtomicBoolean closed = new AtomicBoolean(); | ||
private final CountDownLatch closeLatch = new CountDownLatch(1); | ||
|
||
@Override | ||
public final void run() { | ||
try { | ||
while (!closed.get()) execute(); | ||
} catch (InterruptedException e) { | ||
// swallow | ||
} finally { | ||
try { | ||
cleanup(); | ||
} finally { | ||
closeLatch.countDown(); | ||
} | ||
} | ||
} | ||
|
||
/** looped action. */ | ||
abstract void execute() throws InterruptedException; | ||
|
||
/** final action when leaving loop. */ | ||
void cleanup() {} | ||
|
||
@Override | ||
public void close() { | ||
closed.set(true); | ||
try { | ||
closeLatch.await(); | ||
} catch (InterruptedException e) { | ||
// swallow | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
app/src/main/java/org/astraea/performance/latency/ComponentFactory.java
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,58 @@ | ||
package org.astraea.performance.latency; | ||
|
||
import java.util.Properties; | ||
import java.util.Set; | ||
import org.apache.kafka.clients.CommonClientConfigs; | ||
import org.apache.kafka.clients.admin.AdminClientConfig; | ||
import org.apache.kafka.clients.consumer.ConsumerConfig; | ||
import org.apache.kafka.clients.producer.ProducerConfig; | ||
|
||
interface ComponentFactory { | ||
|
||
/** | ||
* create a factory based on kafka cluster. All components created by this factory will send | ||
* request to kafka to get responses. | ||
* | ||
* @param brokers kafka broker addresses | ||
* @param topics to subscribe | ||
* @return a factory based on kafka | ||
*/ | ||
static ComponentFactory fromKafka(String brokers, Set<String> topics) { | ||
|
||
return new ComponentFactory() { | ||
private final String groupId = "group-id-" + System.currentTimeMillis(); | ||
|
||
@Override | ||
public Producer producer() { | ||
var props = new Properties(); | ||
props.setProperty(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, brokers); | ||
return Producer.fromKafka(props); | ||
} | ||
|
||
@Override | ||
public Consumer createConsumer() { | ||
var props = new Properties(); | ||
props.setProperty(CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG, brokers); | ||
// all consumers are in same group, so there is no duplicate data in read workload. | ||
props.setProperty(ConsumerConfig.GROUP_ID_CONFIG, groupId); | ||
return Consumer.fromKafka(props, topics); | ||
} | ||
|
||
@Override | ||
public TopicAdmin createTopicAdmin() { | ||
var props = new Properties(); | ||
props.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, brokers); | ||
return TopicAdmin.fromKafka(props); | ||
} | ||
}; | ||
} | ||
|
||
/** @return a new producer. Please close it when you don't need it. */ | ||
Producer producer(); | ||
|
||
/** @return a new consumer. Please close it when you don't need it */ | ||
Consumer createConsumer(); | ||
|
||
/** @return a new topic admin. Please close it when you don't need it. */ | ||
TopicAdmin createTopicAdmin(); | ||
} |
45 changes: 45 additions & 0 deletions
45
app/src/main/java/org/astraea/performance/latency/Consumer.java
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,45 @@ | ||
package org.astraea.performance.latency; | ||
|
||
import java.io.Closeable; | ||
import java.time.Duration; | ||
import java.util.Properties; | ||
import java.util.Set; | ||
import org.apache.kafka.clients.consumer.ConsumerRecords; | ||
import org.apache.kafka.clients.consumer.KafkaConsumer; | ||
import org.apache.kafka.common.serialization.ByteArrayDeserializer; | ||
|
||
interface Consumer extends Closeable { | ||
Duration POLL_TIMEOUT = Duration.ofMillis(500); | ||
|
||
static Consumer fromKafka(Properties props, Set<String> topics) { | ||
var kafkaConsumer = | ||
new KafkaConsumer<>(props, new ByteArrayDeserializer(), new ByteArrayDeserializer()); | ||
kafkaConsumer.subscribe(topics); | ||
return new Consumer() { | ||
|
||
@Override | ||
public ConsumerRecords<byte[], byte[]> poll() { | ||
return kafkaConsumer.poll(POLL_TIMEOUT); | ||
} | ||
|
||
@Override | ||
public void wakeup() { | ||
kafkaConsumer.wakeup(); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
kafkaConsumer.close(); | ||
} | ||
}; | ||
} | ||
|
||
/** see {@link KafkaConsumer#poll(Duration)} */ | ||
ConsumerRecords<byte[], byte[]> poll(); | ||
|
||
/** see {@link KafkaConsumer#wakeup()} */ | ||
default void wakeup() {} | ||
|
||
@Override | ||
default void close() {} | ||
} |
46 changes: 46 additions & 0 deletions
46
app/src/main/java/org/astraea/performance/latency/ConsumerThread.java
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,46 @@ | ||
package org.astraea.performance.latency; | ||
|
||
import java.util.Objects; | ||
|
||
class ConsumerThread extends CloseableThread { | ||
|
||
private final Consumer consumer; | ||
private final DataManager dataManager; | ||
private final MeterTracker tracker; | ||
|
||
ConsumerThread(DataManager dataManager, MeterTracker tracker, Consumer consumer) { | ||
this.dataManager = Objects.requireNonNull(dataManager); | ||
this.consumer = Objects.requireNonNull(consumer); | ||
this.tracker = Objects.requireNonNull(tracker); | ||
} | ||
|
||
@Override | ||
public void execute() throws InterruptedException { | ||
try { | ||
var now = System.currentTimeMillis(); | ||
var records = consumer.poll(); | ||
records.forEach( | ||
record -> { | ||
var entry = dataManager.removeSendingRecord(record.key()); | ||
var latency = now - entry.getValue(); | ||
var produceRecord = entry.getKey(); | ||
if (!KafkaUtils.equal(produceRecord, record)) | ||
System.out.println("receive corrupt data!!!"); | ||
else tracker.record(record.serializedKeySize() + record.serializedValueSize(), latency); | ||
}); | ||
} catch (org.apache.kafka.common.errors.WakeupException e) { | ||
throw new InterruptedException(e.getMessage()); | ||
} | ||
} | ||
|
||
@Override | ||
void cleanup() { | ||
consumer.close(); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
consumer.wakeup(); | ||
super.close(); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
app/src/main/java/org/astraea/performance/latency/DataManager.java
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,74 @@ | ||
package org.astraea.performance.latency; | ||
|
||
import java.util.*; | ||
import java.util.concurrent.ConcurrentMap; | ||
import java.util.concurrent.ConcurrentSkipListMap; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
import org.apache.kafka.clients.producer.ProducerRecord; | ||
|
||
class DataManager { | ||
|
||
static DataManager of(String topic, int valueSize) { | ||
return new DataManager(topic, valueSize, true); | ||
} | ||
|
||
static DataManager noConsumer(String topic, int valueSize) { | ||
return new DataManager(topic, valueSize, false); | ||
} | ||
|
||
/** record key -> (record, timestamp_done) */ | ||
private final ConcurrentMap<byte[], Map.Entry<ProducerRecord<byte[], byte[]>, Long>> | ||
sendingRecords = new ConcurrentSkipListMap<>(Arrays::compare); | ||
|
||
private final String topic; | ||
private final int valueSize; | ||
private final boolean hasConsumer; | ||
|
||
private final AtomicLong recordIndex = new AtomicLong(0); | ||
|
||
final AtomicLong producerRecords = new AtomicLong(0); | ||
|
||
private DataManager(String topic, int valueSize, boolean hasConsumer) { | ||
this.topic = Objects.requireNonNull(topic); | ||
this.valueSize = valueSize; | ||
this.hasConsumer = hasConsumer; | ||
} | ||
|
||
/** | ||
* @return generate a new record with random data and specify topic name. The record consists of | ||
* key, value, topic and header. | ||
*/ | ||
ProducerRecord<byte[], byte[]> producerRecord() { | ||
var content = String.valueOf(recordIndex.getAndIncrement()); | ||
var rawContent = content.getBytes(); | ||
var headers = Collections.singletonList(KafkaUtils.header(content, rawContent)); | ||
return new ProducerRecord<>(topic, null, rawContent, new byte[valueSize], headers); | ||
} | ||
|
||
void sendingRecord(ProducerRecord<byte[], byte[]> record, long now) { | ||
if (hasConsumer) { | ||
var previous = | ||
sendingRecords.put(record.key(), new AbstractMap.SimpleImmutableEntry<>(record, now)); | ||
if (previous != null) throw new RuntimeException("duplicate data!!!"); | ||
} | ||
producerRecords.incrementAndGet(); | ||
} | ||
|
||
/** | ||
* get sending record | ||
* | ||
* @param key of completed record | ||
* @return the completed record. Or NullPointerException if there is no related record. | ||
*/ | ||
Map.Entry<ProducerRecord<byte[], byte[]>, Long> removeSendingRecord(byte[] key) { | ||
if (!hasConsumer) | ||
throw new UnsupportedOperationException( | ||
"removeSendingRecord is unsupported when there is no consumer"); | ||
return Objects.requireNonNull(sendingRecords.remove(key)); | ||
} | ||
|
||
/** @return number of completed records */ | ||
long numberOfProducerRecords() { | ||
return producerRecords.get(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@chinghongfang 麻煩參考看看,這邊示範一個方式如何抽象實作,讓我們方便寫unit test驗證自身邏輯又可以不用真的使用kafka