-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTps.cs
88 lines (73 loc) · 2.24 KB
/
Tps.cs
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
77
78
79
80
81
82
83
84
85
86
87
88
using System.Threading;
using UnityEngine;
namespace MOTD
{
public class Tps
{
private float tps1Min, tps5Min, tps15Min;
public float get1Min() { return tps1Min; }
public float get5Min() { return tps5Min; }
public float get15Min() { return tps15Min; }
public Tps()
{
Thread firstTPSUpdate = new Thread(UpdateTPSOnStart);
Thread tpsUpdateThread = new Thread(TPSUpdate);
firstTPSUpdate.Start();
tpsUpdateThread.Start();
}
private void UpdateTPSOnStart() // updates tps until 1 min left after server start
{
for (int i = 0; i < 55; i++)
{
float currentTPS = Time.timeScale / Time.deltaTime;
tps1Min = (currentTPS + tps1Min) / 2;
tps5Min = tps1Min;
tps15Min = tps1Min;
Thread.Sleep(1000);
}
}
private void TPSUpdate()
{
float tpsSum = 0;
int tpsCount = 0;
float[] masivTPS = new float[15];
while (true)
{
tpsSum += Time.timeScale / Time.deltaTime;
tpsCount++;
if (tpsCount == 60)
{
for (int i = 14; i > 0; i--)
{
masivTPS[i] = masivTPS[i - 1];
}
masivTPS[0] = tpsSum / tpsCount;
ConvertToMins(masivTPS);
tpsSum = 0;
tpsCount = 0;
}
Thread.Sleep(1000); // updates ones per second
}
}
private void ConvertToMins(float[] masivTPS)
{
tps1Min = Calc(masivTPS, 1);
tps5Min = Calc(masivTPS, 5);
tps15Min = Calc(masivTPS, 15);
}
private float Calc(float[] masivTPS, int amount)
{
int k = amount;
float val = 0;
for (int i = 0; i < amount; i++)
{
if (masivTPS[i] == 0)
{
k--;
}
val += masivTPS[i];
}
return val / k;
}
}
}