Back to index

lightning-sunbird  0.9+nobinonly
TestAtoms.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 #include "nsIAtom.h"
00038 #include "nsString.h"
00039 #include "nsReadableUtils.h"
00040 #include "prprf.h"
00041 #include "prtime.h"
00042 #include <stdio.h>
00043 
00044 extern "C" int _CrtSetDbgFlag(int);
00045 
00046 int main(int argc, char** argv)
00047 {
00048   FILE* fp = fopen("words.txt", "r");
00049   if (nsnull == fp) {
00050     printf("can't open words.txt\n");
00051     return -1;
00052   }
00053 
00054   PRInt32 count = 0;
00055   PRUnichar** strings = new PRUnichar*[60000];
00056   nsIAtom** ids = new nsIAtom*[60000];
00057   nsAutoString s1, s2;
00058   PRTime start = PR_Now();
00059   PRInt32 i;
00060   for (i = 0; i < 60000; i++) {
00061     char buf[1000];
00062     char* s = fgets(buf, sizeof(buf), fp);
00063     if (nsnull == s) {
00064       break;
00065     }
00066     nsCAutoString sb;
00067     sb.Assign(buf);
00068     strings[count++] = ToNewUnicode(sb);
00069     ToUpperCase(sb);
00070     strings[count++] = ToNewUnicode(sb);
00071   }
00072   PRTime end0 = PR_Now();
00073 
00074   // Find and create idents
00075   for (i = 0; i < count; i++) {
00076     ids[i] = NS_NewAtom(strings[i]);
00077   }
00078   PRUnichar qqs[1]; qqs[0] = 0;
00079   nsIAtom* qq = NS_NewAtom(qqs);
00080   PRTime end1 = PR_Now();
00081 
00082   // Now make sure we can find all the idents we just made
00083   for (i = 0; i < count; i++) {
00084     const char *utf8String;
00085     ids[i]->GetUTF8String(&utf8String);
00086     nsIAtom* id = NS_NewAtom(utf8String);
00087     if (id != ids[i]) {
00088       id->ToString(s1);
00089       ids[i]->ToString(s2);
00090       printf("find failed: id='%s' ids[%d]='%s'\n",
00091              NS_LossyConvertUCS2toASCII(s1).get(), i, NS_LossyConvertUCS2toASCII(s2).get());
00092       return -1;
00093     }
00094     NS_RELEASE(id);
00095   }
00096   PRTime end2 = PR_Now();
00097 
00098   // Destroy all the atoms we just made
00099   NS_RELEASE(qq);
00100   for (i = 0; i < count; i++) {
00101     NS_RELEASE(ids[i]);
00102   }
00103 
00104   // Print out timings
00105   PRTime end3 = PR_Now();
00106   PRTime creates, finds, lookups, dtor, ustoms;
00107   LL_I2L(ustoms, 1000);
00108   LL_SUB(creates, end0, start);
00109   LL_DIV(creates, creates, ustoms);
00110   LL_SUB(finds, end1, end0);
00111   LL_DIV(finds, finds, ustoms);
00112   LL_SUB(lookups, end2, end1);
00113   LL_DIV(lookups, lookups, ustoms);
00114   LL_SUB(dtor, end3, end2);
00115   char buf[500];
00116   PR_snprintf(buf, sizeof(buf), "making %d ident strings took %lldms",
00117               count, creates);
00118   puts(buf);
00119   PR_snprintf(buf, sizeof(buf), "%d new idents took %lldms",
00120               count, finds);
00121   puts(buf);
00122   PR_snprintf(buf, sizeof(buf), "%d ident lookups took %lldms",
00123               count, lookups);
00124   puts(buf);
00125   PR_snprintf(buf, sizeof(buf), "dtor took %lldusec", dtor);
00126   puts(buf);
00127 
00128   printf("%d live atoms\n", NS_GetNumberOfAtoms());
00129   NS_POSTCONDITION(0 == NS_GetNumberOfAtoms(), "dangling atoms");
00130 
00131   return 0;
00132 }