File: watchman.rb

package info (click to toggle)
vim-command-t 5.0.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 760 kB
  • sloc: ruby: 3,433; ansic: 1,177; makefile: 37; xml: 11
file content (76 lines) | stat: -rwxr-xr-x 2,331 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/env ruby
#
# Copyright 2014-present Greg Hurrell. All rights reserved.
# Licensed under the terms of the BSD 2-clause license.

lib  = File.expand_path('../../ruby', File.dirname(__FILE__))
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

require 'command-t/ext'
require 'benchmark'
require 'json'
require 'pathname'
require 'socket'

puts "Starting benchmark run (PID: #{Process.pid})"

TEST_TIMES = 10

Benchmark.bmbm do |b|
  b.report('watchman JSON') do
    TEST_TIMES.times do
      sockname = JSON[%x{watchman get-sockname}]['sockname']
      raise unless $?.exitstatus.zero?
      UNIXSocket.open(sockname) do |s|
        root = Pathname.new(ENV['PWD']).realpath
        s.puts JSON.generate(['watch-list'])
        if !JSON[s.gets]['roots'].include?(root)
          # this path isn't being watched yet; try to set up watch
          s.puts JSON.generate(['watch', root])

          # root_restrict_files setting may prevent Watchman from working
          raise if JSON[s.gets].has_key?('error')
        end

        s.puts JSON.generate(['query', root, {
          'expression' => ['type', 'f'],
          'fields'     => ['name'],
        }])
        paths = JSON[s.gets]

        # could return error if watch is removed
        raise if paths.has_key?('error')
      end
    end
  end

  b.report('watchman binary') do
    TEST_TIMES.times do
      sockname = CommandT::Watchman::Utils.load(
        %x{watchman --output-encoding=bser get-sockname}
      )['sockname']
      raise unless $?.exitstatus.zero?

      UNIXSocket.open(sockname) do |socket|
        root = Pathname.new(ENV['PWD']).realpath.to_s
        roots = CommandT::Watchman::Utils.query(['watch-list'], socket)['roots']
        if !roots.include?(root)
          # this path isn't being watched yet; try to set up watch
          result = CommandT::Watchman::Utils.query(['watch', root], socket)

          # root_restrict_files setting may prevent Watchman from working
          raise if result.has_key?('error')
        end

        query = ['query', root, {
          'expression' => ['type', 'f'],
          'fields'     => ['name'],
        }]
        paths = CommandT::Watchman::Utils.query(query, socket)

        # could return error if watch is removed
        raise if paths.has_key?('error')
      end
    end
  end
end