<commit_before>package org.wso2.throttle;

import org.apache.log4j.Logger;
import org.junit.Test;
import org.wso2.carbon.databridge.core.exception.DataBridgeException;
import org.wso2.carbon.databridge.core.exception.StreamDefinitionStoreException;
import org.wso2.throttle.core.Request;
import org.wso2.throttle.core.Throttler;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class BasicTest {
    private static final Logger log = Logger.getLogger(BasicTest.class);

    @Test
    public void testRule1()
            throws InterruptedException, DataBridgeException, StreamDefinitionStoreException,
                   IOException {
        Throttler throttler = Throttler.getInstance();
        throttler.start();

        throttler.addRule("bronze", null, null);
        throttler.addRule("silver", null, null);
        throttler.addRule("gold", null, null);
        long startTime = System.nanoTime();
        Request request;
        for (int i = 0; i < 1; i++) {
            request = new Request("gold", "somekey", "", "");
            throttler.isThrottled(request);
        }
        long endTime = System.nanoTime();
        log.info(endTime - startTime);

        Thread.sleep(10000);
        throttler.stop();
    }

    @Test
    public void testPerformance()
            throws InterruptedException, DataBridgeException, StreamDefinitionStoreException,
            IOException {
        int numOfThreads = 30;
        long numTasks = 800000;
        final Throttler throttler = Throttler.getInstance();
        final Request request = new Request("gold", "somekey", "", "");
        ThrottlingTask task = new ThrottlingTask(throttler, request);
        ExecutorService executorService = Executors.newFixedThreadPool(numOfThreads);

        throttler.start();
        throttler.addRule("bronze", null, null);
        throttler.addRule("silver", null, null);
        throttler.addRule("gold", null, null);

        long startTimeMillis = System.currentTimeMillis();
        for (int i = 0; i < numTasks; i++) {
            executorService.submit(task);
        }

        executorService.shutdown();
        executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);

        long diff = System.currentTimeMillis() - startTimeMillis;
        log.info("Throughput " + (numTasks * 1000 / diff));
        log.info("Latency " + (diff * 1.0 / numTasks + " ms"));
        log.info("Time in milli sec " + diff);
    }

    @Test
    public void testLatency() throws DataBridgeException, StreamDefinitionStoreException, IOException, InterruptedException {
        int numOfThreads = 3;
        int numTasks = 800000;
        int iterations = 10000;
        List<Long> resultList = new ArrayList<Long>(numTasks);
        final Request request = new Request("gold", "somekey", "", "");
        final Throttler throttler = Throttler.getInstance();
        ThrottlingTask task = new ThrottlingTask(throttler, request);
        ExecutorService executorService = Executors.newFixedThreadPool(numOfThreads);

        throttler.start();
        throttler.addRule("bronze", null, null);
        throttler.addRule("silver", null, null);
        throttler.addRule("gold", null, null);

        //flood the system
        for (int i = 0; i < numTasks; i++) {
            executorService.submit(task);
        }

        for (int j = 0; j < iterations; j++) {
            long startTime = System.nanoTime();
            throttler.isThrottled(request);
            resultList.add(System.nanoTime() - startTime);
        }

        executorService.shutdown();
        Collections.sort(resultList);<commit_msg>fixing test cases<commit_after>package org.wso2.throttle;

import org.apache.log4j.Logger;
import org.junit.Test;
import org.wso2.carbon.databridge.core.exception.DataBridgeException;
import org.wso2.carbon.databridge.core.exception.StreamDefinitionStoreException;
import org.wso2.throttle.core.Request;
import org.wso2.throttle.core.Throttler;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class BasicTest {
    private static final Logger log = Logger.getLogger(BasicTest.class);

    @Test
    public void testRule1()
            throws InterruptedException, DataBridgeException, StreamDefinitionStoreException,
                   IOException {
        Throttler throttler = Throttler.getInstance();
        throttler.start();

        throttler.addRule("bronze", null, null);
        throttler.addRule("silver", null, null);
        throttler.addRule("gold", null, null);
        long startTime = System.nanoTime();
        Request request;
        for (int i = 0; i < 1; i++) {
            request = new Request("gold","2", "gold","test:1.0.0:abcd:admin@carbon.super", "silver",
                    "test:GET-abcd:admin@carbon.super");
            throttler.isThrottled(request);
        }
        long endTime = System.nanoTime();
        log.info(endTime - startTime);

        Thread.sleep(10000);
        throttler.stop();
    }

    @Test
    public void testPerformance()
            throws InterruptedException, DataBridgeException, StreamDefinitionStoreException,
            IOException {
        int numOfThreads = 30;
        long numTasks = 800000;
        final Throttler throttler = Throttler.getInstance();
        final Request request = new Request("gold","2", "gold","test:1.0.0:abcd:admin@carbon.super", "silver",
                "test:GET-abcd:admin@carbon.super");
        ThrottlingTask task = new ThrottlingTask(throttler, request);
        ExecutorService executorService = Executors.newFixedThreadPool(numOfThreads);

        throttler.start();
        throttler.addRule("bronze", null, null);
        throttler.addRule("silver", null, null);
        throttler.addRule("gold", null, null);

        long startTimeMillis = System.currentTimeMillis();
        for (int i = 0; i < numTasks; i++) {
            executorService.submit(task);
        }

        executorService.shutdown();
        executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);

        long diff = System.currentTimeMillis() - startTimeMillis;
        log.info("Throughput " + (numTasks * 1000 / diff));
        log.info("Latency " + (diff * 1.0 / numTasks + " ms"));
        log.info("Time in milli sec " + diff);
    }

    @Test
    public void testLatency() throws DataBridgeException, StreamDefinitionStoreException, IOException, InterruptedException {
        int numOfThreads = 3;
        int numTasks = 800000;
        int iterations = 10000;
        List<Long> resultList = new ArrayList<Long>(numTasks);
        final Request request = new Request("gold","2", "gold","test:1.0.0:abcd:admin@carbon.super", "silver",
                "test:GET-abcd:admin@carbon.super");
        final Throttler throttler = Throttler.getInstance();
        ThrottlingTask task = new ThrottlingTask(throttler, request);
        ExecutorService executorService = Executors.newFixedThreadPool(numOfThreads);

        throttler.start();
        throttler.addRule("bronze", null, null);
        throttler.addRule("silver", null, null);
        throttler.addRule("gold", null, null);

        //flood the system
        for (int i = 0; i < numTasks; i++) {
            executorService.submit(task);
        }

        for (int j = 0; j < iterations; j++) {
            long startTime = System.nanoTime();
            throttler.isThrottled(request);
            resultList.add(System.nanoTime() - startTime);
        }

        executorService.shutdown();
        Collections.sort(resultList);
{'fraction_non_alphanumeric': 0.11630269172168614, 'fraction_numerical': 0.01244286439817166, 'mean_word_length': 2.8386939571150096, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 6, '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': '5010899', 'n_tokens_mistral': 2371, 'n_tokens_neox': 2266, 'n_words': 475}