2002-12-14 22:43:18 +01:00
|
|
|
/*
|
|
|
|
* PROGRAM: Client/Server Common Code
|
|
|
|
* MODULE: class_perf.cpp
|
|
|
|
* DESCRIPTION: Class library performance measurements
|
|
|
|
*
|
2003-09-08 22:23:46 +02:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
* You may obtain a copy of the Licence at
|
|
|
|
* http://www.gnu.org/licences/lgpl.html
|
|
|
|
*
|
|
|
|
* As a special exception this file can also be included in modules
|
|
|
|
* with other source code as long as that source code has been
|
|
|
|
* released under an Open Source Initiative certificed licence.
|
|
|
|
* More information about OSI certification can be found at:
|
|
|
|
* http://www.opensource.org
|
|
|
|
*
|
|
|
|
* This module is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public Licence for more details.
|
|
|
|
*
|
|
|
|
* This module was created by members of the firebird development
|
|
|
|
* team. All individual contributions remain the Copyright (C) of
|
|
|
|
* those individuals and all rights are reserved. Contributors to
|
|
|
|
* this file are either listed below or can be obtained from a CVS
|
|
|
|
* history command.
|
2002-12-14 22:43:18 +01:00
|
|
|
*
|
2003-09-08 22:23:46 +02:00
|
|
|
* Created by: Nickolay Samofatov <skidder@bssys.com>
|
2002-12-14 22:43:18 +01:00
|
|
|
*
|
2003-09-08 22:23:46 +02:00
|
|
|
* Contributor(s):
|
|
|
|
*
|
2002-12-14 22:43:18 +01:00
|
|
|
*
|
2004-05-21 08:16:17 +02:00
|
|
|
* $Id: class_perf.cpp,v 1.11 2004-05-21 06:14:51 robocop Exp $
|
2002-12-14 22:43:18 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "tree.h"
|
2003-01-03 17:03:30 +01:00
|
|
|
#include "alloc.h"
|
2003-08-06 18:30:49 +02:00
|
|
|
//#include "../memory/memory_pool.h"
|
2002-12-14 22:43:18 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <set>
|
|
|
|
|
|
|
|
clock_t t;
|
|
|
|
|
|
|
|
void start() {
|
|
|
|
t = clock();
|
|
|
|
}
|
|
|
|
|
2004-05-17 17:07:49 +02:00
|
|
|
const int TEST_ITEMS = 10000000;
|
2002-12-14 22:43:18 +01:00
|
|
|
|
|
|
|
void report(int scale) {
|
|
|
|
clock_t d = clock();
|
2003-01-03 17:03:30 +01:00
|
|
|
printf("Add+remove %d elements from tree of scale %d took %d milliseconds. \n",
|
2002-12-14 22:43:18 +01:00
|
|
|
TEST_ITEMS, scale, (int)(d-t)*1000/CLOCKS_PER_SEC);
|
|
|
|
}
|
|
|
|
|
2003-01-03 17:03:30 +01:00
|
|
|
using namespace Firebird;
|
|
|
|
|
|
|
|
static void testTree() {
|
2002-12-14 22:43:18 +01:00
|
|
|
printf("Fill array with test data (%d items)...", TEST_ITEMS);
|
|
|
|
Vector<int, TEST_ITEMS> *v = new Vector<int, TEST_ITEMS>();
|
|
|
|
int n = 0;
|
2003-01-06 18:35:21 +01:00
|
|
|
int i;
|
|
|
|
for (i=0;i<TEST_ITEMS;i++) {
|
2002-12-14 22:43:18 +01:00
|
|
|
n = n * 45578 - 17651;
|
|
|
|
// Fill it with quasi-random values in range 0...TEST_ITEMS-1
|
|
|
|
v->add(((i+n) % TEST_ITEMS + TEST_ITEMS)/2);
|
|
|
|
}
|
|
|
|
printf(" DONE\n");
|
|
|
|
MallocAllocator temp;
|
|
|
|
|
|
|
|
start();
|
|
|
|
BePlusTree<int, int, MallocAllocator, DefaultKeyValue<int>,
|
|
|
|
DefaultComparator<int>, 10, 10> tree10(NULL);
|
2003-01-06 18:35:21 +01:00
|
|
|
for (i=0; i<TEST_ITEMS;i++)
|
2002-12-14 22:43:18 +01:00
|
|
|
tree10.add((*v)[i]);
|
2003-01-06 18:35:21 +01:00
|
|
|
for (i=0; i<TEST_ITEMS;i++) {
|
2002-12-14 22:43:18 +01:00
|
|
|
if (tree10.locate((*v)[i]))
|
|
|
|
tree10.fastRemove();
|
|
|
|
}
|
|
|
|
report(10);
|
|
|
|
|
|
|
|
start();
|
|
|
|
BePlusTree<int, int, MallocAllocator, DefaultKeyValue<int>,
|
|
|
|
DefaultComparator<int>, 50, 50> tree50(NULL);
|
2003-01-06 18:35:21 +01:00
|
|
|
for (i=0; i<TEST_ITEMS;i++)
|
2002-12-14 22:43:18 +01:00
|
|
|
tree50.add((*v)[i]);
|
2003-01-06 18:35:21 +01:00
|
|
|
for (i=0; i<TEST_ITEMS;i++) {
|
2002-12-14 22:43:18 +01:00
|
|
|
if (tree50.locate((*v)[i]))
|
|
|
|
tree50.fastRemove();
|
|
|
|
}
|
|
|
|
report(50);
|
|
|
|
|
|
|
|
start();
|
|
|
|
BePlusTree<int, int, MallocAllocator, DefaultKeyValue<int>,
|
|
|
|
DefaultComparator<int>, 75, 75> tree75(NULL);
|
2003-01-06 18:35:21 +01:00
|
|
|
for (i=0; i<TEST_ITEMS;i++)
|
2002-12-14 22:43:18 +01:00
|
|
|
tree75.add((*v)[i]);
|
2003-01-06 18:35:21 +01:00
|
|
|
for (i=0; i<TEST_ITEMS;i++) {
|
2002-12-14 22:43:18 +01:00
|
|
|
if (tree75.locate((*v)[i]))
|
|
|
|
tree75.fastRemove();
|
|
|
|
}
|
|
|
|
report(75);
|
|
|
|
|
|
|
|
start();
|
|
|
|
BePlusTree<int, int, MallocAllocator, DefaultKeyValue<int>,
|
|
|
|
DefaultComparator<int>, 100, 100> tree100(NULL);
|
2003-01-06 18:35:21 +01:00
|
|
|
for (i=0; i<TEST_ITEMS;i++)
|
2002-12-14 22:43:18 +01:00
|
|
|
tree100.add((*v)[i]);
|
2003-01-06 18:35:21 +01:00
|
|
|
for (i=0; i<TEST_ITEMS;i++) {
|
2002-12-14 22:43:18 +01:00
|
|
|
if (tree100.locate((*v)[i]))
|
|
|
|
tree100.fastRemove();
|
|
|
|
}
|
|
|
|
report(100);
|
|
|
|
|
|
|
|
start();
|
|
|
|
BePlusTree<int, int, MallocAllocator, DefaultKeyValue<int>,
|
|
|
|
DefaultComparator<int>, 200, 200> tree200(NULL);
|
2003-01-06 18:35:21 +01:00
|
|
|
for (i=0; i<TEST_ITEMS;i++)
|
2002-12-14 22:43:18 +01:00
|
|
|
tree200.add((*v)[i]);
|
2003-01-06 18:35:21 +01:00
|
|
|
for (i=0; i<TEST_ITEMS;i++) {
|
2002-12-14 22:43:18 +01:00
|
|
|
if (tree200.locate((*v)[i]))
|
|
|
|
tree200.fastRemove();
|
|
|
|
}
|
|
|
|
report(200);
|
|
|
|
|
|
|
|
start();
|
|
|
|
BePlusTree<int, int, MallocAllocator, DefaultKeyValue<int>,
|
|
|
|
DefaultComparator<int>, 250, 250> tree250(NULL);
|
2003-01-06 18:35:21 +01:00
|
|
|
for (i=0; i<TEST_ITEMS;i++)
|
2002-12-14 22:43:18 +01:00
|
|
|
tree250.add((*v)[i]);
|
2003-01-06 18:35:21 +01:00
|
|
|
for (i=0; i<TEST_ITEMS;i++) {
|
2002-12-14 22:43:18 +01:00
|
|
|
if (tree250.locate((*v)[i]))
|
|
|
|
tree250.fastRemove();
|
|
|
|
}
|
|
|
|
report(250);
|
|
|
|
|
|
|
|
start();
|
|
|
|
BePlusTree<int, int, MallocAllocator, DefaultKeyValue<int>,
|
|
|
|
DefaultComparator<int>, 500, 500> tree500(NULL);
|
2003-01-06 18:35:21 +01:00
|
|
|
for (i=0; i<TEST_ITEMS;i++)
|
2002-12-14 22:43:18 +01:00
|
|
|
tree500.add((*v)[i]);
|
2003-01-06 18:35:21 +01:00
|
|
|
for (i=0; i<TEST_ITEMS;i++) {
|
2002-12-14 22:43:18 +01:00
|
|
|
if (tree500.locate((*v)[i]))
|
|
|
|
tree500.fastRemove();
|
|
|
|
}
|
|
|
|
report(500);
|
|
|
|
|
|
|
|
std::set<int> stlTree;
|
|
|
|
start();
|
2003-01-06 18:35:21 +01:00
|
|
|
for (i=0; i<TEST_ITEMS;i++)
|
2002-12-14 22:43:18 +01:00
|
|
|
stlTree.insert((*v)[i]);
|
2003-01-06 18:35:21 +01:00
|
|
|
for (i=0; i<TEST_ITEMS;i++)
|
2002-12-14 22:43:18 +01:00
|
|
|
stlTree.erase((*v)[i]);
|
|
|
|
clock_t d = clock();
|
|
|
|
printf("Just a reference: add+remove %d elements from STL tree took %d milliseconds. \n",
|
|
|
|
TEST_ITEMS, (int)(d-t)*1000/CLOCKS_PER_SEC);
|
|
|
|
}
|
2003-01-03 17:03:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
void report() {
|
|
|
|
clock_t d = clock();
|
|
|
|
printf("Operation took %d milliseconds.\n", (int)(d-t)*1000/CLOCKS_PER_SEC);
|
|
|
|
}
|
|
|
|
|
2004-05-17 17:07:49 +02:00
|
|
|
const int ALLOC_ITEMS = 10000000;
|
|
|
|
const int MAX_ITEM_SIZE = 50;
|
2004-05-21 08:16:17 +02:00
|
|
|
const int BIG_ITEMS = ALLOC_ITEMS / 10;
|
|
|
|
const int BIG_SIZE = MAX_ITEM_SIZE * 5;
|
2003-01-03 17:03:30 +01:00
|
|
|
|
|
|
|
struct AllocItem {
|
|
|
|
int order;
|
|
|
|
void *item;
|
2004-03-26 00:12:50 +01:00
|
|
|
static bool greaterThan(const AllocItem &i1, const AllocItem &i2) {
|
2003-01-03 17:03:30 +01:00
|
|
|
return i1.order > i2.order || (i1.order==i2.order && i1.item > i2.item);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static void testAllocatorOverhead() {
|
|
|
|
printf("Calculating measurement overhead...\n");
|
|
|
|
start();
|
|
|
|
MallocAllocator allocator;
|
|
|
|
BePlusTree<AllocItem,AllocItem,MallocAllocator,DefaultKeyValue<AllocItem>,AllocItem> items(&allocator),
|
|
|
|
bigItems(&allocator);
|
|
|
|
// Allocate small items
|
|
|
|
int n = 0;
|
2003-01-06 18:35:21 +01:00
|
|
|
int i;
|
|
|
|
for (i=0;i<ALLOC_ITEMS;i++) {
|
2003-01-03 17:03:30 +01:00
|
|
|
n = n * 47163 - 57412;
|
2004-03-26 00:12:50 +01:00
|
|
|
AllocItem temp = {n, (void*)(long)i};
|
2003-01-03 17:03:30 +01:00
|
|
|
items.add(temp);
|
|
|
|
}
|
|
|
|
// Deallocate half of small items
|
|
|
|
n = 0;
|
|
|
|
if (items.getFirst()) do {
|
|
|
|
items.current();
|
|
|
|
n++;
|
|
|
|
} while (n < ALLOC_ITEMS/2 && items.getNext());
|
|
|
|
// Allocate big items
|
2003-01-06 18:35:21 +01:00
|
|
|
for (i=0;i<BIG_ITEMS;i++) {
|
2003-01-03 17:03:30 +01:00
|
|
|
n = n * 47163 - 57412;
|
2004-03-26 00:12:50 +01:00
|
|
|
AllocItem temp = {n, (void*)(long)i};
|
2003-01-03 17:03:30 +01:00
|
|
|
bigItems.add(temp);
|
|
|
|
}
|
|
|
|
// Deallocate the rest of small items
|
2003-01-10 22:37:18 +01:00
|
|
|
while (items.getNext()) {
|
2003-01-03 17:03:30 +01:00
|
|
|
items.current();
|
2003-01-10 22:37:18 +01:00
|
|
|
}
|
2003-01-03 17:03:30 +01:00
|
|
|
// Deallocate big items
|
|
|
|
if (bigItems.getFirst()) do {
|
|
|
|
bigItems.current();
|
|
|
|
} while (bigItems.getNext());
|
|
|
|
report();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void testAllocatorMemoryPool() {
|
|
|
|
printf("Test run for Firebird::MemoryPool...\n");
|
|
|
|
start();
|
|
|
|
Firebird::MemoryPool* pool = Firebird::MemoryPool::createPool();
|
|
|
|
MallocAllocator allocator;
|
|
|
|
BePlusTree<AllocItem,AllocItem,MallocAllocator,DefaultKeyValue<AllocItem>,AllocItem> items(&allocator),
|
|
|
|
bigItems(&allocator);
|
|
|
|
// Allocate small items
|
2003-01-07 17:35:10 +01:00
|
|
|
int i, n = 0;
|
|
|
|
for (i=0;i<ALLOC_ITEMS;i++) {
|
|
|
|
n = n * 47163 - 57412;
|
2003-01-16 18:47:10 +01:00
|
|
|
AllocItem temp = {n, pool->allocate((n % MAX_ITEM_SIZE + MAX_ITEM_SIZE)/2+1)};
|
2003-01-07 17:35:10 +01:00
|
|
|
items.add(temp);
|
|
|
|
}
|
|
|
|
// Deallocate half of small items
|
|
|
|
n = 0;
|
|
|
|
if (items.getFirst()) do {
|
2003-01-16 18:47:10 +01:00
|
|
|
pool->deallocate(items.current().item);
|
2003-01-07 17:35:10 +01:00
|
|
|
n++;
|
|
|
|
} while (n < ALLOC_ITEMS/2 && items.getNext());
|
|
|
|
// Allocate big items
|
|
|
|
for (i=0;i<BIG_ITEMS;i++) {
|
|
|
|
n = n * 47163 - 57412;
|
2003-01-16 18:47:10 +01:00
|
|
|
AllocItem temp = {n, pool->allocate((n % BIG_SIZE + BIG_SIZE)/2+1)};
|
2003-01-07 17:35:10 +01:00
|
|
|
bigItems.add(temp);
|
|
|
|
}
|
|
|
|
// Deallocate the rest of small items
|
2003-01-10 22:37:18 +01:00
|
|
|
while (items.getNext()) {
|
2003-01-16 18:47:10 +01:00
|
|
|
pool->deallocate(items.current().item);
|
2003-01-03 17:03:30 +01:00
|
|
|
}
|
|
|
|
// Deallocate big items
|
|
|
|
if (bigItems.getFirst()) do {
|
2003-01-16 18:47:10 +01:00
|
|
|
pool->deallocate(bigItems.current().item);
|
2003-01-03 17:03:30 +01:00
|
|
|
} while (bigItems.getNext());
|
|
|
|
Firebird::MemoryPool::deletePool(pool);
|
|
|
|
report();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void testAllocatorMalloc() {
|
|
|
|
printf("Test reference run for ::malloc...\n");
|
|
|
|
start();
|
|
|
|
MallocAllocator allocator;
|
|
|
|
BePlusTree<AllocItem,AllocItem,MallocAllocator,DefaultKeyValue<AllocItem>,AllocItem> items(&allocator),
|
|
|
|
bigItems(&allocator);
|
|
|
|
// Allocate small items
|
2003-01-07 17:35:10 +01:00
|
|
|
int i, n = 0;
|
|
|
|
for (i=0;i<ALLOC_ITEMS;i++) {
|
2003-01-03 17:03:30 +01:00
|
|
|
n = n * 47163 - 57412;
|
|
|
|
AllocItem temp = {n, malloc((n % MAX_ITEM_SIZE + MAX_ITEM_SIZE)/2+1)};
|
|
|
|
items.add(temp);
|
|
|
|
}
|
|
|
|
// Deallocate half of small items
|
|
|
|
n = 0;
|
|
|
|
if (items.getFirst()) do {
|
|
|
|
free(items.current().item);
|
|
|
|
n++;
|
|
|
|
} while (n < ALLOC_ITEMS/2 && items.getNext());
|
|
|
|
// Allocate big items
|
2003-01-06 18:35:21 +01:00
|
|
|
for (i=0;i<BIG_ITEMS;i++) {
|
2003-01-03 17:03:30 +01:00
|
|
|
n = n * 47163 - 57412;
|
|
|
|
AllocItem temp = {n, malloc((n % BIG_SIZE + BIG_SIZE)/2+1)};
|
|
|
|
bigItems.add(temp);
|
|
|
|
}
|
|
|
|
// Deallocate the rest of small items
|
2003-01-10 22:37:18 +01:00
|
|
|
while (items.getNext()) {
|
2003-01-03 17:03:30 +01:00
|
|
|
free(items.current().item);
|
2003-01-10 22:37:18 +01:00
|
|
|
}
|
2003-01-03 17:03:30 +01:00
|
|
|
// Deallocate big items
|
|
|
|
if (bigItems.getFirst()) do {
|
|
|
|
free(bigItems.current().item);
|
|
|
|
} while (bigItems.getNext());
|
|
|
|
report();
|
|
|
|
}
|
|
|
|
|
2003-08-06 18:30:49 +02:00
|
|
|
/*static void testAllocatorOldPool() {
|
2003-01-03 17:03:30 +01:00
|
|
|
printf("Test run for old MemoryPool...\n");
|
|
|
|
start();
|
|
|
|
::MemoryPool *pool = new ::MemoryPool(0,getDefaultMemoryPool());
|
|
|
|
MallocAllocator allocator;
|
|
|
|
BePlusTree<AllocItem,AllocItem,MallocAllocator,DefaultKeyValue<AllocItem>,AllocItem> items(&allocator),
|
|
|
|
bigItems(&allocator);
|
|
|
|
// Allocate small items
|
|
|
|
int n = 0;
|
2003-01-06 18:35:21 +01:00
|
|
|
int i;
|
|
|
|
for (i=0;i<ALLOC_ITEMS;i++) {
|
2003-01-03 17:03:30 +01:00
|
|
|
n = n * 47163 - 57412;
|
|
|
|
AllocItem temp = {n, pool->allocate((n % MAX_ITEM_SIZE + MAX_ITEM_SIZE)/2+1,0)};
|
|
|
|
items.add(temp);
|
|
|
|
}
|
|
|
|
// Deallocate half of small items
|
|
|
|
n = 0;
|
|
|
|
if (items.getFirst()) do {
|
|
|
|
pool->deallocate(items.current().item);
|
|
|
|
n++;
|
|
|
|
} while (n < ALLOC_ITEMS/2 && items.getNext());
|
|
|
|
// Allocate big items
|
2003-01-06 18:35:21 +01:00
|
|
|
for (i=0;i<BIG_ITEMS;i++) {
|
2003-01-03 17:03:30 +01:00
|
|
|
n = n * 47163 - 57412;
|
|
|
|
AllocItem temp = {n, pool->allocate((n % BIG_SIZE + BIG_SIZE)/2+1,0)};
|
|
|
|
bigItems.add(temp);
|
|
|
|
}
|
2003-01-10 22:37:18 +01:00
|
|
|
// Deallocate the rest of small items
|
|
|
|
while (items.getNext()) {
|
2003-01-03 17:03:30 +01:00
|
|
|
pool->deallocate(items.current().item);
|
2003-01-10 22:37:18 +01:00
|
|
|
}
|
2003-01-03 17:03:30 +01:00
|
|
|
// Deallocate big items
|
|
|
|
if (bigItems.getFirst()) do {
|
|
|
|
pool->deallocate(bigItems.current().item);
|
|
|
|
} while (bigItems.getNext());
|
|
|
|
delete pool;
|
|
|
|
report();
|
2003-08-06 18:30:49 +02:00
|
|
|
}*/
|
2003-01-03 17:03:30 +01:00
|
|
|
|
|
|
|
int main() {
|
|
|
|
testTree();
|
|
|
|
testAllocatorOverhead();
|
|
|
|
testAllocatorMemoryPool();
|
|
|
|
testAllocatorMalloc();
|
2003-08-06 18:30:49 +02:00
|
|
|
// testAllocatorOldPool();
|
2003-01-03 17:03:30 +01:00
|
|
|
}
|