solr 从索引随机读性能测试

本文是对这篇文章中的一个测试: 内存缓存与硬盘缓存访问速度的比较

数据准备代码:

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.payloads.PayloadHelper;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;

public class SolrTest {

	public static void main(String[] args) throws IOException {

		Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);

		RandomAccessFile randomFile = new RandomAccessFile(new File(
				"DocReplication.text"), "rw");

		Directory dir = new SimpleFSDirectory(new File("indexdir"));

		IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_34,
				analyzer);
		iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
		IndexWriter writer = new IndexWriter(dir, iwc);

		for (int i = 0; i < 2000000; i++) {
			// 向一个随机访问文件中写
			randomFile.write(PayloadHelper.encodeInt(i));
			randomFile.write(long2Array(i + 1));
			randomFile.write(long2Array(i + 2));
			// 向lucene中document中写
			Document doc = new Document();
			doc.add(new Field("id", String.valueOf(i), Field.Store.YES,
					Field.Index.NOT_ANALYZED_NO_NORMS));
			doc.add(new Field("id2", String.valueOf(i), Field.Store.YES,
					Field.Index.NOT_ANALYZED_NO_NORMS));
			writer.addDocument(doc);
			System.out.println("point:" + randomFile.getFilePointer());
		}
		writer.commit();
		writer.close();
		randomFile.close();

	}

	static byte[] long2Array(long val) {

		int off = 0;
		byte[] b = new byte[8];
		b[off + 7] = (byte) (val >>> 0);
		b[off + 6] = (byte) (val >>> 8);
		b[off + 5] = (byte) (val >>> 16);
		b[off + 4] = (byte) (val >>> 24);
		b[off + 3] = (byte) (val >>> 32);
		b[off + 2] = (byte) (val >>> 40);
		b[off + 1] = (byte) (val >>> 48);
		b[off + 0] = (byte) (val >>> 56);
		return b;

	}
}

查询测试:

public static void test1() throws Exception {
		RandomAccessFile randomFile = new RandomAccessFile(new File(
				"/home/yunpeng/ali_workspace2/dtrace/DocReplication.text"), "rw");

		long current = System.currentTimeMillis();

		for (int i = 0; i < 100000; i++) {
			int docid = (int) (Math.random() * 2000000);
			randomFile.seek(docid * 20 + 4);

			randomFile.readLong();
			randomFile.readLong();

		}

		System.out.println((System.currentTimeMillis() - current));

		randomFile.close();
	}

	public static void test2() throws Exception {
//		Directory dir = new SimpleFSDirectory(new File("/home/yunpeng/ali_workspace2/dtrace/indexdir"));
		Directory dir = new MMapDirectory(new File("/home/yunpeng/ali_workspace2/dtrace/indexdir"));
		long start = System.currentTimeMillis();
		IndexReader reader = IndexReader.open(dir);
		Document doc = null;
		for (int i = 0; i < 100000; i++) {
			int docid = (int) (Math.random() * 2000000);
			doc = reader.document(docid);
			doc.get("id");
			doc.get("id2");
		}

		System.out.println("consume:" + (System.currentTimeMillis() - start));
	}

	public static void main(String[] args) throws Exception {
		for (int i = 0; i < 10; ++i) {
			test2();
		}
	}

 这里比较了SimpleFSDirectory,MMapDirectory,RandomAccessFile的性能。

MMapDirectory:

consume:916
consume:270
consume:179
consume:169
consume:143
consume:151
consume:157
consume:172
consume:138
consume:140

 SimpleFSDirectory

consume:1083
consume:443
consume:381
consume:377
consume:393
consume:374
consume:397
consume:364
consume:360
consume:395

 RandomAccessFile

684
664
639
645
639
651
639
689
643
652

猜你喜欢

转载自san-yun.iteye.com/blog/2024653