Open Kilda Java Documentation
queue-throughput-test.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 # Copyright 2017 Telstra Open Source
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16 
17 from kafka import KafkaConsumer, TopicPartition
18 import json
19 import time
20 import os
21 
22 print "Connecting to kafka using application defined configuration:"
23 
25 
26  bootstrapServer = 'localhost:9092'
27  topic = 'kilda-test'
28  group = 'python-tpe-tl-consumer-test'
29 
30  while True:
31  try:
32  consumer = KafkaConsumer(bootstrap_servers=bootstrapServer, group_id=group, auto_offset_reset='earliest')
33  consumer.subscribe(['{}'.format(topic)])
34  print "Connected to kafka"
35  break
36  except Exception as e:
37  print "The follow error was generated:"
38  print e
39  time.sleep(5)
40  return consumer
41 
42 def read_message(consumer):
43  try:
44  message = consumer.next()
45  if message.value is not "":
46  return message.value
47  else:
48  print "sleeping"
49  time.sleep(1)
50  except Exception as e:
51  print e
52 
53 
54 consumer = create_consumer()
55 messagecount = 0
56 
57 while True:
58  read_message(consumer)
59  messagecount += 1
60  print "message: {}".format(messagecount)