Search in sources :

Example 1 with CallableTool

use of io.datarouter.util.concurrent.CallableTool in project datarouter by hotpads.

the class LoadTestGetHandler method get.

@Handler(defaultHandler = true)
private Mav get(@Param(P_num) OptionalString num, @Param(P_max) OptionalString max, @Param(P_numThreads) OptionalString numThreads, @Param(P_batchSize) OptionalString batchSize, @Param(P_logPeriod) OptionalString logPeriod, @Param(P_submitAction) OptionalString submitAction) {
    var form = new HtmlForm().withMethod("post");
    form.addTextField().withDisplay("Num").withName(P_num).withPlaceholder("100,000").withValue(num.orElse(null));
    form.addTextField().withDisplay("Max").withName(P_max).withPlaceholder("10").withValue(max.orElse(null));
    form.addTextField().withDisplay("Num Threads").withName(P_numThreads).withPlaceholder("10").withValue(numThreads.orElse(null));
    form.addTextField().withDisplay("Batch Size").withName(P_batchSize).withPlaceholder("100").withValue(batchSize.orElse(null));
    form.addTextField().withDisplay("Log Period").withName(P_logPeriod).withPlaceholder("1,0000").withValue(logPeriod.orElse(null));
    form.addButton().withDisplay("Run Get").withValue("anything");
    if (submitAction.isEmpty() || form.hasErrors()) {
        return pageFactory.startBuilder(request).withTitle("Load Test - Get").withContent(Html.makeContent(form)).buildMav();
    }
    PhaseTimer timer = new PhaseTimer("get");
    // params
    int pNum = num.map(StringTool::nullIfEmpty).map(number -> number.replaceAll(",", "")).map(Integer::valueOf).orElse(DEFAULT_NUM);
    int pMax = max.map(StringTool::nullIfEmpty).map(number -> number.replaceAll(",", "")).map(Integer::valueOf).orElse(pNum);
    int pNumThreads = numThreads.map(StringTool::nullIfEmpty).map(number -> number.replaceAll(",", "")).map(Integer::valueOf).orElse(DEFAULT_NUM_THREADS);
    int pBatchSize = batchSize.map(StringTool::nullIfEmpty).map(number -> number.replaceAll(",", "")).map(Integer::valueOf).orElse(DEFAULT_BATCH_SIZE);
    int pLogPeriod = logPeriod.map(StringTool::nullIfEmpty).map(number -> number.replaceAll(",", "")).map(Integer::valueOf).orElse(DEFAULT_LOG_PERIOD);
    // tracking
    AtomicInteger rowCounter = new AtomicInteger(0);
    AtomicLong lastBatchFinished = new AtomicLong(System.nanoTime());
    // execute
    int numBatches = LoadTestTool.numBatches(pNum, pBatchSize);
    ExecutorService executor = Executors.newFixedThreadPool(pNumThreads);
    Scanner.of(IntStream.range(0, numBatches).mapToObj(Integer::valueOf)).map(batchId -> LoadTestTool.makeRandomIdBatch(pNum, pMax, pBatchSize, batchId)).map(ids -> new GetBatchCallable(dao.getReaderNode(), ids, pLogPeriod, rowCounter, lastBatchFinished)).parallel(new ParallelScannerContext(executor, pNumThreads, true)).forEach(CallableTool::callUnchecked);
    ExecutorServiceTool.shutdown(executor, Duration.ofSeconds(5));
    timer.add("got " + rowCounter.get());
    var message = div(h2("Load Test Get Results"), div(h3("Results"), dl(dt("Total Time"), dd(timer.getElapsedString()), dt("Rows per second"), dd(timer.getItemsPerSecond(rowCounter.get()) + ""))), div(h3("Params"), dl(dt("Num"), dd(pNum + ""), dt("Max"), dd(pMax + ""), dt("Num Threads"), dd(pNumThreads + ""), dt("Batch Size"), dd(pBatchSize + ""), dt("Log Period"), dd(pLogPeriod + "")))).withClass("container");
    logger.warn("total={}, rps={}, num={}, max={}, numThreads={} batchSize={}, logPeriod={}", timer.getElapsedString(), timer.getItemsPerSecond(rowCounter.get()), pNum, pMax, pNumThreads, pBatchSize, pLogPeriod);
    return pageFactory.message(request, message);
}
Also used : IntStream(java.util.stream.IntStream) Scanner(io.datarouter.scanner.Scanner) ParallelScannerContext(io.datarouter.scanner.ParallelScannerContext) TagCreator.dd(j2html.TagCreator.dd) LoggerFactory(org.slf4j.LoggerFactory) TagCreator.h3(j2html.TagCreator.h3) Callable(java.util.concurrent.Callable) OptionalString(io.datarouter.web.handler.types.optional.OptionalString) TagCreator.h2(j2html.TagCreator.h2) TagCreator.dl(j2html.TagCreator.dl) Inject(javax.inject.Inject) NumberFormatter(io.datarouter.util.number.NumberFormatter) ExecutorServiceTool(io.datarouter.util.concurrent.ExecutorServiceTool) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TagCreator.br(j2html.TagCreator.br) TagCreator.dt(j2html.TagCreator.dt) Duration(java.time.Duration) Param(io.datarouter.web.handler.types.Param) Bootstrap4FormHtml(io.datarouter.web.html.j2html.bootstrap4.Bootstrap4FormHtml) ExecutorService(java.util.concurrent.ExecutorService) PhaseTimer(io.datarouter.util.timer.PhaseTimer) Logger(org.slf4j.Logger) Mav(io.datarouter.web.handler.mav.Mav) RandomValue(io.datarouter.loadtest.storage.RandomValue) StringTool(io.datarouter.util.string.StringTool) Executors(java.util.concurrent.Executors) ContainerTag(j2html.tags.ContainerTag) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) LoadTestTool(io.datarouter.loadtest.util.LoadTestTool) HtmlForm(io.datarouter.web.html.form.HtmlForm) BaseHandler(io.datarouter.web.handler.BaseHandler) LoadTestGetDao(io.datarouter.loadtest.service.LoadTestGetDao) CallableTool(io.datarouter.util.concurrent.CallableTool) Bootstrap4PageFactory(io.datarouter.web.html.j2html.bootstrap4.Bootstrap4PageFactory) RandomValueKey(io.datarouter.loadtest.storage.RandomValueKey) TagCreator.div(j2html.TagCreator.div) MapStorageReader(io.datarouter.storage.node.op.raw.read.MapStorageReader) AtomicLong(java.util.concurrent.atomic.AtomicLong) PhaseTimer(io.datarouter.util.timer.PhaseTimer) HtmlForm(io.datarouter.web.html.form.HtmlForm) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ParallelScannerContext(io.datarouter.scanner.ParallelScannerContext) CallableTool(io.datarouter.util.concurrent.CallableTool) ExecutorService(java.util.concurrent.ExecutorService) BaseHandler(io.datarouter.web.handler.BaseHandler)

Example 2 with CallableTool

use of io.datarouter.util.concurrent.CallableTool in project datarouter by hotpads.

the class LoadTestInsertHandler method insert.

@Handler(defaultHandler = true)
private Mav insert(@Param(P_num) OptionalString num, @Param(P_numThreads) OptionalString numThreads, @Param(P_batchSize) OptionalString batchSize, @Param(P_logPeriod) OptionalString logPeriod, @Param(P_persistentPut) OptionalString persistentPut, @Param(P_submitAction) OptionalString submitAction) {
    var form = new HtmlForm().withMethod("post");
    form.addTextField().withDisplay("Num").withName(P_num).withPlaceholder("100,000").withValue(num.orElse(null));
    form.addTextField().withDisplay("Num Threads").withName(P_numThreads).withPlaceholder("10").withValue(numThreads.orElse(null));
    form.addTextField().withDisplay("Batch Size").withName(P_batchSize).withPlaceholder("100").withValue(batchSize.orElse(null));
    form.addTextField().withDisplay("Log Period").withName(P_logPeriod).withPlaceholder("10,000").withValue(logPeriod.orElse(null));
    form.addTextField().withDisplay("Persistent Put").withName(P_persistentPut).withPlaceholder("true").withValue(persistentPut.orElse(null));
    form.addButton().withDisplay("Run Insert").withValue("anything");
    if (submitAction.isEmpty() || form.hasErrors()) {
        return pageFactory.startBuilder(request).withTitle("Load Test - Insert").withContent(Html.makeContent(form)).buildMav();
    }
    // params
    int pNum = num.map(StringTool::nullIfEmpty).map(number -> number.replaceAll(",", "")).map(Integer::valueOf).orElse(DEFAULT_NUM);
    int pNumThreads = numThreads.map(StringTool::nullIfEmpty).map(number -> number.replaceAll(",", "")).map(Integer::valueOf).orElse(DEFAULT_NUM_THREADS);
    int pBatchSize = batchSize.map(StringTool::nullIfEmpty).map(number -> number.replaceAll(",", "")).map(Integer::valueOf).orElse(DEFAULT_BATCH_SIZE);
    int pLogPeriod = logPeriod.map(StringTool::nullIfEmpty).map(number -> number.replaceAll(",", "")).map(Integer::valueOf).orElse(DEFAULT_LOG_PERIOD);
    boolean pPersistentPut = persistentPut.map(StringTool::nullIfEmpty).map(Boolean::valueOf).orElse(DEFAULT_PERSISTENT_PUT);
    PhaseTimer timer = new PhaseTimer("insert");
    // tracking
    AtomicInteger counter = new AtomicInteger(0);
    AtomicLong lastBatchFinished = new AtomicLong(System.nanoTime());
    // execute
    int numBatches = LoadTestTool.numBatches(pNum, pBatchSize);
    ExecutorService executor = Executors.newFixedThreadPool(pNumThreads);
    Scanner.of(IntStream.range(0, numBatches).mapToObj(Integer::valueOf)).map(batchId -> LoadTestTool.makePredictableIdBatch(pNum, pBatchSize, batchId)).map(ids -> new InsertBatchCallable(dao.getWriterNode(), ids, pPersistentPut, pLogPeriod, lastBatchFinished, counter)).parallel(new ParallelScannerContext(executor, pNumThreads, true)).forEach(CallableTool::callUnchecked);
    ExecutorServiceTool.shutdown(executor, Duration.ofSeconds(5));
    timer.add("inserted " + counter.get());
    // results
    DomContent message = div(h2("Load Test Insert Results"), div(h3("Results"), dl(dt("Total Time"), dd(timer.getElapsedString()), dt("Rows per second"), dd(timer.getItemsPerSecond(counter.get()) + ""))), div(h3("Params"), dl(dt("Num"), dd(pNum + ""), dt("Num Threads"), dd(pNumThreads + ""), dt("Batch Size"), dd(pBatchSize + ""), dt("Log Period"), dd(pLogPeriod + ""), dt("Persistent Put"), dd(pPersistentPut + "")))).withClass("container");
    logger.warn("total={}, rps={}, num={}, numThreads={} batchSize={}, logPeriod={}, persistentPut={}", timer.getElapsedString(), timer.getItemsPerSecond(counter.get()), pNum, pNumThreads, pBatchSize, pLogPeriod, pPersistentPut);
    return pageFactory.message(request, message);
}
Also used : IntStream(java.util.stream.IntStream) Scanner(io.datarouter.scanner.Scanner) LoadTestInsertDao(io.datarouter.loadtest.service.LoadTestInsertDao) ParallelScannerContext(io.datarouter.scanner.ParallelScannerContext) TagCreator.dd(j2html.TagCreator.dd) LoggerFactory(org.slf4j.LoggerFactory) TagCreator.h3(j2html.TagCreator.h3) Callable(java.util.concurrent.Callable) OptionalString(io.datarouter.web.handler.types.optional.OptionalString) TagCreator.h2(j2html.TagCreator.h2) TagCreator.dl(j2html.TagCreator.dl) Inject(javax.inject.Inject) NumberFormatter(io.datarouter.util.number.NumberFormatter) ExecutorServiceTool(io.datarouter.util.concurrent.ExecutorServiceTool) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TagCreator.br(j2html.TagCreator.br) TagCreator.dt(j2html.TagCreator.dt) Duration(java.time.Duration) Config(io.datarouter.storage.config.Config) Param(io.datarouter.web.handler.types.Param) Bootstrap4FormHtml(io.datarouter.web.html.j2html.bootstrap4.Bootstrap4FormHtml) ExecutorService(java.util.concurrent.ExecutorService) PhaseTimer(io.datarouter.util.timer.PhaseTimer) StorageWriter(io.datarouter.storage.node.op.raw.write.StorageWriter) Logger(org.slf4j.Logger) Mav(io.datarouter.web.handler.mav.Mav) RandomValue(io.datarouter.loadtest.storage.RandomValue) StringTool(io.datarouter.util.string.StringTool) Executors(java.util.concurrent.Executors) ContainerTag(j2html.tags.ContainerTag) AtomicLong(java.util.concurrent.atomic.AtomicLong) DomContent(j2html.tags.DomContent) List(java.util.List) LoadTestTool(io.datarouter.loadtest.util.LoadTestTool) HtmlForm(io.datarouter.web.html.form.HtmlForm) BaseHandler(io.datarouter.web.handler.BaseHandler) CallableTool(io.datarouter.util.concurrent.CallableTool) Bootstrap4PageFactory(io.datarouter.web.html.j2html.bootstrap4.Bootstrap4PageFactory) RandomValueKey(io.datarouter.loadtest.storage.RandomValueKey) TagCreator.div(j2html.TagCreator.div) PhaseTimer(io.datarouter.util.timer.PhaseTimer) ParallelScannerContext(io.datarouter.scanner.ParallelScannerContext) StringTool(io.datarouter.util.string.StringTool) CallableTool(io.datarouter.util.concurrent.CallableTool) DomContent(j2html.tags.DomContent) AtomicLong(java.util.concurrent.atomic.AtomicLong) HtmlForm(io.datarouter.web.html.form.HtmlForm) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) BaseHandler(io.datarouter.web.handler.BaseHandler)

Aggregations

RandomValue (io.datarouter.loadtest.storage.RandomValue)2 RandomValueKey (io.datarouter.loadtest.storage.RandomValueKey)2 LoadTestTool (io.datarouter.loadtest.util.LoadTestTool)2 ParallelScannerContext (io.datarouter.scanner.ParallelScannerContext)2 Scanner (io.datarouter.scanner.Scanner)2 CallableTool (io.datarouter.util.concurrent.CallableTool)2 ExecutorServiceTool (io.datarouter.util.concurrent.ExecutorServiceTool)2 NumberFormatter (io.datarouter.util.number.NumberFormatter)2 StringTool (io.datarouter.util.string.StringTool)2 PhaseTimer (io.datarouter.util.timer.PhaseTimer)2 BaseHandler (io.datarouter.web.handler.BaseHandler)2 Mav (io.datarouter.web.handler.mav.Mav)2 Param (io.datarouter.web.handler.types.Param)2 OptionalString (io.datarouter.web.handler.types.optional.OptionalString)2 HtmlForm (io.datarouter.web.html.form.HtmlForm)2 Bootstrap4FormHtml (io.datarouter.web.html.j2html.bootstrap4.Bootstrap4FormHtml)2 Bootstrap4PageFactory (io.datarouter.web.html.j2html.bootstrap4.Bootstrap4PageFactory)2 TagCreator.br (j2html.TagCreator.br)2 TagCreator.dd (j2html.TagCreator.dd)2 TagCreator.div (j2html.TagCreator.div)2