Skip to content

Commit e34acc1

Browse files
committed
Improve string concat peformance.
1 parent 54da635 commit e34acc1

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

influxdb/_dataframe_client.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def write_points(self, dataframe, measurement, tags=None,
3939
:param dataframe: data points in a DataFrame
4040
:param measurement: name of measurement
4141
:param tags: dictionary of tags, with string key-values
42-
:param time_precision: [Optional, default 's'] Either 's', 'ms', 'u'
42+
:param time_precision: [Optional, default None] Either 's', 'ms', 'u'
4343
or 'n'.
4444
:param batch_size: [Optional] Value to write the points in batches
4545
instead of all at one time. Useful for when doing data dumps from
@@ -140,7 +140,7 @@ def _convert_dataframe_to_json(self, dataframe, measurement, tags=None):
140140
{'measurement': measurement,
141141
'tags': tags if tags else {},
142142
'fields': rec,
143-
'time': ts.isoformat()
143+
'time': ts.value
144144
}
145145
for ts, rec in zip(dataframe.index, dataframe.to_dict('record'))]
146146
return points
@@ -150,8 +150,8 @@ def _datetime_to_epoch(self, datetime, time_precision='s'):
150150
if time_precision == 's':
151151
return seconds
152152
elif time_precision == 'ms':
153-
return seconds * 10 ** 3
153+
return seconds * 1e3
154154
elif time_precision == 'u':
155-
return seconds * 10 ** 6
155+
return seconds * 1e6
156156
elif time_precision == 'n':
157-
return seconds * 10 ** 9
157+
return seconds * 1e9

influxdb/line_protocol.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,41 +63,51 @@ def make_lines(data):
6363
Extracts the points from the given dict and returns a Unicode string
6464
matching the line protocol introduced in InfluxDB 0.9.0.
6565
"""
66-
lines = ""
66+
lines = []
6767
static_tags = data.get('tags', None)
6868
for point in data['points']:
69+
elements = []
70+
6971
# add measurement name
70-
lines += _escape_tag(_force_text(
72+
measurement = _escape_tag(_force_text(
7173
point.get('measurement', data.get('measurement'))
72-
)) + ","
74+
))
75+
key_values = [measurement]
7376

7477
# add tags
7578
if static_tags is None:
7679
tags = point.get('tags', {})
7780
else:
7881
tags = copy(static_tags)
7982
tags.update(point.get('tags', {}))
83+
8084
# tags should be sorted client-side to take load off server
8185
for tag_key in sorted(tags.keys()):
8286
key = _escape_tag(tag_key)
8387
value = _escape_tag(tags[tag_key])
8488
if key != '' and value != '':
85-
lines += "{key}={value},".format(key=key, value=value)
86-
lines = lines[:-1] + " " # strip the trailing comma
89+
key_values.append("{key}={value}".format(key=key, value=value))
90+
key_values = ','.join(key_values)
91+
elements.append(key_values)
8792

8893
# add fields
94+
field_values = []
8995
for field_key in sorted(point['fields'].keys()):
90-
lines += "{key}={value},".format(
96+
field_values.append("{key}={value}".format(
9197
key=_escape_tag(field_key),
9298
value=_escape_value(point['fields'][field_key]),
93-
)
94-
lines = lines[:-1] # strip the trailing comma
99+
))
100+
field_values = ','.join(field_values)
101+
elements.append(field_values)
95102

96103
# add timestamp
97104
if 'time' in point:
98-
lines += " " + _force_text(str(int(
105+
timestamp = _force_text(str(int(
99106
_convert_timestamp(point['time'])
100107
)))
108+
elements.append(timestamp)
101109

102-
lines += "\n"
103-
return lines
110+
line = ' '.join(elements)
111+
lines.append(line)
112+
lines = '\n'.join(lines)
113+
return lines + '\n'

0 commit comments

Comments
 (0)