github.com/Azareal/Gosora@v0.0.0-20210729070923-553e66b59003/general_test.go (about)

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"database/sql"
     6  	"log"
     7  	"net/http"
     8  	"net/http/httptest"
     9  	"runtime"
    10  	"runtime/debug"
    11  	"strconv"
    12  	"strings"
    13  	"testing"
    14  	"time"
    15  
    16  	"github.com/pkg/errors"
    17  
    18  	c "github.com/Azareal/Gosora/common"
    19  	e "github.com/Azareal/Gosora/extend"
    20  	"github.com/Azareal/Gosora/install"
    21  	qgen "github.com/Azareal/Gosora/query_gen"
    22  	"github.com/Azareal/Gosora/routes"
    23  )
    24  
    25  //var dbTest *sql.DB
    26  var dbProd *sql.DB
    27  var gloinited bool
    28  var installAdapter install.InstallAdapter
    29  
    30  func ResetTables() (err error) {
    31  	err = installAdapter.InitDatabase()
    32  	if err != nil {
    33  		return errors.WithStack(err)
    34  	}
    35  
    36  	err = installAdapter.TableDefs()
    37  	if err != nil {
    38  		return errors.WithStack(err)
    39  	}
    40  
    41  	err = installAdapter.CreateAdmin()
    42  	if err != nil {
    43  		return errors.WithStack(err)
    44  	}
    45  
    46  	return installAdapter.InitialData()
    47  }
    48  
    49  func gloinit() (e error) {
    50  	if gloinited {
    51  		return nil
    52  	}
    53  	// TODO: Make these configurable via flags to the go test command
    54  	c.Dev.DebugMode = false
    55  	c.Dev.SuperDebug = false
    56  	c.Dev.TemplateDebug = false
    57  	qgen.LogPrepares = false
    58  	//nogrouplog = true
    59  	c.StartTime = time.Now()
    60  
    61  	ws := func(e error) error {
    62  		return errors.WithStack(e)
    63  	}
    64  	if e = c.LoadConfig(); e != nil {
    65  		return ws(e)
    66  	}
    67  	if e = c.ProcessConfig(); e != nil {
    68  		return ws(e)
    69  	}
    70  	c.Tasks = c.NewScheduledTasks()
    71  
    72  	e = c.InitTemplates()
    73  	if e != nil {
    74  		return ws(e)
    75  	}
    76  	c.Themes, e = c.NewThemeList()
    77  	if e != nil {
    78  		return ws(e)
    79  	}
    80  	c.TopicListThaw = c.NewTestThaw()
    81  	c.SwitchToTestDB()
    82  
    83  	var ok bool
    84  	installAdapter, ok = install.Lookup(dbAdapter)
    85  	if !ok {
    86  		return ws(errors.New("We couldn't find the adapter '" + dbAdapter + "'"))
    87  	}
    88  	installAdapter.SetConfig(c.DbConfig.Host, c.DbConfig.Username, c.DbConfig.Password, c.DbConfig.Dbname, c.DbConfig.Port)
    89  
    90  	e = ResetTables()
    91  	if e != nil {
    92  		return e
    93  	}
    94  	e = InitDatabase()
    95  	if e != nil {
    96  		return e
    97  	}
    98  	e = afterDBInit()
    99  	if e != nil {
   100  		return e
   101  	}
   102  
   103  	rrcfg := rcfg()
   104  	rrcfg.DisableTick = false
   105  	router, e = NewGenRouter(rrcfg)
   106  	if e != nil {
   107  		return ws(e)
   108  	}
   109  	gloinited = true
   110  	return nil
   111  }
   112  
   113  func rcfg() *RouterConfig {
   114  	return &RouterConfig{
   115  		Uploads:     http.FileServer(http.Dir("./uploads")),
   116  		DisableTick: true,
   117  	}
   118  }
   119  
   120  func init() {
   121  	if e := gloinit(); e != nil {
   122  		log.Print("Something bad happened")
   123  		//debug.PrintStack()
   124  		log.Fatalf("%+v\n", e)
   125  	}
   126  }
   127  
   128  var benchTidI = 1
   129  var benchTid = "1"
   130  
   131  // TODO: Swap out LocalError for a panic for this?
   132  func BenchmarkTopicAdminRouteParallel(b *testing.B) {
   133  	binit(b)
   134  	cfg := NewStashConfig()
   135  	c.Dev.DebugMode = false
   136  	c.Dev.SuperDebug = false
   137  
   138  	admin, err := c.Users.Get(1)
   139  	if err != nil {
   140  		b.Fatal(err)
   141  	}
   142  	if !admin.IsAdmin {
   143  		b.Fatal("UID1 is not an admin")
   144  	}
   145  	adminUIDCookie := http.Cookie{Name: "uid", Value: "1", Path: "/", MaxAge: c.Year}
   146  	adminSessionCookie := http.Cookie{Name: "session", Value: admin.Session, Path: "/", MaxAge: c.Year}
   147  
   148  	b.RunParallel(func(pb *testing.PB) {
   149  		for pb.Next() {
   150  			w := httptest.NewRecorder()
   151  			reqAdmin := httptest.NewRequest("get", "/topic/hm."+benchTid, bytes.NewReader(nil))
   152  			reqAdmin.AddCookie(&adminUIDCookie)
   153  			reqAdmin.AddCookie(&adminSessionCookie)
   154  
   155  			// Deal with the session stuff, etc.
   156  			user, ok := c.PreRoute(w, reqAdmin)
   157  			if !ok {
   158  				b.Fatal("Mysterious error!")
   159  			}
   160  			head, e := c.UserCheck(w, reqAdmin, &user)
   161  			if e != nil {
   162  				b.Fatal(e)
   163  			}
   164  			//w.Body.Reset()
   165  			routes.ViewTopic(w, reqAdmin, &user, head, "1")
   166  			if w.Code != 200 {
   167  				b.Log(w.Body)
   168  				b.Fatal("HTTP Error!")
   169  			}
   170  		}
   171  	})
   172  
   173  	cfg.Restore()
   174  }
   175  
   176  func BenchmarkTopicAdminRouteParallelWithRouter(b *testing.B) {
   177  	binit(b)
   178  	router, e := NewGenRouter(rcfg())
   179  	if e != nil {
   180  		b.Fatal(e)
   181  	}
   182  	cfg := NewStashConfig()
   183  	c.Dev.DebugMode = false
   184  	c.Dev.SuperDebug = false
   185  
   186  	admin, e := c.Users.Get(1)
   187  	if e != nil {
   188  		b.Fatal(e)
   189  	}
   190  	if !admin.IsAdmin {
   191  		b.Fatal("UID1 is not an admin")
   192  	}
   193  	uidCookie := http.Cookie{Name: "uid", Value: "1", Path: "/", MaxAge: c.Year}
   194  	sessionCookie := http.Cookie{Name: "session", Value: admin.Session, Path: "/", MaxAge: c.Year}
   195  	path := "/topic/hm." + benchTid
   196  
   197  	b.RunParallel(func(pb *testing.PB) {
   198  		for pb.Next() {
   199  			w := httptest.NewRecorder()
   200  			reqAdmin := httptest.NewRequest("get", path, bytes.NewReader(nil))
   201  			reqAdmin.AddCookie(&uidCookie)
   202  			reqAdmin.AddCookie(&sessionCookie)
   203  			reqAdmin.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36")
   204  			reqAdmin.Header.Set("Host", "localhost")
   205  			reqAdmin.Host = "localhost"
   206  			//w.Body.Reset()
   207  			router.ServeHTTP(w, reqAdmin)
   208  			if w.Code != 200 {
   209  				b.Log(w.Body)
   210  				b.Fatal("HTTP Error!")
   211  			}
   212  		}
   213  	})
   214  
   215  	cfg.Restore()
   216  }
   217  
   218  func BenchmarkTopicAdminRouteParallelAlt(b *testing.B) {
   219  	BenchmarkTopicAdminRouteParallel(b)
   220  }
   221  
   222  func BenchmarkTopicAdminRouteParallelWithRouterAlt(b *testing.B) {
   223  	BenchmarkTopicAdminRouteParallelWithRouter(b)
   224  }
   225  
   226  func BenchmarkTopicAdminRouteParallelAltAlt(b *testing.B) {
   227  	BenchmarkTopicAdminRouteParallel(b)
   228  }
   229  
   230  func BenchmarkTopicGuestAdminRouteParallelWithRouterPre(b *testing.B) {
   231  	runtime.GC()
   232  }
   233  
   234  func BenchmarkTopicGuestAdminRouteParallelWithRouter(b *testing.B) {
   235  	binit(b)
   236  	router, e := NewGenRouter(rcfg())
   237  	if e != nil {
   238  		b.Fatal(e)
   239  	}
   240  	cfg := NewStashConfig()
   241  	c.Dev.DebugMode = false
   242  	c.Dev.SuperDebug = false
   243  
   244  	admin, e := c.Users.Get(1)
   245  	if e != nil {
   246  		b.Fatal(e)
   247  	}
   248  	if !admin.IsAdmin {
   249  		b.Fatal("UID1 is not an admin")
   250  	}
   251  	uidCookie := http.Cookie{Name: "uid", Value: "1", Path: "/", MaxAge: c.Year}
   252  	sessionCookie := http.Cookie{Name: "session", Value: admin.Session, Path: "/", MaxAge: c.Year}
   253  	path := "/topic/hm." + benchTid
   254  	//runtime.GC()
   255  
   256  	b.RunParallel(func(pb *testing.PB) {
   257  		for pb.Next() {
   258  			w := httptest.NewRecorder()
   259  			reqAdmin := httptest.NewRequest("get", path, bytes.NewReader(nil))
   260  			reqAdmin.AddCookie(&uidCookie)
   261  			reqAdmin.AddCookie(&sessionCookie)
   262  			reqAdmin.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36")
   263  			reqAdmin.Header.Set("Host", "localhost")
   264  			reqAdmin.Host = "localhost"
   265  			router.ServeHTTP(w, reqAdmin)
   266  			if w.Code != 200 {
   267  				b.Log(w.Body)
   268  				b.Fatal("HTTP Error!")
   269  			}
   270  
   271  			{
   272  				w := httptest.NewRecorder()
   273  				req := httptest.NewRequest("GET", path, bytes.NewReader(nil))
   274  				req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36")
   275  				req.Header.Set("Host", "localhost")
   276  				req.Host = "localhost"
   277  				router.ServeHTTP(w, req)
   278  				if w.Code != 200 {
   279  					b.Log(w.Body)
   280  					b.Fatal("HTTP Error!")
   281  				}
   282  			}
   283  		}
   284  	})
   285  
   286  	cfg.Restore()
   287  }
   288  
   289  func BenchmarkTopicGuestAdminRouteParallelWithRouterPre2(b *testing.B) {
   290  	runtime.GC()
   291  }
   292  
   293  func BenchmarkTopicGuestAdminRouteParallelWithRouterGC(b *testing.B) {
   294  	binit(b)
   295  	router, e := NewGenRouter(rcfg())
   296  	if e != nil {
   297  		b.Fatal(e)
   298  	}
   299  	cfg := NewStashConfig()
   300  	c.Dev.DebugMode = false
   301  	c.Dev.SuperDebug = false
   302  
   303  	admin, e := c.Users.Get(1)
   304  	if e != nil {
   305  		b.Fatal(e)
   306  	}
   307  	if !admin.IsAdmin {
   308  		b.Fatal("UID1 is not an admin")
   309  	}
   310  	uidCookie := http.Cookie{Name: "uid", Value: "1", Path: "/", MaxAge: c.Year}
   311  	sessionCookie := http.Cookie{Name: "session", Value: admin.Session, Path: "/", MaxAge: c.Year}
   312  	path := "/topic/hm." + benchTid
   313  	//runtime.GC()
   314  
   315  	b.RunParallel(func(pb *testing.PB) {
   316  		for pb.Next() {
   317  			w := httptest.NewRecorder()
   318  			reqAdmin := httptest.NewRequest("get", path, bytes.NewReader(nil))
   319  			reqAdmin.AddCookie(&uidCookie)
   320  			reqAdmin.AddCookie(&sessionCookie)
   321  			reqAdmin.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36")
   322  			reqAdmin.Header.Set("Host", "localhost")
   323  			reqAdmin.Host = "localhost"
   324  			router.ServeHTTP(w, reqAdmin)
   325  			if w.Code != 200 {
   326  				b.Log(w.Body)
   327  				b.Fatal("HTTP Error!")
   328  			}
   329  
   330  			{
   331  				w := httptest.NewRecorder()
   332  				req := httptest.NewRequest("GET", path, bytes.NewReader(nil))
   333  				req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36")
   334  				req.Header.Set("Host", "localhost")
   335  				req.Host = "localhost"
   336  				router.ServeHTTP(w, req)
   337  				if w.Code != 200 {
   338  					b.Log(w.Body)
   339  					b.Fatal("HTTP Error!")
   340  				}
   341  			}
   342  		}
   343  
   344  		runtime.GC()
   345  	})
   346  
   347  	cfg.Restore()
   348  }
   349  
   350  func BenchmarkTopicGuestRouteParallel(b *testing.B) {
   351  	binit(b)
   352  	cfg := NewStashConfig()
   353  	c.Dev.DebugMode = false
   354  	c.Dev.SuperDebug = false
   355  
   356  	b.RunParallel(func(pb *testing.PB) {
   357  		for pb.Next() {
   358  			w := httptest.NewRecorder()
   359  			req := httptest.NewRequest("get", "/topic/hm."+benchTid, bytes.NewReader(nil))
   360  			user := c.GuestUser
   361  
   362  			head, e := c.UserCheck(w, req, &user)
   363  			if e != nil {
   364  				b.Fatal(e)
   365  			}
   366  			//w.Body.Reset()
   367  			routes.ViewTopic(w, req, &user, head, "1")
   368  			if w.Code != 200 {
   369  				b.Log(w.Body)
   370  				b.Fatal("HTTP Error!")
   371  			}
   372  		}
   373  	})
   374  	cfg.Restore()
   375  }
   376  
   377  //before
   378  
   379  func BenchmarkForumsRouteAdminParallelWithRouterGC2Pre(b *testing.B) {
   380  	runtime.GC()
   381  }
   382  
   383  func BenchmarkForumsRouteAdminParallelWithRouterGC2(b *testing.B) {
   384  	binit(b)
   385  	router, e := NewGenRouter(rcfg())
   386  	if e != nil {
   387  		b.Fatal(e)
   388  	}
   389  	cfg := NewStashConfig()
   390  	c.Dev.DebugMode = false
   391  	c.Dev.SuperDebug = false
   392  
   393  	admin, e := c.Users.Get(1)
   394  	if e != nil {
   395  		b.Fatal(e)
   396  	}
   397  	if !admin.IsAdmin {
   398  		b.Fatal("UID1 is not an admin")
   399  	}
   400  	uidCookie := http.Cookie{Name: "uid", Value: "1", Path: "/", MaxAge: c.Year}
   401  	sessionCookie := http.Cookie{Name: "session", Value: admin.Session, Path: "/", MaxAge: c.Year}
   402  	path := "/forums/"
   403  	//runtime.GC()
   404  
   405  	b.RunParallel(func(pb *testing.PB) {
   406  		for pb.Next() {
   407  			w := httptest.NewRecorder()
   408  			reqAdmin := httptest.NewRequest("get", path, bytes.NewReader(nil))
   409  			reqAdmin.AddCookie(&uidCookie)
   410  			reqAdmin.AddCookie(&sessionCookie)
   411  			reqAdmin.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36")
   412  			reqAdmin.Header.Set("Host", "localhost")
   413  			reqAdmin.Host = "localhost"
   414  			router.ServeHTTP(w, reqAdmin)
   415  			if w.Code != 200 {
   416  				b.Log(w.Body)
   417  				b.Fatal("HTTP Error!")
   418  			}
   419  		}
   420  
   421  		runtime.GC()
   422  	})
   423  
   424  	cfg.Restore()
   425  }
   426  
   427  func BenchmarkForumsRouteAdminParallelWithRouterGCBrotliPre(b *testing.B) {
   428  	runtime.GC()
   429  }
   430  
   431  func BenchmarkForumsRouteAdminParallelWithRouterGCBrotli(b *testing.B) {
   432  	binit(b)
   433  	router, e := NewGenRouter(rcfg())
   434  	if e != nil {
   435  		b.Fatal(e)
   436  	}
   437  	cfg := NewStashConfig()
   438  	c.Dev.DebugMode = false
   439  	c.Dev.SuperDebug = false
   440  
   441  	admin, e := c.Users.Get(1)
   442  	if e != nil {
   443  		b.Fatal(e)
   444  	}
   445  	if !admin.IsAdmin {
   446  		b.Fatal("UID1 is not an admin")
   447  	}
   448  	uidCookie := http.Cookie{Name: "uid", Value: "1", Path: "/", MaxAge: c.Year}
   449  	sessionCookie := http.Cookie{Name: "session", Value: admin.Session, Path: "/", MaxAge: c.Year}
   450  	path := "/forums/"
   451  	//runtime.GC()
   452  
   453  	b.RunParallel(func(pb *testing.PB) {
   454  		for pb.Next() {
   455  			w := httptest.NewRecorder()
   456  			reqAdmin := httptest.NewRequest("get", path, bytes.NewReader(nil))
   457  			reqAdmin.AddCookie(&uidCookie)
   458  			reqAdmin.AddCookie(&sessionCookie)
   459  			reqAdmin.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36")
   460  			reqAdmin.Header.Set("Accept-Encoding", "br")
   461  			reqAdmin.Header.Set("Host", "localhost")
   462  			reqAdmin.Host = "localhost"
   463  			router.ServeHTTP(w, reqAdmin)
   464  			if w.Code != 200 {
   465  				b.Log(w.Body)
   466  				b.Fatal("HTTP Error!")
   467  			}
   468  		}
   469  
   470  		runtime.GC()
   471  	})
   472  
   473  	cfg.Restore()
   474  }
   475  
   476  //end
   477  //before
   478  
   479  func BenchmarkTopicRouteAdminParallelWithRouterGC2Pre(b *testing.B) {
   480  	runtime.GC()
   481  }
   482  
   483  func BenchmarkTopicRouteAdminParallelWithRouterGC2(b *testing.B) {
   484  	binit(b)
   485  	router, e := NewGenRouter(rcfg())
   486  	if e != nil {
   487  		b.Fatal(e)
   488  	}
   489  	cfg := NewStashConfig()
   490  	c.Dev.DebugMode = false
   491  	c.Dev.SuperDebug = false
   492  
   493  	admin, e := c.Users.Get(1)
   494  	if e != nil {
   495  		b.Fatal(e)
   496  	}
   497  	if !admin.IsAdmin {
   498  		b.Fatal("UID1 is not an admin")
   499  	}
   500  	uidCookie := http.Cookie{Name: "uid", Value: "1", Path: "/", MaxAge: c.Year}
   501  	sessionCookie := http.Cookie{Name: "session", Value: admin.Session, Path: "/", MaxAge: c.Year}
   502  	path := "/topic/1"
   503  	//runtime.GC()
   504  
   505  	b.RunParallel(func(pb *testing.PB) {
   506  		for pb.Next() {
   507  			w := httptest.NewRecorder()
   508  			reqAdmin := httptest.NewRequest("get", path, bytes.NewReader(nil))
   509  			reqAdmin.AddCookie(&uidCookie)
   510  			reqAdmin.AddCookie(&sessionCookie)
   511  			reqAdmin.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36")
   512  			reqAdmin.Header.Set("Host", "localhost")
   513  			reqAdmin.Host = "localhost"
   514  			router.ServeHTTP(w, reqAdmin)
   515  			if w.Code != 200 {
   516  				b.Log(w.Body)
   517  				b.Fatal("HTTP Error!")
   518  			}
   519  		}
   520  
   521  		runtime.GC()
   522  	})
   523  
   524  	cfg.Restore()
   525  }
   526  
   527  func BenchmarkTopicRouteAdminParallelWithRouterGCBrotliPre(b *testing.B) {
   528  	runtime.GC()
   529  }
   530  
   531  func BenchmarkTopicRouteAdminParallelWithRouterGCBrotli(b *testing.B) {
   532  	binit(b)
   533  	router, e := NewGenRouter(rcfg())
   534  	if e != nil {
   535  		b.Fatal(e)
   536  	}
   537  	cfg := NewStashConfig()
   538  	c.Dev.DebugMode = false
   539  	c.Dev.SuperDebug = false
   540  
   541  	admin, e := c.Users.Get(1)
   542  	if e != nil {
   543  		b.Fatal(e)
   544  	}
   545  	if !admin.IsAdmin {
   546  		b.Fatal("UID1 is not an admin")
   547  	}
   548  	uidCookie := http.Cookie{Name: "uid", Value: "1", Path: "/", MaxAge: c.Year}
   549  	sessionCookie := http.Cookie{Name: "session", Value: admin.Session, Path: "/", MaxAge: c.Year}
   550  	path := "/topic/1"
   551  	//runtime.GC()
   552  
   553  	b.RunParallel(func(pb *testing.PB) {
   554  		for pb.Next() {
   555  			w := httptest.NewRecorder()
   556  			reqAdmin := httptest.NewRequest("get", path, bytes.NewReader(nil))
   557  			reqAdmin.AddCookie(&uidCookie)
   558  			reqAdmin.AddCookie(&sessionCookie)
   559  			reqAdmin.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36")
   560  			reqAdmin.Header.Set("Accept-Encoding", "br")
   561  			reqAdmin.Header.Set("Host", "localhost")
   562  			reqAdmin.Host = "localhost"
   563  			router.ServeHTTP(w, reqAdmin)
   564  			if w.Code != 200 {
   565  				b.Log(w.Body)
   566  				b.Fatal("HTTP Error!")
   567  			}
   568  		}
   569  
   570  		runtime.GC()
   571  	})
   572  
   573  	cfg.Restore()
   574  }
   575  
   576  //end
   577  
   578  func BenchmarkTopicsRouteAdminParallelWithRouterGC2Pre(b *testing.B) {
   579  	runtime.GC()
   580  }
   581  
   582  func BenchmarkTopicsRouteAdminParallelWithRouterGC2(b *testing.B) {
   583  	binit(b)
   584  	router, e := NewGenRouter(rcfg())
   585  	if e != nil {
   586  		b.Fatal(e)
   587  	}
   588  	cfg := NewStashConfig()
   589  	c.Dev.DebugMode = false
   590  	c.Dev.SuperDebug = false
   591  
   592  	admin, e := c.Users.Get(1)
   593  	if e != nil {
   594  		b.Fatal(e)
   595  	}
   596  	if !admin.IsAdmin {
   597  		b.Fatal("UID1 is not an admin")
   598  	}
   599  	uidCookie := http.Cookie{Name: "uid", Value: "1", Path: "/", MaxAge: c.Year}
   600  	sessionCookie := http.Cookie{Name: "session", Value: admin.Session, Path: "/", MaxAge: c.Year}
   601  	path := "/topics/"
   602  	//runtime.GC()
   603  
   604  	b.RunParallel(func(pb *testing.PB) {
   605  		for pb.Next() {
   606  			w := httptest.NewRecorder()
   607  			reqAdmin := httptest.NewRequest("get", path, bytes.NewReader(nil))
   608  			reqAdmin.AddCookie(&uidCookie)
   609  			reqAdmin.AddCookie(&sessionCookie)
   610  			reqAdmin.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36")
   611  			reqAdmin.Header.Set("Host", "localhost")
   612  			reqAdmin.Host = "localhost"
   613  			router.ServeHTTP(w, reqAdmin)
   614  			if w.Code != 200 {
   615  				b.Log(w.Body)
   616  				b.Fatal("HTTP Error!")
   617  			}
   618  		}
   619  
   620  		runtime.GC()
   621  	})
   622  
   623  	cfg.Restore()
   624  }
   625  
   626  func BenchmarkTopicsRouteAdminParallelWithRouterGCBrotliPre(b *testing.B) {
   627  	runtime.GC()
   628  }
   629  
   630  func BenchmarkTopicsRouteAdminParallelWithRouterGCBrotli(b *testing.B) {
   631  	binit(b)
   632  	router, e := NewGenRouter(rcfg())
   633  	if e != nil {
   634  		b.Fatal(e)
   635  	}
   636  	cfg := NewStashConfig()
   637  	c.Dev.DebugMode = false
   638  	c.Dev.SuperDebug = false
   639  
   640  	admin, e := c.Users.Get(1)
   641  	if e != nil {
   642  		b.Fatal(e)
   643  	}
   644  	if !admin.IsAdmin {
   645  		b.Fatal("UID1 is not an admin")
   646  	}
   647  	uidCookie := http.Cookie{Name: "uid", Value: "1", Path: "/", MaxAge: c.Year}
   648  	sessionCookie := http.Cookie{Name: "session", Value: admin.Session, Path: "/", MaxAge: c.Year}
   649  	path := "/topics/"
   650  	//runtime.GC()
   651  
   652  	b.RunParallel(func(pb *testing.PB) {
   653  		for pb.Next() {
   654  			w := httptest.NewRecorder()
   655  			reqAdmin := httptest.NewRequest("get", path, bytes.NewReader(nil))
   656  			reqAdmin.AddCookie(&uidCookie)
   657  			reqAdmin.AddCookie(&sessionCookie)
   658  			reqAdmin.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36")
   659  			reqAdmin.Header.Set("Accept-Encoding", "br")
   660  			reqAdmin.Header.Set("Host", "localhost")
   661  			reqAdmin.Host = "localhost"
   662  			router.ServeHTTP(w, reqAdmin)
   663  			if w.Code != 200 {
   664  				b.Log(w.Body)
   665  				b.Fatal("HTTP Error!")
   666  			}
   667  		}
   668  
   669  		runtime.GC()
   670  	})
   671  
   672  	cfg.Restore()
   673  }
   674  
   675  func BenchmarkTopicsRouteAdminParallelWithRouterGCGzipPre(b *testing.B) {
   676  	runtime.GC()
   677  }
   678  
   679  func BenchmarkTopicsRouteAdminParallelWithRouterGCGzip(b *testing.B) {
   680  	binit(b)
   681  	router, e := NewGenRouter(rcfg())
   682  	if e != nil {
   683  		b.Fatal(e)
   684  	}
   685  	cfg := NewStashConfig()
   686  	c.Dev.DebugMode = false
   687  	c.Dev.SuperDebug = false
   688  
   689  	admin, e := c.Users.Get(1)
   690  	if e != nil {
   691  		b.Fatal(e)
   692  	}
   693  	if !admin.IsAdmin {
   694  		b.Fatal("UID1 is not an admin")
   695  	}
   696  	uidCookie := http.Cookie{Name: "uid", Value: "1", Path: "/", MaxAge: c.Year}
   697  	sessionCookie := http.Cookie{Name: "session", Value: admin.Session, Path: "/", MaxAge: c.Year}
   698  	path := "/topics/"
   699  	//runtime.GC()
   700  
   701  	b.RunParallel(func(pb *testing.PB) {
   702  		for pb.Next() {
   703  			w := httptest.NewRecorder()
   704  			reqAdmin := httptest.NewRequest("get", path, bytes.NewReader(nil))
   705  			reqAdmin.AddCookie(&uidCookie)
   706  			reqAdmin.AddCookie(&sessionCookie)
   707  			reqAdmin.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36")
   708  			reqAdmin.Header.Set("Accept-Encoding", "gzip")
   709  			reqAdmin.Header.Set("Host", "localhost")
   710  			reqAdmin.Host = "localhost"
   711  			router.ServeHTTP(w, reqAdmin)
   712  			if w.Code != 200 {
   713  				b.Log(w.Body)
   714  				b.Fatal("HTTP Error!")
   715  			}
   716  		}
   717  
   718  		runtime.GC()
   719  	})
   720  
   721  	cfg.Restore()
   722  }
   723  
   724  func BenchmarkTopicGuestRouteParallelDebugMode(b *testing.B) {
   725  	binit(b)
   726  	cfg := NewStashConfig()
   727  	c.Dev.DebugMode = true
   728  	c.Dev.SuperDebug = false
   729  
   730  	b.RunParallel(func(pb *testing.PB) {
   731  		for pb.Next() {
   732  			w := httptest.NewRecorder()
   733  			req := httptest.NewRequest("get", "/topic/hm."+benchTid, bytes.NewReader(nil))
   734  			user := c.GuestUser
   735  
   736  			head, err := c.UserCheck(w, req, &user)
   737  			if err != nil {
   738  				b.Fatal(err)
   739  			}
   740  			//w.Body.Reset()
   741  			routes.ViewTopic(w, req, &user, head, "1")
   742  			if w.Code != 200 {
   743  				b.Log(w.Body)
   744  				b.Fatal("HTTP Error!")
   745  			}
   746  		}
   747  	})
   748  	cfg.Restore()
   749  }
   750  
   751  func BenchmarkAlertsRouteAdminParallelWithRouterGCPre(b *testing.B) {
   752  	runtime.GC()
   753  }
   754  
   755  func BenchmarkAlertsRouteAdminParallelWithRouterGC(b *testing.B) {
   756  	binit(b)
   757  	router, e := NewGenRouter(rcfg())
   758  	if e != nil {
   759  		b.Fatal(e)
   760  	}
   761  	cfg := NewStashConfig()
   762  	c.Dev.DebugMode = false
   763  	c.Dev.SuperDebug = false
   764  
   765  	admin, e := c.Users.Get(1)
   766  	if e != nil {
   767  		b.Fatal(e)
   768  	}
   769  	if !admin.IsAdmin {
   770  		b.Fatal("UID1 is not an admin")
   771  	}
   772  	uidCookie := http.Cookie{Name: "uid", Value: "1", Path: "/", MaxAge: c.Year}
   773  	sessionCookie := http.Cookie{Name: "session", Value: admin.Session, Path: "/", MaxAge: c.Year}
   774  	path := "/api/?m=alerts"
   775  	//runtime.GC()
   776  
   777  	b.RunParallel(func(pb *testing.PB) {
   778  		for pb.Next() {
   779  			w := httptest.NewRecorder()
   780  			reqAdmin := httptest.NewRequest("get", path, bytes.NewReader(nil))
   781  			reqAdmin.AddCookie(&uidCookie)
   782  			reqAdmin.AddCookie(&sessionCookie)
   783  			reqAdmin.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36")
   784  			reqAdmin.Header.Set("Host", "localhost")
   785  			reqAdmin.Host = "localhost"
   786  			router.ServeHTTP(w, reqAdmin)
   787  			if w.Code != 200 {
   788  				b.Log(w.Body)
   789  				b.Fatal("HTTP Error!")
   790  			}
   791  		}
   792  
   793  		runtime.GC()
   794  	})
   795  
   796  	cfg.Restore()
   797  }
   798  
   799  func obRoute(b *testing.B, path string) {
   800  	binit(b)
   801  	cfg := NewStashConfig()
   802  	c.Dev.DebugMode = false
   803  	c.Dev.SuperDebug = false
   804  	b.RunParallel(benchRoute(b, path))
   805  	cfg.Restore()
   806  }
   807  
   808  func obRouteNoError(b *testing.B, path string) {
   809  	binit(b)
   810  	cfg := NewStashConfig()
   811  	c.Dev.DebugMode = false
   812  	c.Dev.SuperDebug = false
   813  	b.RunParallel(benchRouteNoError(b, path))
   814  	cfg.Restore()
   815  }
   816  
   817  func BenchmarkTopicsGuestRouteParallelWithRouter(b *testing.B) {
   818  	obRoute(b, "/topics/")
   819  }
   820  
   821  func BenchmarkTopicsGuestJSRouteParallelWithRouter(b *testing.B) {
   822  	obRoute(b, "/topics/?js=1")
   823  }
   824  
   825  func BenchmarkForumsGuestRouteParallelWithRouter(b *testing.B) {
   826  	obRoute(b, "/forums/")
   827  }
   828  
   829  func BenchmarkForumGuestRouteParallelWithRouter(b *testing.B) {
   830  	obRoute(b, "/forum/general.2")
   831  }
   832  
   833  func BenchmarkTopicGuestRouteParallelWithRouter(b *testing.B) {
   834  	obRoute(b, "/topic/hm."+benchTid)
   835  }
   836  
   837  func BenchmarkTopicGuestRouteParallelWithRouterAlt(b *testing.B) {
   838  	obRoute(b, "/topic/hm."+benchTid)
   839  }
   840  
   841  func BenchmarkBadRouteGuestRouteParallelWithRouter(b *testing.B) {
   842  	obRouteNoError(b, "/garble/haa")
   843  }
   844  
   845  func BenchmarkAlertsRouteGuestParallelWithRouter(b *testing.B) {
   846  	obRoute(b, "/api/?m=alerts")
   847  }
   848  
   849  // TODO: Alternate between member and guest to bust some CPU caches?
   850  
   851  func binit(b *testing.B) {
   852  	b.ReportAllocs()
   853  	if err := gloinit(); err != nil {
   854  		b.Fatal(err)
   855  	}
   856  }
   857  
   858  type StashConfig struct {
   859  	prev  bool
   860  	prev2 bool
   861  }
   862  
   863  func NewStashConfig() *StashConfig {
   864  	prev := c.Dev.DebugMode
   865  	prev2 := c.Dev.SuperDebug
   866  	return &StashConfig{prev, prev2}
   867  }
   868  
   869  func (cfg *StashConfig) Restore() {
   870  	c.Dev.DebugMode = cfg.prev
   871  	c.Dev.SuperDebug = cfg.prev2
   872  }
   873  
   874  func benchRoute(b *testing.B, path string) func(*testing.PB) {
   875  	router, e := NewGenRouter(rcfg())
   876  	if e != nil {
   877  		b.Fatal(e)
   878  	}
   879  	return func(pb *testing.PB) {
   880  		for pb.Next() {
   881  			w := httptest.NewRecorder()
   882  			req := httptest.NewRequest("GET", path, bytes.NewReader(nil))
   883  			req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36")
   884  			req.Header.Set("Host", "localhost")
   885  			req.Host = "localhost"
   886  			router.ServeHTTP(w, req)
   887  			if w.Code != 200 {
   888  				b.Log(w.Body)
   889  				b.Fatal("HTTP Error!")
   890  			}
   891  		}
   892  	}
   893  }
   894  
   895  func benchRouteNoError(b *testing.B, path string) func(*testing.PB) {
   896  	router, e := NewGenRouter(rcfg())
   897  	if e != nil {
   898  		b.Fatal(e)
   899  	}
   900  	return func(pb *testing.PB) {
   901  		for pb.Next() {
   902  			w := httptest.NewRecorder()
   903  			req := httptest.NewRequest("GET", path, bytes.NewReader(nil))
   904  			req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36")
   905  			req.Header.Set("Host", "localhost")
   906  			req.Host = "localhost"
   907  			router.ServeHTTP(w, req)
   908  		}
   909  	}
   910  }
   911  
   912  func BenchmarkProfileGuestRouteParallelWithRouter(b *testing.B) {
   913  	obRoute(b, "/profile/admin.1")
   914  }
   915  
   916  func BenchmarkPopulateTopicWithRouter(b *testing.B) {
   917  	b.ReportAllocs()
   918  	topic, err := c.Topics.Get(benchTidI)
   919  	if err != nil {
   920  		debug.PrintStack()
   921  		b.Fatal(err)
   922  	}
   923  	b.RunParallel(func(pb *testing.PB) {
   924  		for pb.Next() {
   925  			for i := 0; i < 25; i++ {
   926  				_, err := c.Rstore.Create(topic, "hiii", "", 1)
   927  				if err != nil {
   928  					debug.PrintStack()
   929  					b.Fatal(err)
   930  				}
   931  			}
   932  		}
   933  	})
   934  }
   935  
   936  //var fullPage = false
   937  
   938  func BenchmarkTopicAdminFullPageRouteParallelWithRouter(b *testing.B) {
   939  	/*if !fullPage {
   940  		topic, err := c.Topics.Get(benchTidI)
   941  		panicIfErr(err)
   942  		for i := 0; i < 25; i++ {
   943  			_, err = c.Rstore.Create(topic, "hiii", "::1", 1)
   944  			panicIfErr(err)
   945  		}
   946  		fullPage = true
   947  	}*/
   948  	BenchmarkTopicAdminRouteParallel(b)
   949  }
   950  
   951  func BenchmarkTopicGuestFullPageRouteParallelWithRouter(b *testing.B) {
   952  	/*if !fullPage {
   953  		topic, err := c.Topics.Get(benchTidI)
   954  		panicIfErr(err)
   955  		for i := 0; i < 25; i++ {
   956  			_, err = c.Rstore.Create(topic, "hiii", "::1", 1)
   957  			panicIfErr(err)
   958  		}
   959  		fullPage = true
   960  	}*/
   961  	obRoute(b, "/topic/hm."+benchTid)
   962  }
   963  
   964  var benchTidI2 int
   965  var benchTid2 string
   966  
   967  func BenchmarkPopulateTopicMentionWithRouter(b *testing.B) {
   968  	b.ReportAllocs()
   969  	tid, err := c.Topics.Create(2, "test topic", "@1", 1, "")
   970  	if err != nil {
   971  		debug.PrintStack()
   972  		b.Fatal(err)
   973  	}
   974  	benchTidI2 = tid
   975  	benchTid2 = strconv.Itoa(tid)
   976  	topic, err := c.Topics.Get(tid)
   977  	if err != nil {
   978  		debug.PrintStack()
   979  		b.Fatal(err)
   980  	}
   981  	b.RunParallel(func(pb *testing.PB) {
   982  		for pb.Next() {
   983  			for i := 0; i < 25; i++ {
   984  				_, err := c.Rstore.Create(topic, "@1", "", 1)
   985  				if err != nil {
   986  					debug.PrintStack()
   987  					b.Fatal(err)
   988  				}
   989  			}
   990  		}
   991  	})
   992  }
   993  
   994  func BenchmarkTopicMentionAdminFullPageRouteParallelWithRouter(b *testing.B) {
   995  	tI := benchTidI
   996  	t := benchTid
   997  	benchTidI = benchTidI2
   998  	benchTid = benchTid2
   999  	BenchmarkTopicAdminRouteParallel(b)
  1000  	benchTidI = tI
  1001  	benchTid = t
  1002  }
  1003  
  1004  func BenchmarkTopicMentionGuestFullPageRouteParallelWithRouter(b *testing.B) {
  1005  	obRoute(b, "/topic/hm."+benchTid2)
  1006  }
  1007  
  1008  var benchTidI3 int
  1009  var benchTid3 string
  1010  
  1011  func BenchmarkPopulateTopic10MentionWithRouter(b *testing.B) {
  1012  	b.ReportAllocs()
  1013  	tid, err := c.Topics.Create(2, "test topic", "@1 @1 @1 @1 @1 @1 @1 @1 @1 @1", 1, "")
  1014  	if err != nil {
  1015  		debug.PrintStack()
  1016  		b.Fatal(err)
  1017  	}
  1018  	benchTidI3 = tid
  1019  	benchTid3 = strconv.Itoa(tid)
  1020  	topic, err := c.Topics.Get(tid)
  1021  	if err != nil {
  1022  		debug.PrintStack()
  1023  		b.Fatal(err)
  1024  	}
  1025  	b.RunParallel(func(pb *testing.PB) {
  1026  		for pb.Next() {
  1027  			for i := 0; i < 25; i++ {
  1028  				_, err := c.Rstore.Create(topic, "@1 @1 @1 @1 @1 @1 @1 @1 @1 @1", "", 1)
  1029  				if err != nil {
  1030  					debug.PrintStack()
  1031  					b.Fatal(err)
  1032  				}
  1033  			}
  1034  		}
  1035  	})
  1036  }
  1037  
  1038  func BenchmarkTopic10MentionAdminFullPageRouteParallelWithRouter(b *testing.B) {
  1039  	tI := benchTidI
  1040  	t := benchTid
  1041  	benchTidI = benchTidI3
  1042  	benchTid = benchTid3
  1043  	BenchmarkTopicAdminRouteParallel(b)
  1044  	benchTidI = tI
  1045  	benchTid = t
  1046  }
  1047  
  1048  func BenchmarkTopic10MentionGuestFullPageRouteParallelWithRouter(b *testing.B) {
  1049  	obRoute(b, "/topic/hm."+benchTid3)
  1050  }
  1051  
  1052  var benchTidI4 int
  1053  var benchTid4 string
  1054  
  1055  func BenchmarkPopulateTopic1ReplyWithRouter(b *testing.B) {
  1056  	b.ReportAllocs()
  1057  	tid, err := c.Topics.Create(2, "test topic", "hiii", 1, "")
  1058  	if err != nil {
  1059  		debug.PrintStack()
  1060  		b.Fatal(err)
  1061  	}
  1062  	benchTidI4 = tid
  1063  	benchTid4 = strconv.Itoa(tid)
  1064  	topic, err := c.Topics.Get(tid)
  1065  	if err != nil {
  1066  		debug.PrintStack()
  1067  		b.Fatal(err)
  1068  	}
  1069  	b.RunParallel(func(pb *testing.PB) {
  1070  		for pb.Next() {
  1071  			_, err := c.Rstore.Create(topic, "hiii", "", 1)
  1072  			if err != nil {
  1073  				debug.PrintStack()
  1074  				b.Fatal(err)
  1075  			}
  1076  		}
  1077  	})
  1078  }
  1079  
  1080  func BenchmarkTopic1ReplyAdminRouteParallelWithRouter(b *testing.B) {
  1081  	tI := benchTidI
  1082  	t := benchTid
  1083  	benchTidI = benchTidI4
  1084  	benchTid = benchTid4
  1085  	BenchmarkTopicAdminRouteParallel(b)
  1086  	benchTidI = tI
  1087  	benchTid = t
  1088  }
  1089  
  1090  func BenchmarkTopic1ReplyGuestRouteParallelWithRouter(b *testing.B) {
  1091  	obRoute(b, "/topic/hm."+benchTid4)
  1092  }
  1093  
  1094  var benchTidI5 int
  1095  var benchTid5 string
  1096  var benchUidI int
  1097  
  1098  func BenchmarkPopulateTopic2UserWithRouter(b *testing.B) {
  1099  	b.ReportAllocs()
  1100  	nUid, err := c.Users.Create("testing", "testpass", "", 2, true)
  1101  	if err != nil {
  1102  		debug.PrintStack()
  1103  		b.Fatal(err)
  1104  	}
  1105  	benchUidI = nUid
  1106  	tid, err := c.Topics.Create(2, "test topic", "hiii", 1, "")
  1107  	if err != nil {
  1108  		debug.PrintStack()
  1109  		b.Fatal(err)
  1110  	}
  1111  	benchTidI5 = tid
  1112  	benchTid5 = strconv.Itoa(tid)
  1113  	topic, err := c.Topics.Get(tid)
  1114  	if err != nil {
  1115  		debug.PrintStack()
  1116  		b.Fatal(err)
  1117  	}
  1118  	b.RunParallel(func(pb *testing.PB) {
  1119  		var uid int
  1120  		for pb.Next() {
  1121  			for i := 0; i < 25; i++ {
  1122  				if i%2 == 0 {
  1123  					uid = nUid
  1124  				} else {
  1125  					uid = 1
  1126  				}
  1127  				_, err := c.Rstore.Create(topic, "hiii", "", uid)
  1128  				if err != nil {
  1129  					debug.PrintStack()
  1130  					b.Fatal(err)
  1131  				}
  1132  			}
  1133  		}
  1134  	})
  1135  }
  1136  
  1137  func BenchmarkTopic2UserAdminFullPageRouteParallelWithRouter(b *testing.B) {
  1138  	tI := benchTidI
  1139  	t := benchTid
  1140  	benchTidI = benchTidI5
  1141  	benchTid = benchTid5
  1142  	BenchmarkTopicAdminRouteParallel(b)
  1143  	benchTidI = tI
  1144  	benchTid = t
  1145  }
  1146  
  1147  func BenchmarkTopic2UserGuestFullPageRouteParallelWithRouter(b *testing.B) {
  1148  	obRoute(b, "/topic/hm."+benchTid5)
  1149  }
  1150  
  1151  var benchTidI6 int
  1152  var benchTid6 string
  1153  
  1154  func BenchmarkPopulateTopic3UserWithRouter(b *testing.B) {
  1155  	b.ReportAllocs()
  1156  	nUid, err := c.Users.Create("testing2", "testpass", "", 2, true)
  1157  	if err != nil {
  1158  		debug.PrintStack()
  1159  		b.Fatal(err)
  1160  	}
  1161  	tid, err := c.Topics.Create(2, "test topic", "hiii", 1, "")
  1162  	if err != nil {
  1163  		debug.PrintStack()
  1164  		b.Fatal(err)
  1165  	}
  1166  	benchTidI6 = tid
  1167  	benchTid6 = strconv.Itoa(tid)
  1168  	t, err := c.Topics.Get(tid)
  1169  	if err != nil {
  1170  		debug.PrintStack()
  1171  		b.Fatal(err)
  1172  	}
  1173  	b.RunParallel(func(pb *testing.PB) {
  1174  		for pb.Next() {
  1175  			for i := 0; i < 5; i++ {
  1176  				_, err := c.Rstore.Create(t, "hiii", "", 1)
  1177  				if err != nil {
  1178  					debug.PrintStack()
  1179  					b.Fatal(err)
  1180  				}
  1181  				_, err = c.Rstore.Create(t, "hiii", "", benchUidI)
  1182  				if err != nil {
  1183  					debug.PrintStack()
  1184  					b.Fatal(err)
  1185  				}
  1186  				_, err = c.Rstore.Create(t, "hiii", "", nUid)
  1187  				if err != nil {
  1188  					debug.PrintStack()
  1189  					b.Fatal(err)
  1190  				}
  1191  			}
  1192  		}
  1193  	})
  1194  }
  1195  
  1196  func BenchmarkTopic3UserAdminFullPageRouteParallelWithRouter(b *testing.B) {
  1197  	tI := benchTidI
  1198  	t := benchTid
  1199  	benchTidI = benchTidI6
  1200  	benchTid = benchTid6
  1201  	BenchmarkTopicAdminRouteParallel(b)
  1202  	benchTidI = tI
  1203  	benchTid = t
  1204  }
  1205  
  1206  func BenchmarkTopic3UserGuestFullPageRouteParallelWithRouter(b *testing.B) {
  1207  	obRoute(b, "/topic/hm."+benchTid6)
  1208  }
  1209  
  1210  // TODO: Add topic poll page bench
  1211  
  1212  // TODO: Make these routes compatible with the changes to the router
  1213  /*
  1214  func BenchmarkForumsAdminRouteParallel(b *testing.B) {
  1215  	b.ReportAllocs()
  1216  	gloinit()
  1217  	b.RunParallel(func(pb *testing.PB) {
  1218  		admin, err := users.Get(1)
  1219  		if err != nil {
  1220  			panic(err)
  1221  		}
  1222  		if !admin.Is_Admin {
  1223  			panic("UID1 is not an admin")
  1224  		}
  1225  		adminUidCookie := http.Cookie{Name:"uid",Value:"1",Path:"/",MaxAge: year}
  1226  		adminSessionCookie := http.Cookie{Name:"session",Value: admin.Session,Path:"/",MaxAge: year}
  1227  
  1228  		forumsW := httptest.NewRecorder()
  1229  		forumsReq := httptest.NewRequest("get","/forums/",bytes.NewReader(nil))
  1230  		forumsReqAdmin := forums_req
  1231  		forumsReqAdmin.AddCookie(&adminUidCookie)
  1232  		forumsReqAdmin.AddCookie(&adminSessionCookie)
  1233  		forumsHandler := http.HandlerFunc(route_forums)
  1234  
  1235  		for pb.Next() {
  1236  			forumsW.Body.Reset()
  1237  			forumsHandler.ServeHTTP(forumsW,forumsReqAdmin)
  1238  		}
  1239  	})
  1240  }
  1241  
  1242  func BenchmarkForumsAdminRouteParallelProf(b *testing.B) {
  1243  	b.ReportAllocs()
  1244  	gloinit()
  1245  
  1246  	b.RunParallel(func(pb *testing.PB) {
  1247  		admin, err := users.Get(1)
  1248  		if err != nil {
  1249  			panic(err)
  1250  		}
  1251  		if !admin.Is_Admin {
  1252  			panic("UID1 is not an admin")
  1253  		}
  1254  		adminUidCookie := http.Cookie{Name:"uid",Value:"1",Path:"/",MaxAge: year}
  1255  		adminSessionCookie := http.Cookie{Name:"session",Value: admin.Session,Path: "/",MaxAge: year}
  1256  
  1257  		forumsW := httptest.NewRecorder()
  1258  		forumsReq := httptest.NewRequest("get","/forums/",bytes.NewReader(nil))
  1259  		forumsReqAdmin := forumsReq
  1260  		forumsReqAdmin.AddCookie(&admin_uid_cookie)
  1261  		forumsReqAdmin.AddCookie(&admin_session_cookie)
  1262  		forumsHandler := http.HandlerFunc(route_forums)
  1263  		f, err := os.Create("cpu_forums_admin_parallel.prof")
  1264  		if err != nil {
  1265  			log.Fatal(err)
  1266  		}
  1267  		pprof.StartCPUProfile(f)
  1268  		for pb.Next() {
  1269  			forumsW.Body.Reset()
  1270  			forumsHandler.ServeHTTP(forumsW,forumsReqAdmin)
  1271  		}
  1272  		pprof.StopCPUProfile()
  1273  	})
  1274  }
  1275  
  1276  func BenchmarkRoutesSerial(b *testing.B) {
  1277  	b.ReportAllocs()
  1278  	admin, err := users.Get(1)
  1279  	if err != nil {
  1280  		panic(err)
  1281  	}
  1282  	if !admin.Is_Admin {
  1283  		panic("UID1 is not an admin")
  1284  	}
  1285  
  1286  	admin_uid_cookie := http.Cookie{Name:"uid",Value:"1",Path:"/",MaxAge: year}
  1287  	admin_session_cookie := http.Cookie{Name:"session",Value: admin.Session,Path: "/",MaxAge: year}
  1288  
  1289  	if plugins_inited {
  1290  		b.Log("Plugins have already been initialised, they can't be deinitialised so these tests will run with plugins on")
  1291  	}
  1292  	static_w := httptest.NewRecorder()
  1293  	static_req := httptest.NewRequest("get","/s/global.js",bytes.NewReader(nil))
  1294  	static_handler := http.HandlerFunc(route_static)
  1295  
  1296  	topic_w := httptest.NewRecorder()
  1297  	topic_req := httptest.NewRequest("get","/topic/1",bytes.NewReader(nil))
  1298  	topic_req_admin := topic_req
  1299  	topic_req_admin.AddCookie(&admin_uid_cookie)
  1300  	topic_req_admin.AddCookie(&admin_session_cookie)
  1301  	topic_handler := http.HandlerFunc(route_topic_id)
  1302  
  1303  	topics_w := httptest.NewRecorder()
  1304  	topics_req := httptest.NewRequest("get","/topics/",bytes.NewReader(nil))
  1305  	topics_req_admin := topics_req
  1306  	topics_req_admin.AddCookie(&admin_uid_cookie)
  1307  	topics_req_admin.AddCookie(&admin_session_cookie)
  1308  	topics_handler := http.HandlerFunc(route_topics)
  1309  
  1310  	forum_w := httptest.NewRecorder()
  1311  	forum_req := httptest.NewRequest("get","/forum/1",bytes.NewReader(nil))
  1312  	forum_req_admin := forum_req
  1313  	forum_req_admin.AddCookie(&admin_uid_cookie)
  1314  	forum_req_admin.AddCookie(&admin_session_cookie)
  1315  	forum_handler := http.HandlerFunc(route_forum)
  1316  
  1317  	forums_w := httptest.NewRecorder()
  1318  	forums_req := httptest.NewRequest("get","/forums/",bytes.NewReader(nil))
  1319  	forums_req_admin := forums_req
  1320  	forums_req_admin.AddCookie(&admin_uid_cookie)
  1321  	forums_req_admin.AddCookie(&admin_session_cookie)
  1322  	forums_handler := http.HandlerFunc(route_forums)
  1323  
  1324  	gloinit()
  1325  
  1326  	//f, err := os.Create("routes_bench_cpu.prof")
  1327  	//if err != nil {
  1328  	//	log.Fatal(err)
  1329  	//}
  1330  	//pprof.StartCPUProfile(f)
  1331  	///defer pprof.StopCPUProfile()
  1332  	///pprof.StopCPUProfile()
  1333  
  1334  	b.Run("static_recorder", func(b *testing.B) {
  1335  		for i := 0; i < b.N; i++ {
  1336  			//static_w.Code = 200
  1337  			static_w.Body.Reset()
  1338  			static_handler.ServeHTTP(static_w,static_req)
  1339  			//if static_w.Code != 200 {
  1340  			//	b.Print(static_w.Body)
  1341  			//	b.Fatal("HTTP Error!")
  1342  			//}
  1343  		}
  1344  	})
  1345  
  1346  	b.Run("topic_admin_recorder", func(b *testing.B) {
  1347  		//f, err := os.Create("routes_bench_topic_cpu.prof")
  1348  		//if err != nil {
  1349  		//	b.Fatal(err)
  1350  		//}
  1351  		//pprof.StartCPUProfile(f)
  1352  		for i := 0; i < b.N; i++ {
  1353  			//topic_w.Code = 200
  1354  			topic_w.Body.Reset()
  1355  			topic_handler.ServeHTTP(topic_w,topic_req_admin)
  1356  			//if topic_w.Code != 200 {
  1357  			//	b.Print(topic_w.Body)
  1358  			//	b.Fatal("HTTP Error!")
  1359  			//}
  1360  		}
  1361  		//pprof.StopCPUProfile()
  1362  	})
  1363  	b.Run("topic_guest_recorder", func(b *testing.B) {
  1364  		f, err := os.Create("routes_bench_topic_cpu_2.prof")
  1365  		if err != nil {
  1366  			b.Fatal(err)
  1367  		}
  1368  		pprof.StartCPUProfile(f)
  1369  		for i := 0; i < b.N; i++ {
  1370  			//topic_w.Code = 200
  1371  			topic_w.Body.Reset()
  1372  			topic_handler.ServeHTTP(topic_w,topic_req)
  1373  		}
  1374  		pprof.StopCPUProfile()
  1375  	})
  1376  	b.Run("topics_admin_recorder", func(b *testing.B) {
  1377  		for i := 0; i < b.N; i++ {
  1378  			//topics_w.Code = 200
  1379  			topics_w.Body.Reset()
  1380  			topics_handler.ServeHTTP(topics_w,topics_req_admin)
  1381  		}
  1382  	})
  1383  	b.Run("topics_guest_recorder", func(b *testing.B) {
  1384  		for i := 0; i < b.N; i++ {
  1385  			//topics_w.Code = 200
  1386  			topics_w.Body.Reset()
  1387  			topics_handler.ServeHTTP(topics_w,topics_req)
  1388  		}
  1389  	})
  1390  	b.Run("forum_admin_recorder", func(b *testing.B) {
  1391  		for i := 0; i < b.N; i++ {
  1392  			//forum_w.Code = 200
  1393  			forum_w.Body.Reset()
  1394  			forum_handler.ServeHTTP(forum_w,forum_req_admin)
  1395  		}
  1396  	})
  1397  	b.Run("forum_guest_recorder", func(b *testing.B) {
  1398  		for i := 0; i < b.N; i++ {
  1399  			//forum_w.Code = 200
  1400  			forum_w.Body.Reset()
  1401  			forum_handler.ServeHTTP(forum_w,forum_req)
  1402  		}
  1403  	})
  1404  	b.Run("forums_admin_recorder", func(b *testing.B) {
  1405  		for i := 0; i < b.N; i++ {
  1406  			//forums_w.Code = 200
  1407  			forums_w.Body.Reset()
  1408  			forums_handler.ServeHTTP(forums_w,forums_req_admin)
  1409  		}
  1410  	})
  1411  	b.Run("forums_guest_recorder", func(b *testing.B) {
  1412  		//f, err := os.Create("routes_bench_forums_cpu_2.prof")
  1413  		//if err != nil {
  1414  		//	b.Fatal(err)
  1415  		//}
  1416  		//pprof.StartCPUProfile(f)
  1417  		for i := 0; i < b.N; i++ {
  1418  			//forums_w.Code = 200
  1419  			forums_w.Body.Reset()
  1420  			forums_handler.ServeHTTP(forums_w,forums_req)
  1421  		}
  1422  		//pprof.StopCPUProfile()
  1423  	})
  1424  
  1425  	if !plugins_inited {
  1426  		init_plugins()
  1427  	}
  1428  
  1429  	b.Run("topic_admin_recorder_with_plugins", func(b *testing.B) {
  1430  		//f, err := os.Create("routes_bench_topic_cpu.prof")
  1431  		//if err != nil {
  1432  		//	b.Fatal(err)
  1433  		//}
  1434  		//pprof.StartCPUProfile(f)
  1435  		for i := 0; i < b.N; i++ {
  1436  			//topic_w.Code = 200
  1437  			topic_w.Body.Reset()
  1438  			topic_handler.ServeHTTP(topic_w,topic_req_admin)
  1439  			//if topic_w.Code != 200 {
  1440  			//	b.Print(topic_w.Body)
  1441  			//	b.Fatal("HTTP Error!")
  1442  			//}
  1443  		}
  1444  		//pprof.StopCPUProfile()
  1445  	})
  1446  	b.Run("topic_guest_recorder_with_plugins", func(b *testing.B) {
  1447  		//f, err := os.Create("routes_bench_topic_cpu_2.prof")
  1448  		//if err != nil {
  1449  		//	b.Fatal(err)
  1450  		//}
  1451  		//pprof.StartCPUProfile(f)
  1452  		for i := 0; i < b.N; i++ {
  1453  			//topic_w.Code = 200
  1454  			topic_w.Body.Reset()
  1455  			topic_handler.ServeHTTP(topic_w,topic_req)
  1456  		}
  1457  		//pprof.StopCPUProfile()
  1458  	})
  1459  	b.Run("topics_admin_recorder_with_plugins", func(b *testing.B) {
  1460  		for i := 0; i < b.N; i++ {
  1461  			//topics_w.Code = 200
  1462  			topics_w.Body.Reset()
  1463  			topics_handler.ServeHTTP(topics_w,topics_req_admin)
  1464  		}
  1465  	})
  1466  	b.Run("topics_guest_recorder_with_plugins", func(b *testing.B) {
  1467  		for i := 0; i < b.N; i++ {
  1468  			//topics_w.Code = 200
  1469  			topics_w.Body.Reset()
  1470  			topics_handler.ServeHTTP(topics_w,topics_req)
  1471  		}
  1472  	})
  1473  	b.Run("forum_admin_recorder_with_plugins", func(b *testing.B) {
  1474  		for i := 0; i < b.N; i++ {
  1475  			//forum_w.Code = 200
  1476  			forum_w.Body.Reset()
  1477  			forum_handler.ServeHTTP(forum_w,forum_req_admin)
  1478  		}
  1479  	})
  1480  	b.Run("forum_guest_recorder_with_plugins", func(b *testing.B) {
  1481  		for i := 0; i < b.N; i++ {
  1482  			//forum_w.Code = 200
  1483  			forum_w.Body.Reset()
  1484  			forum_handler.ServeHTTP(forum_w,forum_req)
  1485  		}
  1486  	})
  1487  	b.Run("forums_admin_recorder_with_plugins", func(b *testing.B) {
  1488  		for i := 0; i < b.N; i++ {
  1489  			//forums_w.Code = 200
  1490  			forums_w.Body.Reset()
  1491  			forums_handler.ServeHTTP(forums_w,forums_req_admin)
  1492  		}
  1493  	})
  1494  	b.Run("forums_guest_recorder_with_plugins", func(b *testing.B) {
  1495  		//f, err := os.Create("routes_bench_forums_cpu_2.prof")
  1496  		//if err != nil {
  1497  		//	b.Fatal(err)
  1498  		//}
  1499  		//pprof.StartCPUProfile(f)
  1500  		for i := 0; i < b.N; i++ {
  1501  			//forums_w.Code = 200
  1502  			forums_w.Body.Reset()
  1503  			forums_handler.ServeHTTP(forums_w,forums_req)
  1504  		}
  1505  		//pprof.StopCPUProfile()
  1506  	})
  1507  }*/
  1508  
  1509  func BenchmarkQueryTopicParallel(b *testing.B) {
  1510  	b.ReportAllocs()
  1511  	if err := gloinit(); err != nil {
  1512  		b.Fatal(err)
  1513  	}
  1514  
  1515  	b.RunParallel(func(pb *testing.PB) {
  1516  		var tu c.TopicUser
  1517  		for pb.Next() {
  1518  			err := db.QueryRow("select topics.title, topics.content, topics.createdBy, topics.createdAt, topics.is_closed, topics.sticky, topics.parentID, topics.ip, topics.views, topics.postCount, topics.likeCount, users.name, users.avatar, users.group, users.level from topics left join users ON topics.createdBy = users.uid where tid = ?", 1).Scan(&tu.Title, &tu.Content, &tu.CreatedBy, &tu.CreatedAt, &tu.IsClosed, &tu.Sticky, &tu.ParentID, &tu.IP, &tu.ViewCount, &tu.PostCount, &tu.LikeCount, &tu.CreatedByName, &tu.Avatar, &tu.Group, &tu.Level)
  1519  			if err == ErrNoRows {
  1520  				log.Fatal("No rows found!")
  1521  				return
  1522  			} else if err != nil {
  1523  				log.Fatal(err)
  1524  				return
  1525  			}
  1526  		}
  1527  	})
  1528  }
  1529  
  1530  func BenchmarkQueryPreparedTopicParallel(b *testing.B) {
  1531  	b.ReportAllocs()
  1532  	if err := gloinit(); err != nil {
  1533  		b.Fatal(err)
  1534  	}
  1535  
  1536  	b.RunParallel(func(pb *testing.PB) {
  1537  		var tu c.TopicUser
  1538  
  1539  		getTopicUser, err := qgen.Builder.SimpleLeftJoin("topics", "users", "topics.title, topics.content, topics.createdBy, topics.createdAt, topics.is_closed, topics.sticky, topics.parentID, topics.ip, topics.postCount, topics.likeCount, users.name, users.avatar, users.group, users.level", "topics.createdBy=users.uid", "tid=?", "", "")
  1540  		if err != nil {
  1541  			b.Fatal(err)
  1542  		}
  1543  		defer getTopicUser.Close()
  1544  
  1545  		for pb.Next() {
  1546  			err := getTopicUser.QueryRow(1).Scan(&tu.Title, &tu.Content, &tu.CreatedBy, &tu.CreatedAt, &tu.IsClosed, &tu.Sticky, &tu.ParentID, &tu.IP, &tu.PostCount, &tu.LikeCount, &tu.CreatedByName, &tu.Avatar, &tu.Group, &tu.Level)
  1547  			if err == ErrNoRows {
  1548  				b.Fatal("No rows found!")
  1549  				return
  1550  			} else if err != nil {
  1551  				b.Fatal(err)
  1552  				return
  1553  			}
  1554  		}
  1555  	})
  1556  }
  1557  
  1558  func BenchmarkUserGet(b *testing.B) {
  1559  	b.ReportAllocs()
  1560  	if err := gloinit(); err != nil {
  1561  		b.Fatal(err)
  1562  	}
  1563  
  1564  	b.RunParallel(func(pb *testing.PB) {
  1565  		var err error
  1566  		for pb.Next() {
  1567  			_, err = c.Users.Get(1)
  1568  			if err != nil {
  1569  				b.Fatal(err)
  1570  				return
  1571  			}
  1572  		}
  1573  	})
  1574  }
  1575  
  1576  func BenchmarkUserBypassGet(b *testing.B) {
  1577  	b.ReportAllocs()
  1578  	if err := gloinit(); err != nil {
  1579  		b.Fatal(err)
  1580  	}
  1581  
  1582  	// Bypass the cache and always hit the database
  1583  	b.RunParallel(func(pb *testing.PB) {
  1584  		var err error
  1585  		for pb.Next() {
  1586  			_, err = c.Users.BypassGet(1)
  1587  			if err != nil {
  1588  				b.Fatal(err)
  1589  				return
  1590  			}
  1591  		}
  1592  	})
  1593  }
  1594  
  1595  func BenchmarkQueriesSerial(b *testing.B) {
  1596  	b.ReportAllocs()
  1597  	var tu c.TopicUser
  1598  	b.Run("topic", func(b *testing.B) {
  1599  		for i := 0; i < b.N; i++ {
  1600  			err := db.QueryRow("select topics.title, topics.content, topics.createdBy, topics.createdAt, topics.is_closed, topics.sticky, topics.parentID, topics.ip, topics.postCount, topics.likeCount, users.name, users.avatar, users.group, users.level from topics left join users ON topics.createdBy = users.uid where tid = ?", 1).Scan(&tu.Title, &tu.Content, &tu.CreatedBy, &tu.CreatedAt, &tu.IsClosed, &tu.Sticky, &tu.ParentID, &tu.IP, &tu.PostCount, &tu.LikeCount, &tu.CreatedByName, &tu.Avatar, &tu.Group, &tu.Level)
  1601  			if err == ErrNoRows {
  1602  				b.Fatal("No rows found!")
  1603  				return
  1604  			} else if err != nil {
  1605  				b.Fatal(err)
  1606  				return
  1607  			}
  1608  		}
  1609  	})
  1610  	b.Run("topic_replies", func(b *testing.B) {
  1611  		for i := 0; i < b.N; i++ {
  1612  			rows, err := db.Query("select replies.rid, replies.content, replies.createdBy, replies.createdAt, replies.lastEdit, replies.lastEditBy, users.avatar, users.name, users.is_super_admin, users.group, users.level, replies.ip from replies left join users ON replies.createdBy = users.uid where tid = ?", 1)
  1613  			if err != nil {
  1614  				b.Fatal(err)
  1615  				return
  1616  			}
  1617  			defer rows.Close()
  1618  
  1619  			for rows.Next() {
  1620  			}
  1621  			err = rows.Err()
  1622  			if err != nil {
  1623  				b.Fatal(err)
  1624  				return
  1625  			}
  1626  		}
  1627  	})
  1628  
  1629  	var r c.ReplyUser
  1630  	var isSuperAdmin bool
  1631  	var group int
  1632  	b.Run("topic_replies_scan", func(b *testing.B) {
  1633  		for i := 0; i < b.N; i++ {
  1634  			rows, err := db.Query("select replies.rid, replies.content, replies.createdBy, replies.createdAt, replies.lastEdit, replies.lastEditBy, users.avatar, users.name, users.is_super_admin, users.group, users.level, replies.ip from replies left join users ON replies.createdBy = users.uid where tid = ?", 1)
  1635  			if err != nil {
  1636  				b.Fatal(err)
  1637  				return
  1638  			}
  1639  			for rows.Next() {
  1640  				err := rows.Scan(&r.ID, &r.Content, &r.CreatedBy, &r.CreatedAt, &r.LastEdit, &r.LastEditBy, &r.Avatar, &r.CreatedByName, &isSuperAdmin, &group, &r.Level, &r.IP)
  1641  				if err != nil {
  1642  					b.Fatal(err)
  1643  					return
  1644  				}
  1645  			}
  1646  			defer rows.Close()
  1647  			if err = rows.Err(); err != nil {
  1648  				b.Fatal(err)
  1649  				return
  1650  			}
  1651  		}
  1652  	})
  1653  }
  1654  
  1655  // TODO: Take the attachment system into account in these parser benches
  1656  func BenchmarkParserSerial(b *testing.B) {
  1657  	if !c.PluginsInited {
  1658  		c.InitPlugins()
  1659  	}
  1660  	b.ReportAllocs()
  1661  	f := func(name, msg string) func(b *testing.B) {
  1662  		return func(b *testing.B) {
  1663  			for i := 0; i < b.N; i++ {
  1664  				_ = c.ParseMessage(msg, 0, "", nil, nil)
  1665  			}
  1666  		}
  1667  	}
  1668  	f("empty_post", "")
  1669  	f("short_post", "Hey everyone, how's it going?")
  1670  	f("one_smily", "Hey everyone, how's it going? :)")
  1671  	f("five_smilies", "Hey everyone, how's it going? :):):):):)")
  1672  	f("ten_smilies", "Hey everyone, how's it going? :):):):):):):):):):)")
  1673  	f("twenty_smilies", "Hey everyone, how's it going? :):):):):):):):):):):):):):):):):):):):)")
  1674  }
  1675  
  1676  func BenchmarkBBCodePluginWithRegexpSerial(b *testing.B) {
  1677  	if !c.PluginsInited {
  1678  		c.InitPlugins()
  1679  	}
  1680  	b.ReportAllocs()
  1681  	f := func(name string, msg string) {
  1682  		b.Run(name, func(b *testing.B) {
  1683  			for i := 0; i < b.N; i++ {
  1684  				_ = e.BbcodeRegexParse(msg)
  1685  			}
  1686  		})
  1687  	}
  1688  	f("empty_post", "")
  1689  	f("short_post", "Hey everyone, how's it going?")
  1690  	f("one_smily", "Hey everyone, how's it going? :)")
  1691  	f("five_smilies", "Hey everyone, how's it going? :):):):):)")
  1692  	f("ten_smilies", "Hey everyone, how's it going? :):):):):):):):):):)")
  1693  	f("twenty_smilies", "Hey everyone, how's it going? :):):):):):):):):):):):):):):):):):):):)")
  1694  	f("one_bold", "[b]H[/b]ey everyone, how's it going?")
  1695  	f("five_bold", "[b]H[/b][b]e[/b][b]y[/b] [b]e[/b][b]v[/b]eryone, how's it going?")
  1696  	f("ten_bold", "[b]H[/b][b]e[/b][b]y[/b] [b]e[/b][b]v[/b][b]e[/b][b]r[/b][b]y[/b][b]o[/b][b]n[/b]e, how's it going?")
  1697  }
  1698  
  1699  func BenchmarkBBCodePluginWithoutCodeTagSerial(b *testing.B) {
  1700  	b.ReportAllocs()
  1701  	f := func(name string, msg string) {
  1702  		b.Run(name, func(b *testing.B) {
  1703  			for i := 0; i < b.N; i++ {
  1704  				_ = e.BbcodeParseWithoutCode(msg)
  1705  			}
  1706  		})
  1707  	}
  1708  	f("empty_post", "")
  1709  	f("short_post", "Hey everyone, how's it going?")
  1710  	f("one_smily", "Hey everyone, how's it going? :)")
  1711  	f("five_smilies", "Hey everyone, how's it going? :):):):):)")
  1712  	f("ten_smilies", "Hey everyone, how's it going? :):):):):):):):):):)")
  1713  	f("twenty_smilies", "Hey everyone, how's it going? :):):):):):):):):):):):):):):):):):):):)")
  1714  	f("one_bold", "[b]H[/b]ey everyone, how's it going?")
  1715  	f("five_bold", "[b]H[/b][b]e[/b][b]y[/b] [b]e[/b][b]v[/b]eryone, how's it going?")
  1716  	f("ten_bold", "[b]H[/b][b]e[/b][b]y[/b] [b]e[/b][b]v[/b][b]e[/b][b]r[/b][b]y[/b][b]o[/b][b]n[/b]e, how's it going?")
  1717  }
  1718  
  1719  func BenchmarkBBCodePluginWithFullParserSerial(b *testing.B) {
  1720  	b.ReportAllocs()
  1721  	f := func(name string, msg string) {
  1722  		b.Run(name, func(b *testing.B) {
  1723  			for i := 0; i < b.N; i++ {
  1724  				_ = e.BbcodeFullParse(msg)
  1725  			}
  1726  		})
  1727  	}
  1728  	f("empty_post", "")
  1729  	f("short_post", "Hey everyone, how's it going?")
  1730  	f("one_smily", "Hey everyone, how's it going? :)")
  1731  	f("five_smilies", "Hey everyone, how's it going? :):):):):)")
  1732  	f("ten_smilies", "Hey everyone, how's it going? :):):):):):):):):):)")
  1733  	f("twenty_smilies", "Hey everyone, how's it going? :):):):):):):):):):):):):):):):):):):):)")
  1734  	f("one_bold", "[b]H[/b]ey everyone, how's it going?")
  1735  	f("five_bold", "[b]H[/b][b]e[/b][b]y[/b] [b]e[/b][b]v[/b]eryone, how's it going?")
  1736  	f("ten_bold", "[b]H[/b][b]e[/b][b]y[/b] [b]e[/b][b]v[/b][b]e[/b][b]r[/b][b]y[/b][b]o[/b][b]n[/b]e, how's it going?")
  1737  }
  1738  
  1739  func TestLevels(t *testing.T) {
  1740  	levels := c.GetLevels(40)
  1741  	for level, score := range levels {
  1742  		sscore := strconv.FormatFloat(score, 'f', -1, 64)
  1743  		t.Log("Level: " + strconv.Itoa(level) + " Score: " + sscore)
  1744  	}
  1745  }
  1746  
  1747  // TODO: Make this compatible with the changes to the router
  1748  /*
  1749  func TestStaticRoute(t *testing.T) {
  1750  	gloinit()
  1751  	if !plugins_inited {
  1752  		init_plugins()
  1753  	}
  1754  
  1755  	static_w := httptest.NewRecorder()
  1756  	static_req := httptest.NewRequest("get","/s/global.js",bytes.NewReader(nil))
  1757  	static_handler := http.HandlerFunc(route_static)
  1758  
  1759  	static_handler.ServeHTTP(static_w,static_req)
  1760  	if static_w.Code != 200 {
  1761  		t.Fatal(static_w.Body)
  1762  	}
  1763  }
  1764  */
  1765  
  1766  /*func TestTopicAdminRoute(t *testing.T) {
  1767  	gloinit()
  1768  	if !plugins_inited {
  1769  		init_plugins()
  1770  	}
  1771  
  1772  	admin, err := users.Get(1)
  1773  	if err != nil {
  1774  		panic(err)
  1775  	}
  1776  	if !admin.Is_Admin {
  1777  		panic("UID1 is not an admin")
  1778  	}
  1779  
  1780  	admin_uid_cookie := http.Cookie{Name:"uid",Value:"1",Path:"/",MaxAge: year}
  1781  	admin_session_cookie := http.Cookie{Name:"session",Value: admin.Session,Path:"/",MaxAge: year}
  1782  
  1783  	topic_w := httptest.NewRecorder()
  1784  	topic_req := httptest.NewRequest("get","/topic/1",bytes.NewReader(nil))
  1785  	topic_req_admin := topic_req
  1786  	topic_req_admin.AddCookie(&admin_uid_cookie)
  1787  	topic_req_admin.AddCookie(&admin_session_cookie)
  1788  	topic_handler := http.HandlerFunc(route_topic_id)
  1789  
  1790  	topic_handler.ServeHTTP(topic_w,topic_req_admin)
  1791  	if topic_w.Code != 200 {
  1792  		t.Print(topic_w.Body)
  1793  		t.Fatal("HTTP Error!")
  1794  	}
  1795  	t.Print("No problems found in the topic-admin route!")
  1796  }*/
  1797  
  1798  /*func TestTopicGuestRoute(t *testing.T) {
  1799  	gloinit()
  1800  	if !plugins_inited {
  1801  		init_plugins()
  1802  	}
  1803  
  1804  	topic_w := httptest.NewRecorder()
  1805  	topic_req := httptest.NewRequest("get","/topic/1",bytes.NewReader(nil))
  1806  	topic_handler := http.HandlerFunc(route_topic_id)
  1807  
  1808  	topic_handler.ServeHTTP(topic_w,topic_req)
  1809  	if topic_w.Code != 200 {
  1810  		t.Print(topic_w.Body)
  1811  		t.Fatal("HTTP Error!")
  1812  	}
  1813  	t.Print("No problems found in the topic-guest route!")
  1814  }*/
  1815  
  1816  // TODO: Make these routes compatible with the changes to the router
  1817  /*
  1818  func TestForumsAdminRoute(t *testing.T) {
  1819  	gloinit()
  1820  	if !plugins_inited {
  1821  		init_plugins()
  1822  	}
  1823  
  1824  	admin, err := users.Get(1)
  1825  	if err != nil {
  1826  		t.Fatal(err)
  1827  	}
  1828  	if !admin.Is_Admin {
  1829  		t.Fatal("UID1 is not an admin")
  1830  	}
  1831  	adminUidCookie := http.Cookie{Name:"uid",Value:"1",Path:"/",MaxAge: year}
  1832  	adminSessionCookie := http.Cookie{Name:"session",Value: admin.Session,Path:"/",MaxAge: year}
  1833  
  1834  	forumsW := httptest.NewRecorder()
  1835  	forumsReq := httptest.NewRequest("get","/forums/",bytes.NewReader(nil))
  1836  	forumsReqAdmin := forums_req
  1837  	forumsReqAdmin.AddCookie(&adminUidCookie)
  1838  	forumsReqAdmin.AddCookie(&adminSessionCookie)
  1839  	forumsHandler := http.HandlerFunc(route_forums)
  1840  
  1841  	forumsHandler.ServeHTTP(forumsW,forumsReqAdmin)
  1842  	if forumsW.Code != 200 {
  1843  		t.Fatal(forumsW.Body)
  1844  	}
  1845  }
  1846  
  1847  func TestForumsGuestRoute(t *testing.T) {
  1848  	gloinit()
  1849  	if !plugins_inited {
  1850  		init_plugins()
  1851  	}
  1852  
  1853  	forums_w := httptest.NewRecorder()
  1854  	forums_req := httptest.NewRequest("get","/forums/",bytes.NewReader(nil))
  1855  	forums_handler := http.HandlerFunc(route_forums)
  1856  
  1857  	forums_handler.ServeHTTP(forums_w,forums_req)
  1858  	if forums_w.Code != 200 {
  1859  		t.Fatal(forums_w.Body)
  1860  	}
  1861  }
  1862  */
  1863  
  1864  /*func TestForumAdminRoute(t *testing.T) {
  1865  	gloinit()
  1866  	if !plugins_inited {
  1867  		init_plugins()
  1868  	}
  1869  
  1870  	admin, err := users.Get(1)
  1871  	if err != nil {
  1872  		panic(err)
  1873  	}
  1874  	if !admin.Is_Admin {
  1875  		panic("UID1 is not an admin")
  1876  	}
  1877  	admin_uid_cookie := http.Cookie{Name:"uid",Value:"1",Path:"/",MaxAge: year}
  1878  	admin_session_cookie := http.Cookie{Name:"session",Value: admin.Session,Path:"/",MaxAge: year}
  1879  
  1880  	forum_w := httptest.NewRecorder()
  1881  	forum_req := httptest.NewRequest("get","/forum/1",bytes.NewReader(nil))
  1882  	forum_req_admin := forum_req
  1883  	forum_req_admin.AddCookie(&admin_uid_cookie)
  1884  	forum_req_admin.AddCookie(&admin_session_cookie)
  1885  	forum_handler := http.HandlerFunc(route_forum)
  1886  
  1887  	forum_handler.ServeHTTP(forum_w,forum_req_admin)
  1888  	if forum_w.Code != 200 {
  1889  		t.Print(forum_w.Body)
  1890  		t.Fatal("HTTP Error!")
  1891  	}
  1892  }*/
  1893  
  1894  /*func TestForumGuestRoute(t *testing.T) {
  1895  	gloinit()
  1896  	if !plugins_inited {
  1897  		init_plugins()
  1898  	}
  1899  
  1900  	forum_w := httptest.NewRecorder()
  1901  	forum_req := httptest.NewRequest("get","/forum/2",bytes.NewReader(nil))
  1902  	forum_handler := http.HandlerFunc(route_forum)
  1903  
  1904  	forum_handler.ServeHTTP(forum_w,forum_req)
  1905  	if forum_w.Code != 200 {
  1906  		t.Print(forum_w.Body)
  1907  		t.Fatal("HTTP Error!")
  1908  	}
  1909  }*/
  1910  
  1911  /*func TestAlerts(t *testing.T) {
  1912  	gloinit()
  1913  	if !plugins_inited {
  1914  		init_plugins()
  1915  	}
  1916  	db = db_test
  1917  	alert_w := httptest.NewRecorder()
  1918  	alert_req := httptest.NewRequest("get","/api/?action=get&module=alerts&format=json",bytes.NewReader(nil))
  1919  	alert_handler := http.HandlerFunc(route_api)
  1920  	//testdb.StubQuery()
  1921  	testdb.SetQueryFunc(func(query string) (result sql.Rows, err error) {
  1922  		cols := []string{"asid","actor","targetUser","event","elementType","elementID"}
  1923  		rows := `1,1,0,like,post,5
  1924  		1,1,0,friend_invite,user,2`
  1925  		return testdb.RowsFromCSVString(cols,rows), nil
  1926  	})
  1927  
  1928  	alert_handler.ServeHTTP(alert_w,alert_req)
  1929  	t.Print(alert_w.Body)
  1930  	if alert_w.Code != 200 {
  1931  		t.Fatal("HTTP Error!")
  1932  	}
  1933  	db = db_prod
  1934  }*/
  1935  
  1936  func TestSplittyThing(t *testing.T) {
  1937  	var extraData string
  1938  	path := "/pages/hohoho"
  1939  	t.Log("Raw Path:", path)
  1940  	if path[len(path)-1] != '/' {
  1941  		extraData = path[strings.LastIndexByte(path, '/')+1:]
  1942  		path = path[:strings.LastIndexByte(path, '/')+1]
  1943  	}
  1944  	t.Log("Path:", path)
  1945  	t.Log("Extra Data:", extraData)
  1946  	t.Log("Path Bytes:", []byte(path))
  1947  	t.Log("Extra Data Bytes:", []byte(extraData))
  1948  
  1949  	t.Log("Splitty thing test")
  1950  	path = "/topics/"
  1951  	extraData = ""
  1952  	t.Log("Raw Path:", path)
  1953  	if path[len(path)-1] != '/' {
  1954  		extraData = path[strings.LastIndexByte(path, '/')+1:]
  1955  		path = path[:strings.LastIndexByte(path, '/')+1]
  1956  	}
  1957  	t.Log("Path:", path)
  1958  	t.Log("Extra Data:", extraData)
  1959  	t.Log("Path Bytes:", []byte(path))
  1960  	t.Log("Extra Data Bytes:", []byte(extraData))
  1961  }