Skip to content

Commit 2a8b8c9

Browse files
authored
Add csv output (#34)
1 parent 2b0a9ed commit 2a8b8c9

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

hdrh/histogram.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -597,12 +597,21 @@ def get_int_to_double_conversion_ratio(self):
597597
def output_percentile_distribution(self,
598598
out_file,
599599
output_value_unit_scaling_ratio,
600-
ticks_per_half_distance=5):
601-
out_file.write(b'%12s %14s %10s %14s\n\n' %
602-
(b'Value', b'Percentile', b'TotalCount', b'1/(1-Percentile)'))
600+
ticks_per_half_distance=5,
601+
use_csv=False):
602+
if use_csv:
603+
out_file.write(b'"Value","Percentile","TotalCount","1/(1-Percentile)"\n')
604+
else:
605+
out_file.write(b'%12s %14s %10s %14s\n\n' %
606+
(b'Value', b'Percentile', b'TotalCount', b'1/(1-Percentile)'))
607+
608+
if use_csv:
609+
percentile_format = '%.{}f,%.12f,%d,%.2f\n'.format(self.significant_figures)
610+
last_line_percentile_format = '%.{}f,%.12f,%d,Infinity\n'.format(self.significant_figures)
611+
else:
612+
percentile_format = '%12.{}f %2.12f %10d %14.2f\n'.format(self.significant_figures)
613+
last_line_percentile_format = '%12.{}f %2.12f %10d\n'.format(self.significant_figures)
603614

604-
percentile_format = '%12.{}f %2.12f %10d %14.2f\n'.format(self.significant_figures)
605-
last_line_percentile_format = '%12.{}f %2.12f %10d\n'.format(self.significant_figures)
606615
for iter_value in self.get_percentile_iterator(ticks_per_half_distance):
607616
value = iter_value.value_iterated_to / output_value_unit_scaling_ratio
608617
percentile = iter_value.percentile_level_iterated_to / 100
@@ -616,6 +625,9 @@ def output_percentile_distribution(self,
616625
percentile,
617626
total_count))
618627

628+
if use_csv:
629+
return
630+
619631
mean = self.get_mean_value() / output_value_unit_scaling_ratio
620632
stddev = self.get_stddev()
621633
out_file.write('#[Mean = %12.{0}f, StdDeviation = %12.{0}f]\n'.

test/test_hdrhistogram.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from builtins import range
2525
import cProfile
2626
import datetime
27+
import io
2728
import os
2829
import struct
2930
import zlib
@@ -50,6 +51,7 @@
5051
from hdrh.codec import HdrCookieException
5152
from hdrh.dump import dump
5253

54+
5355
def python_bitness():
5456
"cross-platform way of calculating bitness, returns either 32 or 64"
5557
return struct.calcsize("P") * 8
@@ -677,6 +679,16 @@ def test_output_percentile_distribution():
677679
histogram = load_histogram()
678680
histogram.output_percentile_distribution(open(os.devnull, 'wb'), 1000)
679681

682+
@pytest.mark.log
683+
def test_output_percentile_distribution_csv():
684+
buf = io.BytesIO()
685+
histogram = load_histogram()
686+
histogram.output_percentile_distribution(buf, 1000, use_csv=True)
687+
lines = buf.getvalue().decode('utf-8').rstrip().split('\n')
688+
assert lines[0] == '"Value","Percentile","TotalCount","1/(1-Percentile)"'
689+
assert lines[1] == '1.000,0.000000000000,10000,1.00'
690+
assert lines[-1] == '100007.935,1.000000000000,10001,Infinity'
691+
680692

681693
ARRAY_SIZE = 10
682694

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[tox]
2-
envlist = py27, py36, py37, py38, pep8, lint
2+
envlist = py27, py36, py37, py38, py39, pep8, lint
33

44
[testenv:pep8]
55
commands = flake8 hdrh test

0 commit comments

Comments
 (0)