giostream.lua 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #! /usr/bin/env lua
  2. --
  3. -- Sample lgi application for Gio files, streams and usage of
  4. -- Gio.Async It also serves as very crude timing 'benchmark' showing
  5. -- overhead of assorted types operations.
  6. --
  7. local lgi = require 'lgi'
  8. local GLib = lgi.GLib
  9. local Gio = lgi.Gio
  10. local assert = lgi.assert
  11. local app = Gio.Application { application_id = 'org.lgi.samples.giostream' }
  12. local function read_lua(file)
  13. local stream = io.open(file:get_path(), 'r')
  14. local contents = stream:read('*a')
  15. stream:close()
  16. return contents
  17. end
  18. local function write_lua(file, contents)
  19. local stream = io.open(file:get_path(), 'w')
  20. stream:write(contents)
  21. stream:close()
  22. end
  23. local function read_sync(file)
  24. local info = assert(file:query_info('standard::size', 0))
  25. local stream = assert(file:read())
  26. local read_buffers = {}
  27. local remaining = info:get_size()
  28. while remaining > 0 do
  29. local buffer = assert(stream:read_bytes(remaining))
  30. table.insert(read_buffers, buffer.data)
  31. remaining = remaining - #buffer
  32. end
  33. assert(stream:close())
  34. return table.concat(read_buffers)
  35. end
  36. local function read_async(file)
  37. local info = assert(file:async_query_info('standard::size', 'NONE'))
  38. local stream = assert(file:async_read())
  39. local read_buffers = {}
  40. local remaining = info:get_size()
  41. while remaining > 0 do
  42. local buffer = assert(stream:async_read_bytes(remaining))
  43. table.insert(read_buffers, buffer.data)
  44. remaining = remaining - #buffer
  45. end
  46. stream:async_close()
  47. return table.concat(read_buffers)
  48. end
  49. local function write_sync(file, contents)
  50. local stream = assert(file:create('NONE'))
  51. local pos = 1
  52. while pos <= #contents do
  53. local wrote, err = stream:write_bytes(GLib.Bytes(contents:sub(pos)))
  54. assert(wrote >= 0, err)
  55. pos = pos + wrote
  56. end
  57. end
  58. local function write_async(file, contents)
  59. local stream = assert(file:async_create('NONE'))
  60. local pos = 1
  61. while pos <= #contents do
  62. local wrote, err = stream:async_write_bytes(GLib.Bytes(contents:sub(pos)))
  63. assert(wrote >= 0, err)
  64. pos = pos + wrote
  65. end
  66. end
  67. local source_file = Gio.File.new_for_commandline_arg(arg[0])
  68. local function perform(read_op, write_op, target_file)
  69. app:hold()
  70. io.write('+')
  71. local contents = read_op(source_file)
  72. target_file:delete()
  73. write_op(target_file, contents)
  74. local contents_copied = read_op(target_file)
  75. assert(contents == contents_copied)
  76. assert(target_file:delete())
  77. io.write('.')
  78. app:release()
  79. end
  80. local count = 100
  81. local timer = GLib.Timer()
  82. -- Perform sync standard-lua variant of the test.
  83. timer:reset()
  84. for i = 1, count do
  85. perform(read_lua, write_lua, Gio.File.new_for_path('test-lua-' .. i))
  86. end
  87. print((("\n Lua %0.2f secs"):format(timer:elapsed())))
  88. -- Perform sync variant of the test.
  89. timer:reset()
  90. for i = 1, count do
  91. perform(read_sync, write_sync, Gio.File.new_for_path('test-sync-' .. i))
  92. end
  93. print((("\n sync %0.2f secs"):format(timer:elapsed())))
  94. -- Perform async variant of the test.
  95. timer:reset()
  96. for i = 1, count do
  97. Gio.Async.call(perform)(read_async, write_async,
  98. Gio.File.new_for_path('test-async-' .. i))
  99. end
  100. print((("\n async %0.2f secs"):format(timer:elapsed())))
  101. -- Perform parallel variant of the test.
  102. function app:on_activate()
  103. for i = 1, count do
  104. Gio.Async.start(perform)(read_async, write_async,
  105. Gio.File.new_for_path('test-parallel-' .. i))
  106. end
  107. end
  108. timer:reset()
  109. app:run(...)
  110. print((("\n parallel %0.2f secs"):format(timer:elapsed())))