2001-01-23 06:33:03 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2001-01-23 07:16:27 +03:00
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public
|
|
|
|
* License Version 1.1 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS
|
|
|
|
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
* implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
|
|
|
*
|
|
|
|
* The Original Code is nsCacheManager.h, released January 22, 2001.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape Communications
|
|
|
|
* Corporation. Portions created by Netscape are
|
|
|
|
* Copyright (C) 2001 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Doug Turner
|
|
|
|
* Gordon Sheridan
|
|
|
|
*/
|
2001-01-23 06:33:03 +03:00
|
|
|
|
|
|
|
|
|
|
|
#include "nscore.h"
|
|
|
|
#include "nspr.h"
|
|
|
|
#include "mcom_db.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
|
2001-01-24 04:18:23 +03:00
|
|
|
#ifdef WIN32
|
|
|
|
#define TMPDIR "c:\\temp\\"
|
|
|
|
#define DIRSEP "\\"
|
|
|
|
#else
|
|
|
|
#define TMPDIR "/tmp/"
|
|
|
|
#define DIRSEP "/"
|
|
|
|
#endif
|
|
|
|
|
2001-01-23 06:33:03 +03:00
|
|
|
#define DATASIZE 512
|
|
|
|
#define ENTRYCOUNT 512
|
|
|
|
#define USE_ENTRY_ID 1
|
2001-01-23 07:16:27 +03:00
|
|
|
#define EXTRA_LOOKUP 1
|
2001-01-24 04:33:40 +03:00
|
|
|
#define NUM_CYCLES 32
|
2001-01-23 06:33:03 +03:00
|
|
|
|
2001-01-23 07:16:27 +03:00
|
|
|
DB* myDB;
|
|
|
|
HASHINFO hash_info = {
|
|
|
|
16*1024 , /* bucket size */
|
|
|
|
0 , /* fill factor */
|
|
|
|
0 , /* number of elements */
|
|
|
|
0 , /* bytes to cache */
|
|
|
|
0 , /* hash function */
|
|
|
|
0} ; /* byte order */
|
2001-01-23 06:33:03 +03:00
|
|
|
|
2001-01-23 07:16:27 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int writeDBM(int cycles)
|
|
|
|
{
|
2001-01-24 04:18:23 +03:00
|
|
|
printf("writeDBM\n");
|
2001-01-23 06:33:03 +03:00
|
|
|
DBT db_key, db_data ;
|
2001-01-23 07:16:27 +03:00
|
|
|
PRIntervalTime time = PR_IntervalNow();
|
|
|
|
|
|
|
|
while (cycles--) {
|
|
|
|
// create database file
|
2001-01-24 04:18:23 +03:00
|
|
|
myDB = dbopen(TMPDIR "foodb",
|
2001-01-23 07:16:27 +03:00
|
|
|
O_RDWR | O_CREAT ,
|
|
|
|
0600 ,
|
|
|
|
DB_HASH ,
|
|
|
|
&hash_info) ;
|
2001-01-23 06:33:03 +03:00
|
|
|
|
2001-01-23 07:16:27 +03:00
|
|
|
if (!myDB) {
|
|
|
|
printf("no db!\n");
|
2001-01-23 06:33:03 +03:00
|
|
|
return -1;
|
|
|
|
}
|
2001-01-23 07:16:27 +03:00
|
|
|
|
|
|
|
// initalize data to write
|
|
|
|
int x;
|
|
|
|
char * data = (char*) malloc(DATASIZE);
|
|
|
|
for (x=0; x<DATASIZE; x++)
|
|
|
|
*data = '\0';
|
|
|
|
for (x=1; x<=ENTRYCOUNT; x++) {
|
|
|
|
nsCAutoString keyName("foo");
|
|
|
|
keyName.AppendInt( x );
|
|
|
|
|
2001-03-20 08:37:56 +03:00
|
|
|
db_key.data = NS_CONST_CAST(char*, keyName.get());
|
2001-01-23 07:16:27 +03:00
|
|
|
db_key.size = keyName.Length();
|
|
|
|
|
|
|
|
db_data.data = data;
|
|
|
|
db_data.size = DATASIZE ;
|
|
|
|
|
|
|
|
if(0 != (*myDB->put)(myDB, &db_key, &db_data, 0)) {
|
|
|
|
printf("--> Error putting\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2001-01-23 06:33:03 +03:00
|
|
|
#if USE_ENTRY_ID
|
2001-01-23 07:16:27 +03:00
|
|
|
db_key.data = (void*)&x;
|
|
|
|
db_key.size = sizeof(x);
|
2001-03-20 08:37:56 +03:00
|
|
|
db_data.data = NS_CONST_CAST(char*, keyName.get());
|
2001-01-23 07:16:27 +03:00
|
|
|
db_data.size = keyName.Length();
|
|
|
|
|
|
|
|
if(0 != (*myDB->put)(myDB, &db_key, &db_data, 0)) {
|
|
|
|
printf("--> Error putting\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2001-01-23 06:33:03 +03:00
|
|
|
#endif
|
2001-01-23 07:16:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
(*myDB->sync)(myDB, 0);
|
|
|
|
free(data);
|
2001-01-23 06:33:03 +03:00
|
|
|
}
|
2001-01-23 07:16:27 +03:00
|
|
|
return PR_IntervalToMilliseconds( PR_IntervalNow() - time);
|
|
|
|
}
|
|
|
|
|
2001-01-23 06:33:03 +03:00
|
|
|
|
2001-01-23 07:16:27 +03:00
|
|
|
int
|
|
|
|
readDBM(int cycles)
|
|
|
|
{
|
2001-01-24 04:18:23 +03:00
|
|
|
printf("readDBM\n");
|
2001-01-23 06:33:03 +03:00
|
|
|
// begin timing "lookups"
|
|
|
|
int status = 0 ;
|
2001-01-23 07:16:27 +03:00
|
|
|
DBT db_key, db_data ;
|
2001-01-23 06:33:03 +03:00
|
|
|
PRIntervalTime time = PR_IntervalNow();
|
|
|
|
|
|
|
|
while (cycles--) {
|
2001-01-23 07:16:27 +03:00
|
|
|
for (int x=1; x<=ENTRYCOUNT; x++) {
|
2001-01-23 06:33:03 +03:00
|
|
|
#if USE_ENTRY_ID
|
|
|
|
DBT entry_data;
|
|
|
|
|
|
|
|
db_key.data = (void*)&x;
|
|
|
|
db_key.size = sizeof(x) ;
|
|
|
|
|
|
|
|
status = (*myDB->get)(myDB, &db_key, &entry_data, 0);
|
|
|
|
if(status != 0) {
|
|
|
|
printf("Bad Status %d\n", status);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
db_key.data = entry_data.data;
|
|
|
|
db_key.size = entry_data.size;
|
|
|
|
#else
|
|
|
|
nsCAutoString keyName("foo");
|
|
|
|
keyName.AppendInt(x);
|
|
|
|
db_key.data = (char*)keyName;
|
|
|
|
db_key.size = keyName.Length();
|
|
|
|
#endif
|
|
|
|
status = (*myDB->get)(myDB, &db_key, &db_data, 0);
|
|
|
|
if(status != 0) {
|
|
|
|
printf("Bad Status %d\n", status);
|
|
|
|
return -1;
|
|
|
|
}
|
2001-01-23 07:16:27 +03:00
|
|
|
#if EXTRA_LOOKUP
|
|
|
|
db_key.data = (void*)&x;
|
|
|
|
db_key.size = sizeof(x);
|
|
|
|
|
|
|
|
status = (*myDB->get)(myDB, &db_key, &entry_data, 0);
|
|
|
|
if(status != 0) {
|
|
|
|
printf("Bad Status %d\n", status);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2001-01-23 06:33:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
(*myDB->sync)(myDB, 0);
|
|
|
|
(*myDB->close)(myDB);
|
|
|
|
|
|
|
|
return PR_IntervalToMilliseconds( PR_IntervalNow() - time);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2001-01-23 07:16:27 +03:00
|
|
|
writeFile(int cycles)
|
2001-01-23 06:33:03 +03:00
|
|
|
{
|
2001-01-24 04:18:23 +03:00
|
|
|
printf("writeFile\n");
|
|
|
|
PRFileDesc *fd;
|
|
|
|
PRInt32 fStatus;
|
2001-01-23 06:33:03 +03:00
|
|
|
int x;
|
|
|
|
char * data = (char*) malloc(DATASIZE);
|
|
|
|
for (x=0; x<DATASIZE; x++)
|
|
|
|
*data = '\0';
|
|
|
|
|
2001-01-23 07:16:27 +03:00
|
|
|
PRIntervalTime time = PR_IntervalNow();
|
|
|
|
while (cycles--) {
|
2001-01-24 04:18:23 +03:00
|
|
|
printf("writeFile [cycle=%d]\n", cycles);
|
|
|
|
|
2001-01-23 07:16:27 +03:00
|
|
|
// create "cache" directories
|
2001-01-24 04:18:23 +03:00
|
|
|
PR_MakeDir(TMPDIR "foo", 0755);
|
2001-01-23 06:33:03 +03:00
|
|
|
|
2001-01-23 07:16:27 +03:00
|
|
|
for (x=0; x<32; x++) {
|
2001-01-24 04:18:23 +03:00
|
|
|
nsCAutoString filename; filename.Assign(TMPDIR "foo" DIRSEP);
|
2001-01-23 07:16:27 +03:00
|
|
|
filename.AppendInt(x);
|
2001-01-24 04:18:23 +03:00
|
|
|
PR_MakeDir(filename, 0755);
|
2001-01-23 07:16:27 +03:00
|
|
|
}
|
2001-01-23 06:33:03 +03:00
|
|
|
|
2001-01-23 07:16:27 +03:00
|
|
|
// create "cache" files
|
|
|
|
for (x=1; x<=ENTRYCOUNT; x++) {
|
2001-01-24 04:18:23 +03:00
|
|
|
nsCAutoString filename; filename.Assign(TMPDIR "foo" DIRSEP);
|
2001-01-23 07:16:27 +03:00
|
|
|
filename.AppendInt( x % 32 );
|
2001-01-24 04:18:23 +03:00
|
|
|
filename.Append(DIRSEP);
|
2001-01-23 07:16:27 +03:00
|
|
|
filename.AppendInt( x );
|
2001-01-24 04:18:23 +03:00
|
|
|
|
|
|
|
PRIntervalTime i1, i2, i3;
|
|
|
|
i1 = PR_IntervalNow();
|
|
|
|
|
|
|
|
fd = PR_OpenFile(filename, PR_WRONLY|PR_TRUNCATE, 0644);
|
|
|
|
if (!fd)
|
2001-03-20 08:37:56 +03:00
|
|
|
printf("bad filename? %s\n", filename.get());
|
2001-01-24 04:18:23 +03:00
|
|
|
|
|
|
|
i2 = PR_IntervalNow();
|
|
|
|
|
|
|
|
fStatus = PR_Write(fd, data, DATASIZE);
|
2001-01-23 07:16:27 +03:00
|
|
|
if (fStatus == -1) {
|
2001-01-24 04:18:23 +03:00
|
|
|
printf("Bad fStatus %d\n", PR_GetError());
|
2001-01-23 07:16:27 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
2001-01-24 04:18:23 +03:00
|
|
|
i3 = PR_IntervalNow();
|
|
|
|
|
|
|
|
PR_Close(fd);
|
|
|
|
|
|
|
|
/*
|
|
|
|
printf(" - writing %s [topen=%d twrite=%d]\n",
|
|
|
|
(const char *) filename,
|
|
|
|
PR_IntervalToMilliseconds(i2 - i1),
|
|
|
|
PR_IntervalToMilliseconds(i3 - i2));
|
|
|
|
*/
|
2001-01-23 07:16:27 +03:00
|
|
|
}
|
2001-01-23 06:33:03 +03:00
|
|
|
}
|
2001-01-24 04:18:23 +03:00
|
|
|
return PR_IntervalToMilliseconds(PR_IntervalNow() - time);
|
2001-01-23 07:16:27 +03:00
|
|
|
}
|
|
|
|
|
2001-01-23 06:33:03 +03:00
|
|
|
|
2001-01-23 07:16:27 +03:00
|
|
|
int
|
|
|
|
readFile(int cycles)
|
|
|
|
{
|
2001-01-24 04:18:23 +03:00
|
|
|
printf("readFile\n");
|
|
|
|
PRFileDesc* fd;
|
|
|
|
PRInt32 fStatus;
|
2001-01-23 07:16:27 +03:00
|
|
|
|
2001-01-23 06:33:03 +03:00
|
|
|
// begin timing "lookups"
|
|
|
|
PRIntervalTime time = PR_IntervalNow();
|
|
|
|
while (cycles--) {
|
2001-01-23 07:16:27 +03:00
|
|
|
for (int x=1; x<=ENTRYCOUNT; x++) {
|
2001-01-24 04:18:23 +03:00
|
|
|
nsCAutoString filename; filename.Assign(TMPDIR "foo" DIRSEP);
|
2001-01-23 06:33:03 +03:00
|
|
|
filename.AppendInt( x % 32 );
|
2001-01-24 04:18:23 +03:00
|
|
|
filename.Append(DIRSEP);
|
2001-01-23 06:33:03 +03:00
|
|
|
filename.AppendInt( x );
|
|
|
|
|
2001-01-24 04:18:23 +03:00
|
|
|
fd = PR_OpenFile(filename, PR_RDONLY, 0);
|
2001-01-23 06:33:03 +03:00
|
|
|
|
2001-01-24 04:18:23 +03:00
|
|
|
PRInt32 size = PR_Available(fd);
|
2001-01-23 06:33:03 +03:00
|
|
|
|
|
|
|
char* fdBuffer = (char*) malloc (size);
|
|
|
|
|
2001-01-24 04:18:23 +03:00
|
|
|
PRIntervalTime i1, i2, i3;
|
|
|
|
i1 = PR_IntervalNow();
|
|
|
|
|
|
|
|
fStatus = PR_Read(fd, fdBuffer, size);
|
|
|
|
|
|
|
|
i2 = PR_IntervalNow();
|
2001-01-23 06:33:03 +03:00
|
|
|
|
|
|
|
if (fStatus == -1) {
|
2001-01-24 04:18:23 +03:00
|
|
|
printf("Bad fStatus %d\n", PR_GetError());
|
2001-01-23 06:33:03 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
2001-01-24 04:18:23 +03:00
|
|
|
i3 = PR_IntervalNow();
|
2001-01-23 06:33:03 +03:00
|
|
|
|
2001-01-24 04:18:23 +03:00
|
|
|
PR_Close(fd);
|
|
|
|
free(fdBuffer);
|
|
|
|
|
|
|
|
/*
|
|
|
|
printf(" - reading %s [topen=%d tread=%d]\n",
|
|
|
|
(const char *) filename,
|
|
|
|
PR_IntervalToMilliseconds(i2 - i1),
|
|
|
|
PR_IntervalToMilliseconds(i3 - i2));
|
|
|
|
*/
|
2001-01-23 06:33:03 +03:00
|
|
|
}
|
|
|
|
}
|
2001-01-24 04:18:23 +03:00
|
|
|
return PR_IntervalToMilliseconds(PR_IntervalNow() - time);
|
2001-01-23 06:33:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-23 07:16:27 +03:00
|
|
|
int
|
2001-01-23 06:33:03 +03:00
|
|
|
main(void)
|
|
|
|
{
|
2001-01-24 04:18:23 +03:00
|
|
|
int totalDBMTime = writeDBM(NUM_CYCLES);
|
|
|
|
int totalFileTime = writeFile(NUM_CYCLES);
|
2001-01-23 07:16:27 +03:00
|
|
|
|
|
|
|
printf("total write dbm IO ---- > (%d) milliseconds\n", totalDBMTime);
|
|
|
|
printf("total write file IO ---- > (%d) milliseconds\n", totalFileTime);
|
|
|
|
|
2001-01-24 04:18:23 +03:00
|
|
|
totalDBMTime = readDBM(NUM_CYCLES);
|
|
|
|
totalFileTime = readFile(NUM_CYCLES);
|
2001-01-23 07:16:27 +03:00
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
printf("total read dbm IO ---- > (%d) milliseconds\n", totalDBMTime);
|
|
|
|
printf("total read file IO ---- > (%d) milliseconds\n", totalFileTime);
|
2001-01-23 06:33:03 +03:00
|
|
|
|
2001-01-23 07:16:27 +03:00
|
|
|
return 0;
|
2001-01-23 06:33:03 +03:00
|
|
|
}
|
|
|
|
|