Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 77 additions & 16 deletions logstf-test/logstf-test.sp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
#pragma newdecls required

#include <sourcemod>
#include <profiler>


#define PLUGIN_VERSION "1.0.0"

#define PLUGIN_VERSION "2.0.0"

public Plugin myinfo = {
name = "Test: LogsTF",
Expand All @@ -16,32 +15,94 @@ public Plugin myinfo = {
};


#define MAX_LINES 10000
#define MAX_LINE_LENGTH 1024

int ReadFileLines(const char[] filePath, char[][] lines, int maxLines, int maxLineLength) {
File file = OpenFile(filePath, "rt");
if (file == null) {
return -1;
}

int count = 0;
char rawLine[MAX_LINE_LENGTH];

while (!IsEndOfFile(file)) {
if (!ReadFileLine(file, rawLine, sizeof(rawLine))) {
break;
}

if (count >= maxLines) {
SetFailState("Too many lines in %s, increase MAX_LINES", filePath);
return 0;
}

// Remove trailing newline/carriage return
int len = strlen(rawLine);
while (len > 0 && (rawLine[len - 1] == '\n' || rawLine[len - 1] == '\r')) {
rawLine[len - 1] = '\0';
len--;
}

// Remove the 25-character timestamp prefix
if (len > 25) {
strcopy(lines[count], maxLineLength, rawLine[25]);
} else {
// Invalid line, skip it.
continue;
}

count++;
}

delete file;
return count;
}

public void OnPluginStart() {
RegConsoleCmd("test_logstf", Command_test_logstf);
RegConsoleCmd("test_logstf_bench", Command_test_logstf_bench);
}

public Action Command_test_logstf(int client, int args) {
char logline[1024];
File file = OpenFile("testlog.log", "rt");
if (file == null) {
char lines[MAX_LINES][MAX_LINE_LENGTH];
int lineCount = ReadFileLines("testlog.log", lines, MAX_LINES, MAX_LINE_LENGTH);
if (lineCount < 0) {
PrintToServer("Failed to open testlog.log");
return Plugin_Handled;
}

while (!IsEndOfFile(file)) {
if (!ReadFileLine(file, logline, sizeof(logline))) {
PrintToServer("Failed to read line from testlog.log");
delete file;
return Plugin_Handled;
}
for (int i = 0; i < lineCount; i++) {
LogToGame("%s", lines[i]);
}

PrintToServer("Logged %d lines from testlog.log", lineCount);

return Plugin_Handled;
}

strcopy(logline, sizeof(logline), logline[25]); // Remove the timestamp
LogToGame("%s", logline);
public Action Command_test_logstf_bench(int client, int args) {
char lines[MAX_LINES][MAX_LINE_LENGTH];
int lineCount = ReadFileLines("testlog.log", lines, MAX_LINES, MAX_LINE_LENGTH);
if (lineCount < 0) {
PrintToServer("Failed to open testlog.log");
return Plugin_Handled;
}

PrintToServer("Logged contents of testlog.log");
delete file;
Profiler prof = new Profiler();
prof.Start();

for (int j = 0; j < 10; j++) {
for (int i = 0; i < lineCount; i++) {
LogToGame("%s", lines[i]);
}
}

prof.Stop();

float elapsed = prof.Time;
PrintToServer("[Benchmark] Execution took: %.6f seconds", elapsed);


return Plugin_Handled;
}
Loading