Skip to content

Commit 46a4f86

Browse files
committed
Merge pull request #28 from tisba/fix-elixir-example
Fix Elixir Example
2 parents 840fb93 + a202caa commit 46a4f86

File tree

3 files changed

+52
-69
lines changed

3 files changed

+52
-69
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ priv
1111
current_counterexample.eqc
1212
erl_crash.dump
1313
_build
14+
*.hgrm

README.md

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -86,45 +86,7 @@ main(_) ->
8686
io:format("Done!\n").
8787
```
8888

89-
The same library works with other BEAM hosted languages, such as Elixir:
90-
91-
```elixir
92-
#
93-
# Simple histogram capture example using Elixir
94-
#
95-
96-
defmodule Simple do
97-
def main do
98-
# Create a fresh HDR histogram instance
99-
{:ok,r} = :hdr_histogram.open(10000000,3)
100-
101-
# record a random uniform distribution of 1M data points
102-
for n <- 1..1000000, do: :hdr_histogram.record(r,:random.uniform(n))
103-
104-
# print percentiles to stdout as CLASSIC
105-
:hdr_histogram.print(r,:classic)
106-
107-
# log percentiles to file as CSV
108-
# ELIXIR BUG :hdr_histogram.log(r,:csv,"elixir.hgrm")
109-
110-
# print other values
111-
IO.puts "Min #{:hdr_histogram.min(r)}"
112-
IO.puts "Mean #{:hdr_histogram.mean(r)}"
113-
IO.puts "Median #{:hdr_histogram.median(r)}"
114-
IO.puts "Max #{:hdr_histogram.max(r)}"
115-
IO.puts "Stddev #{:hdr_histogram.stddev(r)}"
116-
IO.puts "99ile #{:hdr_histogram.percentile(r,99.0)}"
117-
IO.puts "99.9999ile #{:hdr_histogram.percentile(r,99.9999)}"
118-
IO.puts "Memory Size #{:hdr_histogram.get_memory_size(r)}"
119-
IO.puts "Total Count #{:hdr_histogram.get_total_count(r)}"
120-
121-
# we're done, cleanup any held resources
122-
:hdr_histogram.close(r)
123-
124-
IO.puts "Done!"
125-
end
126-
end
127-
```
89+
The same library works with other BEAM hosted languages, such as Elixir. See `examples/simple.exs`.
12890

12991
[API documentation](doc/README.md)
13092

examples/simple.exs

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,55 @@
1+
# Run with elixir -pa ./ebin examples/simple.exs
2+
13
#
24
# Simple histogram capture example using Elixir
35
#
4-
56
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
3553
end
54+
55+
Simple.main

0 commit comments

Comments
 (0)