| 1 |
#!/usr/bin/perl |
| 2 |
# |
| 3 |
# Copyright (c) 2011-2017 FastMail Pty Ltd. All rights reserved. |
| 4 |
# |
| 5 |
# Redistribution and use in source and binary forms, with or without |
| 6 |
# modification, are permitted provided that the following conditions |
| 7 |
# are met: |
| 8 |
# |
| 9 |
# 1. Redistributions of source code must retain the above copyright |
| 10 |
# notice, this list of conditions and the following disclaimer. |
| 11 |
# |
| 12 |
# 2. Redistributions in binary form must reproduce the above copyright |
| 13 |
# notice, this list of conditions and the following disclaimer in |
| 14 |
# the documentation and/or other materials provided with the |
| 15 |
# distribution. |
| 16 |
# |
| 17 |
# 3. The name "Fastmail Pty Ltd" must not be used to |
| 18 |
# endorse or promote products derived from this software without |
| 19 |
# prior written permission. For permission or any legal |
| 20 |
# details, please contact |
| 21 |
# FastMail Pty Ltd |
| 22 |
# PO Box 234 |
| 23 |
# Collins St West 8007 |
| 24 |
# Victoria |
| 25 |
# Australia |
| 26 |
# |
| 27 |
# 4. Redistributions of any form whatsoever must retain the following |
| 28 |
# acknowledgment: |
| 29 |
# "This product includes software developed by Fastmail Pty. Ltd." |
| 30 |
# |
| 31 |
# FASTMAIL PTY LTD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 32 |
# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
| 33 |
# EVENT SHALL OPERA SOFTWARE AUSTRALIA BE LIABLE FOR ANY SPECIAL, INDIRECT |
| 34 |
# OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF |
| 35 |
# USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 36 |
# TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 37 |
# OF THIS SOFTWARE. |
| 38 |
# |
| 39 |
|
| 40 |
package Cassandane::Util::Sample; |
| 41 |
use strict; |
| 42 |
use warnings; |
| 43 |
use overload qw("") => \&as_string; |
| 44 |
|
| 45 |
sub new |
| 46 |
{ |
| 47 |
my ($class, @args) = @_; |
| 48 |
|
| 49 |
die "Unknown extra arguments" |
| 50 |
if scalar(@args); |
| 51 |
|
| 52 |
my $self = |
| 53 |
{ |
| 54 |
_total => 0.0, |
| 55 |
_total2 => 0.0, |
| 56 |
_n => 0, |
| 57 |
_min => undef, |
| 58 |
_max => undef, |
| 59 |
}; |
| 60 |
return bless($self, $class); |
| 61 |
} |
| 62 |
|
| 63 |
sub add |
| 64 |
{ |
| 65 |
my ($self, $x) = @_; |
| 66 |
|
| 67 |
$self->{_total} += $x; |
| 68 |
$self->{_total2} += $x * $x; |
| 69 |
$self->{_n}++; |
| 70 |
$self->{_min} = $x |
| 71 |
if (!defined $self->{_min} || $x < $self->{_min}); |
| 72 |
$self->{_max} = $x |
| 73 |
if (!defined $self->{_max} || $x > $self->{_max}); |
| 74 |
} |
| 75 |
|
| 76 |
sub nsamples |
| 77 |
{ |
| 78 |
my ($self) = @_; |
| 79 |
return $self->{_n}; |
| 80 |
} |
| 81 |
|
| 82 |
sub average |
| 83 |
{ |
| 84 |
my ($self) = @_; |
| 85 |
die "No samples yet" if (!$self->{_n}); |
| 86 |
return $self->{_total} / $self->{_n}; |
| 87 |
} |
| 88 |
|
| 89 |
sub minimum |
| 90 |
{ |
| 91 |
my ($self) = @_; |
| 92 |
die "No samples yet" if (!$self->{_n}); |
| 93 |
return $self->{_min}; |
| 94 |
} |
| 95 |
|
| 96 |
sub maximum |
| 97 |
{ |
| 98 |
my ($self) = @_; |
| 99 |
die "No samples yet" if (!$self->{_n}); |
| 100 |
return $self->{_max}; |
| 101 |
} |
| 102 |
|
| 103 |
sub sample_deviation |
| 104 |
{ |
| 105 |
my ($self) = @_; |
| 106 |
die "No samples yet" if ($self->{_n} < 2); |
| 107 |
return sqrt( |
| 108 |
($self->{_n} * $self->{_total2} - $self->{_total} * $self->{_total}) |
| 109 |
/ |
| 110 |
($self->{_n} * ($self->{_n} - 1)) |
| 111 |
); |
| 112 |
} |
| 113 |
|
| 114 |
sub as_string |
| 115 |
{ |
| 116 |
my ($self) = @_; |
| 117 |
my $s = "no samples"; |
| 118 |
if ($self->{_n} > 0) |
| 119 |
{ |
| 120 |
$s = "count " . $self->nsamples() . |
| 121 |
" minimum " . $self->minimum() . |
| 122 |
" maximum " . $self->maximum() . |
| 123 |
" average " . $self->average(); |
| 124 |
if ($self->{_n} > 1) |
| 125 |
{ |
| 126 |
$s .= " sample_deviation " . $self->sample_deviation(); |
| 127 |
} |
| 128 |
} |
| 129 |
return $s; |
| 130 |
} |
| 131 |
|
| 132 |
1; |