-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpipestat.h
91 lines (71 loc) · 1.75 KB
/
pipestat.h
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
#pragma once
struct pipestat
{
__int64 totalBytes = 0;
DWORD ticksBegin = 0;
DWORD ticksLastStatus = 0;
std::mutex& outputMutex;
bool quiet = false;
pipestat(std::mutex& outputMutex, bool quiet)
: ticksBegin(::GetTickCount())
, outputMutex(outputMutex)
, quiet(quiet)
{
ticksLastStatus = ticksBegin;
}
void accumulate(__int64 len)
{
totalBytes += len;
if (quiet || !totalBytes) {
return;
}
DWORD tick = ::GetTickCount();
DWORD ticksTotal = tick - ticksBegin;
DWORD ticksSince = tick - ticksLastStatus;
DWORD threshold = 1000;
if (ticksTotal <= 15000) {
threshold = 1000;
} else if (ticksTotal <= 25000) {
threshold = 2000;
} else if (ticksTotal < 60000) {
threshold = 5000;
} else {
threshold = 10000;
}
if (ticksSince < threshold) {
return;
}
ticksLastStatus = tick;
display("Processing... ");
}
void finalize()
{
if (quiet || !totalBytes) {
return;
}
ticksLastStatus = ::GetTickCount();
display("Total ");
}
void display(const char* prefix)
{
DWORD ticksTotal = ticksLastStatus - ticksBegin;
double totalSeconds = ticksTotal / 1000.0;
if (totalSeconds == 0.0) {
totalSeconds = 0.1;
}
double totalKilobytes = totalBytes / 1024.0;
double kilobytesPerSec = totalKilobytes / totalSeconds;
std::unique_lock<std::mutex> lock(outputMutex);
if (totalSeconds <= 1.0) {
nowide::cerr << prefix << std::setw(9) << static_cast<int>(totalKilobytes)
<< " kb in " << " < 1 second"
<< std::endl;
}
else {
nowide::cerr << prefix << std::setw(9) << static_cast<int>(totalKilobytes)
<< " kb in " << std::setw(4) << static_cast<int>(totalSeconds) << " seconds ("
<< std::setw(6) << static_cast<int>(kilobytesPerSec) << " kb/sec)"
<< std::endl;
}
}
};