|
1 | 1 | package session
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "errors" |
| 5 | + "sync" |
4 | 6 | "testing"
|
5 | 7 | "time"
|
6 | 8 |
|
@@ -673,3 +675,230 @@ func Benchmark_Session(b *testing.B) {
|
673 | 675 | utils.AssertEqual(b, nil, err)
|
674 | 676 | })
|
675 | 677 | }
|
| 678 | + |
| 679 | +// go test -v -run=^$ -bench=Benchmark_Session_Parallel -benchmem -count=4 |
| 680 | +func Benchmark_Session_Parallel(b *testing.B) { |
| 681 | + b.Run("default", func(b *testing.B) { |
| 682 | + app, store := fiber.New(), New() |
| 683 | + b.ReportAllocs() |
| 684 | + b.ResetTimer() |
| 685 | + b.RunParallel(func(pb *testing.PB) { |
| 686 | + for pb.Next() { |
| 687 | + c := app.AcquireCtx(&fasthttp.RequestCtx{}) |
| 688 | + c.Request().Header.SetCookie(store.sessionName, "12356789") |
| 689 | + |
| 690 | + sess, _ := store.Get(c) //nolint:errcheck // We're inside a benchmark |
| 691 | + sess.Set("john", "doe") |
| 692 | + _ = sess.Save() //nolint:errcheck // We're inside a benchmark |
| 693 | + app.ReleaseCtx(c) |
| 694 | + } |
| 695 | + }) |
| 696 | + }) |
| 697 | + |
| 698 | + b.Run("storage", func(b *testing.B) { |
| 699 | + app := fiber.New() |
| 700 | + store := New(Config{ |
| 701 | + Storage: memory.New(), |
| 702 | + }) |
| 703 | + b.ReportAllocs() |
| 704 | + b.ResetTimer() |
| 705 | + b.RunParallel(func(pb *testing.PB) { |
| 706 | + for pb.Next() { |
| 707 | + c := app.AcquireCtx(&fasthttp.RequestCtx{}) |
| 708 | + c.Request().Header.SetCookie(store.sessionName, "12356789") |
| 709 | + |
| 710 | + sess, _ := store.Get(c) //nolint:errcheck // We're inside a benchmark |
| 711 | + sess.Set("john", "doe") |
| 712 | + _ = sess.Save() //nolint:errcheck // We're inside a benchmark |
| 713 | + app.ReleaseCtx(c) |
| 714 | + } |
| 715 | + }) |
| 716 | + }) |
| 717 | +} |
| 718 | + |
| 719 | +// go test -v -run=^$ -bench=Benchmark_Session_Asserted -benchmem -count=4 |
| 720 | +func Benchmark_Session_Asserted(b *testing.B) { |
| 721 | + b.Run("default", func(b *testing.B) { |
| 722 | + app, store := fiber.New(), New() |
| 723 | + c := app.AcquireCtx(&fasthttp.RequestCtx{}) |
| 724 | + defer app.ReleaseCtx(c) |
| 725 | + c.Request().Header.SetCookie(store.sessionName, "12356789") |
| 726 | + |
| 727 | + b.ReportAllocs() |
| 728 | + b.ResetTimer() |
| 729 | + for n := 0; n < b.N; n++ { |
| 730 | + sess, err := store.Get(c) |
| 731 | + utils.AssertEqual(b, nil, err) |
| 732 | + sess.Set("john", "doe") |
| 733 | + err = sess.Save() |
| 734 | + utils.AssertEqual(b, nil, err) |
| 735 | + } |
| 736 | + }) |
| 737 | + |
| 738 | + b.Run("storage", func(b *testing.B) { |
| 739 | + app := fiber.New() |
| 740 | + store := New(Config{ |
| 741 | + Storage: memory.New(), |
| 742 | + }) |
| 743 | + c := app.AcquireCtx(&fasthttp.RequestCtx{}) |
| 744 | + defer app.ReleaseCtx(c) |
| 745 | + c.Request().Header.SetCookie(store.sessionName, "12356789") |
| 746 | + |
| 747 | + b.ReportAllocs() |
| 748 | + b.ResetTimer() |
| 749 | + for n := 0; n < b.N; n++ { |
| 750 | + sess, err := store.Get(c) |
| 751 | + utils.AssertEqual(b, nil, err) |
| 752 | + sess.Set("john", "doe") |
| 753 | + err = sess.Save() |
| 754 | + utils.AssertEqual(b, nil, err) |
| 755 | + } |
| 756 | + }) |
| 757 | +} |
| 758 | + |
| 759 | +// go test -v -run=^$ -bench=Benchmark_Session_Asserted_Parallel -benchmem -count=4 |
| 760 | +func Benchmark_Session_Asserted_Parallel(b *testing.B) { |
| 761 | + b.Run("default", func(b *testing.B) { |
| 762 | + app, store := fiber.New(), New() |
| 763 | + b.ReportAllocs() |
| 764 | + b.ResetTimer() |
| 765 | + b.RunParallel(func(pb *testing.PB) { |
| 766 | + for pb.Next() { |
| 767 | + c := app.AcquireCtx(&fasthttp.RequestCtx{}) |
| 768 | + c.Request().Header.SetCookie(store.sessionName, "12356789") |
| 769 | + |
| 770 | + sess, err := store.Get(c) |
| 771 | + utils.AssertEqual(b, nil, err) |
| 772 | + sess.Set("john", "doe") |
| 773 | + utils.AssertEqual(b, nil, sess.Save()) |
| 774 | + app.ReleaseCtx(c) |
| 775 | + } |
| 776 | + }) |
| 777 | + }) |
| 778 | + |
| 779 | + b.Run("storage", func(b *testing.B) { |
| 780 | + app := fiber.New() |
| 781 | + store := New(Config{ |
| 782 | + Storage: memory.New(), |
| 783 | + }) |
| 784 | + b.ReportAllocs() |
| 785 | + b.ResetTimer() |
| 786 | + b.RunParallel(func(pb *testing.PB) { |
| 787 | + for pb.Next() { |
| 788 | + c := app.AcquireCtx(&fasthttp.RequestCtx{}) |
| 789 | + c.Request().Header.SetCookie(store.sessionName, "12356789") |
| 790 | + |
| 791 | + sess, err := store.Get(c) |
| 792 | + utils.AssertEqual(b, nil, err) |
| 793 | + sess.Set("john", "doe") |
| 794 | + utils.AssertEqual(b, nil, sess.Save()) |
| 795 | + app.ReleaseCtx(c) |
| 796 | + } |
| 797 | + }) |
| 798 | + }) |
| 799 | +} |
| 800 | + |
| 801 | +// go test -v -race -run Test_Session_Concurrency ./... |
| 802 | +func Test_Session_Concurrency(t *testing.T) { |
| 803 | + t.Parallel() |
| 804 | + app := fiber.New() |
| 805 | + store := New() |
| 806 | + |
| 807 | + var wg sync.WaitGroup |
| 808 | + errChan := make(chan error, 10) // Buffered channel to collect errors |
| 809 | + const numGoroutines = 10 // Number of concurrent goroutines to test |
| 810 | + |
| 811 | + // Start numGoroutines goroutines |
| 812 | + for i := 0; i < numGoroutines; i++ { |
| 813 | + wg.Add(1) |
| 814 | + go func() { |
| 815 | + defer wg.Done() |
| 816 | + |
| 817 | + localCtx := app.AcquireCtx(&fasthttp.RequestCtx{}) |
| 818 | + |
| 819 | + sess, err := store.Get(localCtx) |
| 820 | + if err != nil { |
| 821 | + errChan <- err |
| 822 | + return |
| 823 | + } |
| 824 | + |
| 825 | + // Set a value |
| 826 | + sess.Set("name", "john") |
| 827 | + |
| 828 | + // get the session id |
| 829 | + id := sess.ID() |
| 830 | + |
| 831 | + // Check if the session is fresh |
| 832 | + if !sess.Fresh() { |
| 833 | + errChan <- errors.New("session should be fresh") |
| 834 | + return |
| 835 | + } |
| 836 | + |
| 837 | + // Save the session |
| 838 | + if err := sess.Save(); err != nil { |
| 839 | + errChan <- err |
| 840 | + return |
| 841 | + } |
| 842 | + |
| 843 | + // Release the context |
| 844 | + app.ReleaseCtx(localCtx) |
| 845 | + |
| 846 | + // Acquire a new context |
| 847 | + localCtx = app.AcquireCtx(&fasthttp.RequestCtx{}) |
| 848 | + defer app.ReleaseCtx(localCtx) |
| 849 | + |
| 850 | + // Set the session id in the header |
| 851 | + localCtx.Request().Header.SetCookie(store.sessionName, id) |
| 852 | + |
| 853 | + // Get the session |
| 854 | + sess, err = store.Get(localCtx) |
| 855 | + if err != nil { |
| 856 | + errChan <- err |
| 857 | + return |
| 858 | + } |
| 859 | + |
| 860 | + // Get the value |
| 861 | + name := sess.Get("name") |
| 862 | + if name != "john" { |
| 863 | + errChan <- errors.New("name should be john") |
| 864 | + return |
| 865 | + } |
| 866 | + |
| 867 | + // Get ID from the session |
| 868 | + if sess.ID() != id { |
| 869 | + errChan <- errors.New("id should be the same") |
| 870 | + return |
| 871 | + } |
| 872 | + |
| 873 | + // Check if the session is fresh |
| 874 | + if sess.Fresh() { |
| 875 | + errChan <- errors.New("session should not be fresh") |
| 876 | + return |
| 877 | + } |
| 878 | + |
| 879 | + // Delete the key |
| 880 | + sess.Delete("name") |
| 881 | + |
| 882 | + // Get the value |
| 883 | + name = sess.Get("name") |
| 884 | + if name != nil { |
| 885 | + errChan <- errors.New("name should be nil") |
| 886 | + return |
| 887 | + } |
| 888 | + |
| 889 | + // Destroy the session |
| 890 | + if err := sess.Destroy(); err != nil { |
| 891 | + errChan <- err |
| 892 | + return |
| 893 | + } |
| 894 | + }() |
| 895 | + } |
| 896 | + |
| 897 | + wg.Wait() // Wait for all goroutines to finish |
| 898 | + close(errChan) // Close the channel to signal no more errors will be sent |
| 899 | + |
| 900 | + // Check for errors sent to errChan |
| 901 | + for err := range errChan { |
| 902 | + utils.AssertEqual(t, nil, err) |
| 903 | + } |
| 904 | +} |
0 commit comments