]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
speedtest: Add tests for the ReadWriteLock class
authorRemi Gacogne <remi.gacogne@powerdns.com>
Thu, 25 Mar 2021 09:56:48 +0000 (10:56 +0100)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Thu, 25 Mar 2021 09:56:48 +0000 (10:56 +0100)
pdns/speedtest.cc

index bcbf36cce6b8be67a97ffc71b8fcb368be52210f..078acd058c75c4d179f9b63b26189914b986fc95 100644 (file)
@@ -10,6 +10,7 @@
 #include <fstream>
 #include "uuid-utils.hh"
 #include "dnssecinfra.hh"
+#include "lock.hh"
 
 #ifndef RECURSOR
 #include "statbag.hh"
@@ -862,6 +863,100 @@ struct NSEC3HashTest
   DNSName d_name = DNSName("www.example.com");
 };
 
+struct ReadWriteLockSharedTest
+{
+  explicit ReadWriteLockSharedTest(ReadWriteLock& lock): d_lock(lock)
+  {
+  }
+
+  string getName() const { return "RW lock shared"; }
+
+  void operator()() const {
+    for (size_t idx = 0; idx < 1000; ) {
+      ReadLock wl(d_lock);
+      ++idx;
+    }
+  }
+
+private:
+  ReadWriteLock& d_lock;
+};
+
+struct ReadWriteLockExclusiveTest
+{
+  explicit ReadWriteLockExclusiveTest(ReadWriteLock& lock): d_lock(lock)
+  {
+  }
+
+  string getName() const { return "RW lock exclusive"; }
+
+  void operator()() const {
+    for (size_t idx = 0; idx < 1000; ) {
+      WriteLock wl(d_lock);
+      ++idx;
+    }
+  }
+
+private:
+  ReadWriteLock& d_lock;
+};
+
+struct ReadWriteLockExclusiveTryTest
+{
+  explicit ReadWriteLockExclusiveTryTest(ReadWriteLock& lock, bool contended): d_lock(lock), d_contended(contended)
+  {
+  }
+
+  string getName() const { return "RW lock try exclusive - " + std::string(d_contended ? "contended" : "non-contended"); }
+
+  void operator()() const {
+    for (size_t idx = 0; idx < 1000; ) {
+      TryWriteLock wl(d_lock);
+      if (!wl.gotIt() && !d_contended) {
+        cerr<<"Error getting the lock"<<endl;
+        _exit(0);
+      }
+      else if (wl.gotIt() && d_contended) {
+        cerr<<"Got a contended lock"<<endl;
+        _exit(0);
+      }
+      ++idx;
+    }
+  }
+
+private:
+  ReadWriteLock& d_lock;
+  bool d_contended;
+};
+
+struct ReadWriteLockSharedTryTest
+{
+  explicit ReadWriteLockSharedTryTest(ReadWriteLock& lock, bool contended): d_lock(lock), d_contended(contended)
+  {
+  }
+
+  string getName() const { return "RW lock try shared - " + std::string(d_contended ? "contended" : "non-contended"); }
+
+  void operator()() const {
+    for (size_t idx = 0; idx < 1000; ) {
+      TryReadLock wl(d_lock);
+      if (!wl.gotIt() && !d_contended) {
+        cerr<<"Error getting the lock"<<endl;
+        _exit(0);
+      }
+      else if (wl.gotIt() && d_contended) {
+        cerr<<"Got a contended lock"<<endl;
+        _exit(0);
+      }
+      ++idx;
+    }
+  }
+
+private:
+  ReadWriteLock& d_lock;
+  bool d_contended;
+};
+
 int main(int argc, char** argv)
 try
 {
@@ -899,6 +994,22 @@ try
   doRun(GetTimeTest());
   
   doRun(GetLockUncontendedTest());
+  {
+    ReadWriteLock rwlock;
+    doRun(ReadWriteLockSharedTest(rwlock));
+    doRun(ReadWriteLockExclusiveTest(rwlock));
+    doRun(ReadWriteLockExclusiveTryTest(rwlock, false));
+    {
+      ReadLock rl(rwlock);
+      doRun(ReadWriteLockExclusiveTryTest(rwlock, true));
+      doRun(ReadWriteLockSharedTryTest(rwlock, false));
+    }
+    {
+      WriteLock wl(rwlock);
+      doRun(ReadWriteLockSharedTryTest(rwlock, true));
+    }
+  }
+
   doRun(StaticMemberTest());
   
   doRun(ARecordTest(1));
@@ -930,8 +1041,6 @@ try
   doRun(GenericRecordTest(4, QType::NS, "powerdnssec1.ds9a.nl"));
   doRun(GenericRecordTest(64, QType::NS, "powerdnssec1.ds9a.nl"));
 
-  
-
   doRun(SOARecordTest(1));
   doRun(SOARecordTest(2));
   doRun(SOARecordTest(4));