Pierre De Rop | 3a00a21 | 2015-03-01 09:27:46 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.apache.felix.dm.benchmark.scenario; |
| 20 | |
| 21 | import java.util.concurrent.ForkJoinPool; |
| 22 | import java.util.concurrent.atomic.AtomicLong; |
| 23 | import java.util.function.Supplier; |
| 24 | |
| 25 | /** |
| 26 | * Helper class containing misc functions, and constants. |
| 27 | * |
| 28 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
| 29 | */ |
| 30 | public class Helper { |
| 31 | /** |
| 32 | * Activate this flag for debugging. |
| 33 | */ |
| 34 | private final static boolean DEBUG = false; |
| 35 | |
| 36 | /** |
| 37 | * Generator used to create unique identifiers. |
| 38 | */ |
| 39 | private final static AtomicLong m_idGenerator = new AtomicLong(); |
| 40 | |
| 41 | /** |
| 42 | * Threadpool which can be optionally used by parallel scenarios. |
| 43 | */ |
| 44 | private final static int CORES = Runtime.getRuntime().availableProcessors(); |
| 45 | private final static ForkJoinPool TPOOL = new ForkJoinPool(CORES); |
| 46 | |
| 47 | /** |
| 48 | * Get the threadpool, possibly needed by some scenario supporting parallel mode |
| 49 | */ |
| 50 | public static ForkJoinPool getThreadPool() { |
| 51 | return TPOOL; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Display some debug messages. |
| 56 | */ |
| 57 | public static void debug(Supplier<String> message) { |
| 58 | if (DEBUG) { |
| 59 | System.out.println(message.get()); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Generates a unique id. |
| 65 | */ |
| 66 | public static long generateId() { |
| 67 | return m_idGenerator.incrementAndGet(); |
| 68 | } |
| 69 | } |