| 1 | |
|---|
| 2 | :- object(benchmarks). |
|---|
| 3 | |
|---|
| 4 | :- info([ |
|---|
| 5 | version is 3.0, |
|---|
| 6 | author is 'Paulo Moura', |
|---|
| 7 | date is 2007/06/11, |
|---|
| 8 | comment is 'Benchmark utility predicates and standard set of benchmarks.']). |
|---|
| 9 | |
|---|
| 10 | :- public(run/0). |
|---|
| 11 | :- mode(run, one). |
|---|
| 12 | :- info(run/0, [ |
|---|
| 13 | comment is 'Runs all benchmarks the default number of times.']). |
|---|
| 14 | |
|---|
| 15 | :- public(run/1). |
|---|
| 16 | :- mode(run(+integer), one). |
|---|
| 17 | :- info(run/1, [ |
|---|
| 18 | comment is 'Runs all benchmarks the specified number of times.', |
|---|
| 19 | argnames is ['N']]). |
|---|
| 20 | |
|---|
| 21 | :- public(run/2). |
|---|
| 22 | :- mode(run(+atom, +integer), one). |
|---|
| 23 | :- info(run/2, [ |
|---|
| 24 | comment is 'Runs a specific benchmark the specified number of times.', |
|---|
| 25 | argnames is ['Id', 'N']]). |
|---|
| 26 | |
|---|
| 27 | :- public(benchmark/2). |
|---|
| 28 | :- mode(move(?atom, -callable), zero_or_more). |
|---|
| 29 | :- info(move/2, [ |
|---|
| 30 | comment is 'Table of benchmark identifiers and benchmark goals.', |
|---|
| 31 | argnames is ['Id', 'Goal']]). |
|---|
| 32 | |
|---|
| 33 | % run all benchmarks the default number of times: |
|---|
| 34 | run :- |
|---|
| 35 | run(100000). |
|---|
| 36 | |
|---|
| 37 | % run all benchmark tests N times: |
|---|
| 38 | run(N) :- |
|---|
| 39 | benchmark(Id, Goal), |
|---|
| 40 | run(Id, N, Looptime, Goaltime, Average, Speed), |
|---|
| 41 | report(Id, Goal, N, Looptime, Goaltime, Average, Speed), |
|---|
| 42 | fail. |
|---|
| 43 | run(_). |
|---|
| 44 | |
|---|
| 45 | % run a specific benchmark test: |
|---|
| 46 | run(Id, N) :- |
|---|
| 47 | benchmark(Id, Goal), |
|---|
| 48 | run(Id, N, Looptime, Goaltime, Average, Speed), |
|---|
| 49 | report(Id, Goal, N, Looptime, Goaltime, Average, Speed). |
|---|
| 50 | |
|---|
| 51 | run(Id, N, Looptime, Goaltime, Average, Speed) :- |
|---|
| 52 | {'$lgt_cpu_time'(Seconds1)}, % defined in the config files |
|---|
| 53 | do_benchmark(empty_loop, N), |
|---|
| 54 | {'$lgt_cpu_time'(Seconds2)}, |
|---|
| 55 | Looptime is Seconds2 - Seconds1, |
|---|
| 56 | {'$lgt_cpu_time'(Seconds3)}, |
|---|
| 57 | do_benchmark(Id, N), |
|---|
| 58 | {'$lgt_cpu_time'(Seconds4)}, |
|---|
| 59 | Goaltime is Seconds4 - Seconds3, |
|---|
| 60 | Average is (Goaltime - Looptime)/N, |
|---|
| 61 | Speed is 1.0/Average. |
|---|
| 62 | |
|---|
| 63 | report(Id, Goal, N, Looptime, Goaltime, Average, Speed) :- |
|---|
| 64 | write(Id), write(': '), |
|---|
| 65 | writeq(Goal), nl, |
|---|
| 66 | write('Number of repetitions: '), write(N), nl, |
|---|
| 67 | write('Loop time: '), write(Looptime), nl, |
|---|
| 68 | write('Goal time: '), write(Goaltime), nl, |
|---|
| 69 | write('Average time per call: '), write(Average), nl, |
|---|
| 70 | write('Number of calls per second: '), write(Speed), nl, |
|---|
| 71 | nl. |
|---|
| 72 | |
|---|
| 73 | % some benchmark tests for static code: |
|---|
| 74 | benchmark(s1, my_length(List, _)) :- |
|---|
| 75 | {generate_list(20, List)}. |
|---|
| 76 | benchmark(s2, object::length(List, _)) :- |
|---|
| 77 | {generate_list(20, List)}. |
|---|
| 78 | |
|---|
| 79 | % some benchmark tests for category predicate calls: |
|---|
| 80 | benchmark(c1, leaf::obj_local). |
|---|
| 81 | benchmark(c2, leaf::ctg_direct). |
|---|
| 82 | benchmark(c3, leaf::ctg_self). |
|---|
| 83 | |
|---|
| 84 | % some benchmark tests for dynamic code: |
|---|
| 85 | benchmark(d1, (create_object(xpto, [], [], []), abolish_object(xpto))). |
|---|
| 86 | benchmark(d2, plain_dyndb(_)). |
|---|
| 87 | benchmark(d3, database::this_dyndb(_)). |
|---|
| 88 | benchmark(d4, database::self_dyndb(_)). |
|---|
| 89 | benchmark(d5, database::obj_dyndb(_)). |
|---|
| 90 | |
|---|
| 91 | % repeat a goal N times without using call/1 and using a failure-driven loop to |
|---|
| 92 | % avoid the interference of Prolog compiler memory management mechanism (such as |
|---|
| 93 | % garbage collection) on the results |
|---|
| 94 | do_benchmark(empty_loop, N) :- |
|---|
| 95 | {my_repeat(N)}, |
|---|
| 96 | fail. |
|---|
| 97 | do_benchmark(empty_loop, _). |
|---|
| 98 | |
|---|
| 99 | do_benchmark(s1, N) :- |
|---|
| 100 | {generate_list(20, List)}, |
|---|
| 101 | {my_repeat(N)}, |
|---|
| 102 | {my_length(List, _)}, |
|---|
| 103 | fail. |
|---|
| 104 | do_benchmark(s1, _). |
|---|
| 105 | |
|---|
| 106 | do_benchmark(s2, N) :- |
|---|
| 107 | {generate_list(20, List)}, |
|---|
| 108 | {my_repeat(N)}, |
|---|
| 109 | object::length(List, _), |
|---|
| 110 | fail. |
|---|
| 111 | do_benchmark(s2, _). |
|---|
| 112 | |
|---|
| 113 | do_benchmark(c1, N) :- |
|---|
| 114 | {my_repeat(N)}, |
|---|
| 115 | leaf::obj_local, |
|---|
| 116 | fail. |
|---|
| 117 | do_benchmark(c1, _). |
|---|
| 118 | |
|---|
| 119 | do_benchmark(c2, N) :- |
|---|
| 120 | {my_repeat(N)}, |
|---|
| 121 | leaf::ctg_direct, |
|---|
| 122 | fail. |
|---|
| 123 | do_benchmark(c2, _). |
|---|
| 124 | |
|---|
| 125 | do_benchmark(c3, N) :- |
|---|
| 126 | {my_repeat(N)}, |
|---|
| 127 | leaf::ctg_self, |
|---|
| 128 | fail. |
|---|
| 129 | do_benchmark(c3, _). |
|---|
| 130 | |
|---|
| 131 | do_benchmark(d1, N) :- |
|---|
| 132 | {my_repeat(N)}, |
|---|
| 133 | create_object(xpto, [], [], []), |
|---|
| 134 | abolish_object(xpto), |
|---|
| 135 | fail. |
|---|
| 136 | do_benchmark(d1, _). |
|---|
| 137 | |
|---|
| 138 | do_benchmark(d2, N) :- |
|---|
| 139 | {my_repeat(N)}, |
|---|
| 140 | {plain_dyndb(N)}, |
|---|
| 141 | fail. |
|---|
| 142 | do_benchmark(d2, _). |
|---|
| 143 | |
|---|
| 144 | do_benchmark(d3, N) :- |
|---|
| 145 | {my_repeat(N)}, |
|---|
| 146 | database::this_dyndb(N), |
|---|
| 147 | fail. |
|---|
| 148 | do_benchmark(d3, _). |
|---|
| 149 | |
|---|
| 150 | do_benchmark(d4, N) :- |
|---|
| 151 | {my_repeat(N)}, |
|---|
| 152 | database::self_dyndb(N), |
|---|
| 153 | fail. |
|---|
| 154 | do_benchmark(d4, _). |
|---|
| 155 | |
|---|
| 156 | do_benchmark(d5, N) :- |
|---|
| 157 | {my_repeat(N)}, |
|---|
| 158 | database::obj_dyndb(N), |
|---|
| 159 | fail. |
|---|
| 160 | do_benchmark(d5, _). |
|---|
| 161 | |
|---|
| 162 | :- end_object. |
|---|