-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproducerTest.go
More file actions
62 lines (51 loc) · 1.29 KB
/
producerTest.go
File metadata and controls
62 lines (51 loc) · 1.29 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
package main
import (
"flag"
"log"
"os"
"github.com/IBM/sarama"
"github.com/ankush-003/distributed-load-testing/kafka"
)
func main() {
topic := flag.String("topic", "test", "Producer topic name")
broker := flag.String("broker", "localhost:9092", "Kafka broker address")
flag.Parse()
config := sarama.NewConfig()
config.Producer.Return.Successes = true
logger := log.New(os.Stdout, "KafkaProducer: ", log.Ldate|log.Ltime|log.Lshortfile)
producer, err := kafka.NewProducer([]string{*broker}, config, logger)
if err != nil {
logger.Fatalf("Error creating producer: %s", err)
}
defer func() {
if err := producer.Close(); err != nil {
logger.Fatalf("Error closing producer: %s", err)
}
}()
messages := []kafka.MetricsMessage{
{
NodeID: "node1",
TestID: "test1",
ReportID: "report1",
Metrics: kafka.MetricsData{
MeanLatency: "1",
MedianLatency: "2",
MinLatency: "3",
MaxLatency: "4",
},
},
{
NodeID: "node2",
TestID: "test2",
ReportID: "report2",
Metrics: kafka.MetricsData{
MeanLatency: "5",
MedianLatency: "6",
MinLatency: "7",
MaxLatency: "8",
},
},
}
enqueued, errors := producer.ProduceMetricsMessages(*topic, messages)
logger.Printf("Enqueued: %d; errors: %d\n", enqueued, errors)
}