<commit_before>/**
 * Copyright 2007-2015, Kaazing Corporation. All rights reserved.
 *
 * 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 org.kaazing.nuklei.bench.jmh.echo;

import static java.util.concurrent.TimeUnit.SECONDS;
import static org.kaazing.nuklei.Configuration.DIRECTORY_PROPERTY_NAME;
import static org.kaazing.nuklei.Configuration.STREAMS_CAPACITY_PROPERTY_NAME;

import java.nio.charset.StandardCharsets;
import java.util.Properties;

import org.kaazing.nuklei.Configuration;
import org.kaazing.nuklei.echo.internal.Context;
import org.kaazing.nuklei.echo.internal.EchoNukleus;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Group;
import org.openjdk.jmh.annotations.GroupThreads;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.TearDown;
import org.openjdk.jmh.annotations.Warmup;

import uk.co.real_logic.agrona.MutableDirectBuffer;
import uk.co.real_logic.agrona.concurrent.UnsafeBuffer;

@State(Scope.Benchmark)
@BenchmarkMode(Mode.Throughput)
@Fork(3)
@Warmup(iterations = 5, time = 1, timeUnit = SECONDS)
@Measurement(iterations = 5, time = 1, timeUnit = SECONDS)
@OutputTimeUnit(SECONDS)
public class EchoServerBM
{
    private EchoNukleus nukleus;
    private EchoController controller;
    private EchoStreams streams;
    private MutableDirectBuffer sendBuffer;
    private long streamId;

    @Setup
    public void create() throws Exception
    {
        final Properties properties = new Properties();
        properties.setProperty(DIRECTORY_PROPERTY_NAME, "target/nukleus-benchmarks");
        properties.setProperty(STREAMS_CAPACITY_PROPERTY_NAME, Long.toString(1024 * 1024 * 16));
        final Configuration config = new Configuration(properties);

        final Context context = new Context();
        context.conclude(config);

        this.nukleus = new EchoNukleus(context);
        this.controller = new EchoController(context);

        final long sourceRef = (long) Math.random() * Long.MAX_VALUE;
        final long bindCorrelationId = controller.bind("source", sourceRef);
        while (this.nukleus.process() != 0L)
        {
            // intentional
        }
        final long bindRef = controller.supplyResponse(bindCorrelationId);

        this.streams = new EchoStreams(context, "source.accepts");

        long streamId = (long) Math.random() * Long.MAX_VALUE;
        this.streams.begin(bindRef, streamId);
        while (this.nukleus.process() != 0L)
        {
            // intentional
        }
        this.streams.drain();

        byte[] byteArray = "Hello, world".getBytes(StandardCharsets.UTF_8);
        this.sendBuffer = new UnsafeBuffer(byteArray);
    }

    @TearDown
    public void close() throws Exception
    {
        this.nukleus.close();
    }

    @Benchmark
    @Group("process")
    @GroupThreads(1)
    public void nukleus() throws Exception
    {
        this.nukleus.reflector().process();
    }

    @Benchmark
    @Group("process")
    @GroupThreads(1)
    public void handler() throws Exception
    {
        this.streams.data(streamId, sendBuffer, 0, sendBuffer.capacity());
        this.streams.drain();
    }
}<commit_msg>Use JMH control to decide when to stop writing messages<commit_after>/**
 * Copyright 2007-2015, Kaazing Corporation. All rights reserved.
 *
 * 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 org.kaazing.nuklei.bench.jmh.echo;

import static java.util.concurrent.TimeUnit.SECONDS;
import static org.kaazing.nuklei.Configuration.DIRECTORY_PROPERTY_NAME;
import static org.kaazing.nuklei.Configuration.STREAMS_CAPACITY_PROPERTY_NAME;

import java.nio.charset.StandardCharsets;
import java.util.Properties;

import org.kaazing.nuklei.Configuration;
import org.kaazing.nuklei.echo.internal.Context;
import org.kaazing.nuklei.echo.internal.EchoNukleus;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Group;
import org.openjdk.jmh.annotations.GroupThreads;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.TearDown;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Control;

import uk.co.real_logic.agrona.MutableDirectBuffer;
import uk.co.real_logic.agrona.concurrent.UnsafeBuffer;

@State(Scope.Benchmark)
@BenchmarkMode(Mode.Throughput)
@Fork(3)
@Warmup(iterations = 5, time = 1, timeUnit = SECONDS)
@Measurement(iterations = 5, time = 1, timeUnit = SECONDS)
@OutputTimeUnit(SECONDS)
public class EchoServerBM
{
    private EchoNukleus nukleus;
    private EchoController controller;
    private EchoStreams streams;
    private MutableDirectBuffer sendBuffer;
    private long streamId;

    @Setup
    public void create() throws Exception
    {
        final Properties properties = new Properties();
        properties.setProperty(DIRECTORY_PROPERTY_NAME, "target/nukleus-benchmarks");
        properties.setProperty(STREAMS_CAPACITY_PROPERTY_NAME, Long.toString(1024 * 1024 * 16));
        final Configuration config = new Configuration(properties);

        final Context context = new Context();
        context.conclude(config);

        this.nukleus = new EchoNukleus(context);
        this.controller = new EchoController(context);

        final long sourceRef = (long) Math.random() * Long.MAX_VALUE;
        final long bindCorrelationId = controller.bind("source", sourceRef);
        while (this.nukleus.process() != 0L)
        {
            // intentional
        }
        final long bindRef = controller.supplyResponse(bindCorrelationId);

        this.streams = new EchoStreams(context, "source.accepts");

        long streamId = (long) Math.random() * Long.MAX_VALUE;
        this.streams.begin(bindRef, streamId);
        while (this.nukleus.process() != 0L)
        {
            // intentional
        }
        this.streams.drain();

        byte[] byteArray = "Hello, world".getBytes(StandardCharsets.UTF_8);
        this.sendBuffer = new UnsafeBuffer(byteArray);
    }

    @TearDown
    public void close() throws Exception
    {
        this.nukleus.close();
    }

    @Benchmark
    @Group("process")
    @GroupThreads(1)
    public void nukleus() throws Exception
    {
        this.nukleus.reflector().process();
    }

    @Benchmark
    @Group("process")
    @GroupThreads(1)
    public void handler(Control control) throws Exception
    {
        while (!control.stopMeasurement)
        {
            this.streams.data(streamId, sendBuffer, 0, sendBuffer.capacity());
            this.streams.drain();
        }
    }
}
{'fraction_non_alphanumeric': 0.10100391772771793, 'fraction_numerical': 0.008080313418217433, 'mean_word_length': 3.6362088535754826, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6347458', 'n_tokens_mistral': 2604, 'n_tokens_neox': 2461, 'n_words': 566}