Ejemplo n.º 1
0
    void testIO(unsigned nr, SocketEndpoint *ep)
    {
        IFile *ifile;
        IFileIO *ifileio;
        unsigned fsize = (unsigned)(((double)nr * (double)rs) / (1024.0 * 1024.0));

        fflush(NULL);
        fprintf(stdout,"\n");
        fflush(NULL);

        for(int j=0; j<2; j++)
        {
            if (j==0)
                fprintf(stdout, "File size: %d (MB) Cache, ", fsize);
            else
                fprintf(stdout, "\nFile size: %d (MB) Nocache, ", fsize);

            if (ep != NULL)
            {
                ifile = createRemoteFile(*ep, tmpfile);
                fprintf(stdout, "Remote: (%s)\n", server.toCharArray());
            }
            else
            {
                ifile = createIFile(tmpfile);
                fprintf(stdout, "Local:\n");
            }

            ifile->remove();

            unsigned st = msTick();

            IFEflags extraFlags = IFEcache;
            if (j==1)
                extraFlags = IFEnocache;
            ifileio = ifile->open(IFOcreate, extraFlags);

            unsigned iter = nr / 40;

            __int64 pos = 0;
            for (int i=0;i<nr;i++)
            {
                ifileio->write(pos, rs, record);
                pos += rs;
                if ((i % iter) == 0)
                {
                    fprintf(stdout,".");
                    fflush(NULL);
                }
            }

            ifileio->close();

            double rsec = (double)(msTick() - st)/1000.0;
            unsigned iorate = (unsigned)((double)fsize / rsec);

            fprintf(stdout, "\nwrite - elapsed time = %6.2f (s) iorate = %4d (MB/s)\n", rsec, iorate);

            st = msTick();

            extraFlags = IFEcache;
            if (j==1)
                extraFlags = IFEnocache;
            ifileio = ifile->open(IFOread, extraFlags);

            pos = 0;
            for (int i=0;i<nr;i++)
            {
                ifileio->read(pos, rs, record);
                pos += rs;
                if ((i % iter) == 0)
                {
                    fprintf(stdout,".");
                    fflush(NULL);
                }
            }

            ifileio->close();

            rsec = (double)(msTick() - st)/1000.0;
            iorate = (unsigned)((double)fsize / rsec);

            fprintf(stdout, "\nread -- elapsed time = %6.2f (s) iorate = %4d (MB/s)\n", rsec, iorate);

            ifileio->Release();
            ifile->remove();
            ifile->Release();
        }
    }
Ejemplo n.º 2
0
void cGeneticAlgorithm::GenerateHtml(const cString& fileNamePrefix)
{
	static int htmlIndex = 0;
	IFileIO * pFile = IFileIO::CreateFileIO();
	GenomeDataList data = m_Population[0].m_Data;
	data.push_back(data[0]);

	if(pFile->VOpenFile(cStringUtilities::MakeFormatted("Routes/%s_%d.html", fileNamePrefix.GetData(), htmlIndex), ios_base::out))
	{
		pFile->VWriteLine("<!DOCTYPE html>\n");
		pFile->VWriteLine("<html lang=\"en\">\n");
		pFile->VWriteLine("\t<head>\n");
		pFile->VWriteLine("\t\t<meta charset=\"utf-8\">\n");
		//pFile->VWriteLine("\t<meta name="viewport" content="initial-scale=1.0, user-scalable=no">\n");
		//pFile->VWriteLine("\t<meta name="description" content="Randy Olson uses machine learning to find the optimal road trip across the U.S.">
		pFile->VWriteLine("\t\t<meta name=\"author\" content=\"Ankur Sheel\">\n");

		pFile->VWriteLine("\t\t<title>The optimal road trip using GA</title>\n");
		pFile->VWriteLine("\t\t<style>\n");
		pFile->VWriteLine("\t\t\thtml, body, #map-canvas {\n");
		pFile->VWriteLine("\t\t\t\theight: 100%;\n");
		pFile->VWriteLine("\t\t\t\tmargin: 0px;\n");
		pFile->VWriteLine("\t\t\t\tpadding: 0px\n");
		pFile->VWriteLine("\t\t\t}\n");
		pFile->VWriteLine("\t\t\t#panel {\n");
		pFile->VWriteLine("\t\t\t\tposition: absolute;\n");
		pFile->VWriteLine("\t\t\t\ttop: 5px;\n");
		pFile->VWriteLine("\t\t\t\tleft: 50%;\n");
		pFile->VWriteLine("\t\t\t\tmargin-left: -180px;\n");
		pFile->VWriteLine("\t\t\t\tz-index: 5;\n");
		pFile->VWriteLine("\t\t\t\tbackground-color: #fff;\n");
		pFile->VWriteLine("\t\t\t\tpadding: 10px;\n");
		pFile->VWriteLine("\t\t\t\tborder: 1px solid #999;\n");
		pFile->VWriteLine("\t\t\t}\n");
		pFile->VWriteLine("\t\t</style>\n");
		pFile->VWriteLine("\t\t<script src=\"https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true\"></script>\n");
		pFile->VWriteLine("\t\t<script>\n");

		const int googleWaypointsAllowed  = 8;
		int numberOfDirectionVariables = static_cast<int>(ceil(data.size() / static_cast<float>(googleWaypointsAllowed)));
		for (int directionIndex = 1; directionIndex <= numberOfDirectionVariables; directionIndex++)
		{
			pFile->VWriteLine(cStringUtilities::MakeFormatted("\t\t\tvar directionsDisplay%d\n", directionIndex));
		}
		pFile->VWriteLine("\t\t\tvar markerOptions = {icon: \"http://maps.gstatic.com/mapfiles/markers2/marker.png\"};\n");
		pFile->VWriteLine("\t\t\tvar directionsDisplayOptions = {preserveViewport: true,\n");
		pFile->VWriteLine("\t\t\t\tmarkerOptions: markerOptions};\n");
		pFile->VWriteLine("\t\t\tvar directionsService = new google.maps.DirectionsService();\n");
		pFile->VWriteLine("\t\t\tvar map;\n");
		pFile->VWriteLine("\t\t\t\n");
		pFile->VWriteLine("\t\t\tfunction initialize() {\n");
		pFile->VWriteLine("\t\t\t\tvar center = new google.maps.LatLng(39, -96);\n");
		pFile->VWriteLine("\t\t\t\tvar mapOptions = {\n");
		pFile->VWriteLine("\t\t\t\t\tzoom: 5,\n");
		pFile->VWriteLine("\t\t\t\t\tcenter: center\n");
		pFile->VWriteLine("\t\t\t\t};\n");
		pFile->VWriteLine("\t\t\t\tmap = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);\n");
		for (int directionIndex = 1; directionIndex <= numberOfDirectionVariables; directionIndex++)
		{
			pFile->VWriteLine(cStringUtilities::MakeFormatted("\t\t\t\tdirectionsDisplay%d.setMap(map);\n", directionIndex));
			pFile->VWriteLine(cStringUtilities::MakeFormatted("\t\t\t\tdirectionsDisplay%d.setPanel(document.getElementById('directionsPanel'));\n", directionIndex));
		}
		pFile->VWriteLine("\t\t\t}\n");
		pFile->VWriteLine("\t\t\t\n");
		pFile->VWriteLine("\t\t\tfunction calcRoute(start, end, routes) {\n");
		pFile->VWriteLine("\t\t\t\tswitch (start) {\n");
		int directionIndex = 1;
		for (int startIndex = 0; startIndex < data.size(); startIndex= startIndex + googleWaypointsAllowed)
		{
			cString route = "\t\t\t\t\tcase \"" + m_WaypointNames[data[startIndex]].GetString() + "\":\n";
			route += cStringUtilities::MakeFormatted("\t\t\t\t\t\tdirectionsDisplay%d= new google.maps.DirectionsRenderer(directionsDisplayOptions);\n", directionIndex);
			route += "\t\t\t\t\t\tbreak;\n";
			pFile->VWriteLine(route);
			directionIndex++;
		}
		pFile->VWriteLine("\t\t\t\t}\n");
		pFile->VWriteLine("\n");
		pFile->VWriteLine("\t\t\t\tvar waypts = [];\n");
		pFile->VWriteLine("\t\t\t\t\tfor (var i = 0; i < routes.length; i++) {\n");
		pFile->VWriteLine("\t\t\t\t\t\twaypts.push({\n");
		pFile->VWriteLine("\t\t\t\t\t\tlocation:routes[i],\n");
		pFile->VWriteLine("\t\t\t\t\t\tstopover:true});\n");
		pFile->VWriteLine("\t\t\t\t}\n");
		pFile->VWriteLine("\n");
		pFile->VWriteLine("\t\t\t\tvar request = \"\";\n");
		pFile->VWriteLine("\t\t\t\tif (waypts.length > 0) {\n");
		pFile->VWriteLine("\t\t\t\t\trequest = {\n");
		pFile->VWriteLine("\t\t\t\t\t\torigin: start,\n");
		pFile->VWriteLine("\t\t\t\t\t\tdestination: end,\n");
		pFile->VWriteLine("\t\t\t\t\t\twaypoints: waypts,\n");
		pFile->VWriteLine("\t\t\t\t\t\toptimizeWaypoints: false,\n");
		pFile->VWriteLine("\t\t\t\t\t\ttravelMode: google.maps.TravelMode.DRIVING\n");
		pFile->VWriteLine("\t\t\t\t\t};\n");
		pFile->VWriteLine("\t\t\t\t}\n");
		pFile->VWriteLine("\t\t\t\telse {\n");
		pFile->VWriteLine("\t\t\t\t\trequest = {\n");
		pFile->VWriteLine("\t\t\t\t\t\torigin: start,\n");
		pFile->VWriteLine("\t\t\t\t\t\tdestination: end,\n");
		pFile->VWriteLine("\t\t\t\t\t\ttravelMode: google.maps.TravelMode.DRIVING\n");
		pFile->VWriteLine("\t\t\t\t\t};\n");
		pFile->VWriteLine("\t\t\t\t}\n");
		pFile->VWriteLine("\t\t\t\tdirectionsService.route(request, function(response, status) {\n");
		pFile->VWriteLine("\t\t\t\t\tif (status == google.maps.DirectionsStatus.OK) {\n");
		pFile->VWriteLine("\t\t\t\t\t\tswitch (start) {\n");
		
		directionIndex = 1;
		for (int startIndex = 0; startIndex < data.size(); startIndex= startIndex + googleWaypointsAllowed)
		{
			cString route = "\t\t\t\t\t\t\tcase \"" + m_WaypointNames[data[startIndex]].GetString() + "\":\n";
			route += cStringUtilities::MakeFormatted("\t\t\t\t\t\t\t\tdirectionsDisplay%d.setDirections(response);\n", directionIndex);
			route += "\t\t\t\t\t\t\t\tbreak;\n";
			pFile->VWriteLine(route);
			directionIndex++;
		}
		pFile->VWriteLine("\t\t\t\t\t\t}\n");
		pFile->VWriteLine("\t\t\t\t\t}\n");
		pFile->VWriteLine("\t\t\t\t});\n");
		pFile->VWriteLine("\t\t\t}\n");
		pFile->VWriteLine("\t\t\tgoogle.maps.event.addDomListener(window, 'load', initialize);\n");
		int waypointIndex = 0;
		while (waypointIndex < data.size())
		{
			int startIndex = waypointIndex;
			int endIndex = startIndex + googleWaypointsAllowed;
			if(endIndex >= data.size())
			{
				endIndex = data.size() - 1;
			}
			cString route = "\t\t\tcalcRoute(\"" + m_WaypointNames[data[startIndex]].GetString() + "\", \"" + m_WaypointNames[data[endIndex]].GetString() + "\", [";
			startIndex++;
			for (; startIndex < endIndex; startIndex++)
			{
				route += "\"" + m_WaypointNames[data[startIndex]].GetString() + "\"";
				if (startIndex < (endIndex - 1))
				{
					route += ", ";
				}
			}
			route += "]);\n";
			waypointIndex += googleWaypointsAllowed;
			pFile->VWriteLine(route);
		}
		pFile->VWriteLine("\t\t</script>\n");
		pFile->VWriteLine("\t</head>\n");
		pFile->VWriteLine("\t<body>\n");
		pFile->VWriteLine("\t\t<div id=\"map-canvas\" style=\"float:left;width:70%; height:100%\"></div>\n");
		pFile->VWriteLine("\t\t<div id=\"directionsPanel\" style=\"float:right;width:30%;height 100%\">\n");
		//pFile->VWriteLine("\t\t<p>Total Distance: <span id=\"total\"></span></p>\n");

		pFile->VWriteLine("\t</body>\n");
		pFile->VWriteLine("</html>\n");
		pFile->VCloseFile();
	}
	SafeDelete(&pFile);
	htmlIndex++;
}
Ejemplo n.º 3
0
void copyCompress(const char *from, const char *to, size32_t rowsize, bool fast, bool flzstrm, bool stats)
{
    Owned<IFile> srcfile = createIFile(from);
    Owned<IFileIO> baseio = srcfile->open(IFOread);
    if (!baseio) {
        printf("ERROR: could not open '%s' for read\n",from);
        doexit(3);
    }
    Owned<ICompressedFileIO> cmpio = createCompressedFileReader(baseio);
    Owned<IFileIOStream>  flzstrmsrc = cmpio?NULL:createFastLZStreamRead(baseio);
    bool plaincopy = false;
    IFileIO *srcio = NULL;
    if (cmpio) {
        srcio = cmpio;
        if (rowsize&&(cmpio->recordSize()==rowsize))
            plaincopy = true;
        else if (!rowsize) {
            if (fast&&(cmpio->method()==COMPRESS_METHOD_FASTLZ))
                plaincopy = true;
            else if (!fast&&(cmpio->method()==COMPRESS_METHOD_LZW))
                plaincopy = true;
        }
    }
    else if (flzstrmsrc) {
        if (flzstrm)
            plaincopy = true;
    }
    else
        srcio = baseio; 
    if (plaincopy) {
        cmpio.clear();
        srcio = baseio.get(); 
    }
    Owned<IFile> dstfile = createIFile(to);
    StringBuffer fulldst;
    if (dstfile->isDirectory()==foundYes) {
        dstfile.clear();
        addPathSepChar(fulldst.append(to)).append(pathTail(from));
        to = fulldst.str();
        dstfile.setown(createIFile(to));
    }

    if (dstfile->exists()) {
        printf("ERROR: file '%s' already exists\n",to);
        doexit(4);
    }
    unsigned start;
    unsigned startu;
    if (stats) {
         start = msTick();
         startu = usTick();
    }
    Owned<IFileIO> dstio;
    Owned<IFileIOStream>  flzstrmdst;
    if (plaincopy||flzstrm) {
        dstio.setown(dstfile->open(IFOcreate));
        if (dstio&&!plaincopy)
            flzstrmdst.setown(createFastLZStreamWrite(dstio));
    }
    else 
        dstio.setown(createCompressedFileWriter(dstfile,rowsize,false,true,NULL,fast));

    if (!dstio) {
        printf("ERROR: could not open '%s' for write\n",to);
        doexit(5);
    }
#ifdef __linux__
    // this is not really needed in windows - if it is we will have to
    // test the file extension - .exe, .bat

    struct stat info;
    if (stat(from, &info) == 0)  // cannot fail - exception would have been thrown above
        dstfile->setCreateFlags(info.st_mode&(S_IRUSR|S_IRGRP|S_IROTH|S_IWUSR|S_IWGRP|S_IWOTH|S_IXUSR|S_IXGRP|S_IXOTH));
#endif
    MemoryAttr mb;
    void * buffer = mb.allocate(BUFFERSIZE);

    offset_t offset = 0;
    try
    {
        loop {
            size32_t got = cmpio.get()?cmpio->read(offset, BUFFERSIZE, buffer):srcio->read(offset, BUFFERSIZE, buffer);
            if (got == 0)
                break;
            if (flzstrmdst)
                flzstrmdst->write(got,buffer);
            else
                dstio->write(offset, got, buffer);
            offset += got;
        }
    }
    catch (IException *e)
    {
        // try to delete partial copy
        dstio.clear();
        try {
            dstfile->remove();
        }
        catch (IException *e2) {
            StringBuffer s;
            pexception(s.clear().append("Removing partial copy file: ").append(to).str(),e2);
            e2->Release();
        }
        throw e;
    }
    flzstrmdst.clear();
    dstio.clear();
    if (stats) 
        printStats(offset,start,startu);
    CDateTime createTime, modifiedTime;
    if (srcfile->getTime(&createTime, &modifiedTime, NULL))
        dstfile->setTime(&createTime, &modifiedTime, NULL);
    printf("copied %s to %s%s\n",from,to,plaincopy?"":" compressing");
    { // print details 
        dstio.setown(dstfile->open(IFOread));
        if (dstio) {
            Owned<ICompressedFileIO> cmpio = createCompressedFileReader(dstio);
            Owned<IFileIOStream>  flzstrm = cmpio?NULL:createFastLZStreamRead(dstio);
            if (cmpio||flzstrm) 
                printCompDetails(to,dstio,cmpio,flzstrm);
            else 
                printf("destination %s not compressed\n",to);
        }
        else
            printf("destination %s could not be read\n",to);
    }
}