Skip to content

Instantly share code, notes, and snippets.

@cooldaemon
Created July 23, 2008 10:05
Show Gist options
  • Save cooldaemon/1641 to your computer and use it in GitHub Desktop.
Save cooldaemon/1641 to your computer and use it in GitHub Desktop.
CouchDB(btree) vs dets.
-module(couchdb_btree_vs_dets).
-author('cooldaemon@gmail.com').
-export([test/0, test/1]).
test() -> test(1000).
test(Count) ->
KVs = lists:map(
fun (N) -> {N, {foo, bar, baz}} end,
lists:seq(1, Count)
),
lists:foreach(
fun ({F, TargetName}) ->
{[SetRunTime, SetWallClock], [GetRunTime, GetWallClock]} = F(KVs),
io:fwrite(
"--<~s>--~nset:~p(~p)ms~nget:~p(~p)ms~n",
[TargetName, SetRunTime, SetWallClock, GetRunTime, GetWallClock]
)
end,
[
{fun dets/1, "dets"},
{fun btree1/1, "CouchDB B-Tree(case1)"},
{fun btree2/1, "CouchDB B-Tree(case2)"},
{fun btree3/1, "CouchDB B-Tree(case3)"},
{fun btree4/1, "CouchDB B-Tree(case4)"}
]
).
dets(KVs) ->
{ok, _R} = dets:open_file(dets_test, []),
{_SetResult, SetTimes} = benchmark(
fun () ->
lists:foreach(fun (KV) -> dets:insert(dets_test, KV) end, KVs)
end
),
{_GetResult, GetTimes} = benchmark(
fun () ->
lists:map(
fun ({K, _}) -> dets:lookup(dets_test, K) end, KVs
)
end
),
{SetTimes, GetTimes}.
btree_open(FileName) ->
{ok, Fd} = couch_file:open(FileName, [create, overwrite]),
{ok, Bt} = couch_btree:open(nil, Fd),
Bt.
btree_bulk_set(Bt, KVs) ->
{{ok, NewBt}, SetTimes} = benchmark(
fun () -> couch_btree:add(Bt, KVs) end
),
{NewBt, SetTimes}.
btree_bulk_get(Bt, KVs) ->
Ks = lists:map(fun ({K, _V}) -> K end, KVs),
{_GetResult, GetTimes} = benchmark(
fun () -> couch_btree:lookup(Bt, Ks) end
),
GetTimes.
btree_set(Bt, KVs) ->
{{ok, NewBt}, SetTimes} = benchmark(
fun () ->
lists:foldl(
fun (KV, {ok, TmpBt}) -> couch_btree:add(TmpBt, [KV]) end,
{ok, Bt}, KVs
)
end
),
{NewBt, SetTimes}.
btree_get(Bt, KVs) ->
Ks = lists:map(fun ({K, _V}) -> K end, KVs),
{_GetResult, GetTimes} = benchmark(
fun () -> lists:map(fun (N) -> couch_btree:lookup(Bt, [N]) end, Ks) end
),
GetTimes.
btree(KVs, FileName, SetFunc, GetFunc) ->
Bt = btree_open(FileName),
{NewBt, SetTimes} = SetFunc(Bt, KVs),
GetTimes = GetFunc(NewBt, KVs),
{SetTimes, GetTimes}.
btree1(KVs) ->
btree(KVs, "btree1_test", fun btree_bulk_set/2, fun btree_bulk_get/2).
btree2(KVs) ->
btree(KVs, "btree2_test", fun btree_set/2, fun btree_get/2).
btree3(KVs) ->
btree(KVs, "btree3_test", fun btree_set/2, fun btree_bulk_get/2).
btree4(KVs) ->
btree(KVs, "btree4_test", fun btree_bulk_set/2, fun btree_get/2).
benchmark(TargetFunction) ->
lists:foreach(fun statistics/1, [runtime, wall_clock]),
Result = TargetFunction(),
Times = lists:map(
fun (Type) -> {_, T} = statistics(Type), T end,
[runtime, wall_clock]
),
{Result, Times}.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment