GenYaceFiles.java 3.68 KB
package com.yoho.search.restapi.tools;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang.StringUtils;

public class GenYaceFiles {

	private static final String brandIdFile = "C:/Users/gufei.hu/Desktop/双11压测/品牌ids.txt";
	private static final String piUrl = "http://10.66.4.30/yohosearch/pressProductIndex.json?";
	private static final String piBaseUrl = "http://10.66.4.30/yohosearch/pressProductBaseIndex.json?";

	private static final String productindexFile = "C:/Users/gufei.hu/Desktop/双11压测/yace/productindex_Rainbow_%s_%s_%s.txt";
	private static final String productindexBaseFile = "C:/Users/gufei.hu/Desktop/双11压测/yace/productindexbase_Rainbow_%s_%s_%s.txt";

	public static List<String> readbrandIds = new ArrayList<String>();

	static void initBrandIds() throws Exception {
		BufferedReader from = new BufferedReader(new FileReader(brandIdFile));
		String s = from.readLine();
		while (s != null) {
			readbrandIds.addAll(Arrays.asList(s.split(",")));
			s = from.readLine();
		}
		from.close();
		System.out.println("品牌id初始化完成:数量为" + readbrandIds.size());
	}

	public static String getRandomBrandIdCount(int size) {
		int totalSize = readbrandIds.size();
		List<String> results = new ArrayList<String>();
		while (results.size() < size) {
			int randomIndex = (int) (totalSize * Math.random());
			String brandId = readbrandIds.get(randomIndex);
			if (!results.contains(brandId)) {
				results.add(brandId);
			}
		}
		return StringUtils.join(results, ",");
	}

	public static class PressParam {
		String brandIds = "";
		String smallSortIds = "";
		int size = 0;
		String order = "firstShelveTime";
		boolean withRange = false;

		public String toString() {
			String results = "";
			results = results + "brandIds=" + brandIds;
			results = results + "&smallSortIds=" + smallSortIds;
			results = results + "&size=" + size;
			results = results + "&order=" + order;
			results = results + "&withRange=" + withRange;
			return results;
		}
	}

	public static String getPiUrl(PressParam pressParam) {
		return piUrl + pressParam.toString();
	}

	static int brandCount = 1;
	static String order = "firstShelveTime";
	//static String order = "random";
	//static String order = "salesPrice";
	static int size = 60;
	static int totalCount = 1000000;

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

		String piFile = String.format(productindexFile, brandCount, order, size);
		String piBaseFile = String.format(productindexBaseFile, brandCount, order, size);
		// 创建以及删除文件
		File f = new File(piFile);
		if (f.exists()) {
			f.delete();
		}
		f.createNewFile();

		// 创建以及删除文件
		File f1 = new File(piBaseFile);
		if (f1.exists()) {
			f1.delete();
		}
		f1.createNewFile();

		BufferedWriter piWriter = new BufferedWriter(new FileWriter(piFile));
		BufferedWriter piBaseWriter = new BufferedWriter(new FileWriter(piBaseFile));

		for (int i = 0; i < totalCount; i++) {
			//String brandIds = getRandomBrandIdCount(brandCount);
			PressParam pressParam = new PressParam();
			pressParam.brandIds = "514";
			pressParam.order = order;
			pressParam.size = size;

			String productindexUrl = piUrl + pressParam.toString();
			String productindexBaseUrl = piBaseUrl + pressParam.toString();

			piWriter.write(productindexUrl);// 写入文件
			piWriter.newLine();// 换行

			piBaseWriter.write(productindexBaseUrl);// 写入文件
			piBaseWriter.newLine();// 换行
		}
		piWriter.close();
		piBaseWriter.close();
		System.out.println("finish....");
	}
}