[前][次][番号順一覧][スレッド一覧]

ruby-changes:50173

From: k0kubun <ko1@a...>
Date: Thu, 8 Feb 2018 01:15:26 +0900 (JST)
Subject: [ruby-changes:50173] k0kubun:r62291 (trunk): test_jit.rb: add initial test for JIT

k0kubun	2018-02-08 01:15:21 +0900 (Thu, 08 Feb 2018)

  New Revision: 62291

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=62291

  Log:
    test_jit.rb: add initial test for JIT

  Added files:
    trunk/test/ruby/test_jit.rb
Index: test/ruby/test_jit.rb
===================================================================
--- test/ruby/test_jit.rb	(nonexistent)
+++ test/ruby/test_jit.rb	(revision 62291)
@@ -0,0 +1,58 @@ https://github.com/ruby/ruby/blob/trunk/test/ruby/test_jit.rb#L1
+# frozen_string_literal: true
+require 'test/unit'
+
+# Test for --jit option
+class TestJIT < Test::Unit::TestCase
+  JIT_TIMEOUT = 600 # 10min for each...
+  JIT_SUCCESS_PREFIX = 'JIT success \(\d+\.\dms\)'
+
+  def test_jit
+    assert_eval_with_jit('print proc { 1 + 1 }.call', stdout: '2', success_count: 1)
+  end
+
+  def test_jit_output
+    skip unless jit_available?
+
+    out, err = eval_with_jit('5.times { puts "MJIT" }', verbose: 1, min_calls: 5)
+    assert_equal("MJIT\n" * 5, out)
+    assert_match(/^#{JIT_SUCCESS_PREFIX}: block in <main>@-e:1 -> .+_ruby_mjit_p\d+u\d+\.c$/, err)
+    assert_match(/^Successful MJIT finish$/, err)
+  end
+
+  private
+
+  # Shorthand for normal test cases
+  def assert_eval_with_jit(script, stdout: nil, success_count:)
+    out, err = eval_with_jit(script, verbose: 1, min_calls: 1)
+    actual = err.scan(/^#{JIT_SUCCESS_PREFIX}:/).size
+    assert_equal(
+      success_count, actual,
+      "Expected #{success_count} times of JIT success, but succeeded #{actual} times.\n\n"\
+      "script:\n#{code_block(script)}\nstderr:\n#{code_block(err)}",
+    )
+    if stdout
+      assert_match(stdout, out, "Expected stderr #{out.inspect} to match #{stdout.inspect} with script:\n#{code_block(script)}")
+    end
+  end
+
+  # Run Ruby script with --jit-wait (Synchronous JIT compilation).
+  # Returns [stdout, stderr]
+  def eval_with_jit(script, verbose: 0, min_calls: 5)
+    stdout, stderr, status = EnvUtil.invoke_ruby(
+      ['--disable-gems', '--jit-wait', "--jit-verbose=#{verbose}", "--jit-min-calls=#{min_calls}", '-e', script],
+      '', true, true, timeout: JIT_TIMEOUT,
+    )
+    assert_equal(true, status.success?, "Failed to run script with JIT:\n#{code_block(script)}")
+    [stdout, stderr]
+  end
+
+  def code_block(code)
+    "```\n#{code}\n```\n\n"
+  end
+
+  # If this is false, tests which require JIT should be skipped.
+  # When this is not checked, probably the test expects Ruby to behave in the same way even if JIT is not supported.
+  def jit_available?
+    @jit_available ||= (EnvUtil.invoke_ruby(['--jit', '-e', 'print RubyVM::MJIT.enabled?'], '', true, true).first == 'true')
+  end
+end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]