<commit_before>package au.com.williamhill.flywheel.rig;

import java.net.*;

import org.awaitility.*;
import org.junit.*;

import com.obsidiandynamics.indigo.benchmark.*;
import com.obsidiandynamics.indigo.util.*;

import au.com.williamhill.flywheel.edge.*;
import au.com.williamhill.flywheel.remote.*;
import au.com.williamhill.flywheel.rig.EdgeRig.*;
import au.com.williamhill.flywheel.rig.RemoteRig.*;
import au.com.williamhill.flywheel.socketx.*;
import au.com.williamhill.flywheel.topic.*;
import au.com.williamhill.flywheel.util.*;

public final class RigBenchmark implements TestSupport {
  private static final String HOST = "localhost";
  private static final int PREFERRED_PORT = 8080;
  
  abstract static class Config implements Spec {
    static {
      Awaitility.doNotCatchUncaughtExceptionsByDefault();
    }
    
    ThrowingFunction<Config, Summary> runner = RigBenchmark::test;
    String host;
    int port;
    int pulses;
    int pulseDurationMillis;
    int syncFrames;
    TopicSpec topicSpec;
    boolean initiate;
    boolean text;
    int bytes;
    double normalMinNanos = Double.NaN;
    float warmupFrac;
    LogConfig log;
    
    /* Derived fields. */
    int warmupPulses;
    
    private boolean initialised;
    
    @Override
    public void init() {
      if (initialised) return;
      
      warmupPulses = (int) (pulses * warmupFrac);
      initialised = true;
    }

    @Override
    public LogConfig getLog() {
      return log;
    }

    @Override
    public String describe() {
      return String.format("%,d pulses, %,d ms duration, %.0f%% warmup fraction",
                           pulses, pulseDurationMillis, warmupFrac * 100);
    }
    
    SpecMultiplier applyDefaults() {
      host = HOST;
      port = SocketTestSupport.getAvailablePort(PREFERRED_PORT);
      warmupFrac = 0.05f;
      initiate = true;
      normalMinNanos = 50_000f;
      log = new LogConfig() {{
        summary = stages = LOG;
        verbose = false;
      }};
      return times(2);
    }

    @Override
    public Summary run() throws Exception {
      return runner.apply(this);
    }
  }

  @Test
  public void testText() throws Exception {
    new Config() {{
      pulses = 10;
      pulseDurationMillis = 1;
      syncFrames = 10;
      topicSpec = TopicLibrary.smallLeaves();
      text = true;
      bytes = 16;
    }}.applyDefaults().test();
  }

  @Test
  public void testBinary() throws Exception {
    new Config() {{
      pulses = 10;
      pulseDurationMillis = 1;
      syncFrames = 10;
      topicSpec = TopicLibrary.smallLeaves();
      text = false;
      bytes = 16;
    }}.applyDefaults().test();
  }

  private static Summary test(Config c) throws Exception {
    final EdgeNode edge = EdgeNode.builder()
        .withServerConfig(new XServerConfig() {{ port = c.port; }})
        .build();
    final EdgeRig edgeRig = new EdgeRig(edge, new EdgeRigConfig() {{
      topicSpec = c.topicSpec;
      pulseDurationMillis = c.pulseDurationMillis;
      pulses = c.pulses;
      warmupPulses = c.warmupPulses;
      text = c.text;
      bytes = c.bytes;
      log = c.log;
    }});
    
    final RemoteNode remote = RemoteNode.builder()
        .build();
    final RemoteRig remoteRig = new RemoteRig(remote, new RemoteRigConfig() {{<commit_msg>Trying to break ByteBuffer marshaling<commit_after>package au.com.williamhill.flywheel.rig;

import java.net.*;

import org.awaitility.*;
import org.junit.*;

import com.obsidiandynamics.indigo.benchmark.*;
import com.obsidiandynamics.indigo.util.*;

import au.com.williamhill.flywheel.edge.*;
import au.com.williamhill.flywheel.remote.*;
import au.com.williamhill.flywheel.rig.EdgeRig.*;
import au.com.williamhill.flywheel.rig.RemoteRig.*;
import au.com.williamhill.flywheel.socketx.*;
import au.com.williamhill.flywheel.topic.*;
import au.com.williamhill.flywheel.util.*;

public final class RigBenchmark implements TestSupport {
  private static final String HOST = "localhost";
  private static final int PREFERRED_PORT = 8080;
  
  abstract static class Config implements Spec {
    static {
      Awaitility.doNotCatchUncaughtExceptionsByDefault();
    }
    
    ThrowingFunction<Config, Summary> runner = RigBenchmark::test;
    String host;
    int port;
    int pulses;
    int pulseDurationMillis;
    int syncFrames;
    TopicSpec topicSpec;
    boolean initiate;
    boolean text;
    int bytes;
    double normalMinNanos = Double.NaN;
    float warmupFrac;
    LogConfig log;
    
    /* Derived fields. */
    int warmupPulses;
    
    private boolean initialised;
    
    @Override
    public void init() {
      if (initialised) return;
      
      warmupPulses = (int) (pulses * warmupFrac);
      initialised = true;
    }

    @Override
    public LogConfig getLog() {
      return log;
    }

    @Override
    public String describe() {
      return String.format("%,d pulses, %,d ms duration, %.0f%% warmup fraction",
                           pulses, pulseDurationMillis, warmupFrac * 100);
    }
    
    SpecMultiplier applyDefaults() {
      host = HOST;
      port = SocketTestSupport.getAvailablePort(PREFERRED_PORT);
      warmupFrac = 0.05f;
      initiate = true;
      normalMinNanos = 50_000f;
      log = new LogConfig() {{
        summary = stages = LOG;
        verbose = false;
      }};
      return times(20);//TODO
    }

    @Override
    public Summary run() throws Exception {
      return runner.apply(this);
    }
  }

  @Test
  public void testTextSmallSingleton() throws Exception {
    new Config() {{
      pulses = 10;
      pulseDurationMillis = 1;
      syncFrames = 10;
      topicSpec = TopicLibrary.singleton(10);
      text = true;
      bytes = 16;
    }}.applyDefaults().test();
  }

  @Test
  public void testBinarySmallSingleton() throws Exception {
    new Config() {{
      pulses = 1000; //TODO
      pulseDurationMillis = 1;
      syncFrames = 10;
      topicSpec = TopicLibrary.singleton(100);//TODO
      text = false;
      bytes = 1600; //TODO
    }}.applyDefaults().test();
  }

  @Test
  public void testTextSmallLeaves() throws Exception {
    new Config() {{
      pulses = 10;
      pulseDurationMillis = 1;
      syncFrames = 10;
      topicSpec = TopicLibrary.smallLeaves();
      text = true;
      bytes = 16;
    }}.applyDefaults().test();
  }

  @Test
  public void testBinarySmallLeaves() throws Exception {
    new Config() {{
      pulses = 10;
      pulseDurationMillis = 1;
      syncFrames = 10;
      topicSpec = TopicLibrary.smallLeaves();
      text = false;
      bytes = 16;
    }}.applyDefaults().test();
  }

  private static Summary test(Config c) throws Exception {
    final EdgeNode edge = EdgeNode.builder()
        .withServerConfig(new XServerConfig() {{ port = c.port; }})
        .build();
    final EdgeRig edgeRig = new EdgeRig(edge, new EdgeRigConfig() {{
      topicSpec = c.topicSpec;
      pulseDurationMillis = c.pulseDurationMillis;
      pulses = c.pulses;
      warmupPulses = c.warmupPulses;
      text = c.text;
      bytes = c.bytes;
      log = c.log;
    }});
    
    final RemoteNode remote = RemoteNode.builder()
        .build();
    final RemoteRig remoteRig = new RemoteRig(remote, new RemoteRigConfig() {{
{'fraction_non_alphanumeric': 0.10789874118135288, 'fraction_numerical': 0.011896527873841472, 'mean_word_length': 2.616808404202101, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '6345350', 'n_tokens_mistral': 2325, 'n_tokens_neox': 2225, 'n_words': 538}