alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Lucene example source code file (BenchmarkTestCase.java)

This example Lucene source code file (BenchmarkTestCase.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Lucene tags/keywords

benchmark, benchmark, benchmarktestcase, exception, file, file, fileoutputstream, inputstream, io, ioexception, outputstream, string, string, stringbuilder, stringbuilder

The Lucene BenchmarkTestCase.java source code

package org.apache.lucene.benchmark;

/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringReader;

import org.apache.lucene.benchmark.byTask.Benchmark;
import org.apache.lucene.util.LuceneTestCase;

/** Base class for all Benchmark unit tests. */
public abstract class BenchmarkTestCase extends LuceneTestCase {
  
  public File getWorkDir() {
    return TEMP_DIR;
  }
  
  /** Copy a resource into the workdir */
  public void copyToWorkDir(String resourceName) throws IOException {
    InputStream resource = getClass().getResourceAsStream(resourceName);
    OutputStream dest = new FileOutputStream(new File(getWorkDir(), resourceName));
    byte[] buffer = new byte[8192];
    int len;
    
    while ((len = resource.read(buffer)) > 0) {
        dest.write(buffer, 0, len);
    }

    resource.close();
    dest.close();
  }
  
  /** Return a path, suitable for a .alg config file, for a resource in the workdir */
  public String getWorkDirResourcePath(String resourceName) {
    return new File(getWorkDir(), resourceName).getAbsolutePath().replace("\\", "/");
  }
  
  /** Return a path, suitable for a .alg config file, for the workdir */
  public String getWorkDirPath() {
    return getWorkDir().getAbsolutePath().replace("\\", "/");
  }
  
  // create the benchmark and execute it. 
  public Benchmark execBenchmark(String[] algLines) throws Exception {
    String algText = algLinesToText(algLines);
    logTstLogic(algText);
    Benchmark benchmark = new Benchmark(new StringReader(algText));
    benchmark.execute();
    return benchmark;
  }
  
  // properties in effect in all tests here
  final String propLines [] = {
    "work.dir=" + getWorkDirPath(),
    "directory=RAMDirectory",
    "print.props=false",
  };
  
  static final String NEW_LINE = System.getProperty("line.separator");
  
  // catenate alg lines to make the alg text
  private String algLinesToText(String[] algLines) {
    String indent = "  ";
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < propLines.length; i++) {
      sb.append(indent).append(propLines[i]).append(NEW_LINE);
    }
    for (int i = 0; i < algLines.length; i++) {
      sb.append(indent).append(algLines[i]).append(NEW_LINE);
    }
    return sb.toString();
  }

  private static void logTstLogic (String txt) {
    if (!VERBOSE) 
      return;
    System.out.println("Test logic of:");
    System.out.println(txt);
  }

}

Other Lucene examples (source code examples)

Here is a short list of links related to this Lucene BenchmarkTestCase.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.