Stxxl  1.4.0
io/sd_test.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *  io/sd_test.cpp
00003  *
00004  *  Part of the STXXL. See http://stxxl.sourceforge.net
00005  *
00006  *  Copyright (C) 2002 Roman Dementiev <dementiev@mpi-sb.mpg.de>
00007  *
00008  *  Distributed under the Boost Software License, Version 1.0.
00009  *  (See accompanying file LICENSE_1_0.txt or copy at
00010  *  http://www.boost.org/LICENSE_1_0.txt)
00011  **************************************************************************/
00012 
00013 // Tests sim_disk file implementation
00014 // must be run on in-memory swap partition !
00015 
00016 #include <cmath>
00017 #include <stxxl/io>
00018 #include <stxxl/random>
00019 #include <stxxl/aligned_alloc>
00020 
00021 using stxxl::file;
00022 using stxxl::timestamp;
00023 
00024 
00025 int main()
00026 {
00027     const stxxl::int64 disk_size = stxxl::int64(1024 * 1024) * 1024 * 40;
00028     std::cout << sizeof(void *) << std::endl;
00029     const int block_size = 4 * 1024 * 1024;
00030     char * buffer = static_cast<char *>(stxxl::aligned_alloc<BLOCK_ALIGN>(block_size));
00031     memset(buffer, 0, block_size);
00032     const char * paths[2] = { "/tmp/data1", "/tmp/data2" };
00033     stxxl::sim_disk_file file1(paths[0], file::CREAT | file::RDWR /* | file::DIRECT */, 0);
00034     file1.set_size(disk_size);
00035 
00036     stxxl::sim_disk_file file2(paths[1], file::CREAT | file::RDWR /* | file::DIRECT */, 1);
00037     file2.set_size(disk_size);
00038 
00039     unsigned i = 0;
00040 
00041     stxxl::int64 pos = 0;
00042 
00043     stxxl::request_ptr req;
00044 
00045     STXXL_MSG("Estimated time:" << block_size / double(AVERAGE_SPEED));
00046     STXXL_MSG("Sequential write");
00047 
00048     for (i = 0; i < 40; i++)
00049     {
00050         double begin = timestamp();
00051         req = file1.awrite(buffer, pos, block_size, stxxl::default_completion_handler());
00052         req->wait();
00053         double end = timestamp();
00054 
00055         STXXL_MSG("Pos: " << pos << " block_size:" << block_size << " time:" << (end - begin));
00056         pos += 1024 * 1024 * 1024;
00057     }
00058 
00059     double sum = 0.;
00060     double sum2 = 0.;
00061     STXXL_MSG("Random write");
00062     const unsigned int times = 80;
00063     for (i = 0; i < times; i++)
00064     {
00065         stxxl::random_number<> rnd;
00066         pos = rnd(disk_size / block_size) * block_size;
00067         double begin = timestamp();
00068         req = file1.awrite(buffer, pos, block_size, stxxl::default_completion_handler());
00069         req->wait();
00070         double diff = timestamp() - begin;
00071 
00072         sum += diff;
00073         sum2 += diff * diff;
00074 
00075         STXXL_MSG("Pos: " << pos << " block_size:" << block_size << " time:" << (diff));
00076     }
00077 
00078     sum = sum / double(times);
00079     sum2 = sum2 / double(times);
00080     assert(sum2 - sum * sum >= 0.0);
00081     double err = sqrt(sum2 - sum * sum);
00082     STXXL_MSG("Standard Deviation: " << err << " s, " << 100. * (err / sum) << " %");
00083 
00084     stxxl::aligned_dealloc<BLOCK_ALIGN>(buffer);
00085 
00086     unlink(paths[0]);
00087     unlink(paths[1]);
00088 
00089     return 0;
00090 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines