view src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/xml/TransactionPerSecondMeasurement.java @ 20:195c1a644550

benchmark add
author tatsuki
date Tue, 17 Mar 2015 15:37:19 +0900
parents 17cc11b04157
children c8d8864714d4
line wrap: on
line source

package jp.ac.u_ryukyu.ie.cr.tatsuki.xml;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.ByteBuffer;
import java.util.Random;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import jp.ac.u_ryukyu.cr.ie.tatsuki.xmlTestBbs.JuGrix;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTreeEditor;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath;

public class TransactionPerSecondMeasurement {

  public static void main(String[] args) throws InterruptedException, IOException, ParserConfigurationException, SAXException {
    System.out.println("endCreatejugrix");
    JuGrix jugrix = extendXmlTree.createJuGrix(true,400);
     System.out.println(Runtime.getRuntime().availableProcessors());

    if (args.length > 0) {
      
      if (args[0].equals("read"))
        readOnly(jugrix);
      if (args[0].equals("write"))
        readAndWrite(jugrix);

    } else
      System.out.println("Please with the argument");
  }

  private static void readAndWrite(JuGrix jugrix) throws InterruptedException, IOException {
    System.out.println("read and write");
    Runtime rt = Runtime.getRuntime();
    int cpuNum = rt.availableProcessors();
    
    File file = new File("./time/newTransactionReadAndWrite");
    PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file)));

    for (int THREAD_COUNT = 1; THREAD_COUNT <= cpuNum; THREAD_COUNT++) {
      ReadJungleThread readThread[] = new ReadJungleThread[THREAD_COUNT];
      for (int count = 0; THREAD_COUNT > count; count++) {
        readThread[count] = new ReadJungleThread(jugrix);
      }

      WriteJungleThread writeThread =  new WriteJungleThread(jugrix);
      writeThread.start();
      for (int count = 0; THREAD_COUNT > count; count++) {
        readThread[count].start();
      }

      System.out.println("StartThread");

      Thread.sleep(1000);
      writeThread.set(false);
      int readCount = 0;

      for (int count = 0; THREAD_COUNT > count; count++) {
        readCount = readCount + readThread[count].getFindCount();
        readThread[count].set(false);
      }


      pw.println(THREAD_COUNT + " " + readCount);
      System.out.println(THREAD_COUNT + "readCount = " + readCount);
      Thread.sleep(1000);
    }
    pw.close();
  }

  private static void readOnly(JuGrix jugrix) throws InterruptedException, IOException {
    System.out.println("read only");
    Runtime rt = Runtime.getRuntime();
    int cpuNum = rt.availableProcessors();

    File file = new File("./time/readOnly");
    PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file)));
    for (int THREAD_COUNT = 1; THREAD_COUNT <= cpuNum; THREAD_COUNT++) {
      ReadJungleThread readThread[] = new ReadJungleThread[THREAD_COUNT];
      ;
      for (int count = 0; THREAD_COUNT > count; count++) {
        readThread[count] = new ReadJungleThread(jugrix);
      }

      for (int count = 0; THREAD_COUNT > count; count++) {
        readThread[count].start();
      }

      System.out.println("StartThread");

      Thread.sleep(1000);
      int readCount = 0;

      for (int count = 0; THREAD_COUNT > count; count++) {
        readCount = readCount + readThread[count].getFindCount();
        readThread[count].set(false);
      }
      
      pw.println(THREAD_COUNT + " " + readCount);
      System.out.println(THREAD_COUNT + "readCount = " + readCount);
    }
    
    pw.close();
  }

  static String key = "KEY";

  public static JungleTreeEditor createTree(int deep, NodePath path, JungleTreeEditor editor) {

    Random rnd = new Random();
    String value1 = String.valueOf(rnd.nextInt(1000));
    String value2 = String.valueOf(rnd.nextInt(1000));
    editor = editor.addNewChildAt(path, 0).b();
    editor = editor.putAttribute(path.add(0), key, ByteBuffer.wrap(value1.getBytes())).b();
    editor = editor.addNewChildAt(path, 1).b();
    editor = editor.putAttribute(path.add(0), key, ByteBuffer.wrap(value2.toString().getBytes())).b();
    deep++;

    if (deep < 12) {
      editor = createTree(deep, path.add(0), editor);
      editor = createTree(deep, path.add(1), editor);
    }
    return editor;
  }
}