1919import static org .apache .commons .io .file .PathUtils .deleteDirectory ;
2020import static org .apache .solr .bench .BaseBenchState .log ;
2121
22- import com .codahale .metrics .Meter ;
2322import java .io .IOException ;
2423import java .io .OutputStream ;
2524import java .io .PrintStream ;
3736import java .util .concurrent .Executors ;
3837import java .util .concurrent .ScheduledExecutorService ;
3938import java .util .concurrent .TimeUnit ;
39+ import java .util .concurrent .atomic .AtomicLong ;
4040import org .apache .solr .client .solrj .SolrServerException ;
4141import org .apache .solr .client .solrj .jetty .HttpJettySolrClient ;
4242import org .apache .solr .client .solrj .request .CollectionAdminRequest ;
@@ -396,7 +396,8 @@ public void index(String collection, Docs docs, int docCount, boolean parallel)
396396 @ SuppressForbidden (reason = "Benchmarks may use JDK ExecutorService impls" )
397397 private void indexParallel (String collection , Docs docs , int docCount )
398398 throws InterruptedException {
399- Meter meter = new Meter ();
399+ AtomicLong meterCount = new AtomicLong ();
400+ long meterStartNanos = System .nanoTime ();
400401 ExecutorService executorService =
401402 Executors .newFixedThreadPool (
402403 Runtime .getRuntime ().availableProcessors (),
@@ -406,10 +407,11 @@ private void indexParallel(String collection, Docs docs, int docCount)
406407 new SolrNamedThreadFactory ("SolrJMH Indexer Progress" ));
407408 scheduledExecutor .scheduleAtFixedRate (
408409 () -> {
409- if (meter .getCount () == docCount ) {
410+ long count = meterCount .get ();
411+ if (count == docCount ) {
410412 scheduledExecutor .shutdown ();
411413 } else {
412- log ( meter . getCount () + " docs at " + meter . getMeanRate () + " doc/s" );
414+ logIndexingRate ( count , meterStartNanos );
413415 }
414416 },
415417 10 ,
@@ -427,7 +429,7 @@ public void run() {
427429 SolrInputDocument doc = docs .inputDocument ();
428430 // log("add doc " + doc);
429431 updateRequest .add (doc );
430- meter . mark ();
432+ meterCount . incrementAndGet ();
431433
432434 try {
433435 client .requestWithBaseUrl (url , updateRequest , collection );
@@ -451,27 +453,28 @@ public void run() {
451453
452454 private void indexBatch (String collection , Docs docs , int docCount , int batchSize )
453455 throws SolrServerException , IOException {
454- Meter meter = new Meter ();
456+ long meterCount = 0 ;
457+ long meterStartNanos = System .nanoTime ();
455458 List <SolrInputDocument > batch = new ArrayList <>(batchSize );
456459 for (int i = 1 ; i <= docCount ; i ++) {
457460 batch .add (docs .inputDocument ());
458461 if (i % batchSize == 0 ) {
459462 UpdateRequest updateRequest = new UpdateRequest ();
460463 updateRequest .add (batch );
461464 client .requestWithBaseUrl (nodes .get (0 ), updateRequest , collection );
462- meter . mark ( batch .size () );
465+ meterCount += batch .size ();
463466 batch .clear ();
464- log ( meter . getCount () + " docs at " + ( long ) meter . getMeanRate () + " doc/s" );
467+ logIndexingRate ( meterCount , meterStartNanos );
465468 }
466469 }
467470 if (!batch .isEmpty ()) {
468471 UpdateRequest updateRequest = new UpdateRequest ();
469472 updateRequest .add (batch );
470473 client .requestWithBaseUrl (nodes .get (0 ), updateRequest , collection );
471- meter . mark ( batch .size () );
474+ meterCount += batch .size ();
472475 batch = null ;
473476 }
474- log ( meter . getCount () + " docs at " + ( long ) meter . getMeanRate () + " doc/s" );
477+ logIndexingRate ( meterCount , meterStartNanos );
475478 }
476479
477480 /**
@@ -586,4 +589,10 @@ public static Path getFile(String name) {
586589 + " CWD="
587590 + Path .of ("" ).toAbsolutePath ());
588591 }
592+
593+ private static void logIndexingRate (long docCount , long startNanos ) {
594+ long elapsedSec = TimeUnit .NANOSECONDS .toSeconds (System .nanoTime () - startNanos );
595+ long rate = elapsedSec > 0 ? docCount / elapsedSec : 0 ;
596+ log (docCount + " docs at " + rate + " doc/s" );
597+ }
589598}
0 commit comments