<commit_before>package org.drools.integrationtests;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StringReader;

import org.drools.RuleBase;
import org.drools.RuleBaseFactory;
import org.drools.compiler.DrlParser;
import org.drools.compiler.DroolsParserException;
import org.drools.compiler.PackageBuilder;
import org.drools.lang.descr.PackageDescr;
import org.drools.rule.Package;

import junit.framework.Assert;

/**
 * This generates a large number of rules (complex ones) and then times
 * compiling, serializing etc.
 */
public class LargeRuleBase {

	private static final int RULE_COUNT = 20000;

	public static void main(String[] args) throws Exception {
		System.err.println(Runtime.getRuntime().freeMemory());

		// bigBlobCompile();
		realisticSmallBlobCompile();
		System.gc();
		Thread.sleep(5000);
		System.err.println(Runtime.getRuntime().freeMemory());

	}

	private static void bigBlobCompile() throws DroolsParserException,
			IOException, Exception {
		StringBuffer buf = new StringBuffer();
		buf.append(getHeader());

		for (int i = 0; i < RULE_COUNT; i++) {
			String name = "x" + i;
			int status = i;

			String r = getTemplate1(name, status);
			buf.append(r);
		}

		/* love you */long time = System.currentTimeMillis();

		DrlParser ps = new DrlParser();
		PackageDescr pkg = ps.parse(new StringReader(buf.toString()));

		System.err.println("Time taken for parsing: "
				+ (System.currentTimeMillis() - time));

		time = System.currentTimeMillis();
		PackageBuilder b = new PackageBuilder();
		b.addPackage(pkg);
		Assert.assertFalse(b.getErrors().toString(), b.hasErrors());

		System.err.println("Time taken for compiling: "
				+ (System.currentTimeMillis() - time));
		time = System.currentTimeMillis();

		Package p = b.getPackage();
		RuleBase rb = RuleBaseFactory.newRuleBase();

		rb.addPackage(p);

		System.err.println("Time taken rete building: "
				+ (System.currentTimeMillis() - time));

		File f = new File("foo.rulebase");
		if (f.exists())
			f.delete();

		time = System.currentTimeMillis();
		ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(f));
		out.writeObject(rb);
		out.flush();
		out.close();
		System.err.println("Time taken serializing rulebase: "
				+ (System.currentTimeMillis() - time));

		time = System.currentTimeMillis();
		ObjectInputStream in = new ObjectInputStream(new FileInputStream(f));
		RuleBase rb_ = (RuleBase) in.readObject();
		System.err.println("Time taken de-serializing rulebase: "
				+ (System.currentTimeMillis() - time));

	}

	private static void smallBlobCompile() throws DroolsParserException,<commit_msg>using DroolsObjectOutputStream to generates bytes<commit_after>package org.drools.integrationtests;

import junit.framework.Assert;
import org.drools.RuleBase;
import org.drools.RuleBaseFactory;
import org.drools.common.DroolsObjectOutputStream;
import org.drools.compiler.DrlParser;
import org.drools.compiler.DroolsParserException;
import org.drools.compiler.PackageBuilder;
import org.drools.lang.descr.PackageDescr;
import org.drools.rule.Package;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.StringReader;

/**
 * This generates a large number of rules (complex ones) and then times
 * compiling, serializing etc.
 */
public class LargeRuleBase {

	private static final int RULE_COUNT = 20000;

	public static void main(String[] args) throws Exception {
		System.err.println(Runtime.getRuntime().freeMemory());

		// bigBlobCompile();
		realisticSmallBlobCompile();
		System.gc();
		Thread.sleep(5000);
		System.err.println(Runtime.getRuntime().freeMemory());

	}

	private static void bigBlobCompile() throws DroolsParserException,
			IOException, Exception {
		StringBuffer buf = new StringBuffer();
		buf.append(getHeader());

		for (int i = 0; i < RULE_COUNT; i++) {
			String name = "x" + i;
			int status = i;

			String r = getTemplate1(name, status);
			buf.append(r);
		}

		/* love you */long time = System.currentTimeMillis();

		DrlParser ps = new DrlParser();
		PackageDescr pkg = ps.parse(new StringReader(buf.toString()));

		System.err.println("Time taken for parsing: "
				+ (System.currentTimeMillis() - time));

		time = System.currentTimeMillis();
		PackageBuilder b = new PackageBuilder();
		b.addPackage(pkg);
		Assert.assertFalse(b.getErrors().toString(), b.hasErrors());

		System.err.println("Time taken for compiling: "
				+ (System.currentTimeMillis() - time));
		time = System.currentTimeMillis();

		Package p = b.getPackage();
		RuleBase rb = RuleBaseFactory.newRuleBase();

		rb.addPackage(p);

		System.err.println("Time taken rete building: "
				+ (System.currentTimeMillis() - time));

		File f = new File("foo.rulebase");
		if (f.exists())
			f.delete();

		time = System.currentTimeMillis();
		ObjectOutput out = new DroolsObjectOutputStream(new FileOutputStream(f));
		out.writeObject(rb);
		out.flush();
		out.close();
		System.err.println("Time taken serializing rulebase: "
				+ (System.currentTimeMillis() - time));

		time = System.currentTimeMillis();
		ObjectInputStream in = new ObjectInputStream(new FileInputStream(f));
		RuleBase rb_ = (RuleBase) in.readObject();
		System.err.println("Time taken de-serializing rulebase: "
				+ (System.currentTimeMillis() - time));

	}

	private static void smallBlobCompile() throws DroolsParserException,
{'fraction_non_alphanumeric': 0.12967581047381546, 'fraction_numerical': 0.003918774492340577, 'mean_word_length': 4.700507614213198, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7662736', 'n_tokens_mistral': 1870, 'n_tokens_neox': 1683, 'n_words': 397}