File size: 2,079 Bytes
b6a38d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
if Platform.developer then

function MeasureMaxGameSpeedAchievable(speed_test_time, lo, hi)
	if not IsRealTimeThread() then
		CreateRealTimeThread(function() MeasureMaxGameSpeedAchievable(speed_test_time, lo, hi) end)
		return
	end
	
	speed_test_time = speed_test_time or 10000
	lo = lo or 1000
	hi = hi or 10000
	
	table.change(config, "MeasureMaxGameSpeedAchievable", {
		StoryBitsSuspended = true
	})
	
	local old_ignoreerrors = IgnoreDebugErrors(true)
	Msg("LuaPerformanceBegin")
	hr.GameTimeBehindDetect = true
	
	local time_factor = (lo < const.DefaultTimeFactor) and const.DefaultTimeFactor or lo
	while hi - lo >= 1000 do
		print(string.format("<color 128 128 128>Testing time factor</color> <color 255 255 255>%d</color> <color 128 128 128>for %dms(+%dms tolerance)</color>", time_factor, speed_test_time, config.GameTimeBehindTimeTolerance))
		SetTimeFactor(time_factor)
		local start_time = GetPreciseTicks()
		while (not hr.GameTimeBehindFlag) and (GetPreciseTicks() - start_time < speed_test_time + config.GameTimeBehindTimeTolerance) do
			Sleep(1000)
		end
		if hr.GameTimeBehindFlag then
			hi = time_factor
			print(string.format("<color 128 128 128>Time Factor</color> %d <color 255 0 0>FAIL</color>", time_factor))
		else
			lo = time_factor
			print(string.format("<color 128 128 128>Time Factor</color> %d <color 0 255 0>SUCCESS</color>", time_factor))
		end
		time_factor = (lo + hi) / 2
	end
	print(string.format("Max Time Factor: %d", lo))
	
	hr.GameTimeBehindDetect = false
	Msg("LuaPerformanceEnd")
	IgnoreDebugErrors(old_ignoreerrors)
	
	table.restore(config, "MeasureMaxGameSpeedAchievable")
	
	return lo
end

function MeasureLuaPerformance(time)
	time = time or 10000
	
	CreateRealTimeThread(function()
		local start_time = GetPreciseTicks()
		local lo, hi = 1000, 100000
		local time_factor = MeasureMaxGameSpeedAchievable(time, lo, hi)
		print(string.format("Time factor supported: <color 0 255 0>%d</color>", time_factor))
		ReloadLua()
		print(string.format("Measurements executed in %ds", (GetPreciseTicks() - start_time) / 1000))
	end)
end

end