|
| 1 | +/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
| 2 | +/* |
| 3 | + * Copyright 2016 Couchbase, Inc |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#include <stdio.h> |
| 19 | +#include <stdlib.h> |
| 20 | +#include <string.h> |
| 21 | + |
| 22 | +#include "test.h" |
| 23 | +#include "memory_pool.h" |
| 24 | + |
| 25 | +struct worker_args{ |
| 26 | + int num_runs; |
| 27 | + int num_bins; |
| 28 | + size_t bin_size; |
| 29 | + MemoryPool *mp; |
| 30 | +}; |
| 31 | + |
| 32 | +void *basic_tester(void *args_) |
| 33 | +{ |
| 34 | + TEST_INIT(); |
| 35 | + struct worker_args *args = (struct worker_args *)args_; |
| 36 | + size_t bin_size = args->bin_size; |
| 37 | + for (int i = args->num_runs; i; --i) { |
| 38 | + uint8_t *buf; |
| 39 | + const int idx = args->mp->fetchBlock(&buf); |
| 40 | + TEST_CHK(idx != -1); |
| 41 | + for (int j = 100; j; --j) { |
| 42 | + buf[rand() % (bin_size - 1)] = 'X'; |
| 43 | + } |
| 44 | + args->mp->returnBlock(idx); |
| 45 | + } |
| 46 | + return NULL; |
| 47 | +} |
| 48 | + |
| 49 | +void basic_test(int iterations, int num_bins, size_t bin_size) |
| 50 | +{ |
| 51 | + TEST_INIT(); |
| 52 | + struct worker_args mpool; |
| 53 | + struct timeval ts_begin, ts_cur, ts_gap; |
| 54 | + gettimeofday(&ts_begin, NULL); |
| 55 | + |
| 56 | + mpool.num_runs = iterations; |
| 57 | + mpool.num_bins = num_bins; |
| 58 | + mpool.bin_size = bin_size; |
| 59 | + mpool.mp = new MemoryPool(mpool.num_bins, mpool.bin_size); |
| 60 | + void *ret = basic_tester(&mpool); |
| 61 | + TEST_CHK(!ret); |
| 62 | + delete mpool.mp; |
| 63 | + |
| 64 | + gettimeofday(&ts_cur, NULL); |
| 65 | + ts_gap = _utime_gap(ts_begin, ts_cur); |
| 66 | + char res[128]; |
| 67 | + sprintf(res, "basic test with %d runs of %d bins x %" _F64 "bytes" |
| 68 | + " in %ld us", |
| 69 | + iterations, num_bins, uint64_t(bin_size), |
| 70 | + ts_gap.tv_sec*1000000 + ts_gap.tv_usec); |
| 71 | + TEST_RESULT(res); |
| 72 | +} |
| 73 | + |
| 74 | +void multi_thread_test(int num_threads, int iterations, int num_bins, |
| 75 | + size_t bin_size) |
| 76 | +{ |
| 77 | + TEST_INIT(); |
| 78 | + thread_t *tid = alca(thread_t, num_threads); |
| 79 | + struct timeval ts_begin, ts_cur, ts_gap; |
| 80 | + gettimeofday(&ts_begin, NULL); |
| 81 | + |
| 82 | + struct worker_args mpool; |
| 83 | + mpool.num_runs = iterations; |
| 84 | + mpool.num_bins = num_bins; |
| 85 | + mpool.bin_size = bin_size; |
| 86 | + mpool.mp = new MemoryPool(mpool.num_bins, mpool.bin_size); |
| 87 | + |
| 88 | + for (int i = num_threads - 1; i; --i) { |
| 89 | + thread_create(&tid[i], basic_tester, &mpool); |
| 90 | + } |
| 91 | + |
| 92 | + for (int i = num_threads - 1; i; --i) { |
| 93 | + void *ret; |
| 94 | + thread_join(tid[i], &ret); |
| 95 | + TEST_CHK(!ret); |
| 96 | + } |
| 97 | + |
| 98 | + delete mpool.mp; |
| 99 | + |
| 100 | + gettimeofday(&ts_cur, NULL); |
| 101 | + ts_gap = _utime_gap(ts_begin, ts_cur); |
| 102 | + char res[128]; |
| 103 | + sprintf(res, "multi-thread test with %d threads each with " |
| 104 | + "%d runs of %d bins x %" _F64 "bytes in %ld usec", |
| 105 | + num_threads, iterations, num_bins, uint64_t(bin_size), |
| 106 | + ts_gap.tv_sec*1000000 + ts_gap.tv_usec); |
| 107 | + TEST_RESULT(res); |
| 108 | +} |
| 109 | + |
| 110 | +int main() |
| 111 | +{ |
| 112 | + basic_test(10000, 8, 10485760); //1000 runs of 8 x 10MB buffers |
| 113 | + multi_thread_test(8, 10000, 8, 10485760); // repeat with 8 threads |
| 114 | + return 0; |
| 115 | +} |
0 commit comments