// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. #include #include #include #include #include #include #include #include using namespace vespalib; using namespace vespalib::coro; Lazy make_expensive_task(Executor &executor, int value) { co_await schedule(executor); auto cpu_cost = 20ms; std::this_thread::sleep_for(cpu_cost); co_return value; } Lazy make_cheap_task(Executor &, int value) { co_return value; } Lazy concurrent_sum(Executor &executor, std::vector values, std::function(Executor &,int)> make_task) { std::vector> work; for (int v: values) { work.push_back(make_task(executor, v)); } ActiveWork active; for (auto &task: work) { active.start(task); } co_await active.join(); int res = 0; for (auto &task: work) { res += co_await task; // await_ready == true } co_return res; } TEST(ActiveWorkTest, run_expensive_subtasks_concurrently) { vespalib::ThreadStackExecutor executor(8); auto t0 = steady_clock::now(); auto result = sync_wait(concurrent_sum(executor, {1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16}, make_expensive_task)); auto td = steady_clock::now() - t0; EXPECT_EQ(result, 136); fprintf(stderr, "time spent: %" PRId64 " ms\n", count_ms(td)); } TEST(ActiveWorkTest, run_cheap_subtasks_concurrently) { vespalib::ThreadStackExecutor executor(1); auto t0 = steady_clock::now(); auto result = sync_wait(concurrent_sum(executor, {1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16}, make_cheap_task)); auto td = steady_clock::now() - t0; EXPECT_EQ(result, 136); fprintf(stderr, "time spent: %" PRId64 " ms\n", count_ms(td)); } GTEST_MAIN_RUN_ALL_TESTS()