<commit_before>/*
 * Copyright 2011 <NAME>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package stormpot.benchmark;

import stormpot.Pool;
import stormpot.UnitKit;

import com.google.caliper.Param;

/*
 * Sadly, it seems that Caliper benchmarks can't extend one another :(
 */
public class ContendedPoolSpin extends PoolSpin {
  
  @Param({"1", "2", "4"}) int threads;
  @Param({"0", "1", "2"}) int poolType;
  
  Thread[] contenders;
  
  @Override
  protected void setUp() throws Exception {
    super.setUp();
    super.poolType = poolType;
    contenders = new Thread[threads-1];
    for (int i = 0; i < contenders.length; i++) {
      contenders[i] = new ContenderThread(pool);
      contenders[i].start();
    }
    for (Thread th : contenders) {
      UnitKit.waitForThreadState(th, Thread.State.RUNNABLE);
    }
  }
  
  @Override
  public int timeClaimReleaseSpin(int reps) throws Exception {
    return super.timeClaimReleaseSpin(reps);
  }

  @Override
  protected void tearDown() throws Exception {
    super.tearDown();
    for (Thread th : contenders) {
      th.interrupt();
      try {
        th.join();
      } catch (Exception _) {}
    }
  }
  
  public static class ContenderThread extends Thread {
    private final Pool pool;
    public ContenderThread(Pool pool) {
      this.pool = pool;
    }
    
    @Override
    public void run() {
      try {
        for (;;) {
          pool.claim().release();
        }
      } catch (Exception e) {
        // ... aaand, we're done.
      }
    }<commit_msg>make the ContendedPoolSpin benchmark actually use the pool implementation that was requested, and remove the possibility of the shut down procedure getting stuck on objects lost to the shutting down of the traffic worker threads.<commit_after>/*
 * Copyright 2011 <NAME>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package stormpot.benchmark;

import stormpot.Pool;
import stormpot.Poolable;
import stormpot.UnitKit;

import com.google.caliper.Param;

/*
 * Sadly, it seems that Caliper benchmarks can't extend one another :(
 */
public class ContendedPoolSpin extends PoolSpin {
  
  @Param({"1", "2", "4"}) int threads;
  @Param({"0", "1", "2"}) int poolType;
  
  Thread[] contenders;
  
  @Override
  protected void setUp() throws Exception {
    super.poolType = poolType;
    super.setUp();
    contenders = new Thread[threads-1];
    for (int i = 0; i < contenders.length; i++) {
      contenders[i] = new ContenderThread(pool);
      contenders[i].start();
    }
    for (Thread th : contenders) {
      UnitKit.waitForThreadState(th, Thread.State.RUNNABLE);
    }
  }
  
  @Override
  public int timeClaimReleaseSpin(int reps) throws Exception {
    return super.timeClaimReleaseSpin(reps);
  }

  @Override
  protected void tearDown() throws Exception {
    super.tearDown();
    for (Thread th : contenders) {
      th.interrupt();
      try {
        th.join();
      } catch (Exception _) {}
    }
  }
  
  public static class ContenderThread extends Thread {
    private final Pool pool;
    public ContenderThread(Pool pool) {
      this.pool = pool;
    }
    
    @Override
    public void run() {
      Poolable obj = null;
      try {
        for (;;) {
          try {
            obj = pool.claim();
          } finally {
            if (obj != null) {
              obj.release();
            }
            obj = null;
          }
        }
      } catch (Exception e) {
        // ... aaand, we're done.
      }
    }
{'fraction_non_alphanumeric': 0.1051230871590153, 'fraction_numerical': 0.007096917276557995, 'mean_word_length': 2.6577453365774533, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2365476', 'n_tokens_mistral': 1330, 'n_tokens_neox': 1279, 'n_words': 446}