Skip to content

Commit

Permalink
[PLAT-4187] Fix stdDevTime formula
Browse files Browse the repository at this point in the history
Summary:
Fix stdDevTime formula by adding square root.
(Previously was returning variance).

Test Plan:
- Run queries on a universe and check the slow queries tab
- Verify that the standard deviation is as expected

Reviewers: spotachev, andrew

Reviewed By: andrew

Subscribers: jenkins-bot

Differential Revision: https://phabricator.dev.yugabyte.com/D17350
  • Loading branch information
Jethro-M committed Jun 3, 2022
1 parent 8625a0b commit 99983d2
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions managed/src/main/java/com/yugabyte/yw/queries/QueryHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,18 +193,22 @@ public JsonNode query(
/*
* Formula to calculate std dev of two samples: Let mean, std dev, and size of
* sample A be X_a, S_a, n_a respectively; and mean, std dev, and size of sample B
* be X_b, S_b, n_b respectively. Then mean of combined sample X is given by n_a
* X_a + n_b X_b X = ----------------- n_a + n_b
* be X_b, S_b, n_b respectively. Then mean of combined sample X is given by
* n_a X_a + n_b X_b
* X = -----------------
* n_a + n_b
*
* <p>The std dev of combined sample S is n_a ( S_a^2 + (X_a - X)^2) + n_b(S_b^2 +
* (X_b - X)^2) S = ----------------------------------------------------- n_a +
* n_b
* The std dev of combined sample S is
* n_a ( S_a^2 + (X_a - X)^2) + n_b(S_b^2 + (X_b - X)^2)
* S = sqrt( ----------------------------------------------------- )
* n_a + n_b
*/
double averageTime = (n_a * X_a + n_b * X_b) / totalCalls;
double stdDevTime =
(n_a * (Math.pow(S_a, 2) + Math.pow(X_a - averageTime, 2))
+ n_b * (Math.pow(S_b, 2) + Math.pow(X_b - averageTime, 2)))
/ totalCalls;
Math.sqrt(
(n_a * (Math.pow(S_a, 2) + Math.pow(X_a - averageTime, 2))
+ n_b * (Math.pow(S_b, 2) + Math.pow(X_b - averageTime, 2)))
/ totalCalls);
previousQueryObj.put("total_time", totalTime);
previousQueryObj.put("calls", totalCalls);
previousQueryObj.put("rows", rows);
Expand Down

0 comments on commit 99983d2

Please sign in to comment.