1 package org.apache.lucene.benchmark.byTask.tasks;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import java.lang.reflect.Constructor;
21 import java.lang.reflect.Method;
22 import java.util.Locale;
23 import java.util.StringTokenizer;
25 import org.apache.lucene.analysis.Analyzer;
26 import org.apache.lucene.benchmark.byTask.PerfRunData;
29 * Task to support benchmarking collation.
32 * <li> <code>NewCollationAnalyzer</code> with the default jdk impl
33 * <li> <code>NewCollationAnalyzer(impl:icu)</code> specify an impl (jdk,icu)
37 public class NewCollationAnalyzerTask extends PerfTask {
38 public enum Implementation {
39 JDK("org.apache.lucene.collation.CollationKeyAnalyzer",
40 "java.text.Collator"),
41 ICU("org.apache.lucene.collation.ICUCollationKeyAnalyzer",
42 "com.ibm.icu.text.Collator");
45 String collatorClassName;
47 Implementation(String className, String collatorClassName) {
48 this.className = className;
49 this.collatorClassName = collatorClassName;
53 private Implementation impl = Implementation.JDK;
55 public NewCollationAnalyzerTask(PerfRunData runData) {
59 static Analyzer createAnalyzer(Locale locale, Implementation impl)
61 final Class<?> collatorClazz = Class.forName(impl.collatorClassName);
62 Method collatorMethod = collatorClazz.getMethod("getInstance",
63 new Class[] {Locale.class});
64 Object collator = collatorMethod.invoke(null, locale);
66 final Class<? extends Analyzer> clazz = Class.forName(impl.className)
67 .asSubclass(Analyzer.class);
68 Constructor<? extends Analyzer> ctor = clazz.getConstructor(collatorClazz);
69 return ctor.newInstance(collator);
73 public int doLogic() throws Exception {
75 Locale locale = getRunData().getLocale();
76 if (locale == null) throw new RuntimeException(
77 "Locale must be set with the NewLocale task!");
78 Analyzer analyzer = createAnalyzer(locale, impl);
79 getRunData().setAnalyzer(analyzer);
80 System.out.println("Changed Analyzer to: "
81 + analyzer.getClass().getName() + "(" + locale + ")");
82 } catch (Exception e) {
83 throw new RuntimeException("Error creating Analyzer: impl=" + impl, e);
89 public void setParams(String params) {
90 super.setParams(params);
92 StringTokenizer st = new StringTokenizer(params, ",");
93 while (st.hasMoreTokens()) {
94 String param = st.nextToken();
95 StringTokenizer expr = new StringTokenizer(param, ":");
96 String key = expr.nextToken();
97 String value = expr.nextToken();
98 // for now we only support the "impl" parameter.
99 // TODO: add strength, decomposition, etc
100 if (key.equals("impl")) {
101 if (value.equalsIgnoreCase("icu"))
102 impl = Implementation.ICU;
103 else if (value.equalsIgnoreCase("jdk"))
104 impl = Implementation.JDK;
106 throw new RuntimeException("Unknown parameter " + param);
108 throw new RuntimeException("Unknown parameter " + param);
114 public boolean supportsParams() {