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

Lucene example source code file (TimeData.java)

This example Lucene source code file (TimeData.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

object, override, override, string, string, stringbuilder, stringbuilder, timedata, timedata

The Lucene TimeData.java source code

package org.apache.lucene.benchmark.stats;
/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed 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.
 */


/**
 * This class holds a data point measuring speed of processing.
 *
 */
public class TimeData {
  /** Name of the data point - usually one of a data series with the same name */
  public String name;
  /** Number of records processed. */
  public long count = 0;
  /** Elapsed time in milliseconds. */
  public long elapsed = 0L;

  private long delta = 0L;
  /** Free memory at the end of measurement interval. */
  public long freeMem = 0L;
  /** Total memory at the end of measurement interval. */
  public long totalMem = 0L;

  public TimeData() {}

  public TimeData(String name) {
    this.name = name;
  }

  /** Start counting elapsed time. */
  public void start() {
    delta = System.currentTimeMillis();
  }

  /** Stop counting elapsed time. */
  public void stop() {
    count++;
    elapsed += (System.currentTimeMillis() - delta);
  }

  /** Record memory usage. */
  public void recordMemUsage() {
    freeMem = Runtime.getRuntime().freeMemory();
    totalMem = Runtime.getRuntime().totalMemory();
  }

  /** Reset counters. */
  public void reset() {
    count = 0;
    elapsed = 0L;
    delta = elapsed;
  }

  @Override
  protected Object clone() {
    TimeData td = new TimeData(name);
    td.name = name;
    td.elapsed = elapsed;
    td.count = count;
    td.delta = delta;
    td.freeMem = freeMem;
    td.totalMem = totalMem;
    return td;
  }

  /** Get rate of processing, defined as number of processed records per second. */
  public double getRate() {
    double rps = count * 1000.0 / (elapsed > 0 ? elapsed : 1); // assume at least 1ms for any countable op
    return rps;
  }

  /** Get a short legend for toString() output. */
  public static String getLabels() {
    return "# count\telapsed\trec/s\tfreeMem\ttotalMem";
  }

  @Override
  public String toString() { return toString(true); }
  /**
   * Return a tab-separated string containing this data.
   * @param withMem if true, append also memory information
   * @return The String
   */
  public String toString(boolean withMem) {
    StringBuilder sb = new StringBuilder();
    sb.append(count + "\t" + elapsed + "\t" + getRate());
    if (withMem) sb.append("\t" + freeMem + "\t" + totalMem);
    return sb.toString();
  }
}

Other Lucene examples (source code examples)

Here is a short list of links related to this Lucene TimeData.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.