wakatime/wakatime-cli

High CPU usage for wakatime-cli --today

MaximilianGaedig opened this issue · 3 comments

Expected behavior (what should have happened):
Lower CPU usage

Actual behavior (what went wrong):
Running neovim with the wakatime plugin I get a wakatime-cli with 10% CPU usage every ~15s

Environment:

  • OS: Linux
  • Platform: aarch64(M1 Pro)

Logs:

There are logs, but they do not get generated usually, only in some unrelated cases

Please enable debug logging then send or attach your ~/.wakatime/wakatime.log file. I want to compare the timestamps of every logging step to see which step is taking the most time to complete, because that's most likely the part using CPU.

@gandarez when debug is enabled can we add current CPU and MEM usage to each log?
https://stackoverflow.com/questions/11356330/how-to-get-cpu-usage

The only message I am getting after turning on debug each time the problem occurs: "{"caller":"cmd/today/today.go:30","func":"today.Run","level":"debug","message":"successfully fetched today for status bar","now":"2023-10-14T12:02:29+02:00","os/arch":"linux/arm64","version":"v1.85.2"}", I assume this is an expensive operation, but I don't see much of a reason why, it calls the api and just returns the result no?

Just to calrify, every 10s my statusline plugin is calling wakatime-cli --today which is causing the issue, not the official wakatime nvim plugin.

My plugin:

local Job = require 'plenary.job'
local async = require 'plenary.async'
local uv = require 'luv'

local get_wakatime_time = function()
  local tx, rx = async.control.channel.oneshot()
  local ok, job = pcall(Job.new, Job, {
    command = os.getenv 'HOME' .. '/.wakatime/wakatime-cli',
    args = { '--today' },
    on_exit = function(j, _)
      tx(j:result()[1] or '')
    end,
  })
  if not ok then
    vim.notify('Bad WakaTime call: ' .. job, 'warn')
    return ''
  end

  job:start()
  return rx()
end

---@diagnostic disable
local state = { comp_wakatime_time = '' }

-- Yield statusline value
local wakatime = function()
  local WAKATIME_UPDATE_INTERVAL = 10000

  if not Wakatime_routine_init then
    local timer = uv.new_timer()
    if timer == nil then
      return ''
    end
    -- Update wakatime every 10s
    uv.timer_start(timer, 500, WAKATIME_UPDATE_INTERVAL, function()
      async.run(get_wakatime_time, function(time)
        state.comp_wakatime_time = time
      end)
    end)
    Wakatime_routine_init = true
  end

  return state.comp_wakatime_time
end

return wakatime

I have now reduced the time to every 2 minutes, 10s is too frequent anyways, but I'd say this still should not be using 10% of my CPU when running it.

Also thanks for the fast reply :)

Should be fixed with #960.