-
Notifications
You must be signed in to change notification settings - Fork 0
/
uConStatSum.lua
executable file
·93 lines (85 loc) · 2.59 KB
/
uConStatSum.lua
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
#!/usr/bin/luajit
local table_insert = table.insert
local function split(str, sep)
local ret = {}
for item in string.gmatch(str, "([^" .. sep .. "]+)") do
table_insert(ret, item)
end
return ret
end
local function readfile(filename)
local file = assert(io.open(filename, "r"))
local first = true
local ret = {}
local header
for line in file:lines() do
if (first) then
first = false
header = split(line, ",")
else
if line:sub(1, 1) ~= "#" then
local row = {}
ret[#ret + 1] = row
for i, v in ipairs(split(line, ",")) do
row[header[i]] = tonumber(v)
end
row.watt = row.current / 1000 * row.voltage
end
end
end
return ret
end
local function sum(data, columns)
columns = type(columns) == "table" and columns or { columns }
local ret = {}
for _, col in ipairs(columns) do
ret[col] = {
min = 10000000000,
max = -10000000000,
avg = 0,
sum = 0,
runtime = 0,
values = {}
}
end
for r, row in ipairs(data) do
ret.runtime = row.runtime
for _, col in ipairs(columns) do
table.insert(ret[col].values, row[col])
ret[col].sum = ret[col].sum + row[col]
ret[col].min = math.min(ret[col].min, row[col])
ret[col].max = math.max(ret[col].max, row[col])
end
end
for _, col in ipairs(columns) do
local count = #ret[col].values
ret[col].avg = ret[col].sum / count
table.sort(ret[col].values)
if math.fmod(count, 2) == 0 then
ret[col].median = (ret[col].values[count / 2] + ret[col].values[count / 2 + 1]) / 2
else
ret[col].median = ret[col].values[math.ceil(count / 2)]
end
end
return ret
end
if #arg == 0 then
io.stderr:write([[
usage: sum.lua [filename1] [filename2] ... [filenameN]
]])
os.exit(1)
end
for _, filename in ipairs(arg) do
local stat = sum(readfile(filename), { 'clock', 'current', 'voltage', 'watt', 'temp' })
local data_name = filename:sub(-4, -1) == ".csv" and filename:sub(1, -5) or filename
print(data_name)
print(",average,min,max,median")
for _, name in ipairs { 'clock', 'current', 'voltage', 'watt', 'temp' } do
print(table.concat({
name,
stat[name].avg, stat[name].min, stat[name].max, stat[name].median
}, ","))
end
print("runtime," .. stat.runtime)
print ""
end