/*
 * Copyright 2016-2022 chronicle.software
 *
 *       https://chronicle.software
 *
 * 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 net.openhft.chronicle.wire.channel.book;

import java.util.List;
import java.util.OptionalInt;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.LongStream;

/**
 * Benchmark for just allocations
 */
public class PerfTopOfBookAllocationMain {
    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        final long count = 3_000_000_000L;
        final int batch = 10;
        final OptionalInt max = LongStream.range(0, count / batch)
                .parallel()
                .mapToObj(l -> IntStream.range(0, batch)
                        .mapToObj(i -> new TopOfBook().sendingTimeNS(i))
                        .collect(Collectors.toList()))
                .mapToInt(List::size)
                .max();
        long time = System.currentTimeMillis() - start;
        System.out.println("batch: " + max.getAsInt());
        System.out.println("time: " + time / 1e3 + " sec");
        System.out.println("rate: " + count / time / 1e3 + " M objs/sec");
        final int proc = Runtime.getRuntime().availableProcessors();
        System.out.println("proc: " + proc);
        long avgLatency = proc * time * 1_000_000 / count;
        System.out.println("avgLatency: " + avgLatency + " ns");
    }
}

@@ -946,48 +946,546 @@
     
-public static void main(String%5B%5D args) %7B
+static final int PROCS = Runtime.getRuntime().availableProcessors();%0A    static final int THREADS = Integer.getInteger(%22threads%22, PROCS);%0A    static final int COUNT = Integer.getInteger(%22count%22, 2);%0A    // adjusted to approximate what is seen in more realistic benchmarks%0A    static final int BATCH = Integer.getInteger(%22batch%22, 10);%0A%0A    public static void main(String%5B%5D args) %7B%0A        System.out.println(%22%22%0A                + %22-Dthreads=%22 + THREADS + %22 %22%0A                + %22-Dcount=%22 + COUNT + %22 %22%0A                + %22-Dbatch=%22 + BATCH);
 %0A   
@@ -1557,17 +1557,25 @@
 count = 
-3
+COUNT * 1
 _000_000
@@ -1607,18 +1607,21 @@
 batch = 
-10
+BATCH
 ;%0A      
@@ -1645,16 +1645,32 @@
 nt max =
+%0A               
  LongStr
@@ -1686,23 +1686,25 @@
 (0, 
-count / batch)%0A
+THREADS)%0A        
     
@@ -1727,16 +1727,108 @@
 allel()%0A
+                        .flatMap(i -%3E LongStream.range(0, count / batch / THREADS))%0A        
         
@@ -1876,16 +1876,24 @@
  batch)%0A
+        
         
@@ -1981,16 +1981,24 @@
         
+   
+     
 .collect
@@ -2032,24 +2032,32 @@
             
+        
 .mapToInt(Li
@@ -2066,16 +2066,24 @@
 ::size)%0A
+        
         
@@ -2357,144 +2357,50 @@
     
-final int proc = Runtime.getRuntime().availableProcessors();%0A        System.out.println(%22proc: %22 + proc);%0A        long avgLatency = proc
+long avgLatency = Math.min(THREADS, PROCS)
  * t

Make the threads, count, batch configurable from the cmdline