File: cmp.cc

package info (click to toggle)
snapper 0.2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,576 kB
  • ctags: 2,049
  • sloc: cpp: 15,605; sh: 11,424; ansic: 1,401; makefile: 287; python: 159
file content (113 lines) | stat: -rw-r--r-- 2,582 bytes parent folder | download
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113

#include <algorithm>
#include <iostream>
#include <fstream>

#include "snapper/Log.h"
#include "snapper/AppUtil.h"
#include "snapper/File.h"
#include "snapper/Filesystem.h"
#include "snapper/SnapperTmpl.h"


using namespace std;
using namespace snapper;


struct helper
{
    helper(vector<string>& result)
	: result(result) {}
    void operator()(const string& name, unsigned int status)
	{ result.push_back(name + " " + statusToString(status)); }
    vector<string>& result;
};


bool
cmp(const string& fstype, const string& subvolume, unsigned int num1, unsigned int num2)
{
    Filesystem* filesystem = Filesystem::create(fstype, subvolume);

    SDir dir1 = filesystem->openSnapshotDir(num1);
    SDir dir2 = filesystem->openSnapshotDir(num2);

    vector<string> result1;
    double t1;

    {
	StopWatch sw1;

#if 1
	cmpdirs_cb_t cb1 = helper(result1);
#else
	cmpdirs_cb_t cb1 = [&result1](const string& name, unsigned int status) {
	    result1.push_back(name + " " + statusToString(status));
	};
#endif

	filesystem->cmpDirs(dir1, dir2, cb1);

	t1 = sw1.read();
	y2mil("stopwatch1 " << sw1);

	sort(result1.begin(), result1.end());
    }

    vector<string> result2;
    double t2;

    {
	StopWatch sw2;

#if 1
	cmpdirs_cb_t cb2 = helper(result2);
#else
	cmpdirs_cb_t cb2 = [&result2](const string& name, unsigned int status) {
	    result2.push_back(name + " " + statusToString(status));
	};
#endif

	snapper::cmpDirs(dir1, dir2, cb2);

	t2 = sw2.read();
	y2mil("stopwatch2 " << sw2);

	sort(result2.begin(), result2.end());
    }

    y2mil("speedup " << fixed << 100.0 * (t2 - t1) / t1 << "% (" << t1 << "s vs. " << t2 << "s)");

    std::ofstream fout1(("/tmp/result1-" + decString(num1) + "-" + decString(num2)).c_str());
    for (vector<string>::const_iterator it = result1.begin(); it != result1.end(); ++it)
	fout1 << *it << endl;

    std::ofstream fout2(("/tmp/result2-" + decString(num1) + "-" + decString(num2)).c_str());
    for (vector<string>::const_iterator it = result2.begin(); it != result2.end(); ++it)
	fout2 << *it << endl;

    bool ok = result1 == result2;

    cout << fstype << " " << subvolume << " " << num1 << " " << num2
	 << (ok ? " ok" : " failure") << endl;

    return ok;
}


int
main(int argc, char** argv)
{
    if (argc != 5)
    {
	cerr << "usage: fstype subvolume num1 num2" << endl;
	exit(EXIT_FAILURE);
    }

    string fstype = argv[1];
    string subvolume = argv[2];
    unsigned int num1 = atoi(argv[3]);
    unsigned int num2 = atoi(argv[4]);

    return cmp(fstype, subvolume, num1, num2) ? EXIT_SUCCESS : EXIT_FAILURE;
}