|
| 1 | +# Run with elixir -pa ./ebin examples/simple.exs |
| 2 | + |
1 | 3 | #
|
2 | 4 | # Simple histogram capture example using Elixir
|
3 | 5 | #
|
4 |
| - |
5 | 6 | defmodule Simple do
|
6 |
| - def main do |
7 |
| - # Create a fresh HDR histogram instance |
8 |
| - {:ok,r} = :hdr_histogram.open(10000000,3) |
9 |
| - |
10 |
| - # record a random uniform distribution of 1M data points |
11 |
| - for n <- 1..1000000, do: :hdr_histogram.record(r,:random.uniform(n)) |
12 |
| - |
13 |
| - # print percentiles to stdout as CLASSIC |
14 |
| - :hdr_histogram.print(r,:classic) |
15 |
| - |
16 |
| - # log percentiles to file as CSV |
17 |
| - # ELIXIR BUG :hdr_histogram.log(r,:csv,"elixir.hgrm") |
18 |
| - |
19 |
| - # print other values |
20 |
| - IO.puts "Min #{:hdr_histogram.min(r)}" |
21 |
| - IO.puts "Mean #{:hdr_histogram.mean(r)}" |
22 |
| - IO.puts "Median #{:hdr_histogram.median(r)}" |
23 |
| - IO.puts "Max #{:hdr_histogram.max(r)}" |
24 |
| - IO.puts "Stddev #{:hdr_histogram.stddev(r)}" |
25 |
| - IO.puts "99ile #{:hdr_histogram.percentile(r,99.0)}" |
26 |
| - IO.puts "99.9999ile #{:hdr_histogram.percentile(r,99.9999)}" |
27 |
| - IO.puts "Memory Size #{:hdr_histogram.get_memory_size(r)}" |
28 |
| - IO.puts "Total Count #{:hdr_histogram.get_total_count(r)}" |
29 |
| - |
30 |
| - # we're done, cleanup any held resources |
31 |
| - :hdr_histogram.close(r) |
32 |
| - |
33 |
| - IO.puts "Done!" |
34 |
| - end |
| 7 | + def main do |
| 8 | + # create a fresh HDR histogram instance |
| 9 | + {:ok, ref} = :hdr_histogram.open(1000000, 3) |
| 10 | + |
| 11 | + n = 10000000 |
| 12 | + |
| 13 | + # record a random uniform distribution of 1M data points |
| 14 | + started = :os.timestamp |
| 15 | + loop(ref, n) |
| 16 | + ended = :os.timestamp |
| 17 | + |
| 18 | + duration = :timer.now_diff(ended, started) / 1.0e6 |
| 19 | + rate = case duration > 1 do |
| 20 | + true -> n/duration |
| 21 | + false -> n*duration |
| 22 | + end |
| 23 | + :io.format("Runtime: ~psecs ~.5frps~n", [duration, rate]) |
| 24 | + |
| 25 | + # print percentiles to stdout as CSV |
| 26 | + :hdr_histogram.print(ref, :csv) |
| 27 | + |
| 28 | + # log percentiles to file as CLASSIC |
| 29 | + :hdr_histogram.log(ref, :classic, 'elixir.hgrm') |
| 30 | + |
| 31 | + # print other values |
| 32 | + IO.puts "Min #{:hdr_histogram.min(ref)}" |
| 33 | + IO.puts "Mean #{:hdr_histogram.mean(ref)}" |
| 34 | + IO.puts "Median #{:hdr_histogram.median(ref)}" |
| 35 | + IO.puts "Max #{:hdr_histogram.max(ref)}" |
| 36 | + IO.puts "Stddev #{:hdr_histogram.stddev(ref)}" |
| 37 | + IO.puts "99ile #{:hdr_histogram.percentile(ref,99.0)}" |
| 38 | + IO.puts "99.9999ile #{:hdr_histogram.percentile(ref,99.9999)}" |
| 39 | + IO.puts "Memory Size #{:hdr_histogram.get_memory_size(ref)}" |
| 40 | + IO.puts "Total Count #{:hdr_histogram.get_total_count(ref)}" |
| 41 | + |
| 42 | + # we're done, cleanup any held resources |
| 43 | + :hdr_histogram.close(ref) |
| 44 | + |
| 45 | + IO.puts "Done!" |
| 46 | + end |
| 47 | + |
| 48 | + def loop(_ref, 0), do: :ok |
| 49 | + def loop(ref, x) do |
| 50 | + :hdr_histogram.record(ref, :random.uniform(1000000)) |
| 51 | + loop(ref, x - 1) |
| 52 | + end |
35 | 53 | end
|
| 54 | + |
| 55 | +Simple.main |
0 commit comments