• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<?php
2/*
3 *
4 * Copyright 2017 gRPC authors.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19
20// Histogram class for use in performance testing and measurement
21class Histogram {
22  private $resolution;
23  private $max_possible;
24  private $sum;
25  private $sum_of_squares;
26  private $multiplier;
27  private $count;
28  private $min_seen;
29  private $max_seen;
30  private $buckets;
31
32  private function bucket_for($value) {
33    return (int)(log($value) / log($this->multiplier));
34  }
35
36  public function __construct($resolution, $max_possible) {
37    $this->resolution = $resolution;
38    $this->max_possible = $max_possible;
39    $this->sum = 0;
40    $this->sum_of_squares = 0;
41    $this->multiplier = 1+$resolution;
42    $this->count = 0;
43    $this->min_seen = $max_possible;
44    $this->max_seen = 0;
45    $this->buckets = array_fill(0, $this->bucket_for($max_possible)+1, 0);
46  }
47
48  public function add($value) {
49    $this->sum += $value;
50    $this->sum_of_squares += $value * $value;
51    $this->count += 1;
52    if ($value < $this->min_seen) {
53      $this->min_seen = $value;
54    }
55    if ($value > $this->max_seen) {
56      $this->max_seen = $value;
57    }
58    $this->buckets[$this->bucket_for($value)] += 1;
59  }
60
61  public function minimum() {
62    return $this->min_seen;
63  }
64
65  public function maximum() {
66    return $this->max_seen;
67  }
68
69  public function sum() {
70    return $this->sum;
71  }
72
73  public function sum_of_squares() {
74    return $this->sum_of_squares;
75  }
76
77  public function count() {
78    return $this->count;
79  }
80
81  public function contents() {
82    return $this->buckets;
83  }
84
85  public function clean() {
86    $this->sum = 0;
87    $this->sum_of_squares = 0;
88    $this->count = 0;
89    $this->min_seen = $this->max_possible;
90    $this->max_seen = 0;
91    $this->buckets = array_fill(0, $this->bucket_for($this->max_possible)+1, 0);
92  }
93}
94