File: test_sim_disk.cpp

package info (click to toggle)
libstxxl 1.4.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,476 kB
  • sloc: cpp: 45,101; ansic: 4,071; perl: 610; sh: 555; xml: 174; makefile: 18
file content (89 lines) | stat: -rw-r--r-- 2,725 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/***************************************************************************
 *  tests/io/test_sim_disk.cpp
 *
 *  Part of the STXXL. See http://stxxl.sourceforge.net
 *
 *  Copyright (C) 2002 Roman Dementiev <dementiev@mpi-sb.mpg.de>
 *
 *  Distributed under the Boost Software License, Version 1.0.
 *  (See accompanying file LICENSE_1_0.txt or copy at
 *  http://www.boost.org/LICENSE_1_0.txt)
 **************************************************************************/

// Tests sim_disk file implementation
// must be run on in-memory swap partition !

#include <cmath>
#include <stxxl/io>
#include <stxxl/random>
#include <stxxl/aligned_alloc>

using stxxl::file;
using stxxl::timestamp;

int main()
{
    const stxxl::int64 disk_size = stxxl::int64(1024 * 1024) * 1024 * 40;
    std::cout << sizeof(void*) << std::endl;
    const int block_size = 4 * 1024 * 1024;
    char* buffer = static_cast<char*>(stxxl::aligned_alloc<STXXL_BLOCK_ALIGN>(block_size));
    memset(buffer, 0, block_size);
    const char* paths[2] = { "/tmp/data1", "/tmp/data2" };
    stxxl::sim_disk_file file1(paths[0], file::CREAT | file::RDWR | file::DIRECT, 0);
    file1.set_size(disk_size);

    stxxl::sim_disk_file file2(paths[1], file::CREAT | file::RDWR | file::DIRECT, 1);
    file2.set_size(disk_size);

    unsigned i = 0;

    stxxl::int64 pos = 0;

    stxxl::request_ptr req;

    STXXL_MSG("Estimated time:" << block_size / stxxl::simdisk_geometry::s_average_speed);
    STXXL_MSG("Sequential write");

    for (i = 0; i < 40; i++)
    {
        double begin = timestamp();
        req = file1.awrite(buffer, pos, block_size);
        req->wait();
        double end = timestamp();

        STXXL_MSG("Pos: " << pos << " block_size:" << block_size << " time:" << (end - begin));
        pos += 1024 * 1024 * 1024;
    }

    double sum = 0.;
    double sum2 = 0.;
    STXXL_MSG("Random write");
    const unsigned int times = 80;
    stxxl::random_number<> rnd;
    for (i = 0; i < times; i++)
    {
        pos = (stxxl::int64)rnd(disk_size / block_size) * block_size;
        double begin = timestamp();
        req = file1.awrite(buffer, pos, block_size);
        req->wait();
        double diff = timestamp() - begin;

        sum += diff;
        sum2 += diff * diff;

        STXXL_MSG("Pos: " << pos << " block_size:" << block_size << " time:" << (diff));
    }

    sum = sum / double(times);
    sum2 = sum2 / double(times);
    STXXL_CHECK(sum2 - sum * sum >= 0.0);
    double err = sqrt(sum2 - sum * sum);
    STXXL_MSG("Standard Deviation: " << err << " s, " << 100. * (err / sum) << " %");

    stxxl::aligned_dealloc<STXXL_BLOCK_ALIGN>(buffer);

    file1.close_remove();
    file2.close_remove();

    return 0;
}