Skip to content

Commit

Permalink
fix: Do not sort data and add new 'change' field into stats
Browse files Browse the repository at this point in the history
  • Loading branch information
jhutar committed Feb 15, 2024
1 parent 379c650 commit 8e4ec9e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
4 changes: 3 additions & 1 deletion core/opl/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def percentile(data, percent):
if not data:
return None

data.sort()
data = sorted(data)
k = (len(data) - 1) * percent / 100
# Python 2.x returns float for floor an ceil, so cast to int
f = int(math.floor(k))
Expand Down Expand Up @@ -226,6 +226,7 @@ def data_stats(data):
else 0.0,
"stdev": statistics.stdev(data) if len(data) > 1 else 0.0,
"range": max(data) - min(data),
"change": data[-1] - data[0],
"percentile25": q25,
"percentile75": q75,
"percentile90": q90,
Expand All @@ -240,6 +241,7 @@ def data_stats(data):
"max": max(data),
"mean": (max(data) - min(data)) / len(data),
"range": max(data) - min(data),
"change": data[-1] - data[0],
}
else:
raise Exception(f"Do not know how to get stats for list of {type(data[0])}")
Expand Down
4 changes: 3 additions & 1 deletion opl/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def percentile(data, percent):
if not data:
return None

data.sort()
data = sorted(data)
k = (len(data) - 1) * percent / 100
# Python 2.x returns float for floor an ceil, so cast to int
f = int(math.floor(k))
Expand Down Expand Up @@ -226,6 +226,7 @@ def data_stats(data):
else 0.0,
"stdev": statistics.stdev(data) if len(data) > 1 else 0.0,
"range": max(data) - min(data),
"change": data[-1] - data[0],
"percentile25": q25,
"percentile75": q75,
"percentile90": q90,
Expand All @@ -240,6 +241,7 @@ def data_stats(data):
"max": max(data),
"mean": (max(data) - min(data)) / len(data),
"range": max(data) - min(data),
"change": data[-1] - data[0],
}
else:
raise Exception(f"Do not know how to get stats for list of {type(data[0])}")
Expand Down
10 changes: 10 additions & 0 deletions tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def test_data_stats(self):
self.assertEqual(stats["mean"], 1)
self.assertEqual(stats["max"], 2)
self.assertEqual(stats["range"], 2)
self.assertEqual(stats["change"], 0)
self.assertEqual(stats["percentile25"], 0.75)
self.assertEqual(stats["percentile75"], 1.25)
self.assertEqual(stats["iqr"], 0.5)
Expand Down Expand Up @@ -46,6 +47,7 @@ def test_data_stats_datetime(self):
datetime.datetime.fromisoformat("2021-03-22T11:00:00.000000+00:00"),
)
self.assertEqual(stats["range"].total_seconds(), 3600)
self.assertEqual(stats["change"].total_seconds(), -3600)

def test_data_stats_strange(self):
data = [
Expand Down Expand Up @@ -99,3 +101,11 @@ def test_get_rps(self):
rps_vals = opl.data.get_rps([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
self.assertEqual(len(rps_vals), 5)
self.assertEqual(sum(rps_vals) / len(rps_vals), 1.0)

def test_percentile(self):
data = [0, 1, 1, 2, 2, 1, 1, 0]
perc25 = opl.data.percentile(data, 25)
perc75 = opl.data.percentile(data, 75)
self.assertEqual(data, [0, 1, 1, 2, 2, 1, 1, 0]) # make sure data is not reordered
self.assertEqual(perc25, 0.75)
self.assertEqual(perc75, 1.25)

0 comments on commit 8e4ec9e

Please sign in to comment.