001: package org.apache.lucene.benchmark.byTask.tasks;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import org.apache.lucene.benchmark.byTask.PerfRunData;
021: import org.apache.lucene.benchmark.byTask.feeds.DocMaker;
022: import org.apache.lucene.document.Document;
023: import java.text.NumberFormat;
024:
025: /**
026: * Add a document, optionally with of a certain size.
027: * <br>Other side effects: none.
028: * <br>Relevant properties: <code>doc.add.log.step</code>.
029: * <br>Takes optional param: document size.
030: */
031: public class AddDocTask extends PerfTask {
032:
033: /**
034: * Default value for property <code>doc.add.log.step<code> - indicating how often
035: * an "added N docs" message should be logged.
036: */
037: public static final int DEFAULT_ADD_DOC_LOG_STEP = 500;
038:
039: public AddDocTask(PerfRunData runData) {
040: super (runData);
041: }
042:
043: private int logStep = -1;
044: private int docSize = 0;
045: int count = 0;
046:
047: // volatile data passed between setup(), doLogic(), tearDown().
048: private Document doc = null;
049:
050: /*
051: * (non-Javadoc)
052: * @see PerfTask#setup()
053: */
054: public void setup() throws Exception {
055: super .setup();
056: DocMaker docMaker = getRunData().getDocMaker();
057: if (docSize > 0) {
058: doc = docMaker.makeDocument(docSize);
059: } else {
060: doc = docMaker.makeDocument();
061: }
062: }
063:
064: /* (non-Javadoc)
065: * @see PerfTask#tearDown()
066: */
067: public void tearDown() throws Exception {
068: log(++count);
069: doc = null;
070: super .tearDown();
071: }
072:
073: public int doLogic() throws Exception {
074: getRunData().getIndexWriter().addDocument(doc);
075: return 1;
076: }
077:
078: private void log(int count) {
079: if (logStep < 0) {
080: // init once per instance
081: logStep = getRunData().getConfig().get("doc.add.log.step",
082: DEFAULT_ADD_DOC_LOG_STEP);
083: }
084: if (logStep > 0 && (count % logStep) == 0) {
085: double seconds = (System.currentTimeMillis() - getRunData()
086: .getStartTimeMillis()) / 1000.0;
087: NumberFormat nf = NumberFormat.getInstance();
088: nf.setMaximumFractionDigits(2);
089: System.out.println("--> " + nf.format(seconds) + " sec: "
090: + Thread.currentThread().getName()
091: + " processed (add) " + count + " docs");
092: }
093: }
094:
095: /**
096: * Set the params (docSize only)
097: * @param params docSize, or 0 for no limit.
098: */
099: public void setParams(String params) {
100: super .setParams(params);
101: docSize = (int) Float.parseFloat(params);
102: }
103:
104: /* (non-Javadoc)
105: * @see org.apache.lucene.benchmark.byTask.tasks.PerfTask#supportsParams()
106: */
107: public boolean supportsParams() {
108: return true;
109: }
110:
111: }
|