Delete JunctionProjectDefs.cmake; simplify AddSample.cmake and move it to samples
[junction.git] / samples / MapMemoryBench / MapMemoryBench.cpp
1 /*------------------------------------------------------------------------
2   Junction: Concurrent data structures in C++
3   Copyright (c) 2016 Jeff Preshing
4
5   Distributed under the Simplified BSD License.
6   Original location: https://github.com/preshing/junction
7
8   This software is distributed WITHOUT ANY WARRANTY; without even the
9   implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10   See the LICENSE file for more information.
11 ------------------------------------------------------------------------*/
12
13 #include <junction/Core.h>
14 #include <turf/Heap.h>
15 #include <turf/Util.h>
16 #include <stdio.h>
17 #include <junction/extra/MapAdapter.h>
18
19 #if TURF_USE_DLMALLOC && TURF_DLMALLOC_FAST_STATS
20
21 using namespace turf::intTypes;
22 typedef junction::extra::MapAdapter MapAdapter;
23
24 static const ureg MaxPopulation = 1000000;
25 static const ureg MinPopulation = 1000;
26 static const ureg StepSize = 500;
27 static const u32 Prime = 0x4190ab09;
28
29 int main() {
30     MapAdapter adapter(1);
31     MapAdapter::ThreadContext threadCtx(adapter, 0);
32     threadCtx.registerThread();
33
34     ureg startMem = TURF_HEAP.getInUseBytes();
35     ureg mem = 0;
36     ureg initialCapacity = MapAdapter::getInitialCapacity(MaxPopulation);
37     MapAdapter::Map* map = new MapAdapter::Map(initialCapacity);
38
39     printf("[\n");
40     ureg population = 0;
41     while (population < MaxPopulation) {
42         ureg loMem = mem;
43         ureg hiMem = mem;
44         for (ureg target = population + StepSize; population < target; population++) {
45             u32 key = u32(population + 1) * Prime;
46             if (key >= 2) {
47                 map->insert(key, (void*) uptr(key));
48                 population++;
49                 threadCtx.update();
50                 ureg inUseBytes = TURF_HEAP.getInUseBytes();
51                 mem = inUseBytes - startMem;
52                 loMem = turf::util::min(loMem, mem);
53                 hiMem = turf::util::max(hiMem, mem);
54             }
55         }
56         printf("    (%d, %d, %d),\n", (int) population, (int) loMem, (int) hiMem);
57     }
58     printf("]\n");
59
60     delete map;
61     return 0;
62 }
63
64 #else // TURF_USE_DLMALLOC && TURF_DLMALLOC_FAST_STATS
65
66 int main() {
67     fprintf(stderr, "Must configure with TURF_USE_DLMALLOC=1 and TURF_DLMALLOC_FAST_STATS=1\n");
68     return 1;
69 }
70
71 #endif // TURF_USE_DLMALLOC && TURF_DLMALLOC_FAST_STATS