Skip to content

Instantly share code, notes, and snippets.

@julKali
Created February 8, 2021 19:29
Show Gist options
  • Save julKali/9aeab1f0ff506c5123308260aaa68341 to your computer and use it in GitHub Desktop.
Save julKali/9aeab1f0ff506c5123308260aaa68341 to your computer and use it in GitHub Desktop.
class Stresstest {
static boolean startThreads = false;
private static void stressTestPinguTextCollection() {
startThreads = false;
int threadCount = 100;
final int execsPerThread = 100;
final PinguTextCollection coll = new PinguTextCollection();
runWorkers(threadCount, () -> {
while (!startThreads) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int j = 0; j < execsPerThread; j++) {
PinguText pt = coll.add("mywork", Thread.currentThread().getName(), Integer.toString(j));
PinguText pt2 = coll.findById(pt.getId());
if (!pt.equals(pt2))
throw new AssertionError();
coll.findById(j);
coll.getAll();
coll.findPlagiarismFor(j);
}
});
for (long id = 0; id < threadCount * execsPerThread; id++) {
if (coll.findById(id) == null)
throw new AssertionError(id);
}
List<PinguText> data = coll.getAll();
if (data.size() != threadCount * execsPerThread)
throw new AssertionError(data.size());
for (int i = 0; i < data().size() - 1; i++)
if (data.get(i).getId() >= data.get(i+1).getId())
throw new AssertionError();
}
private static BlockingQueue<Entry> data() {
return new LinkedBlockingQueue<>(List.of(
Entry.of("asd", "jasd", "kalsdasllask lasdjlask!m lkasd $$ öals"),
Entry.of("jkasd", "", "asdjk lasdjlask!m lkasd $$ öals"),
Entry.of("j", "", "asdjk !m lkasd $$ öals"),
Entry.of("klasdka", "klas HJASD", "asdjk !m --- $$"),
Entry.of("xxx!!", "klasd uwie", "asdjk daskdö!m --- $$"),
Entry.of("xxxklasd", "223ja", "asdjm --- $$"),
Entry.of("jkasd asdas kasd!", "ajskd ju!", "a!m --- $$"),
Entry.of("My First Text", "jaks a", "asdkl !öalsdöasdm --- $$"),
Entry.of("jaksd jkasd", "jkasd 9034ö", "asdjk !öl23om --- $$"),
Entry.of("klasd ajskdjaksd jaksd", "ajskd kalskd", "asdjk !m --- $$")
));
}
private static void runWorkers(int workerCount, Runnable r) {
Thread[] threads = new Thread[workerCount];
for (int i = 0; i < workerCount; i++) {
threads[i] = new Thread(r);
threads[i].start();
}
startThreads = true;
for (int i = 0; i < workerCount; i++) {
try {
threads[i].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private static class Entry {
String title;
String author;
String text;
static Entry of(String title, String author, String text) {
Entry e = new Entry();
e.title = title;
e.author = author;
e.text = text;
return e;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Entry entry = (Entry) o;
return Objects.equals(title, entry.title) && Objects.equals(author, entry.author) && Objects.equals(text, entry.text);
}
@Override
public int hashCode() {
return Objects.hash(title, author, text);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment