/** 
 * Copyright 2005-2007 Xue Yong Zhi
 * Distributed under the GNU General Public License 2.0
 */

package com.xruby;

import com.xruby.runtime.lang.*;
import com.xruby.runtime.value.*;

/*
 * A simple console program that test XRuby runtime's performance 
 */
public class TestingPerformance {

	private static final int LOOP = 30000000;
	
	private static long test_createFixnum() {
		long start = System.currentTimeMillis();
		for (int i = 0; i < LOOP; ++i) {
			ObjectFactory.createFixnum(i);
		}
		long end = System.currentTimeMillis();
		return end - start;
	}
	
	private static long test_createString() {
		long start = System.currentTimeMillis();
		for (int i = 0; i < LOOP; ++i) {
			ObjectFactory.createString("xxxxx");
		}
		long end = System.currentTimeMillis();
		return end - start;
	}
	
	private static long test_createArray() {
		long start = System.currentTimeMillis();
		for (int i = 0; i < LOOP; ++i) {
			new RubyArray();
		}
		long end = System.currentTimeMillis();
		return end - start;
	}
	
	private static long test_findMethod() {
		long start = System.currentTimeMillis();
		for (int i = 0; i < LOOP; ++i) {
			ObjectFactory.fixnum1.findMethod("+");
		}
		long end = System.currentTimeMillis();
		return end - start;
	}
	
	private static long test_invokeMethod() {
		RubyMethod m = ObjectFactory.fixnum1.findMethod("+");
		
		long start = System.currentTimeMillis();
		for (int i = 0; i < LOOP; ++i) {
			m.invoke(ObjectFactory.fixnum1, new RubyArray(ObjectFactory.fixnum1), null);
		}
		long end = System.currentTimeMillis();
		return end - start;
	}
	
	private static long test_callMethod() {
		long start = System.currentTimeMillis();
		for (int i = 0; i < 30000000; ++i) {
			RubyAPI.callMethod(ObjectFactory.fixnum1, ObjectFactory.fixnum1, "+");
		}
		long end = System.currentTimeMillis();
		return end - start;
	}
	
	/* Sample result:
	Fixnum Creation: 897
	String Creation: 3211
	Array Creation: 2003
	Method Finding: 3987
	Method Invoking: 4066
	Method Calling: 12585
	*/
	public static void main(String[] args) {
		System.out.println("Fixnum Creation: " + test_createFixnum());
		System.out.println("String Creation: " + test_createString());
		System.out.println("Array Creation: " + test_createArray());
		System.out.println("Method Finding: " + test_findMethod());
		System.out.println("Method Invoking: " + test_invokeMethod());
		System.out.println("Method Calling: " + test_callMethod());
	}

}

@@ -972,20 +972,41 @@
 byArray(
+ObjectFactory.fixnum1
 );%0D%0A
-
 %09%09%7D%0D%0A%09%09l
@@ -1417,16 +1417,74 @@
 (%22+%22);%0D%0A
+%09%09RubyArray args = new RubyArray(ObjectFactory.fixnum1);%0D%0A
 %09%09%0D%0A%09%09lo
@@ -1598,44 +1598,12 @@
 m1, 
-new RubyArray(ObjectFactory.fixnum1)
+args
 , nu
@@ -2005,11 +2005,11 @@
 on: 
-8
 97
+5
 %0D%0A%09S
@@ -2029,10 +2029,10 @@
 n: 3
-21
+47
 1%0D%0A%09
@@ -2052,11 +2052,11 @@
 n: 2
-003
+605
 %0D%0A%09M
@@ -2073,17 +2073,17 @@
 ing: 398
-7
+9
 %0D%0A%09Metho
@@ -2098,12 +2098,12 @@
 ng: 
-4066
+1683
 %0D%0A%09M
@@ -2122,12 +2122,12 @@
 g: 1
-2585
+0296
 %0D%0A%09*

update test