<commit_before>package net.spy.memcached;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import junit.framework.TestCase;

/**
 * Adaptation of http://code.google.com/p/spcached/wiki/benchmarktool
 */
public class MemcachedThreadBench extends TestCase {

	private static class WorkerStat {
		public int start, runs;

		public long setterTime, getterTime;

		public WorkerStat() {
			start = runs = 0;
			setterTime = getterTime = 0;
		}
	}

	public void testCrap() throws Exception {
		main(new String[]{"1000", "100", "11211", "100"});
	}

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

		if (args.length != 4) {
			System.out.println("Usage: java "
					+ MemcachedThreadBench.class.getName()
					+ " <runs> <start> <port> <threads>");
			System.exit(1);
		}

		int runs = Integer.parseInt(args[0]);
		int start = Integer.parseInt(args[1]);
		String serverlist =  "127.0.0.1:" + args[2];
		int threads = Integer.parseInt(args[3]);

		MemcachedClient client=new MemcachedClient(
			new DefaultConnectionFactory(100000, 32768),
			AddrUtil.getAddresses(serverlist));

		WorkerStat[] statArray = new WorkerStat[threads];
		Thread[] threadArray = new Thread[threads];

		WorkerStat mainStat = new WorkerStat();
		mainStat.runs = runs * threads;

		long begin = System.currentTimeMillis();

		for (int i = 0; i < threads; i++) {
			statArray[i] = new WorkerStat();
			statArray[i].start = start + i * runs;
			statArray[i].runs = runs;
			threadArray[i] = new SetterThread(client, statArray[i]);
			threadArray[i].start();
		}

		for (int i = 0; i < threads; i++) {
			threadArray[i].join();
		}

		mainStat.setterTime = System.currentTimeMillis() - begin;

		begin = System.currentTimeMillis();

		for (int i = 0; i < threads; i++) {
			threadArray[i] = new GetterThread(client, statArray[i]);
			threadArray[i].start();
		}

		for (int i = 0; i < threads; i++) {
			threadArray[i].join();
		}

		mainStat.getterTime = System.currentTimeMillis() - begin;

		client.shutdown();

		WorkerStat totalStat = new WorkerStat();

		System.out.println("Thread\tstart\truns\tset time(ms)\tget time(ms)");
		for (int i = 0; i < threads; i++) {
			System.out.println("" + i + "\t" + statArray[i].start + "\t"
					+ statArray[i].runs + "\t" + statArray[i].setterTime
					+ "\t\t" + statArray[i].getterTime);

			totalStat.runs = totalStat.runs + statArray[i].runs;
			totalStat.setterTime = totalStat.setterTime
					+ statArray[i].setterTime;
			totalStat.getterTime = totalStat.getterTime
					+ statArray[i].getterTime;
		}

		System.out.println("\nAvg\t\t" + runs + "\t" + totalStat.setterTime
				/ threads + "\t\t" + totalStat.getterTime / threads);

		System.out.println("\nTotal\t\t" + totalStat.runs + "\t"
				+ totalStat.setterTime + "\t\t" + totalStat.getterTime);
		System.out.println("\tReqPerSecond\tset - " + 1000 * totalStat.runs
				/ totalStat.setterTime + "\tget - " + 1000 * totalStat.runs
				/ totalStat.getterTime);

		System.out.println("\nMain\t\t" + mainStat.runs + "\t"
				+ mainStat.setterTime + "\t\t" + mainStat.getterTime);
		System.out.println("\tReqPerSecond\tset - " + 1000 * mainStat.runs
				/ mainStat.setterTime + "\tget - " + 1000 * mainStat.runs
				/ mainStat.getterTime);
	}

	private static class SetterThread extends Thread {
		private static final AtomicInteger total=new AtomicInteger(0);
		private static final int MAX_QUEUE=10000;
		private final MemcachedClient mc;
		private WorkerStat stat;

		SetterThread(MemcachedClient c, WorkerStat stat) {
			this.stat = stat;
			mc=c;
		}

		@Override
		public void run() {
			String keyBase = "testKey";
			String object = "This is a test of an object blah blah es, "
					+ "serialization does not seem to slow things down so much.  "
					+ "The gzip compression is horrible horrible performance, "
					+ "so we only use it for very large objects.  "
					+ "I have not done any heavy benchmarking recently";

			long begin = System.currentTimeMillis();
			for (int i = stat.start; i < stat.start + stat.runs; i++) {
				mc.set("" + i + keyBase, 3600, object);
				if(total.incrementAndGet() >= MAX_QUEUE) {
					flush();
				}
			}
			long end = System.currentTimeMillis();

			stat.setterTime = end - begin;
		}

		private synchronized void flush() {
			if(total.intValue() >= MAX_QUEUE) {
				mc.waitForQueues(5, TimeUnit.SECONDS);
				total.set(0);
			}
		}
	}

	private static class GetterThread extends Thread {
		private final MemcachedClient mc;
		private WorkerStat stat;

		GetterThread(MemcachedClient c, WorkerStat stat) {
			this.stat = stat;
			mc=c;
		}

		@Override
		public void run() {
			String keyBase = "testKey";<commit_msg>	Fix a warning in a test.<commit_after>package net.spy.memcached;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import junit.framework.TestCase;

/**
 * Adaptation of http://code.google.com/p/spcached/wiki/benchmarktool
 */
public class MemcachedThreadBench extends TestCase {

	private static class WorkerStat {
		public int start, runs;

		public long setterTime, getterTime;

		public WorkerStat() {
			start = runs = 0;
			setterTime = getterTime = 0;
		}
	}

	public void testCrap() throws Exception {
		main(new String[]{"1000", "100", "11211", "100"});
	}

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

		if (args.length != 4) {
			System.out.println("Usage: java "
					+ MemcachedThreadBench.class.getName()
					+ " <runs> <start> <port> <threads>");
			System.exit(1);
		}

		int runs = Integer.parseInt(args[0]);
		int start = Integer.parseInt(args[1]);
		String serverlist =  "127.0.0.1:" + args[2];
		int threads = Integer.parseInt(args[3]);

		MemcachedClient client=new MemcachedClient(
			new DefaultConnectionFactory(100000, 32768),
			AddrUtil.getAddresses(serverlist));

		WorkerStat[] statArray = new WorkerStat[threads];
		Thread[] threadArray = new Thread[threads];

		WorkerStat mainStat = new WorkerStat();
		mainStat.runs = runs * threads;

		long begin = System.currentTimeMillis();

		for (int i = 0; i < threads; i++) {
			statArray[i] = new WorkerStat();
			statArray[i].start = start + i * runs;
			statArray[i].runs = runs;
			threadArray[i] = new SetterThread(client, statArray[i]);
			threadArray[i].start();
		}

		for (int i = 0; i < threads; i++) {
			threadArray[i].join();
		}

		mainStat.setterTime = System.currentTimeMillis() - begin;

		begin = System.currentTimeMillis();

		for (int i = 0; i < threads; i++) {
			threadArray[i] = new GetterThread(client, statArray[i]);
			threadArray[i].start();
		}

		for (int i = 0; i < threads; i++) {
			threadArray[i].join();
		}

		mainStat.getterTime = System.currentTimeMillis() - begin;

		client.shutdown();

		WorkerStat totalStat = new WorkerStat();

		System.out.println("Thread\tstart\truns\tset time(ms)\tget time(ms)");
		for (int i = 0; i < threads; i++) {
			System.out.println("" + i + "\t" + statArray[i].start + "\t"
					+ statArray[i].runs + "\t" + statArray[i].setterTime
					+ "\t\t" + statArray[i].getterTime);

			totalStat.runs = totalStat.runs + statArray[i].runs;
			totalStat.setterTime = totalStat.setterTime
					+ statArray[i].setterTime;
			totalStat.getterTime = totalStat.getterTime
					+ statArray[i].getterTime;
		}

		System.out.println("\nAvg\t\t" + runs + "\t" + totalStat.setterTime
				/ threads + "\t\t" + totalStat.getterTime / threads);

		System.out.println("\nTotal\t\t" + totalStat.runs + "\t"
				+ totalStat.setterTime + "\t\t" + totalStat.getterTime);
		System.out.println("\tReqPerSecond\tset - " + 1000 * totalStat.runs
				/ totalStat.setterTime + "\tget - " + 1000 * totalStat.runs
				/ totalStat.getterTime);

		System.out.println("\nMain\t\t" + mainStat.runs + "\t"
				+ mainStat.setterTime + "\t\t" + mainStat.getterTime);
		System.out.println("\tReqPerSecond\tset - " + 1000 * mainStat.runs
				/ mainStat.setterTime + "\tget - " + 1000 * mainStat.runs
				/ mainStat.getterTime);
	}

	private static class SetterThread extends Thread {
		private static final AtomicInteger total=new AtomicInteger(0);
		private static final int MAX_QUEUE=10000;
		private final MemcachedClient mc;
		private final WorkerStat stat;

		SetterThread(MemcachedClient c, WorkerStat st) {
			stat = st;
			mc=c;
		}

		@Override
		public void run() {
			String keyBase = "testKey";
			String object = "This is a test of an object blah blah es, "
					+ "serialization does not seem to slow things down so much.  "
					+ "The gzip compression is horrible horrible performance, "
					+ "so we only use it for very large objects.  "
					+ "I have not done any heavy benchmarking recently";

			long begin = System.currentTimeMillis();
			for (int i = stat.start; i < stat.start + stat.runs; i++) {
				mc.set("" + i + keyBase, 3600, object);
				if(total.incrementAndGet() >= MAX_QUEUE) {
					flush();
				}
			}
			long end = System.currentTimeMillis();

			stat.setterTime = end - begin;
		}

		private synchronized void flush() {
			if(total.intValue() >= MAX_QUEUE) {
				mc.waitForQueues(5, TimeUnit.SECONDS);
				total.set(0);
			}
		}
	}

	private static class GetterThread extends Thread {
		private final MemcachedClient mc;
		private final WorkerStat stat;

		GetterThread(MemcachedClient c, WorkerStat st) {
			stat = st;
			mc=c;
		}

		@Override
		public void run() {
			String keyBase = "testKey";
{'fraction_non_alphanumeric': 0.14211087420042645, 'fraction_numerical': 0.01556503198294243, 'mean_word_length': 3.4417613636363638, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '1884085', 'n_tokens_mistral': 3753, 'n_tokens_neox': 3184, 'n_words': 755}