Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Speed |
|
| 2.1666666666666665;2.167 |
1 | /** | |
2 | * Distribution License: | |
3 | * JSword is free software; you can redistribute it and/or modify it under | |
4 | * the terms of the GNU Lesser General Public License, version 2.1 or later | |
5 | * as published by the Free Software Foundation. This program is distributed | |
6 | * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even | |
7 | * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
8 | * See the GNU Lesser General Public License for more details. | |
9 | * | |
10 | * The License is available on the internet at: | |
11 | * http://www.gnu.org/copyleft/lgpl.html | |
12 | * or by writing to: | |
13 | * Free Software Foundation, Inc. | |
14 | * 59 Temple Place - Suite 330 | |
15 | * Boston, MA 02111-1307, USA | |
16 | * | |
17 | * © CrossWire Bible Society, 2005 - 2016 | |
18 | * | |
19 | */ | |
20 | package org.crosswire.jsword.examples; | |
21 | ||
22 | import org.crosswire.common.util.Reporter; | |
23 | import org.crosswire.jsword.book.Book; | |
24 | import org.crosswire.jsword.book.BookData; | |
25 | import org.crosswire.jsword.book.BookException; | |
26 | import org.crosswire.jsword.index.search.DefaultSearchModifier; | |
27 | import org.crosswire.jsword.index.search.DefaultSearchRequest; | |
28 | import org.crosswire.jsword.passage.Key; | |
29 | import org.crosswire.jsword.passage.KeyUtil; | |
30 | import org.crosswire.jsword.passage.NoSuchKeyException; | |
31 | import org.crosswire.jsword.passage.Passage; | |
32 | import org.crosswire.jsword.passage.PassageTally; | |
33 | ||
34 | /** | |
35 | * Speed is a simple benchmark that tests how fast a version is. The current set | |
36 | * of tasks that we perform are rather arbitrary. But that is something that we | |
37 | * can improve on when we have more usage information. | |
38 | * | |
39 | * <p> | |
40 | * Progress report. All builds are Debug unless *ed: | |
41 | * | |
42 | * <pre> | |
43 | * Date Bible VM Time/s | |
44 | * 1999.12.08 Raw (Mem) HS 1.0.1 20 | |
45 | * 1999.12.08 Raw (Mem) MVM 5.00.3167 541 | |
46 | * 1999.12.09 Raw (Disk) HS 1.0.1 >600 | |
47 | * 1999.12.10 Ser HS 1.0.1 78 | |
48 | * 1999.12.11 Ser HS 1.0.1 6.7 | |
49 | * 1999.12.11 Raw (Mem) HS 1.0.1 11 | |
50 | * 1999.12.11 Raw (Disk) HS 1.0.1 1072 | |
51 | * 1999.12.11 Ser MVM 5.00.3167 8 | |
52 | * 1999.12.12 Ser HS 1.0.1 4 | |
53 | * 1999.12.12 Ser * HS 1.0.1 3 | |
54 | * </pre> | |
55 | * | |
56 | * </p> | |
57 | * | |
58 | * @see gnu.lgpl.License The GNU Lesser General Public License for details. | |
59 | * @author Joe Walker | |
60 | */ | |
61 | public class Speed implements Runnable { | |
62 | /** | |
63 | * Basic constructor | |
64 | * | |
65 | * @param book the book | |
66 | */ | |
67 | 0 | public Speed(Book book) { |
68 | 0 | this.book = book; |
69 | 0 | } |
70 | ||
71 | /** | |
72 | * This is what to call to execute a benchmark | |
73 | */ | |
74 | public void run() { | |
75 | try { | |
76 | 0 | startTime = System.currentTimeMillis(); |
77 | ||
78 | ||
79 | 0 | DefaultSearchModifier modifier = new DefaultSearchModifier(); |
80 | 0 | modifier.setRanked(true); |
81 | ||
82 | // If ranking see if the results are being limited. | |
83 | 0 | int rankCount = 35; |
84 | 0 | modifier.setMaxResults(rankCount); |
85 | ||
86 | // Part 1, a best match, and doc generate | |
87 | 0 | String param = "\"In the beginning god created the heavens and the earth\""; |
88 | 0 | Key results = book.find(new DefaultSearchRequest(param, modifier)); |
89 | ||
90 | // we should get PassageTallys for rank searches | |
91 | 0 | if (results instanceof PassageTally) { |
92 | 0 | PassageTally tally = (PassageTally) results; |
93 | 0 | tally.setOrdering(PassageTally.Order.TALLY); |
94 | 0 | tally.trimVerses(rankCount); |
95 | 0 | dummyDisplay(tally); |
96 | 0 | results = null; |
97 | } | |
98 | ||
99 | // Part 2, another best match, and doc generate | |
100 | 0 | param = "\"for god so loves the world that he gave his only begotten son\""; |
101 | 0 | if (results instanceof PassageTally) { |
102 | 0 | PassageTally tally = (PassageTally) results; |
103 | 0 | tally.setOrdering(PassageTally.Order.TALLY); |
104 | 0 | tally.trimVerses(rankCount); |
105 | 0 | dummyDisplay(tally); |
106 | 0 | results = null; |
107 | } | |
108 | ||
109 | // Part 3, a power match, and doc generate | |
110 | 0 | String nextInput = book.find("aaron & manna").getName(); |
111 | 0 | Key key = book.getKey(nextInput); |
112 | 0 | Passage ref = KeyUtil.getPassage(key); |
113 | 0 | ref.trimVerses(35); |
114 | 0 | dummyDisplay(ref); |
115 | 0 | ref = null; |
116 | ||
117 | 0 | endTime = System.currentTimeMillis(); |
118 | 0 | } catch (BookException ex) { |
119 | 0 | Reporter.informUser(this, ex); |
120 | 0 | } catch (NoSuchKeyException ex) { |
121 | 0 | Reporter.informUser(this, ex); |
122 | 0 | } |
123 | 0 | } |
124 | ||
125 | /** | |
126 | * Dummy display routine. We might want to add some XSL styling to this. | |
127 | * | |
128 | * @param ref | |
129 | * The passage to format for display | |
130 | * @throws BookException | |
131 | */ | |
132 | private void dummyDisplay(Passage ref) throws BookException { | |
133 | 0 | new BookData(book, ref).getOsisFragment(); |
134 | 0 | } |
135 | ||
136 | /** | |
137 | * Accessor for the speed of the test | |
138 | * | |
139 | * @return the benchmark for the test | |
140 | */ | |
141 | public long getBenchmark() { | |
142 | 0 | if (startTime == 0 || endTime == 0) { |
143 | 0 | throw new IllegalStateException("The benchmark has not finished yet."); |
144 | } | |
145 | ||
146 | 0 | return endTime - startTime; |
147 | } | |
148 | ||
149 | /** | |
150 | * Accessor for the version that we are testing | |
151 | * @return the book | |
152 | */ | |
153 | public Book getBook() { | |
154 | 0 | return book; |
155 | } | |
156 | ||
157 | /** | |
158 | * Accessor for the version that we are testing | |
159 | * | |
160 | * @param book the book | |
161 | */ | |
162 | public void setBook(Book book) { | |
163 | 0 | this.book = book; |
164 | 0 | } |
165 | ||
166 | /** | |
167 | * The start time of the benchmark | |
168 | */ | |
169 | private long startTime; | |
170 | ||
171 | /** | |
172 | * The end time of the benchmark | |
173 | */ | |
174 | private long endTime; | |
175 | ||
176 | /** | |
177 | * The version to test | |
178 | */ | |
179 | private Book book; | |
180 | } |