1import Cachex.Spec
2
3one_hour = :timer.hours(1)
4tomorrow = now() + (1000 * 60 * 60 * 24)
5
6Application.ensure_all_started(:cachex)
7
8Cachex.start(:bench_cache)
9
10Cachex.set(:bench_cache, "decr_test", 0)
11Cachex.set(:bench_cache, "expire_at_test", "expire_at_value")
12Cachex.set(:bench_cache, "expire_test", "expire_value")
13Cachex.set(:bench_cache, "fetch_test", "fetch_value")
14Cachex.set(:bench_cache, "get_test", "get_value")
15Cachex.set(:bench_cache, "gad_test", "gad_value")
16Cachex.set(:bench_cache, "incr_test", 0)
17Cachex.set(:bench_cache, "persist_test", 0)
18Cachex.set(:bench_cache, "refresh_test", "refresh_value", ttl: one_hour)
19Cachex.set(:bench_cache, "ttl_test", "ttl_value", ttl: one_hour)
20Cachex.set(:bench_cache, "update_test", "update_value")
21
22use_state = System.get_env("CACHEX_BENCH_STATE") == "true"
23use_trans = System.get_env("CACHEX_BENCH_TRANSACTIONS") == "true"
24
25if use_trans do
26  Cachex.Services.Overseer.update(:bench_cache, fn(state) ->
27    cache(state, transactional: true)
28  end)
29end
30
31cache = if use_state do
32  Cachex.inspect!(:bench_cache, :cache)
33else
34  :bench_cache
35end
36
37benchmarks = %{
38  "count" => fn ->
39    Cachex.count(cache)
40  end,
41  "decr" => fn ->
42    Cachex.decr(cache, "decr_test")
43  end,
44  "del" => fn ->
45    Cachex.del(cache, "del_test")
46  end,
47  "empty?" => fn ->
48    Cachex.empty?(cache)
49  end,
50  "exists?" => fn ->
51    Cachex.exists?(cache, "exists_test")
52  end,
53  "expire" => fn ->
54    Cachex.expire(cache, "expire_test", one_hour)
55  end,
56  "expire_at" => fn ->
57    Cachex.expire_at(cache, "expire_at_test", tomorrow)
58  end,
59  "fetch" => fn ->
60    Cachex.fetch(cache, "fetch_test", &(&1))
61  end,
62  "get" => fn ->
63    Cachex.get(cache, "get_test")
64  end,
65  "get_and_update" => fn ->
66    Cachex.get_and_update(cache, "gad_test", &(&1))
67  end,
68  "incr" => fn ->
69    Cachex.incr(cache, "incr_test")
70  end,
71  "keys" => fn ->
72    Cachex.keys(cache)
73  end,
74  "persist" => fn ->
75    Cachex.persist(cache, "persist_test")
76  end,
77  "refresh" => fn ->
78    Cachex.refresh(cache, "refresh_test")
79  end,
80  "set" => fn ->
81    Cachex.set(cache, "set_test", "set_value")
82  end,
83  "size" => fn ->
84    Cachex.size(cache)
85  end,
86  "stream" => fn ->
87    Cachex.stream(cache)
88  end,
89  "take" => fn ->
90    Cachex.take(cache, "take_test")
91  end,
92  "ttl" => fn ->
93    Cachex.ttl(cache, "ttl_test")
94  end,
95  "update" => fn ->
96    Cachex.update(cache, "update_test", "update_value")
97  end
98}
99
100Benchee.run(benchmarks, [
101  console: [
102    comparison: false,
103    extended_statistics: true
104  ],
105  formatters: [
106    Benchee.Formatters.Console,
107    Benchee.Formatters.HTML
108  ],
109  formatter_options: [
110    html: [
111      auto_open: false
112    ]
113  ],
114  print: [
115    fast_warning: false
116  ]
117])
118