Skip to content
meh. edited this page Jul 24, 2013 · 8 revisions

load Benchmark

{:benchmark,"0.0.1",git: "https://github.com/meh/elixir-benchmark.git"}

fire up iex and amnesia and your database from the crash course

iex -S mix
iex(1)> Amnesia.start
iex(10)> use Database
[nil, nil, nil, nil]
iex(12)> require Benchmark
nil

create a few functions

awriter = fn(id) -> 
  Amnesia.transaction! do
    user = User[id: id]
    user.add_message("yo dawg #{id}")
    user.write
  end
end

dwriter = fn(id) -> 
  user = User[id: id]
  user.add_message!("yo dawg #{id}")
  user.write!
end
areader = fn(id) -> Amnesia.transaction! do
    user = User.read(id)
  end
end
dreader = fn(id) -> User.read!(id) end

create a record

iex(13)> awriter.(1)
:ok

run a test

iex(14)> Benchmark.times 10000, do: dreader.(1)
[min: 7 microseconds, max: 3.681 milliseconds, median: 8 microseconds,
 average: 8.9682 microseconds]

compare to transaction mode

iex(15)> Benchmark.times 10000, do: areader.(1)
[min: 151 microseconds, max: 1.935 milliseconds, median: 166 microseconds,
 average: 168.6909 microseconds]

now writes

iex(16)> Benchmark.times 10000, do: awriter.(1)
[min: 228 microseconds, max: 594 microseconds, median: 240 microseconds,
 average: 246.7776 microseconds]
iex(18)> Benchmark.times 10000, do: dwriter.(1)
[min: 37 microseconds, max: 644 microseconds, median: 39 microseconds,
 average: 40.8607 microseconds]

Counting 1..1000000

iex(26)> c = Amnesia.Counter.get(:foo)
{Amnesia.Counter, :foo, Amnesia.Counter}
iex(27)> c
{Amnesia.Counter, :foo, Amnesia.Counter}
iex(28)> c.increase!
1
iex(30)> c.value!
1
iex(31)> Benchmark.times 1000000, do: c.increase!
[min: 9 microseconds, max: 55.851 milliseconds, median: 10 microseconds,
 average: 10.168972 microseconds]

iex(32)> c.value!
1000001
Clone this wiki locally