1e66f31c5Sopenharmony_ci/* Copyright libuv contributors. All rights reserved. 2e66f31c5Sopenharmony_ci * 3e66f31c5Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy 4e66f31c5Sopenharmony_ci * of this software and associated documentation files (the "Software"), to 5e66f31c5Sopenharmony_ci * deal in the Software without restriction, including without limitation the 6e66f31c5Sopenharmony_ci * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7e66f31c5Sopenharmony_ci * sell copies of the Software, and to permit persons to whom the Software is 8e66f31c5Sopenharmony_ci * furnished to do so, subject to the following conditions: 9e66f31c5Sopenharmony_ci * 10e66f31c5Sopenharmony_ci * The above copyright notice and this permission notice shall be included in 11e66f31c5Sopenharmony_ci * all copies or substantial portions of the Software. 12e66f31c5Sopenharmony_ci * 13e66f31c5Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14e66f31c5Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15e66f31c5Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16e66f31c5Sopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17e66f31c5Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18e66f31c5Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19e66f31c5Sopenharmony_ci * IN THE SOFTWARE. 20e66f31c5Sopenharmony_ci */ 21e66f31c5Sopenharmony_ci 22e66f31c5Sopenharmony_ci#include "task.h" 23e66f31c5Sopenharmony_ci#include "uv.h" 24e66f31c5Sopenharmony_ci 25e66f31c5Sopenharmony_cistatic int done = 0; 26e66f31c5Sopenharmony_cistatic unsigned events = 0; 27e66f31c5Sopenharmony_cistatic unsigned result; 28e66f31c5Sopenharmony_ci 29e66f31c5Sopenharmony_cistatic unsigned fastrand(void) { 30e66f31c5Sopenharmony_ci static unsigned g = 0; 31e66f31c5Sopenharmony_ci g = g * 214013 + 2531011; 32e66f31c5Sopenharmony_ci return g; 33e66f31c5Sopenharmony_ci} 34e66f31c5Sopenharmony_ci 35e66f31c5Sopenharmony_cistatic void work_cb(uv_work_t* req) { 36e66f31c5Sopenharmony_ci req->data = &result; 37e66f31c5Sopenharmony_ci *(unsigned*)req->data = fastrand(); 38e66f31c5Sopenharmony_ci} 39e66f31c5Sopenharmony_ci 40e66f31c5Sopenharmony_cistatic void after_work_cb(uv_work_t* req, int status) { 41e66f31c5Sopenharmony_ci events++; 42e66f31c5Sopenharmony_ci if (!done) 43e66f31c5Sopenharmony_ci ASSERT_OK(uv_queue_work(req->loop, req, work_cb, after_work_cb)); 44e66f31c5Sopenharmony_ci} 45e66f31c5Sopenharmony_ci 46e66f31c5Sopenharmony_cistatic void timer_cb(uv_timer_t* handle) { done = 1; } 47e66f31c5Sopenharmony_ci 48e66f31c5Sopenharmony_ciBENCHMARK_IMPL(queue_work) { 49e66f31c5Sopenharmony_ci char fmtbuf[2][32]; 50e66f31c5Sopenharmony_ci uv_timer_t timer_handle; 51e66f31c5Sopenharmony_ci uv_work_t work; 52e66f31c5Sopenharmony_ci uv_loop_t* loop; 53e66f31c5Sopenharmony_ci int timeout; 54e66f31c5Sopenharmony_ci 55e66f31c5Sopenharmony_ci loop = uv_default_loop(); 56e66f31c5Sopenharmony_ci timeout = 5000; 57e66f31c5Sopenharmony_ci 58e66f31c5Sopenharmony_ci ASSERT_OK(uv_timer_init(loop, &timer_handle)); 59e66f31c5Sopenharmony_ci ASSERT_OK(uv_timer_start(&timer_handle, timer_cb, timeout, 0)); 60e66f31c5Sopenharmony_ci 61e66f31c5Sopenharmony_ci ASSERT_OK(uv_queue_work(loop, &work, work_cb, after_work_cb)); 62e66f31c5Sopenharmony_ci ASSERT_OK(uv_run(loop, UV_RUN_DEFAULT)); 63e66f31c5Sopenharmony_ci 64e66f31c5Sopenharmony_ci printf("%s async jobs in %.1f seconds (%s/s)\n", 65e66f31c5Sopenharmony_ci fmt(&fmtbuf[0], events), 66e66f31c5Sopenharmony_ci timeout / 1000., 67e66f31c5Sopenharmony_ci fmt(&fmtbuf[1], events / (timeout / 1000.))); 68e66f31c5Sopenharmony_ci 69e66f31c5Sopenharmony_ci MAKE_VALGRIND_HAPPY(loop); 70e66f31c5Sopenharmony_ci return 0; 71e66f31c5Sopenharmony_ci} 72