milo perf

This commit is contained in:
kepler155c
2018-10-29 16:04:41 -04:00
parent ff892dfac2
commit 46ed01630d
7 changed files with 75 additions and 52 deletions

View File

@@ -150,7 +150,7 @@ UI:setPage(page)
Event.onInterval(5, function() Event.onInterval(5, function()
if not Milo:isCraftingPaused() and context.storage:isOnline() then if not Milo:isCraftingPaused() and context.storage:isOnline() then
Milo:resetCraftingStatus() Milo:resetCraftingStatus()
Milo:refreshItems() --Milo:refreshItems()
for _, task in ipairs(context.tasks) do for _, task in ipairs(context.tasks) do
local s, m = pcall(function() task:cycle(context) end) local s, m = pcall(function() task:cycle(context) end)

View File

@@ -60,6 +60,7 @@ local page = UI.Page {
{ heading = ' Qty', key = 'count' , width = 4, justify = 'right' }, { heading = ' Qty', key = 'count' , width = 4, justify = 'right' },
{ heading = 'Name', key = 'displayName' }, { heading = 'Name', key = 'displayName' },
}, },
values = { },
sortColumn = 'displayName', sortColumn = 'displayName',
help = '^(s)tack, ^(a)ll' help = '^(s)tack, ^(a)ll'
}, },
@@ -103,17 +104,6 @@ local page = UI.Page {
[ 'control-a' ] = 'eject_all', [ 'control-a' ] = 'eject_all',
q = 'quit', q = 'quit',
[ 'control-1' ] = 'eject_1',
[ 'control-2' ] = 'eject_1',
[ 'control-3' ] = 'eject_1',
[ 'control-4' ] = 'eject_1',
[ 'control-5' ] = 'eject_1',
[ 'control-6' ] = 'eject_1',
[ 'control-7' ] = 'eject_1',
[ 'control-8' ] = 'eject_1',
[ 'control-9' ] = 'eject_1',
[ 'control-0' ] = 'eject_1',
}, },
displayMode = 0, displayMode = 0,
} }
@@ -178,10 +168,6 @@ debug('got response')
return response return response
end end
function page.statusBar:draw()
return UI.Window.draw(self)
end
function page.grid:getRowTextColor(row, selected) function page.grid:getRowTextColor(row, selected)
if row.is_craftable then if row.is_craftable then
return colors.yellow return colors.yellow
@@ -195,12 +181,6 @@ end
function page.grid:getDisplayValues(row) function page.grid:getDisplayValues(row)
row = Util.shallowCopy(row) row = Util.shallowCopy(row)
row.count = row.count > 0 and Util.toBytes(row.count) or '' row.count = row.count > 0 and Util.toBytes(row.count) or ''
if row.low then
row.low = Util.toBytes(row.low)
end
if row.limit then
row.limit = Util.toBytes(row.limit)
end
return row return row
end end
@@ -224,6 +204,7 @@ function page:eventHandler(event)
local item = self.grid:getSelected() local item = self.grid:getSelected()
if item then if item then
self:setStatus('requesting stack ...') self:setStatus('requesting stack ...')
-- TODO: send a stack request - have server figure out stack size
local response = self:sendRequest({ request = 'transfer', item = item, count = 64 }) local response = self:sendRequest({ request = 'transfer', item = item, count = 64 })
item.count = response.count item.count = response.count
self.grid:draw() self.grid:draw()
@@ -233,6 +214,7 @@ function page:eventHandler(event)
local item = self.grid:getSelected() local item = self.grid:getSelected()
if item then if item then
self:setStatus('requesting all ...') self:setStatus('requesting all ...')
-- TODO: let server figure out count
local response = self:sendRequest({ request = 'transfer', item = item, count = item.count }) local response = self:sendRequest({ request = 'transfer', item = item, count = item.count })
item.count = response.count item.count = response.count
self.grid:draw() self.grid:draw()
@@ -264,7 +246,6 @@ function page:eventHandler(event)
[1] = 'I', [1] = 'I',
[2] = 'C', [2] = 'C',
} }
event.button.value = (event.button.value + 1) % 3 event.button.value = (event.button.value + 1) % 3
self.displayMode = event.button.value self.displayMode = event.button.value
event.button.text = values[event.button.value] event.button.text = values[event.button.value]
@@ -306,13 +287,17 @@ function page:applyFilter()
self.grid:setValues(t) self.grid:setValues(t)
end end
_G.p4 = Event
debug(options.slot) debug(options.slot)
if options.slot.value then if options.slot.value then
debug('Transfer items initialized') debug('Transfer items initialized')
Event.addRoutine(function() Event.addRoutine(function()
while true do while true do
debug('sleeping')
os.sleep(1.5) os.sleep(1.5)
local neural = device.neuralInterface local neural = device.neuralInterface
debug(neural)
if neural and neural.getInventory then if neural and neural.getInventory then
local item = neural.getInventory().getItem(options.slot.value) local item = neural.getInventory().getItem(options.slot.value)
if item then if item then
@@ -321,6 +306,8 @@ if options.slot.value then
-- local item = -- local item =
-- TODO: update count for this one item -- TODO: update count for this one item
-- page.grid:draw() page:sync() -- page.grid:draw() page:sync()
else
debug('empty')
end end
else else
debug('missing Introspection module') debug('missing Introspection module')

View File

@@ -16,6 +16,8 @@ function Storage:init(args)
listCount = 0, listCount = 0,
activity = { }, activity = { },
storageOnline = true, storageOnline = true,
hits = 0,
misses = 0,
} }
Util.merge(self, defaults) Util.merge(self, defaults)
Util.merge(self, args) Util.merge(self, args)
@@ -29,6 +31,7 @@ debug('%s: %s', e, tostring(dev))
end) end)
Event.onInterval(15, function() Event.onInterval(15, function()
self:showStorage() self:showStorage()
debug('STORAGE: cache: %d/%d', self.hits, self.misses)
end) end)
end end
@@ -43,7 +46,7 @@ function Storage:showStorage()
if #t > 0 then if #t > 0 then
debug('Storage:') debug('Storage:')
for _, k in pairs(t) do for _, k in pairs(t) do
debug(' %s: %s', online and ' online' or 'offline', k) debug(' offline: ' .. k)
end end
debug('') debug('')
end end
@@ -71,6 +74,7 @@ function Storage:initStorage()
elseif device[k] and device[k].list and device[k].size and device[k].pullItems then elseif device[k] and device[k].list and device[k].size and device[k].pullItems then
v.adapter = InventoryAdapter.wrap({ side = k }) v.adapter = InventoryAdapter.wrap({ side = k })
v.adapter.online = true v.adapter.online = true
v.adapter.dirty = true
end end
if v.mtype == 'storage' then if v.mtype == 'storage' then
online = online and not not (v.adapter and v.adapter.online) online = online and not not (v.adapter and v.adapter.online)
@@ -132,6 +136,10 @@ end
function Storage:refresh(throttle) function Storage:refresh(throttle)
self.dirty = true self.dirty = true
debug('STORAGE: Forcing full refresh')
for _, adapter in self:onlineAdapters() do
adapter.dirty = true
end
return self:listItems(throttle) return self:listItems(throttle)
end end
@@ -144,33 +152,35 @@ self.listCount = self.listCount + 1
--debug(self.listCount) --debug(self.listCount)
-- todo: only listItems from dirty remotes -- todo: only listItems from dirty remotes
local ct = os.clock()
local cache = { } local cache = { }
local items = { } local items = { }
throttle = throttle or Util.throttle() throttle = throttle or Util.throttle()
for _, adapter in self:onlineAdapters() do for _, adapter in self:onlineAdapters() do
adapter:listItems(throttle) if adapter.dirty then
debug('STORAGE: refresh: ' .. adapter.name)
adapter:listItems(throttle)
adapter.dirty = false
end
local rcache = adapter.cache or { } local rcache = adapter.cache or { }
-- TODO: add a method in each adapter that only updates a passed cache -- TODO: add a method in each adapter that only updates a passed cache
for key,v in pairs(rcache) do for key,v in pairs(rcache) do
if v.count > 0 then local entry = cache[key]
local entry = cache[key] if not entry then
if not entry then entry = Util.shallowCopy(v)
entry = Util.shallowCopy(v) entry.count = v.count
entry.count = v.count entry.key = key
entry.key = key cache[key] = entry
cache[key] = entry table.insert(items, entry)
table.insert(items, entry) else
else entry.count = entry.count + v.count
entry.count = entry.count + v.count
end
throttle()
end end
throttle()
end end
end end
debug('STORAGE: refresh in ' .. (os.clock() - ct))
self.dirty = false self.dirty = false
self.cache = cache self.cache = cache
@@ -185,6 +195,29 @@ end
function Storage:provide(item, qty, slot, direction) function Storage:provide(item, qty, slot, direction)
local total = 0 local total = 0
local key = item.key or table.concat({ item.name, item.damage, item.nbtHash }, ':')
for _, adapter in self:onlineAdapters() do
if adapter.cache and adapter.cache[key] then
local amount = adapter:provide(item, qty, slot, direction or self.localName)
if amount > 0 then
self.hits = self.hits + 1
debug('EXT: %s(%d): %s -> %s%s',
item.name, amount, adapter.name, direction or self.localName,
slot and string.format('[%d]', slot) or '')
self.dirty = true
adapter.dirty = true
end
qty = qty - amount
total = total + amount
if qty <= 0 then
return total
end
end
end
debug('miss: %s - %d', key, qty)
self.misses = self.misses + 1
for _, adapter in self:onlineAdapters() do for _, adapter in self:onlineAdapters() do
local amount = adapter:provide(item, qty, slot, direction or self.localName) local amount = adapter:provide(item, qty, slot, direction or self.localName)
if amount > 0 then if amount > 0 then
@@ -241,6 +274,14 @@ debug('INS: %s(%d): %s[%d] -> %s',
adapter.dirty = true adapter.dirty = true
local entry = self.activity[key] or 0 local entry = self.activity[key] or 0
self.activity[key] = entry + amount self.activity[key] = entry + amount
--[[
local cached = adapter.cache[key]
if cached then
cached.count = cached.count + amount
else
end
]]
end end
qty = qty - amount qty = qty - amount
total = total + amount total = total + amount

View File

@@ -6,6 +6,7 @@ local Autocraft = {
priority = 100, priority = 100,
} }
-- TODO: fix/test
function Autocraft:cycle(context) function Autocraft:cycle(context)
local list = { } local list = { }

View File

@@ -8,6 +8,8 @@ local device = _G.device
local context = Milo:getContext() local context = Milo:getContext()
-- TODO: allow change of machine
local itemPage = UI.Page { local itemPage = UI.Page {
titleBar = UI.TitleBar { titleBar = UI.TitleBar {
title = 'Limit Resource', title = 'Limit Resource',

View File

@@ -89,23 +89,15 @@ local listingPage = UI.Page {
accelerators = { accelerators = {
r = 'refresh', r = 'refresh',
[ 'control-r' ] = 'refresh', [ 'control-r' ] = 'refresh',
q = 'quit',
[ 'control-e' ] = 'eject', [ 'control-e' ] = 'eject',
[ 'control-s' ] = 'eject_stack', [ 'control-s' ] = 'eject_stack',
[ 'control-a' ] = 'eject_all', [ 'control-a' ] = 'eject_all',
[ 'control-1' ] = 'eject_1',
[ 'control-2' ] = 'eject_1',
[ 'control-3' ] = 'eject_1',
[ 'control-4' ] = 'eject_1',
[ 'control-5' ] = 'eject_1',
[ 'control-6' ] = 'eject_1',
[ 'control-7' ] = 'eject_1',
[ 'control-8' ] = 'eject_1',
[ 'control-9' ] = 'eject_1',
[ 'control-0' ] = 'eject_1',
[ 'control-m' ] = 'machines', [ 'control-m' ] = 'machines',
[ 'control-l' ] = 'resume', [ 'control-l' ] = 'resume',
q = 'quit',
}, },
displayMode = 0, displayMode = 0,
} }

View File

@@ -37,7 +37,7 @@ local function client(socket)
end end
debug('remote: ' .. data.request) debug('remote: ' .. data.request)
if data.request == 'list' then if data.request == 'list' then
local items = Milo:refreshItems() local items = Milo:listItems()
Milo:mergeResources(items) Milo:mergeResources(items)
socket:write(items) socket:write(items)