Commits

Alexandre Macabies committed 56e7c5e

speed indicators

  • Participants
  • Parent commits e024301

Comments (0)

Files changed (1)

File src/formflood.cpp

 #include <time.h>
 #include <random>
 
+#ifndef AVG_TIMEDIFF
+#define AVG_TIMEDIFF 1000
+#endif
 static const std::string _version("0.1");
 
 // Random generators
     std::uniform_int_distribution<char>(),
     std::mt19937(std::random_device{}()));
 
+typedef std::chrono::high_resolution_clock Clock;
+typedef std::chrono::milliseconds milliseconds;
+
 // Structures
 struct FloodConf {
     long min_size, max_size;
 };
 
 struct FloodStat {
-    long requests;
-    long errors;
-    long http_errors;
-    long long request_bytes;
-    double avg_speed, avg_time;
+    long requests, last_requests, errors, http_errors;
+    long long bytes, last_bytes;
+    double avg_speed, avg_time, speed_requests, speed_bytes;
 };
 
+// Chrono for stats
+static Clock::time_point time_ref = Clock::now();
+
 // Utility funcs
 void rolling_avg(double alpha, double input, double &avg) {
     avg -= avg / alpha;
 
 static const char units[] = {0, 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'};
 
-std::string bytes2readablefs(long size, long base=1024) {
+std::string double2readable(double s, long base=1024) {
     int i = 0;
-    double s = static_cast<double>(size);
     while (s > base) {
         s /= base;
         i++;
     }
     std::stringstream ret;
-    ret << std::fixed << std::setprecision(i) << s << ' ';
+    ret << std::fixed << std::setprecision(i) << s;
     if(units[i])
         ret << units[i];
     return ret.str();
 }
 
-long readablefs2bytes(const std::string &buff) {
+long readable2double(const std::string &buff) {
     long size = 0;
     std::string unit;
     std::stringstream stream;
     return size * nmemb;
 }
 
-void display_stats(const FloodStat &stats) {
+void display_stats(FloodStat &stats) {
+    double diff_requests = 0, diff_bytes = 0;
+    Clock::time_point now = Clock::now();
+    long ms = std::chrono::duration_cast<milliseconds>(now - time_ref).count();
+    if(ms > AVG_TIMEDIFF) {
+        time_ref = now;
+        stats.speed_requests = 1000. * (stats.requests - stats.last_requests) / ms;
+        stats.speed_bytes = 1000. * (stats.bytes - stats.last_bytes) / ms;
+        stats.last_requests = stats.requests;
+        stats.last_bytes = stats.bytes;
+    }
     std::cout << '\r'
-              << "[" << std::setfill(' ') << std::setw(7) << bytes2readablefs(stats.requests, 1000) << '/'
-              << std::setfill(' ') << std::setw(7) << bytes2readablefs(stats.requests - stats.errors - stats.http_errors, 1000) << '/'
-              << std::setfill(' ') << std::setw(7) << bytes2readablefs(stats.errors, 1000) << '/'
-              << std::setfill(' ') << std::setw(7) << bytes2readablefs(stats.http_errors, 1000) << " TOT/OK/SOCK/HTTP] "
-              << "[" << std::setfill(' ') << std::fixed << std::setw(8) << std::setprecision(1) << stats.avg_speed << " avg Bps] "
-              << "[" << std::setfill(' ') << std::setw(8) << std::setprecision(1) << (stats.avg_time * 1000) << " avg msec] "
-              << "[" << std::setfill(' ') << std::setw(7) << bytes2readablefs(stats.request_bytes) << "B total]";
+              //  "| total     /s        valid     er conn   er http | total     /s      |         |
+              //  "| 1234.0K | 1234.0K | 1234.0K | 1234.0K | 1234.0K | 1234.0K | 1234.0K | 1234.0K | 1234.0K
+              <<  "| " << std::setfill(' ') << std::setw(7) << double2readable(stats.requests, 1000)
+              << " | " << std::setfill(' ') << std::setw(7) << double2readable(stats.speed_requests, 1000)
+              << " | " << std::setfill(' ') << std::setw(7) << double2readable(stats.requests - stats.errors - stats.http_errors, 1000)
+              << " | " << std::setfill(' ') << std::setw(7) << double2readable(stats.errors, 1000)
+              << " | " << std::setfill(' ') << std::setw(7) << double2readable(stats.http_errors, 1000)
+              << " | " << std::setfill(' ') << std::setw(7) << double2readable(stats.bytes)
+              << " | " << std::setfill(' ') << std::setw(7) << double2readable(stats.speed_bytes, 1000)
+              << " | " << std::setfill(' ') << std::setw(7) << std::fixed << std::setprecision(1) << (stats.avg_time * 1000)
+              << " | " << std::setfill(' ') << std::setw(7) << std::fixed << std::setprecision(1) << stats.avg_speed
+    ;
     std::cout.flush();
 }
 
     long respcode, req_bytes;
     double speed, time;
     // char *effective_url = new char[1024];
-    FloodStat stats = {0, 0, 0, 0, 0.};
+    FloodStat stats = {0, 0, 0, 0, 0, 0, 0., 0., 0., 0.};
 
     auto random_lengths = new long[rsize]();
     auto random_values = new char*[rsize]();
     std::cout << "Formflood v" << _version << "\n"
               << "Sending to base URL `" << *conf.url << "` with:\n"
               << "\t" << csize << " constant fields, " << rsize << " randomized fields\n"
-              << "\t" << bytes2readablefs(conf.min_size) << "B min size, " << bytes2readablefs(conf.max_size) << "B max size.\n"
-              << "Press CTRL+C to abort.\n" << std::endl;
+              << "\t" << double2readable(conf.min_size) << "B min size, " << double2readable(conf.max_size) << "B max size.\n"
+              << "Press CTRL+C to abort.\n\n"
+              << "| Requests                                        | Length            | Time    | Speed   \n"
+              << "|   total        /s     valid   er conn   er http |   total        /s |         |         " << std::endl;
+              // "| 1234.0K | 1234.0K | 1234.0K | 1234.0K | 1234.0K | 1234.0K | 1234.0K | 1234.0K | 1234.0K
 
     while(true) {
 
             curl_easy_getinfo(curl, CURLINFO_SPEED_UPLOAD, &speed);
             curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &respcode);
             curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &time);
-            stats.request_bytes += req_bytes;
+            stats.bytes += req_bytes;
             if(respcode >= 400)
                 stats.http_errors++;
             rolling_avg(10, time, stats.avg_time);
     long min_size, max_size;
     std::string size;
     opt.get("-s")->getString(size);
-    min_size = readablefs2bytes(size);
+    min_size = readable2double(size);
     opt.get("-S")->getString(size);
-    max_size = readablefs2bytes(size);
+    max_size = readable2double(size);
 
     long sdiff = max_size - min_size;
     if(sdiff < 0) {